(t *testing.T)
| 72 | } |
| 73 | |
| 74 | func TestWatchdog_Timeout(t *testing.T) { |
| 75 | t.Parallel() |
| 76 | ctx := testutil.Context(t, testutil.WaitShort) |
| 77 | mClock := quartz.NewMock(t) |
| 78 | logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug) |
| 79 | fPS := newFakePubsub() |
| 80 | |
| 81 | // trap the ticker calls |
| 82 | pubTrap := mClock.Trap().TickerFunc("publish") |
| 83 | defer pubTrap.Close() |
| 84 | |
| 85 | uut := pubsub.NewWatchdogWithClock(ctx, logger, fPS, mClock) |
| 86 | |
| 87 | // wait for the ticker to be created so that we know it starts from the |
| 88 | // right baseline time. |
| 89 | pc, err := pubTrap.Wait(ctx) |
| 90 | require.NoError(t, err) |
| 91 | pc.MustRelease(ctx) |
| 92 | require.Equal(t, 15*time.Second, pc.Duration) |
| 93 | |
| 94 | // we subscribe after starting the timer, so we know the timer also starts |
| 95 | // from the baseline. |
| 96 | sub := testutil.TryReceive(ctx, t, fPS.subs) |
| 97 | require.Equal(t, pubsub.EventPubsubWatchdog, sub.event) |
| 98 | |
| 99 | // 5 min / 15 sec = 20, so do 19 ticks without timing out |
| 100 | for i := 0; i < 19; i++ { |
| 101 | d, w := mClock.AdvanceNext() |
| 102 | w.MustWait(ctx) |
| 103 | require.LessOrEqual(t, d, 15*time.Second) |
| 104 | p := testutil.TryReceive(ctx, t, fPS.pubs) |
| 105 | require.Equal(t, pubsub.EventPubsubWatchdog, p) |
| 106 | mClock.Advance(30 * time.Millisecond). // reasonable round-trip |
| 107 | MustWait(ctx) |
| 108 | // we DO NOT forward the heartbeat |
| 109 | // we shouldn't time out |
| 110 | select { |
| 111 | case <-uut.Timeout(): |
| 112 | t.Fatal("watchdog tripped") |
| 113 | default: |
| 114 | // OK! |
| 115 | } |
| 116 | } |
| 117 | d, w := mClock.AdvanceNext() |
| 118 | w.MustWait(ctx) |
| 119 | require.LessOrEqual(t, d, 15*time.Second) |
| 120 | p := testutil.TryReceive(ctx, t, fPS.pubs) |
| 121 | require.Equal(t, pubsub.EventPubsubWatchdog, p) |
| 122 | testutil.TryReceive(ctx, t, uut.Timeout()) |
| 123 | |
| 124 | err = uut.Close() |
| 125 | require.NoError(t, err) |
| 126 | } |
| 127 | |
| 128 | type subscribe struct { |
| 129 | event string |
nothing calls this directly
no test coverage detected