@@ -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
)
@@ -54,28 +56,34 @@ func TestArduinoCliDaemon(t *testing.T) {
54
56
55
57
testWatcher := func () {
56
58
// Run watcher
57
- watcher , err := grpcInst .BoardListWatch ()
59
+ ctx , cancel := context .WithCancel (context .TODO ())
60
+ watcher , err := grpcInst .BoardListWatch (ctx )
58
61
require .NoError (t , err )
59
- ctx , cancel := context . WithCancel ( context . Background () )
62
+ watcherCanceldCh := make ( chan struct {} )
60
63
go func () {
61
- defer cancel ()
62
64
for {
63
65
msg , err := watcher .Recv ()
64
66
if err == io .EOF {
65
67
fmt .Println ("Watcher EOF" )
66
68
return
67
69
}
68
- require .Empty (t , msg .Error , "Board list watcher returned an error" )
70
+ if s , ok := status .FromError (err ); ok && s .Code () == codes .Canceled {
71
+ fmt .Println ("Watcher canceled" )
72
+ watcherCanceldCh <- struct {}{}
73
+ return
74
+ }
69
75
require .NoError (t , err , "BoardListWatch grpc call returned an error" )
70
- fmt .Printf ("WATCH> %v\n " , msg )
76
+ require .Empty (t , msg .Error , "Board list watcher returned an error" )
77
+ fmt .Printf ("WATCH> %v %v\n " , msg , err )
71
78
}
72
79
}()
73
80
time .Sleep (time .Second )
74
- require .NoError (t , watcher .CloseSend ())
81
+ cancel ()
82
+ time .Sleep (time .Second )
75
83
select {
76
- case <- ctx . Done () :
84
+ case <- watcherCanceldCh :
77
85
// all right!
78
- case <- time .After (time .Second ):
86
+ case <- time .After (2 * time .Second ):
79
87
require .Fail (t , "BoardListWatch didn't close" )
80
88
}
81
89
}
0 commit comments