@@ -551,3 +551,75 @@ func TestClientWithInterceptor(t *testing.T) {
551
551
552
552
test .That (t , conn .Close (), test .ShouldBeNil )
553
553
}
554
+
555
+ func TestClientStreamAfterClose (t * testing.T ) {
556
+ // Set up gRPC server
557
+ logger := logging .NewTestLogger (t )
558
+ listener , err := net .Listen ("tcp" , "localhost:0" )
559
+ test .That (t , err , test .ShouldBeNil )
560
+ rpcServer , err := rpc .NewServer (logger .AsZap (), rpc .WithUnauthenticated ())
561
+ test .That (t , err , test .ShouldBeNil )
562
+
563
+ // Set up camera that can stream images
564
+ img := image .NewNRGBA (image .Rect (0 , 0 , 4 , 4 ))
565
+ injectCamera := & inject.Camera {}
566
+ injectCamera .PropertiesFunc = func (ctx context.Context ) (camera.Properties , error ) {
567
+ return camera.Properties {}, nil
568
+ }
569
+ injectCamera .StreamFunc = func (ctx context.Context , errHandlers ... gostream.ErrorHandler ) (gostream.VideoStream , error ) {
570
+ return gostream .NewEmbeddedVideoStreamFromReader (gostream .VideoReaderFunc (func (ctx context.Context ) (image.Image , func (), error ) {
571
+ return img , func () {}, nil
572
+ })), nil
573
+ }
574
+
575
+ // Register CameraService API in our gRPC server.
576
+ resources := map [resource.Name ]camera.Camera {
577
+ camera .Named (testCameraName ): injectCamera ,
578
+ }
579
+ cameraSvc , err := resource .NewAPIResourceCollection (camera .API , resources )
580
+ test .That (t , err , test .ShouldBeNil )
581
+ resourceAPI , ok , err := resource.LookupAPIRegistration [camera.Camera ](camera .API )
582
+ test .That (t , err , test .ShouldBeNil )
583
+ test .That (t , ok , test .ShouldBeTrue )
584
+ test .That (t , resourceAPI .RegisterRPCService (context .Background (), rpcServer , cameraSvc ), test .ShouldBeNil )
585
+
586
+ // Start serving requests.
587
+ go rpcServer .Serve (listener )
588
+ defer rpcServer .Stop ()
589
+
590
+ // Make client connection
591
+ conn , err := viamgrpc .Dial (context .Background (), listener .Addr ().String (), logger )
592
+ test .That (t , err , test .ShouldBeNil )
593
+ client , err := camera .NewClientFromConn (context .Background (), conn , "" , camera .Named (testCameraName ), logger )
594
+ test .That (t , err , test .ShouldBeNil )
595
+
596
+ // Get a stream
597
+ stream , err := client .Stream (context .Background ())
598
+ test .That (t , stream , test .ShouldNotBeNil )
599
+ test .That (t , err , test .ShouldBeNil )
600
+
601
+ // Read from stream
602
+ media , _ , err := stream .Next (context .Background ())
603
+ test .That (t , media , test .ShouldNotBeNil )
604
+ test .That (t , err , test .ShouldBeNil )
605
+
606
+ // Close client and read from stream
607
+ test .That (t , client .Close (context .Background ()), test .ShouldBeNil )
608
+ media , _ , err = stream .Next (context .Background ())
609
+ test .That (t , media , test .ShouldBeNil )
610
+ test .That (t , err .Error (), test .ShouldContainSubstring , "context canceled" )
611
+
612
+ // Get a new stream
613
+ stream , err = client .Stream (context .Background ())
614
+ test .That (t , stream , test .ShouldNotBeNil )
615
+ test .That (t , err , test .ShouldBeNil )
616
+
617
+ // Read from the new stream
618
+ media , _ , err = stream .Next (context .Background ())
619
+ test .That (t , media , test .ShouldNotBeNil )
620
+ test .That (t , err , test .ShouldBeNil )
621
+
622
+ // Close client and connection
623
+ test .That (t , client .Close (context .Background ()), test .ShouldBeNil )
624
+ test .That (t , conn .Close (), test .ShouldBeNil )
625
+ }
0 commit comments