Debian Codegraph @

internal/holsterv4/clock/clock.go

raw
1 //go:build !holster_test_mode
2
3 // Package clock provides the same functions as the system package time. In
4 // production it forwards all calls to the system time package, but in tests
5 // the time can be frozen by calling Freeze function and from that point it has
6 // to be advanced manually with Advance function making all scheduled calls
7 // deterministic.
8 //
9 // The functions provided by the package have the same parameters and return
10 // values as their system counterparts with a few exceptions. Where either
11 // *time.Timer or *time.Ticker is returned by a system function, the clock
12 // package counterpart returns clock.Timer or clock.Ticker interface
13 // respectively. The interfaces provide API as respective structs except C is
14 // not a channel, but a function that returns <-chan time.Time.
15 package clock
16
17 import "time"
18
19 var (
20 frozenAt time.Time
21 realtime = &systemTime{}
22 provider Clock = realtime
23 )
24
25 // Freeze after this function is called all time related functions start
26 // generate deterministic timers that are triggered by Advance function. It is
27 // supposed to be used in tests only. Returns an Unfreezer so it can be a
28 // one-liner in tests: defer clock.Freeze(clock.Now()).Unfreeze()
29 func Freeze(now time.Time) Unfreezer {
30 frozenAt = now.UTC()
31 provider = &frozenTime{now: now}
32 return Unfreezer{}
33 }
34
35 type Unfreezer struct{}
36
37 func (u Unfreezer) Unfreeze() {
38 Unfreeze()
39 }
40
41 // Unfreeze reverses effect of Freeze.
42 func Unfreeze() {
43 provider = realtime
44 }
45
46 // Realtime returns a clock provider wrapping the SDK's time package. It is
47 // supposed to be used in tests when time is frozen to schedule test timeouts.
48 func Realtime() Clock {
49 return realtime
50 }
51
52 // Makes the deterministic time move forward by the specified duration, firing
53 // timers along the way in the natural order. It returns how much time has
54 // passed since it was frozen. So you can assert on the return value in tests
55 // to make it explicit where you stand on the deterministic time scale.
56 func Advance(d time.Duration) time.Duration {
57 ft, ok := provider.(*frozenTime)
58 if !ok {
59 panic("Freeze time first!")
60 }
61 ft.advance(d)
62 return Now().UTC().Sub(frozenAt)
63 }
64
65 // Wait4Scheduled blocks until either there are n or more scheduled events, or
66 // the timeout elapses. It returns true if the wait condition has been met
67 // before the timeout expired, false otherwise.
68 func Wait4Scheduled(count int, timeout time.Duration) bool {
69 return provider.Wait4Scheduled(count, timeout)
70 }
71
72 // Now see time.Now.
73 func Now() time.Time {
74 return provider.Now()
75 }
76
77 // Sleep see time.Sleep.
78 func Sleep(d time.Duration) {
79 provider.Sleep(d)
80 }
81
82 // After see time.After.
83 func After(d time.Duration) <-chan time.Time {
84 return provider.After(d)
85 }
86
87 // NewTimer see time.NewTimer.
88 func NewTimer(d time.Duration) Timer {
89 return provider.NewTimer(d)
90 }
91
92 // AfterFunc see time.AfterFunc.
93 func AfterFunc(d time.Duration, f func()) Timer {
94 return provider.AfterFunc(d, f)
95 }
96
97 // NewTicker see time.Ticker.
98 func NewTicker(d time.Duration) Ticker {
99 return provider.NewTicker(d)
100 }
101
102 // Tick see time.Tick.
103 func Tick(d time.Duration) <-chan time.Time {
104 return provider.Tick(d)
105 }