Debian Codegraph @

internal/holsterv4/clock/clock_mutex.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 (
18 "sync"
19 "time"
20 )
21
22 var (
23 frozenAt time.Time
24 realtime = &systemTime{}
25 provider Clock = realtime
26 rwMutex = sync.RWMutex{}
27 )
28
29 // Freeze after this function is called all time related functions start
30 // generate deterministic timers that are triggered by Advance function. It is
31 // supposed to be used in tests only. Returns an Unfreezer so it can be a
32 // one-liner in tests: defer clock.Freeze(clock.Now()).Unfreeze()
33 func Freeze(now time.Time) Unfreezer {
34 frozenAt = now.UTC()
35 rwMutex.Lock()
36 defer rwMutex.Unlock()
37 provider = &frozenTime{now: now}
38 return Unfreezer{}
39 }
40
41 type Unfreezer struct{}
42
43 func (u Unfreezer) Unfreeze() {
44 Unfreeze()
45 }
46
47 // Unfreeze reverses effect of Freeze.
48 func Unfreeze() {
49 rwMutex.Lock()
50 defer rwMutex.Unlock()
51 provider = realtime
52 }
53
54 // Realtime returns a clock provider wrapping the SDK's time package. It is
55 // supposed to be used in tests when time is frozen to schedule test timeouts.
56 func Realtime() Clock {
57 return realtime
58 }
59
60 // Makes the deterministic time move forward by the specified duration, firing
61 // timers along the way in the natural order. It returns how much time has
62 // passed since it was frozen. So you can assert on the return value in tests
63 // to make it explicit where you stand on the deterministic time scale.
64 func Advance(d time.Duration) time.Duration {
65 rwMutex.RLock()
66 ft, ok := provider.(*frozenTime)
67 rwMutex.RUnlock()
68 if !ok {
69 panic("Freeze time first!")
70 }
71 ft.advance(d)
72 return Now().UTC().Sub(frozenAt)
73 }
74
75 // Wait4Scheduled blocks until either there are n or more scheduled events, or
76 // the timeout elapses. It returns true if the wait condition has been met
77 // before the timeout expired, false otherwise.
78 func Wait4Scheduled(count int, timeout time.Duration) bool {
79 rwMutex.RLock()
80 defer rwMutex.RUnlock()
81 return provider.Wait4Scheduled(count, timeout)
82 }
83
84 // Now see time.Now.
85 func Now() time.Time {
86 rwMutex.RLock()
87 defer rwMutex.RUnlock()
88 return provider.Now()
89 }
90
91 // Sleep see time.Sleep.
92 func Sleep(d time.Duration) {
93 rwMutex.RLock()
94 defer rwMutex.RUnlock()
95 provider.Sleep(d)
96 }
97
98 // After see time.After.
99 func After(d time.Duration) <-chan time.Time {
100 rwMutex.RLock()
101 defer rwMutex.RUnlock()
102 return provider.After(d)
103 }
104
105 // NewTimer see time.NewTimer.
106 func NewTimer(d time.Duration) Timer {
107 rwMutex.RLock()
108 defer rwMutex.RUnlock()
109 return provider.NewTimer(d)
110 }
111
112 // AfterFunc see time.AfterFunc.
113 func AfterFunc(d time.Duration, f func()) Timer {
114 rwMutex.RLock()
115 defer rwMutex.RUnlock()
116 return provider.AfterFunc(d, f)
117 }
118
119 // NewTicker see time.Ticker.
120 func NewTicker(d time.Duration) Ticker {
121 rwMutex.RLock()
122 defer rwMutex.RUnlock()
123 return provider.NewTicker(d)
124 }
125
126 // Tick see time.Tick.
127 func Tick(d time.Duration) <-chan time.Time {
128 rwMutex.RLock()
129 defer rwMutex.RUnlock()
130 return provider.Tick(d)
131 }