|
1 | 1 | package clock |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "testing" |
4 | 5 | "time" |
5 | 6 |
|
6 | | - "github.com/benbjohnson/clock" |
| 7 | + "github.com/jonboulle/clockwork" |
7 | 8 | ) |
8 | 9 |
|
9 | | -// init initializes the Clock variable with a real Clock. |
10 | | -func init() { |
11 | | - Restore() |
| 10 | +type Clock clockwork.Clock |
| 11 | + |
| 12 | +type Timer clockwork.Timer |
| 13 | + |
| 14 | +type Ticker clockwork.Ticker |
| 15 | + |
| 16 | +// clk represents a global clock. |
| 17 | +var clk Clock = clockwork.NewRealClock() |
| 18 | + |
| 19 | +// Mock replaces the clk with a mock frozen at the given time and returns it. |
| 20 | +func Mock(t *testing.T, now time.Time) *clockwork.FakeClock { |
| 21 | + fake := clockwork.NewFakeClockAt(now) |
| 22 | + Set(fake) |
| 23 | + t.Cleanup(func() { |
| 24 | + clk = clockwork.NewRealClock() |
| 25 | + }) |
| 26 | + return fake |
12 | 27 | } |
13 | 28 |
|
14 | | -// Clock represents a global clock. |
15 | | -var Clock clock.Clock |
| 29 | +// Set sets the global clock. |
| 30 | +func Set(c Clock) { |
| 31 | + clk = c |
| 32 | +} |
16 | 33 |
|
17 | 34 | // After waits for the duration to elapse and then sends the current time |
18 | 35 | func After(d time.Duration) <-chan time.Time { |
19 | | - return Clock.After(d) |
| 36 | + return clk.After(d) |
20 | 37 | } |
21 | 38 |
|
22 | 39 | // AfterFunc waits for the duration to elapse and then calls f in its own goroutine. |
23 | | -func AfterFunc(d time.Duration, f func()) *clock.Timer { |
24 | | - return Clock.AfterFunc(d, f) |
| 40 | +func AfterFunc(d time.Duration, f func()) Timer { |
| 41 | + return clk.AfterFunc(d, f) |
25 | 42 | } |
26 | 43 |
|
27 | 44 | // Now returns the current local time. |
28 | 45 | func Now() time.Time { |
29 | | - return Clock.Now() |
| 46 | + return clk.Now() |
30 | 47 | } |
31 | 48 |
|
32 | 49 | // Since returns the time elapsed since t. |
33 | 50 | func Since(t time.Time) time.Duration { |
34 | | - return Clock.Since(t) |
| 51 | + return clk.Since(t) |
35 | 52 | } |
36 | 53 |
|
37 | 54 | // Sleep pauses the current goroutine for at least the duration d. |
38 | 55 | func Sleep(d time.Duration) { |
39 | | - Clock.Sleep(d) |
| 56 | + clk.Sleep(d) |
40 | 57 | } |
41 | 58 |
|
42 | 59 | // Tick is a convenience wrapper for NewTicker providing access to the ticking channel only. |
43 | 60 | func Tick(d time.Duration) <-chan time.Time { |
44 | | - return Clock.Tick(d) |
| 61 | + return clk.NewTicker(d).Chan() |
45 | 62 | } |
46 | 63 |
|
47 | | -// Ticker returns a new Ticker containing a channel that will send the |
| 64 | +// NewTicker returns a new Ticker containing a channel that will send the |
48 | 65 | // time with a period specified by the duration argument. |
49 | | -func Ticker(d time.Duration) *clock.Ticker { |
50 | | - return Clock.Ticker(d) |
51 | | -} |
52 | | - |
53 | | -// Timer creates a new Timer that will send the current time on its channel after at least duration d. |
54 | | -func Timer(d time.Duration) *clock.Timer { |
55 | | - return Clock.Timer(d) |
56 | | -} |
57 | | - |
58 | | -// Mock replaces the Clock with a mock frozen at the given time and returns it. |
59 | | -func Mock(now time.Time) *clock.Mock { |
60 | | - mock := clock.NewMock() |
61 | | - mock.Set(now) |
62 | | - |
63 | | - Clock = mock |
64 | | - |
65 | | - return mock |
| 66 | +func NewTicker(d time.Duration) clockwork.Ticker { |
| 67 | + return clk.NewTicker(d) |
66 | 68 | } |
67 | 69 |
|
68 | | -// Restore replaces the Clock with the real clock. |
69 | | -func Restore() { |
70 | | - Clock = clock.New() |
| 70 | +// NewTimer creates a new Timer that will send the current time on its channel after at least duration d. |
| 71 | +func NewTimer(d time.Duration) clockwork.Timer { |
| 72 | + return clk.NewTimer(d) |
71 | 73 | } |
0 commit comments