Skip to content

Commit b4f8849

Browse files
authored
grpc: fixed BoardListWatch streaming call (#2664)
* grpc: fixed BoardListWatch streaming call * Added integration test * Fixed also existing calls to BoardListWatch
1 parent 625aaac commit b4f8849

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

Diff for: commands/service_board_list.go

+1
Original file line numberDiff line numberDiff line change
@@ -318,5 +318,6 @@ func (s *arduinoCoreServerImpl) BoardListWatch(req *rpc.BoardListWatchRequest, s
318318
}
319319
}()
320320

321+
<-stream.Context().Done()
321322
return nil
322323
}

Diff for: internal/cli/arguments/port.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,14 @@ func (p *Port) GetPort(ctx context.Context, instance *rpc.Instance, srv rpc.Ardu
8989
}
9090
logrus.WithField("port", address).Tracef("Upload port")
9191

92-
ctx, cancel := context.WithCancel(ctx)
92+
ctx, cancel := context.WithTimeout(ctx, p.timeout.Get())
9393
defer cancel()
9494

9595
stream, watcher := commands.BoardListWatchProxyToChan(ctx)
96-
err := srv.BoardListWatch(&rpc.BoardListWatchRequest{Instance: instance}, stream)
96+
go func() {
97+
_ = srv.BoardListWatch(&rpc.BoardListWatchRequest{Instance: instance}, stream)
98+
}()
9799

98-
if err != nil {
99-
return nil, err
100-
}
101-
102-
deadline := time.After(p.timeout.Get())
103100
for {
104101
select {
105102
case portEvent := <-watcher:
@@ -111,7 +108,7 @@ func (p *Port) GetPort(ctx context.Context, instance *rpc.Instance, srv rpc.Ardu
111108
return port, nil
112109
}
113110

114-
case <-deadline:
111+
case <-ctx.Done():
115112
// No matching port found
116113
if protocol == "" {
117114
return &rpc.Port{

Diff for: internal/cli/board/list.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@ func runListCommand(ctx context.Context, srv rpc.ArduinoCoreServiceServer, watch
9191

9292
func watchList(ctx context.Context, inst *rpc.Instance, srv rpc.ArduinoCoreServiceServer) {
9393
stream, eventsChan := commands.BoardListWatchProxyToChan(ctx)
94-
err := srv.BoardListWatch(&rpc.BoardListWatchRequest{Instance: inst}, stream)
95-
if err != nil {
96-
feedback.Fatal(i18n.Tr("Error detecting boards: %v", err), feedback.ErrNetwork)
97-
}
94+
go func() {
95+
err := srv.BoardListWatch(&rpc.BoardListWatchRequest{Instance: inst}, stream)
96+
if err != nil {
97+
feedback.Fatal(i18n.Tr("Error detecting boards: %v", err), feedback.ErrNetwork)
98+
}
99+
}()
98100

99101
// This is done to avoid printing the header each time a new event is received
100102
if feedback.GetFormat() == feedback.Text {

Diff for: internal/integrationtest/daemon/daemon_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ func TestArduinoCliDaemon(t *testing.T) {
9191

9292
testWatcher()
9393
testWatcher()
94+
95+
{
96+
// Test that the watcher stays open until the grpc call is canceled
97+
98+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
99+
defer cancel()
100+
101+
start := time.Now()
102+
watcher, err := grpcInst.BoardListWatch(ctx)
103+
require.NoError(t, err)
104+
for {
105+
_, err := watcher.Recv()
106+
if err != nil {
107+
break
108+
}
109+
}
110+
require.Greater(t, time.Since(start), 2*time.Second)
111+
}
94112
}
95113

96114
func TestDaemonAutoUpdateIndexOnFirstInit(t *testing.T) {

0 commit comments

Comments
 (0)