Skip to content

Commit

Permalink
fix: remove possibility of zero-effect Stop from api watcher test
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
manadart committed Nov 28, 2024
1 parent 7143742 commit e4f021d
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions api/watcher/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,26 +100,31 @@ func setupWatcher[T any](c *gc.C, caller *apimocks.MockAPICaller, facadeName str
eventCh := make(chan T)

stopped := make(chan bool)
caller.EXPECT().APICall(gomock.Any(), facadeName, 666, "id-666", "Stop", nil, gomock.Any()).DoAndReturn(func(_ context.Context, _ string, _ int, _ string, _ string, _ any, _ any) error {
select {
case stopped <- true:
default:
}
return nil
}).Return(nil).AnyTimes()

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 {
select {
case ev, ok := <-eventCh:
if !ok {
c.FailNow()
caller.EXPECT().APICall(gomock.Any(), facadeName, 666, "id-666", "Stop", nil, gomock.Any()).DoAndReturn(
func(context.Context, string, int, string, string, any, any) error {
select {
case stopped <- true:
case <-time.After(testing.LongWait):
c.Fatalf("timed out waiting for stop call")
}
*(*r.(*any)).(*any) = ev
return nil
case <-stopped:
}
return &params.Error{Code: params.CodeStopped}
}).AnyTimes()
},
).Return(nil).AnyTimes()

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 {
select {
case ev, ok := <-eventCh:
if !ok {
c.Fatalf("next channel closed")
}
*(*r.(*any)).(*any) = ev
return nil
case <-stopped:
}
return &params.Error{Code: params.CodeStopped}
},
).AnyTimes()
return "id-666", eventCh
}

Expand Down

0 comments on commit e4f021d

Please sign in to comment.