Skip to content

Commit 0ce8989

Browse files
committed
test/cli-plugins: Try to make TestConnectAndWait less flaky
- Add runtime.Gosched() calls to encourage goroutine scheduling - Increase the timeout from 10ms to 500ms - Use poll.WaitOn with appropriate delays to ensure the goroutine has spawned before checking - Lock the test goroutines to its own thread Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
1 parent 2d74733 commit 0ce8989

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

cli-plugins/socket/socket_test.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -178,24 +178,39 @@ func TestConnectAndWait(t *testing.T) {
178178
// TODO: this test cannot be executed with `t.Parallel()`, due to
179179
// relying on goroutine numbers to ensure correct behaviour
180180
t.Run("connect goroutine exits after EOF", func(t *testing.T) {
181+
runtime.LockOSThread()
182+
defer runtime.UnlockOSThread()
181183
srv, err := NewPluginServer(nil)
182184
assert.NilError(t, err, "failed to setup server")
183185

184186
defer srv.Close()
185187

186188
t.Setenv(EnvKey, srv.Addr().String())
189+
190+
runtime.Gosched()
187191
numGoroutines := runtime.NumGoroutine()
188192

189193
ConnectAndWait(func() {})
190-
assert.Equal(t, runtime.NumGoroutine(), numGoroutines+1)
194+
195+
runtime.Gosched()
196+
poll.WaitOn(t, func(t poll.LogT) poll.Result {
197+
// +1 goroutine for the poll.WaitOn
198+
// +1 goroutine for the connect goroutine
199+
if runtime.NumGoroutine() < numGoroutines+1+1 {
200+
return poll.Continue("waiting for connect goroutine to spawn")
201+
}
202+
return poll.Success()
203+
}, poll.WithDelay(1*time.Millisecond), poll.WithTimeout(500*time.Millisecond))
191204

192205
srv.Close()
193206

207+
runtime.Gosched()
194208
poll.WaitOn(t, func(t poll.LogT) poll.Result {
209+
// +1 goroutine for the poll.WaitOn
195210
if runtime.NumGoroutine() > numGoroutines+1 {
196211
return poll.Continue("waiting for connect goroutine to exit")
197212
}
198213
return poll.Success()
199-
}, poll.WithDelay(1*time.Millisecond), poll.WithTimeout(10*time.Millisecond))
214+
}, poll.WithDelay(1*time.Millisecond), poll.WithTimeout(500*time.Millisecond))
200215
})
201216
}

0 commit comments

Comments
 (0)