File: clock.go

package info (click to toggle)
golang-github-hlandau-goutils 0.0~git20160722.0.0cdb66a-2~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 156 kB
  • sloc: makefile: 2
file content (218 lines) | stat: -rw-r--r-- 3,554 bytes parent folder | download | duplicates (4)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package clock

import (
	"sync"
	"time"
)

type Ticker interface {
	C() <-chan time.Time
	Stop()
}

type Clock interface {
	Now() time.Time
	Sleep(time.Duration)
	After(time.Duration) <-chan time.Time
	NewTicker(time.Duration) Ticker
}

var Real Clock

func init() {
	Real = realClock{}
}

type realClock struct{}

func (realClock) Now() time.Time {
	return time.Now()
}

func (realClock) Sleep(d time.Duration) {
	time.Sleep(d)
}

func (realClock) After(d time.Duration) <-chan time.Time {
	return time.After(d)
}

func (realClock) NewTicker(d time.Duration) Ticker {
	return realTicker{time.NewTicker(d)}
}

type realTicker struct {
	*time.Ticker
}

func (rt realTicker) C() <-chan time.Time {
	return rt.Ticker.C
}

type Fake interface {
	Clock
	Advance(time.Duration)
}

// A fast fake clock returns from Sleep calls immediately.
//
// Any waiting operation appears to complete immediately, as though time is
// running infinitely fast, but only when waiting.
func NewFast(from Clock) Fake {
	if from == nil {
		from = Real
	}

	return NewFastAt(from.Now())
}

func NewFastAt(t time.Time) Fake {
	return &fastFake{t: t}
}

type fastFake struct {
	t     time.Time
	mutex sync.RWMutex
}

func (f *fastFake) Now() time.Time {
	f.mutex.RLock()
	defer f.mutex.RUnlock()
	t := f.t
	return t
}

func (f *fastFake) Sleep(d time.Duration) {
	f.mutex.Lock()
	defer f.mutex.Unlock()
	f.t = f.t.Add(d)
}

func (f *fastFake) Advance(d time.Duration) {
	f.Sleep(d)
}

func (f *fastFake) After(d time.Duration) <-chan time.Time {
	f.Sleep(d)
	c := make(chan time.Time, 1)
	c <- f.Now()
	return c
}

func (f *fastFake) NewTicker(d time.Duration) Ticker {
	return newFakeTicker(f, d)
}

// A slow clock doesn't return from Sleep calls until Advance has been called
// enough.
func NewSlow(from Clock) Fake {
	if from == nil {
		from = Real
	}

	return NewSlowAt(from.Now())
}

func NewSlowAt(t time.Time) Fake {
	return &slowFake{t: t}
}

type slowFake struct {
	t        time.Time
	mutex    sync.RWMutex
	sleepers []*slowSleeper
}

type slowSleeper struct {
	until time.Time
	done  chan<- time.Time
}

func (f *slowFake) Now() time.Time {
	f.mutex.RLock()
	defer f.mutex.RUnlock()
	t := f.t
	return t
}

func (f *slowFake) Sleep(d time.Duration) {
	<-f.After(d)
}

func (f *slowFake) Advance(d time.Duration) {
	f.mutex.Lock()
	defer f.mutex.Unlock()

	t2 := f.t.Add(d)
	var newSleepers []*slowSleeper
	for _, s := range f.sleepers {
		if t2.Sub(s.until) >= 0 {
			s.done <- t2
		} else {
			newSleepers = append(newSleepers, s)
		}
	}

	f.sleepers = newSleepers
	f.t = t2
}

func (f *slowFake) After(d time.Duration) <-chan time.Time {
	f.mutex.Lock()
	defer f.mutex.Unlock()

	done := make(chan time.Time, 1)
	if d == 0 {
		done <- f.t
		return done
	}

	s := &slowSleeper{
		until: f.t.Add(d),
		done:  done,
	}

	f.sleepers = append(f.sleepers, s)
	return done
}

func (f *slowFake) NewTicker(d time.Duration) Ticker {
	return newFakeTicker(f, d)
}

type fakeTicker struct {
	clock    Clock
	c        chan time.Time
	stopChan chan struct{}
}

func newFakeTicker(c Clock, d time.Duration) Ticker {
	ft := &fakeTicker{
		clock:    c,
		c:        make(chan time.Time, 1),
		stopChan: make(chan struct{}),
	}
	go ft.tickLoop(d)
	return ft
}

func (ft *fakeTicker) tickLoop(d time.Duration) {
	for {
		ft.clock.Sleep(d)
		select {
		case ft.c <- ft.clock.Now():
		case <-ft.stopChan:
			return
		}
	}
}

func (ft *fakeTicker) C() <-chan time.Time {
	return ft.c
}

func (ft *fakeTicker) Stop() {
	close(ft.stopChan)
}

// © 2015 Jonathan Boulle   Apache 2.0 License