Debian Codegraph @

clock/clock.go

raw symbols SCIP
1 package clock
2
3 import (
4 "sync"
5 "time"
6 )
7
8 type Ticker interface {
9 C() <-chan time.Time
10 Stop()
11 }
12
13 type Clock interface {
14 Now() time.Time
15 Sleep(time.Duration)
16 After(time.Duration) <-chan time.Time
17 NewTicker(time.Duration) Ticker
18 }
19
20 var Real Clock
21
22 func init() {
23 Real = realClock{}
24 }
25
26 type realClock struct{}
27
28 func (realClock) Now() time.Time {
29 return time.Now()
30 }
31
32 func (realClock) Sleep(d time.Duration) {
33 time.Sleep(d)
34 }
35
36 func (realClock) After(d time.Duration) <-chan time.Time {
37 return time.After(d)
38 }
39
40 func (realClock) NewTicker(d time.Duration) Ticker {
41 return realTicker{time.NewTicker(d)}
42 }
43
44 type realTicker struct {
45 *time.Ticker
46 }
47
48 func (rt realTicker) C() <-chan time.Time {
49 return rt.Ticker.C
50 }
51
52 type Fake interface {
53 Clock
54 Advance(time.Duration)
55 }
56
57 // A fast fake clock returns from Sleep calls immediately.
58 //
59 // Any waiting operation appears to complete immediately, as though time is
60 // running infinitely fast, but only when waiting.
61 func NewFast(from Clock) Fake {
62 if from == nil {
63 from = Real
64 }
65
66 return NewFastAt(from.Now())
67 }
68
69 func NewFastAt(t time.Time) Fake {
70 return &fastFake{t: t}
71 }
72
73 type fastFake struct {
74 t time.Time
75 mutex sync.RWMutex
76 }
77
78 func (f *fastFake) Now() time.Time {
79 f.mutex.RLock()
80 defer f.mutex.RUnlock()
81 t := f.t
82 return t
83 }
84
85 func (f *fastFake) Sleep(d time.Duration) {
86 f.mutex.Lock()
87 defer f.mutex.Unlock()
88 f.t = f.t.Add(d)
89 }
90
91 func (f *fastFake) Advance(d time.Duration) {
92 f.Sleep(d)
93 }
94
95 func (f *fastFake) After(d time.Duration) <-chan time.Time {
96 f.Sleep(d)
97 c := make(chan time.Time, 1)
98 c <- f.Now()
99 return c
100 }
101
102 func (f *fastFake) NewTicker(d time.Duration) Ticker {
103 return newFakeTicker(f, d)
104 }
105
106 // A slow clock doesn't return from Sleep calls until Advance has been called
107 // enough.
108 func NewSlow(from Clock) Fake {
109 if from == nil {
110 from = Real
111 }
112
113 return NewSlowAt(from.Now())
114 }
115
116 func NewSlowAt(t time.Time) Fake {
117 return &slowFake{t: t}
118 }
119
120 type slowFake struct {
121 t time.Time
122 mutex sync.RWMutex
123 sleepers []*slowSleeper
124 }
125
126 type slowSleeper struct {
127 until time.Time
128 done chan<- time.Time
129 }
130
131 func (f *slowFake) Now() time.Time {
132 f.mutex.RLock()
133 defer f.mutex.RUnlock()
134 t := f.t
135 return t
136 }
137
138 func (f *slowFake) Sleep(d time.Duration) {
139 <-f.After(d)
140 }
141
142 func (f *slowFake) Advance(d time.Duration) {
143 f.mutex.Lock()
144 defer f.mutex.Unlock()
145
146 t2 := f.t.Add(d)
147 var newSleepers []*slowSleeper
148 for _, s := range f.sleepers {
149 if t2.Sub(s.until) >= 0 {
150 s.done <- t2
151 } else {
152 newSleepers = append(newSleepers, s)
153 }
154 }
155
156 f.sleepers = newSleepers
157 f.t = t2
158 }
159
160 func (f *slowFake) After(d time.Duration) <-chan time.Time {
161 f.mutex.Lock()
162 defer f.mutex.Unlock()
163
164 done := make(chan time.Time, 1)
165 if d == 0 {
166 done <- f.t
167 return done
168 }
169
170 s := &slowSleeper{
171 until: f.t.Add(d),
172 done: done,
173 }
174
175 f.sleepers = append(f.sleepers, s)
176 return done
177 }
178
179 func (f *slowFake) NewTicker(d time.Duration) Ticker {
180 return newFakeTicker(f, d)
181 }
182
183 type fakeTicker struct {
184 clock Clock
185 c chan time.Time
186 stopChan chan struct{}
187 }
188
189 func newFakeTicker(c Clock, d time.Duration) Ticker {
190 ft := &fakeTicker{
191 clock: c,
192 c: make(chan time.Time, 1),
193 stopChan: make(chan struct{}),
194 }
195 go ft.tickLoop(d)
196 return ft
197 }
198
199 func (ft *fakeTicker) tickLoop(d time.Duration) {
200 for {
201 ft.clock.Sleep(d)
202 select {
203 case ft.c <- ft.clock.Now():
204 case <-ft.stopChan:
205 return
206 }
207 }
208 }
209
210 func (ft *fakeTicker) C() <-chan time.Time {
211 return ft.c
212 }
213
214 func (ft *fakeTicker) Stop() {
215 close(ft.stopChan)
216 }
217
218 // © 2015 Jonathan Boulle Apache 2.0 License