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

Function TestAPI

agent/agentcontainers/api_test.go:516–4118  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

514}
515
516func TestAPI(t *testing.T) {
517 t.Parallel()
518
519 t.Run("NoUpdaterLoopLogspam", func(t *testing.T) {
520 t.Parallel()
521
522 var (
523 ctx = testutil.Context(t, testutil.WaitShort)
524 logbuf strings.Builder
525 logger = slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug).AppendSinks(sloghuman.Sink(&logbuf))
526 mClock = quartz.NewMock(t)
527 tickerTrap = mClock.Trap().TickerFunc("updaterLoop")
528 firstErr = xerrors.New("first error")
529 secondErr = xerrors.New("second error")
530 fakeCLI = &fakeContainerCLI{
531 listErr: firstErr,
532 }
533 fWatcher = newFakeWatcher(t)
534 )
535
536 api := agentcontainers.NewAPI(logger,
537 agentcontainers.WithWatcher(fWatcher),
538 agentcontainers.WithClock(mClock),
539 agentcontainers.WithContainerCLI(fakeCLI),
540 )
541 api.Start()
542 defer api.Close()
543
544 // The watcherLoop writes a log when it is initialized.
545 // We want to ensure this has happened before we start
546 // the test so that it does not intefere.
547 fWatcher.waitNext(ctx)
548
549 // Make sure the ticker function has been registered
550 // before advancing the clock.
551 tickerTrap.MustWait(ctx).MustRelease(ctx)
552 tickerTrap.Close()
553
554 logbuf.Reset()
555
556 // First tick should handle the error.
557 _, aw := mClock.AdvanceNext()
558 aw.MustWait(ctx)
559
560 // Verify first error is logged.
561 got := logbuf.String()
562 t.Logf("got log: %q", got)
563 require.Contains(t, got, "updater loop ticker failed", "first error should be logged")
564 require.Contains(t, got, "first error", "should contain first error message")
565 logbuf.Reset()
566
567 // Second tick should handle the same error without logging it again.
568 _, aw = mClock.AdvanceNext()
569 aw.MustWait(ctx)
570
571 // Verify same error is not logged again.
572 got = logbuf.String()
573 t.Logf("got log: %q", got)

Callers

nothing calls this directly

Calls 15

StartMethod · 0.95
CloseMethod · 0.95
RoutesMethod · 0.95
CloseMethod · 0.95
RefreshContainersMethod · 0.95
CreateDevcontainerMethod · 0.95
getLastCommandMethod · 0.95
ContextFunction · 0.92
NewAPIFunction · 0.92
WithWatcherFunction · 0.92
WithClockFunction · 0.92
WithContainerCLIFunction · 0.92

Tested by

no test coverage detected