@@ -23,6 +23,7 @@ import (
23
23
"log"
24
24
"os"
25
25
"testing"
26
+ "time"
26
27
27
28
"github.com/stretchr/testify/assert"
28
29
"google.golang.org/grpc"
@@ -90,8 +91,10 @@ func TestMain(m *testing.M) {
90
91
}
91
92
92
93
testRPCServer , err = rpc .NewServer (& rpc.Config {
93
- Port : helper .RPCPort ,
94
- MaxRequestBytes : helper .RPCMaxRequestBytes ,
94
+ Port : helper .RPCPort ,
95
+ MaxRequestBytes : helper .RPCMaxRequestBytes ,
96
+ MaxConnectionAge : helper .RPCMaxConnectionAge .String (),
97
+ MaxConnectionAgeGrace : helper .RPCMaxConnectionAgeGrace .String (),
95
98
}, be )
96
99
if err != nil {
97
100
log .Fatal (err )
@@ -622,6 +625,50 @@ func TestRPCServerBackend(t *testing.T) {
622
625
)
623
626
assert .Equal (t , codes .FailedPrecondition , status .Convert (err ).Code ())
624
627
})
628
+
629
+ t .Run ("watch document test" , func (t * testing.T ) {
630
+ activateResp , err := testClient .ActivateClient (
631
+ context .Background (),
632
+ & api.ActivateClientRequest {ClientKey : t .Name ()},
633
+ )
634
+ assert .NoError (t , err )
635
+
636
+ packWithNoChanges := & api.ChangePack {
637
+ DocumentKey : helper .TestDocKey (t ).String (),
638
+ Checkpoint : & api.Checkpoint {ServerSeq : 0 , ClientSeq : 0 },
639
+ }
640
+
641
+ resPack , err := testClient .AttachDocument (
642
+ context .Background (),
643
+ & api.AttachDocumentRequest {
644
+ ClientId : activateResp .ClientId ,
645
+ ChangePack : packWithNoChanges ,
646
+ },
647
+ )
648
+ assert .NoError (t , err )
649
+
650
+ // watch document
651
+ watchResp , err := testClient .WatchDocument (
652
+ context .Background (),
653
+ & api.WatchDocumentRequest {
654
+ Client : & api.Client {Id : activateResp .ClientId , Presence : & api.Presence {}},
655
+ DocumentId : resPack .DocumentId ,
656
+ },
657
+ )
658
+ assert .NoError (t , err )
659
+
660
+ // check if stream is open
661
+ _ , err = watchResp .Recv ()
662
+ assert .NoError (t , err )
663
+
664
+ // wait for MaxConnectionAge + MaxConnectionAgeGrace
665
+ time .Sleep (helper .RPCMaxConnectionAge + helper .RPCMaxConnectionAgeGrace )
666
+
667
+ // check if stream has closed by server (EOF)
668
+ _ , err = watchResp .Recv ()
669
+ assert .Equal (t , codes .Unavailable , status .Code (err ))
670
+ assert .Contains (t , err .Error (), "EOF" )
671
+ })
625
672
}
626
673
627
674
func TestConfig_Validate (t * testing.T ) {
@@ -633,9 +680,23 @@ func TestConfig_Validate(t *testing.T) {
633
680
{config : & rpc.Config {Port : 11101 , CertFile : "noSuchCertFile" }, expected : rpc .ErrInvalidCertFile },
634
681
{config : & rpc.Config {Port : 11101 , KeyFile : "noSuchKeyFile" }, expected : rpc .ErrInvalidKeyFile },
635
682
// not to use tls
636
- {config : & rpc.Config {Port : 11101 , CertFile : "" , KeyFile : "" }, expected : nil },
683
+ {config : & rpc.Config {
684
+ Port : 11101 ,
685
+ CertFile : "" ,
686
+ KeyFile : "" ,
687
+ MaxConnectionAge : "50s" ,
688
+ MaxConnectionAgeGrace : "10s" ,
689
+ },
690
+ expected : nil },
637
691
// pass any file existing
638
- {config : & rpc.Config {Port : 11101 , CertFile : "server_test.go" , KeyFile : "server_test.go" }, expected : nil },
692
+ {config : & rpc.Config {
693
+ Port : 11101 ,
694
+ CertFile : "server_test.go" ,
695
+ KeyFile : "server_test.go" ,
696
+ MaxConnectionAge : "50s" ,
697
+ MaxConnectionAgeGrace : "10s" ,
698
+ },
699
+ expected : nil },
639
700
}
640
701
for _ , scenario := range scenarios {
641
702
assert .ErrorIs (t , scenario .config .Validate (), scenario .expected , "provided config: %#v" , scenario .config )
0 commit comments