From e4f021dd420b138947db4ec2a3e9ddd24720c795 Mon Sep 17 00:00:00 2001 From: Joseph Phillips Date: Thu, 28 Nov 2024 11:41:08 +0100 Subject: [PATCH] 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. --- api/watcher/watcher_test.go | 41 +++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/api/watcher/watcher_test.go b/api/watcher/watcher_test.go index 11862e71868..1a84fd4e079 100644 --- a/api/watcher/watcher_test.go +++ b/api/watcher/watcher_test.go @@ -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 ¶ms.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 ¶ms.Error{Code: params.CodeStopped} + }, + ).AnyTimes() return "id-666", eventCh }