Skip to content

Commit e4f021d

Browse files
committed
fix: remove possibility of zero-effect Stop from api watcher test
This handles a situation where we may be entrant into the eventCh case in the call to Next when Stop is called. In that case, the default clause will be invoked and we effectively ignore the stop call. This means the watcher will never stop, causing test failure.
1 parent 7143742 commit e4f021d

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

api/watcher/watcher_test.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,26 +100,31 @@ func setupWatcher[T any](c *gc.C, caller *apimocks.MockAPICaller, facadeName str
100100
eventCh := make(chan T)
101101

102102
stopped := make(chan bool)
103-
caller.EXPECT().APICall(gomock.Any(), facadeName, 666, "id-666", "Stop", nil, gomock.Any()).DoAndReturn(func(_ context.Context, _ string, _ int, _ string, _ string, _ any, _ any) error {
104-
select {
105-
case stopped <- true:
106-
default:
107-
}
108-
return nil
109-
}).Return(nil).AnyTimes()
110-
111-
caller.EXPECT().APICall(gomock.Any(), facadeName, 666, "id-666", "Next", nil, gomock.Any()).DoAndReturn(func(_ context.Context, _ string, _ int, _ string, _ string, _ any, r any) error {
112-
select {
113-
case ev, ok := <-eventCh:
114-
if !ok {
115-
c.FailNow()
103+
caller.EXPECT().APICall(gomock.Any(), facadeName, 666, "id-666", "Stop", nil, gomock.Any()).DoAndReturn(
104+
func(context.Context, string, int, string, string, any, any) error {
105+
select {
106+
case stopped <- true:
107+
case <-time.After(testing.LongWait):
108+
c.Fatalf("timed out waiting for stop call")
116109
}
117-
*(*r.(*any)).(*any) = ev
118110
return nil
119-
case <-stopped:
120-
}
121-
return &params.Error{Code: params.CodeStopped}
122-
}).AnyTimes()
111+
},
112+
).Return(nil).AnyTimes()
113+
114+
caller.EXPECT().APICall(gomock.Any(), facadeName, 666, "id-666", "Next", nil, gomock.Any()).DoAndReturn(
115+
func(_ context.Context, _ string, _ int, _ string, _ string, _ any, r any) error {
116+
select {
117+
case ev, ok := <-eventCh:
118+
if !ok {
119+
c.Fatalf("next channel closed")
120+
}
121+
*(*r.(*any)).(*any) = ev
122+
return nil
123+
case <-stopped:
124+
}
125+
return &params.Error{Code: params.CodeStopped}
126+
},
127+
).AnyTimes()
123128
return "id-666", eventCh
124129
}
125130

0 commit comments

Comments
 (0)