Skip to content

feat: send native system notification on scheduled workspace shutdown #1414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
notifier: refactor, add more unit tests
  • Loading branch information
johnstcn committed May 13, 2022
commit 8313a52d345a6a67d9a4cbb494269707b77506cc
11 changes: 8 additions & 3 deletions coderd/autobuild/notify/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,15 @@ func (n *Notifier) pollOnce(tick time.Time) {

timeRemaining := deadline.Sub(tick)
for _, tock := range n.countdown {
if timeRemaining <= tock && !n.notifiedAt[tock] {
callback()
n.notifiedAt[tock] = true
if n.notifiedAt[tock] {
continue
}
if timeRemaining > tock {
continue
}
callback()
n.notifiedAt[tock] = true
return
}
}

Expand Down
30 changes: 19 additions & 11 deletions coderd/autobuild/notify/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,43 +27,51 @@ func TestNotifier(t *testing.T) {
{
Name: "zero deadline",
Countdown: durations(),
Ticks: ticks(now, 0),
Ticks: fakeTicker(now, time.Second, 0),
ConditionDeadline: time.Time{},
NumConditions: 1,
NumCallbacks: 0,
},
{
Name: "no calls",
Countdown: durations(),
Ticks: ticks(now, 0),
Ticks: fakeTicker(now, time.Second, 0),
ConditionDeadline: now,
NumConditions: 1,
NumCallbacks: 0,
},
{
Name: "exactly one call",
Countdown: durations(time.Second),
Ticks: ticks(now, 2),
ConditionDeadline: now.Add(2 * time.Second),
Ticks: fakeTicker(now, time.Second, 1),
ConditionDeadline: now.Add(time.Second),
NumConditions: 2,
NumCallbacks: 1,
},
{
Name: "two calls",
Countdown: durations(4*time.Second, 2*time.Second),
Ticks: ticks(now, 5),
Ticks: fakeTicker(now, time.Second, 5),
ConditionDeadline: now.Add(5 * time.Second),
NumConditions: 6,
NumCallbacks: 2,
},
{
Name: "wrong order should not matter",
Countdown: durations(2*time.Second, 4*time.Second),
Ticks: ticks(now, 5),
Ticks: fakeTicker(now, time.Second, 5),
ConditionDeadline: now.Add(5 * time.Second),
NumConditions: 6,
NumCallbacks: 2,
},
{
Name: "ssh autostop notify",
Countdown: durations(5*time.Minute, time.Minute),
Ticks: fakeTicker(now, 30*time.Second, 120),
ConditionDeadline: now.Add(30 * time.Minute),
NumConditions: 121,
NumCallbacks: 2,
},
}

for _, testCase := range testCases {
Expand Down Expand Up @@ -91,6 +99,7 @@ func TestNotifier(t *testing.T) {
close(ch)
wg.Wait()
require.Equal(t, testCase.NumCallbacks, numCalls.Load())
require.Equal(t, testCase.NumConditions, numConditions.Load())
})
}
}
Expand All @@ -99,11 +108,10 @@ func durations(ds ...time.Duration) []time.Duration {
return ds
}

func ticks(t time.Time, n int) []time.Time {
ts := make([]time.Time, n+1)
ts = append(ts, t)
for i := 0; i < n; i++ {
ts = append(ts, t.Add(time.Duration(n)*time.Second))
func fakeTicker(t time.Time, d time.Duration, n int) []time.Time {
ts := make([]time.Time, 0)
for i := 1; i <= n; i++ {
ts = append(ts, t.Add(time.Duration(n)*d))
}
return ts
}
Expand Down