Skip to content

Commit 96ec45e

Browse files
committed
fix flaky test:
ref b5ac225
1 parent fd27d19 commit 96ec45e

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

pkg/apiserver/api/device.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ func (s *grpcServer) GetDeviceConfiguration(request *pb.GetDeviceConfigurationRe
5959
if err != nil {
6060
log.Errorf("make device config: %v", err)
6161
} else {
62-
stream.Send(cfg)
62+
err := stream.Send(cfg)
63+
if err != nil {
64+
log.Debugf("stream end for device %d failed, err: %v", deviceId, err)
65+
}
6366
}
6467
}
6568
}

pkg/apiserver/api/grpcserver_test.go

+7-19
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package api_test
22

33
import (
44
"context"
5-
"io"
65
"net"
76
"testing"
87
"time"
@@ -17,6 +16,7 @@ import (
1716
"google.golang.org/grpc/codes"
1817
"google.golang.org/grpc/status"
1918
"google.golang.org/grpc/test/bufconn"
19+
"google.golang.org/protobuf/types/known/timestamppb"
2020
)
2121

2222
const bufSize = 1024 * 1024
@@ -36,7 +36,11 @@ func TestGetDeviceConfiguration(t *testing.T) {
3636
accessGroups := []string{"auth"}
3737

3838
db := &database.MockAPIServer{}
39-
db.On("ReadSessionInfo", mock.Anything, mock.Anything).Return(&pb.Session{Groups: accessGroups}, nil)
39+
db.On("ReadSessionInfo", mock.Anything, mock.Anything).Return(
40+
&pb.Session{
41+
Groups: accessGroups,
42+
Expiry: timestamppb.New(time.Now().Add(10 * time.Second)),
43+
}, nil)
4044
db.On("ReadDeviceById", mock.Anything, mock.Anything).Return(&pb.Device{
4145
Healthy: true,
4246
}, nil)
@@ -74,23 +78,7 @@ func TestGetDeviceConfiguration(t *testing.T) {
7478
configClient, err := client.GetDeviceConfiguration(ctx, &pb.GetDeviceConfigurationRequest{})
7579
assert.NoError(t, err)
7680

77-
var resp *pb.GetDeviceConfigurationResponse
78-
for attempt := 0; attempt < 10; attempt++ {
79-
resp, err = configClient.Recv()
80-
if err == nil {
81-
break
82-
}
83-
84-
if err == io.EOF {
85-
time.Sleep(100 * time.Millisecond)
86-
continue
87-
} else {
88-
t.Fatalf("get device config: got unexpected err: %v", err)
89-
}
90-
}
91-
if err != nil {
92-
t.Fatalf("could not get device config in 10 attempts, last was err: %v", err)
93-
}
81+
resp, err := configClient.Recv()
9482
assert.NoError(t, err)
9583

9684
gw := resp.Gateways[0]

0 commit comments

Comments
 (0)