MCPcopy Index your code
hub / github.com/coder/coder / StartWithWaiter

Function StartWithWaiter

cli/clitest/clitest.go:269–310  ·  view source on GitHub ↗

StartWithWaiter runs the command in a goroutine but returns the error instead of asserting it. This is useful for testing error cases.

(t *testing.T, inv *serpent.Invocation)

Source from the content-addressed store, hash-verified

267// StartWithWaiter runs the command in a goroutine but returns the error instead
268// of asserting it. This is useful for testing error cases.
269func StartWithWaiter(t *testing.T, inv *serpent.Invocation) *ErrorWaiter {
270 t.Helper()
271
272 var (
273 ctx = inv.Context()
274 cancel func()
275
276 cleaningUp atomic.Bool
277 errCh = make(chan error, 1)
278 doneCh = make(chan struct{})
279 )
280 if _, ok := ctx.Deadline(); !ok {
281 ctx, cancel = context.WithDeadline(ctx, time.Now().Add(testutil.WaitMedium))
282 } else {
283 ctx, cancel = context.WithCancel(inv.Context())
284 }
285
286 inv = inv.WithContext(ctx)
287
288 go func() {
289 defer close(doneCh)
290 defer close(errCh)
291 err := inv.Run()
292 if cleaningUp.Load() && errors.Is(err, context.DeadlineExceeded) {
293 // If we're cleaning up, this error is likely related to the CLI
294 // teardown process. E.g., the server could be slow to shut down
295 // Postgres.
296 t.Logf("command %q timed out during test cleanup", inv.Command.FullName())
297 }
298 // Whether or not this fails the test is left to the caller.
299 t.Logf("command %q exited with error: %v", inv.Command.FullName(), err)
300 errCh <- err
301 }()
302
303 // Don't exit test routine until server is done.
304 t.Cleanup(func() {
305 cancel()
306 cleaningUp.Store(true)
307 <-doneCh
308 })
309 return &ErrorWaiter{c: errCh, t: t, cancelFunc: cancel}
310}

Callers 15

TestExternalAuthFunction · 0.92
TestExpTaskResumeFunction · 0.92
TestVSCodeSSHFunction · 0.92
TestServerFunction · 0.92
TestCreateFunction · 0.92
TestTemplatePushFunction · 0.92
TestLoginFunction · 0.92
TestConfigSSHFunction · 0.92
TestFixOIDCLinksFunction · 0.92
TestSecretDeleteFunction · 0.92
TestVPNDaemonRunFunction · 0.92

Calls 11

WithContextMethod · 0.80
HelperMethod · 0.65
ContextMethod · 0.65
AddMethod · 0.65
RunMethod · 0.65
LogfMethod · 0.65
CleanupMethod · 0.65
LoadMethod · 0.45
IsMethod · 0.45
FullNameMethod · 0.45
StoreMethod · 0.45

Tested by 15

TestExternalAuthFunction · 0.74
TestExpTaskResumeFunction · 0.74
TestVSCodeSSHFunction · 0.74
TestServerFunction · 0.74
TestCreateFunction · 0.74
TestTemplatePushFunction · 0.74
TestLoginFunction · 0.74
TestConfigSSHFunction · 0.74
TestFixOIDCLinksFunction · 0.74
TestSecretDeleteFunction · 0.74
TestVPNDaemonRunFunction · 0.74