This repository has been archived by the owner on Jan 9, 2020. It is now read-only.
forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.go
73 lines (63 loc) · 1.7 KB
/
clock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package testing
import (
"sort"
"sync"
"time"
)
// Clock implements a mock clock.Clock for testing purposes.
type Clock struct {
mu sync.Mutex
now time.Time
alarms []alarm
}
// NewClock returns a new clock set to the supplied time.
func NewClock(now time.Time) *Clock {
return &Clock{now: now}
}
// Now is part of the clock.Clock interface.
func (clock *Clock) Now() time.Time {
clock.mu.Lock()
defer clock.mu.Unlock()
return clock.now
}
// After is part of the clock.Clock interface.
func (clock *Clock) After(d time.Duration) <-chan time.Time {
clock.mu.Lock()
defer clock.mu.Unlock()
notify := make(chan time.Time, 1)
if d <= 0 {
notify <- clock.now
} else {
clock.alarms = append(clock.alarms, alarm{clock.now.Add(d), notify})
sort.Sort(byTime(clock.alarms))
}
return notify
}
// Advance advances the result of Now by the supplied duration, and sends
// the "current" time on all alarms which are no longer "in the future".
func (clock *Clock) Advance(d time.Duration) {
clock.mu.Lock()
defer clock.mu.Unlock()
clock.now = clock.now.Add(d)
rung := 0
for _, alarm := range clock.alarms {
if clock.now.Before(alarm.time) {
break
}
alarm.notify <- clock.now
rung++
}
clock.alarms = clock.alarms[rung:]
}
// alarm records the time at which we're expected to send on notify.
type alarm struct {
time time.Time
notify chan time.Time
}
// byTime is used to sort alarms by time.
type byTime []alarm
func (a byTime) Len() int { return len(a) }
func (a byTime) Less(i, j int) bool { return a[i].time.Before(a[j].time) }
func (a byTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }