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

Function TestWatchdog_NoTimeout

coderd/database/pubsub/watchdog_test.go:16–72  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

14)
15
16func TestWatchdog_NoTimeout(t *testing.T) {
17 t.Parallel()
18 ctx := testutil.Context(t, testutil.WaitShort)
19 mClock := quartz.NewMock(t)
20 logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
21 fPS := newFakePubsub()
22
23 // trap the ticker and timer.Stop() calls
24 pubTrap := mClock.Trap().TickerFunc("publish")
25 defer pubTrap.Close()
26 subTrap := mClock.Trap().TimerStop("subscribe")
27 defer subTrap.Close()
28
29 uut := pubsub.NewWatchdogWithClock(ctx, logger, fPS, mClock)
30
31 // wait for the ticker to be created so that we know it starts from the
32 // right baseline time.
33 pc, err := pubTrap.Wait(ctx)
34 require.NoError(t, err)
35 pc.MustRelease(ctx)
36 require.Equal(t, 15*time.Second, pc.Duration)
37
38 // we subscribe after starting the timer, so we know the timer also starts
39 // from the baseline.
40 sub := testutil.TryReceive(ctx, t, fPS.subs)
41 require.Equal(t, pubsub.EventPubsubWatchdog, sub.event)
42
43 // 5 min / 15 sec = 20, so do 21 ticks
44 for i := 0; i < 21; i++ {
45 d, w := mClock.AdvanceNext()
46 w.MustWait(ctx)
47 require.LessOrEqual(t, d, 15*time.Second)
48 p := testutil.TryReceive(ctx, t, fPS.pubs)
49 require.Equal(t, pubsub.EventPubsubWatchdog, p)
50 mClock.Advance(30 * time.Millisecond). // reasonable round-trip
51 MustWait(ctx)
52 // forward the beat
53 sub.listener(ctx, []byte{})
54 // we shouldn't time out
55 select {
56 case <-uut.Timeout():
57 t.Fatal("watchdog tripped")
58 default:
59 // OK!
60 }
61 }
62
63 errCh := make(chan error, 1)
64 go func() {
65 errCh <- uut.Close()
66 }()
67 sc, err := subTrap.Wait(ctx) // timer.Stop() called
68 require.NoError(t, err)
69 sc.MustRelease(ctx)
70 err = testutil.TryReceive(ctx, t, errCh)
71 require.NoError(t, err)
72}
73

Callers

nothing calls this directly

Calls 10

ContextFunction · 0.92
NewWatchdogWithClockFunction · 0.92
TryReceiveFunction · 0.92
newFakePubsubFunction · 0.85
listenerMethod · 0.80
FatalMethod · 0.80
CloseMethod · 0.65
WaitMethod · 0.65
EqualMethod · 0.45
TimeoutMethod · 0.45

Tested by

no test coverage detected