@@ -27,6 +27,8 @@ import (
27
27
"github.com/arduino/arduino-cli/internal/integrationtest"
28
28
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
29
29
"github.com/arduino/go-paths-helper"
30
+ "google.golang.org/grpc/codes"
31
+ "google.golang.org/grpc/status"
30
32
31
33
"github.com/stretchr/testify/require"
32
34
)
@@ -52,28 +54,34 @@ func TestArduinoCliDaemon(t *testing.T) {
52
54
require .NoError (t , err )
53
55
fmt .Printf ("Got boardlist response with %d ports\n " , len (boardListResp .GetPorts ()))
54
56
57
+ // When the client closes the connection we expect that the streaming from the server closes.
55
58
testWatcher := func () {
56
59
// Run watcher
57
- watcher , err := grpcInst .BoardListWatch ()
58
- require .NoError (t , err )
59
60
ctx , cancel := context .WithCancel (context .Background ())
61
+ watcher , err := grpcInst .BoardListWatch (ctx )
62
+ require .NoError (t , err )
63
+ watcherCanceldCh := make (chan struct {})
60
64
go func () {
61
- defer cancel ()
62
65
for {
63
66
msg , err := watcher .Recv ()
64
67
if err == io .EOF {
65
68
fmt .Println ("Watcher EOF" )
66
69
return
67
70
}
68
- require .Empty (t , msg .Error , "Board list watcher returned an error" )
71
+ if s , ok := status .FromError (err ); ok && s .Code () == codes .Canceled {
72
+ fmt .Println ("Watcher canceled" )
73
+ watcherCanceldCh <- struct {}{}
74
+ return
75
+ }
69
76
require .NoError (t , err , "BoardListWatch grpc call returned an error" )
70
- fmt .Printf ("WATCH> %v\n " , msg )
77
+ require .Empty (t , msg .Error , "Board list watcher returned an error" )
78
+ fmt .Printf ("WATCH> %v %v\n " , msg , err )
71
79
}
72
80
}()
73
81
time .Sleep (time .Second )
74
- require . NoError ( t , watcher . CloseSend () )
82
+ cancel ( )
75
83
select {
76
- case <- ctx . Done () :
84
+ case <- watcherCanceldCh :
77
85
// all right!
78
86
case <- time .After (time .Second ):
79
87
require .Fail (t , "BoardListWatch didn't close" )
0 commit comments