Skip to content

Commit 8313a52

Browse files
committed
notifier: refactor, add more unit tests
1 parent 125584c commit 8313a52

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

coderd/autobuild/notify/notifier.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,15 @@ func (n *Notifier) pollOnce(tick time.Time) {
6464

6565
timeRemaining := deadline.Sub(tick)
6666
for _, tock := range n.countdown {
67-
if timeRemaining <= tock && !n.notifiedAt[tock] {
68-
callback()
69-
n.notifiedAt[tock] = true
67+
if n.notifiedAt[tock] {
68+
continue
7069
}
70+
if timeRemaining > tock {
71+
continue
72+
}
73+
callback()
74+
n.notifiedAt[tock] = true
75+
return
7176
}
7277
}
7378

coderd/autobuild/notify/notifier_test.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,51 @@ func TestNotifier(t *testing.T) {
2727
{
2828
Name: "zero deadline",
2929
Countdown: durations(),
30-
Ticks: ticks(now, 0),
30+
Ticks: fakeTicker(now, time.Second, 0),
3131
ConditionDeadline: time.Time{},
3232
NumConditions: 1,
3333
NumCallbacks: 0,
3434
},
3535
{
3636
Name: "no calls",
3737
Countdown: durations(),
38-
Ticks: ticks(now, 0),
38+
Ticks: fakeTicker(now, time.Second, 0),
3939
ConditionDeadline: now,
4040
NumConditions: 1,
4141
NumCallbacks: 0,
4242
},
4343
{
4444
Name: "exactly one call",
4545
Countdown: durations(time.Second),
46-
Ticks: ticks(now, 2),
47-
ConditionDeadline: now.Add(2 * time.Second),
46+
Ticks: fakeTicker(now, time.Second, 1),
47+
ConditionDeadline: now.Add(time.Second),
4848
NumConditions: 2,
4949
NumCallbacks: 1,
5050
},
5151
{
5252
Name: "two calls",
5353
Countdown: durations(4*time.Second, 2*time.Second),
54-
Ticks: ticks(now, 5),
54+
Ticks: fakeTicker(now, time.Second, 5),
5555
ConditionDeadline: now.Add(5 * time.Second),
5656
NumConditions: 6,
5757
NumCallbacks: 2,
5858
},
5959
{
6060
Name: "wrong order should not matter",
6161
Countdown: durations(2*time.Second, 4*time.Second),
62-
Ticks: ticks(now, 5),
62+
Ticks: fakeTicker(now, time.Second, 5),
6363
ConditionDeadline: now.Add(5 * time.Second),
6464
NumConditions: 6,
6565
NumCallbacks: 2,
6666
},
67+
{
68+
Name: "ssh autostop notify",
69+
Countdown: durations(5*time.Minute, time.Minute),
70+
Ticks: fakeTicker(now, 30*time.Second, 120),
71+
ConditionDeadline: now.Add(30 * time.Minute),
72+
NumConditions: 121,
73+
NumCallbacks: 2,
74+
},
6775
}
6876

6977
for _, testCase := range testCases {
@@ -91,6 +99,7 @@ func TestNotifier(t *testing.T) {
9199
close(ch)
92100
wg.Wait()
93101
require.Equal(t, testCase.NumCallbacks, numCalls.Load())
102+
require.Equal(t, testCase.NumConditions, numConditions.Load())
94103
})
95104
}
96105
}
@@ -99,11 +108,10 @@ func durations(ds ...time.Duration) []time.Duration {
99108
return ds
100109
}
101110

102-
func ticks(t time.Time, n int) []time.Time {
103-
ts := make([]time.Time, n+1)
104-
ts = append(ts, t)
105-
for i := 0; i < n; i++ {
106-
ts = append(ts, t.Add(time.Duration(n)*time.Second))
111+
func fakeTicker(t time.Time, d time.Duration, n int) []time.Time {
112+
ts := make([]time.Time, 0)
113+
for i := 1; i <= n; i++ {
114+
ts = append(ts, t.Add(time.Duration(n)*d))
107115
}
108116
return ts
109117
}

0 commit comments

Comments
 (0)