Repository navigation
Expand file tree
/
Copy pathrecentflush_test.go
More file actions
263 lines (241 loc) · 7.44 KB
/
Copy pathrecentflush_test.go
File metadata and controls
263 lines (241 loc) · 7.44 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package main
import (
"sync/atomic"
"testing"
"time"
)
type flushCounts struct {
saves, rebuilds atomic.Int32
}
func newCountingFlusher(delay, maxHold, saveTakes time.Duration) (*recentFlusher, *flushCounts) {
c := &flushCounts{}
save := func() {
time.Sleep(saveTakes)
c.saves.Add(1)
}
rebuild := func() { c.rebuilds.Add(1) }
return newRecentFlusher(delay, maxHold, save, rebuild, func(fn func()) { fn() }), c
}
func waitFor(t *testing.T, cond func() bool) {
t.Helper()
deadline := time.Now().Add(2 * time.Second)
for !cond() {
if time.Now().After(deadline) {
t.Fatal("condition not met within 2s")
}
time.Sleep(time.Millisecond)
}
}
// newBlockingFlusher returns a flusher whose first save signals started and
// then waits for release, so a test acts while that save is in progress.
func newBlockingFlusher(delay time.Duration) (f *recentFlusher, c *flushCounts, started, release chan struct{}) {
c = &flushCounts{}
started, release = make(chan struct{}), make(chan struct{})
save := func() {
if c.saves.Load() == 0 {
close(started)
<-release
}
c.saves.Add(1)
}
rebuild := func() { c.rebuilds.Add(1) }
return newRecentFlusher(delay, time.Hour, save, rebuild, func(fn func()) { fn() }), c, started, release
}
// returnsWhileBlocked reports whether fn returns within 50ms.
func returnsWhileBlocked(fn func()) (done chan struct{}, returned bool) {
done = make(chan struct{})
go func() {
fn()
close(done)
}()
select {
case <-done:
return done, true
case <-time.After(50 * time.Millisecond):
return done, false
}
}
func waitDone(t *testing.T, done chan struct{}) {
t.Helper()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("did not return within 2s")
}
}
func TestRecentFlushMergesQuickOpensIntoOneSave(t *testing.T) {
f, c := newCountingFlusher(30*time.Millisecond, time.Hour, 0)
for range 5 {
f.Record()
}
waitFor(t, func() bool { return c.rebuilds.Load() == 1 })
time.Sleep(60 * time.Millisecond)
if c.saves.Load() != 1 || c.rebuilds.Load() != 1 {
t.Fatalf("saves=%d rebuilds=%d, want 1 and 1", c.saves.Load(), c.rebuilds.Load())
}
}
// Each open takes longer than the delay; the record lands after the open ends
// and before the next one starts, as on every open path.
func TestRecentFlushSavesSlowOpensInARowOnce(t *testing.T) {
f, c := newCountingFlusher(100*time.Millisecond, time.Hour, 0)
for range 3 {
ended := f.OpenStarted()
time.Sleep(150 * time.Millisecond)
ended()
f.Record()
}
if c.saves.Load() != 0 {
t.Fatalf("saved %d times during the run, want 0", c.saves.Load())
}
waitFor(t, func() bool { return c.saves.Load() == 1 })
time.Sleep(150 * time.Millisecond)
if c.saves.Load() != 1 {
t.Fatalf("saves=%d, want 1", c.saves.Load())
}
}
func TestRecentFlushARunLongerThanMaxHoldStillSavesOnce(t *testing.T) {
f, c := newCountingFlusher(100*time.Millisecond, 300*time.Millisecond, 0)
for range 6 {
ended := f.OpenStarted()
time.Sleep(150 * time.Millisecond)
ended()
f.Record()
}
if c.saves.Load() != 0 {
t.Fatalf("saved %d times during a 900ms run of 150ms opens, want 0", c.saves.Load())
}
waitFor(t, func() bool { return c.saves.Load() == 1 })
}
func TestRecentFlushWaitsForEveryOpenInFlight(t *testing.T) {
f, c := newCountingFlusher(time.Millisecond, time.Hour, 0)
endA := f.OpenStarted()
endB := f.OpenStarted()
f.Record()
endA()
time.Sleep(20 * time.Millisecond)
if c.saves.Load() != 0 {
t.Fatal("saved with an open still in flight")
}
endB()
waitFor(t, func() bool { return c.saves.Load() == 1 })
}
// startStuckOpen starts an open whose start lies past maxHold, as for an open
// that never returned.
func startStuckOpen(f *recentFlusher) (ended func()) {
ended = f.OpenStarted()
f.mu.Lock()
f.opens[f.nextOpen-1] = time.Now().Add(-2 * f.maxHold)
f.mu.Unlock()
return ended
}
func TestRecentFlushAnOpenInFlightHoldsTheSaveUntilMaxHold(t *testing.T) {
f, c := newCountingFlusher(time.Millisecond, 300*time.Millisecond, 0)
f.OpenStarted()
f.Record()
time.Sleep(50 * time.Millisecond)
if c.saves.Load() != 0 {
t.Fatal("saved with an open in flight before maxHold")
}
waitFor(t, func() bool { return c.saves.Load() == 1 })
}
func TestRecentFlushAStuckOpenNoLongerHoldsTheSave(t *testing.T) {
f, c := newCountingFlusher(time.Millisecond, time.Hour, 0)
startStuckOpen(f)
f.Record()
waitFor(t, func() bool { return c.saves.Load() == 1 })
}
func TestRecentFlushAStuckOpenDoesNotReleaseTheHoldOfAFreshOne(t *testing.T) {
f, c := newCountingFlusher(time.Millisecond, time.Hour, 0)
startStuckOpen(f)
ended := f.OpenStarted()
f.Record()
time.Sleep(50 * time.Millisecond)
if c.saves.Load() != 0 {
t.Fatal("saved while a fresh open was in flight")
}
ended()
waitFor(t, func() bool { return c.saves.Load() == 1 })
}
func TestRecentFlushAnEndThatHeldNothingDoesNotDelayThePendingSave(t *testing.T) {
f, c := newCountingFlusher(300*time.Millisecond, time.Hour, 0)
endStuck := startStuckOpen(f)
f.Record()
time.Sleep(150 * time.Millisecond)
endStuck()
// Saved at 300ms; restarting the delay at this end would save at 450ms.
time.Sleep(250 * time.Millisecond)
if c.saves.Load() != 1 {
t.Fatalf("saves=%d 400ms after the record, want 1", c.saves.Load())
}
}
func TestRecentFlushCloseSavesAWaitingChangeAndStopsTheTimer(t *testing.T) {
f, c := newCountingFlusher(20*time.Millisecond, time.Hour, 0)
f.Record()
f.Close()
if c.saves.Load() != 1 {
t.Fatalf("saves=%d after Close, want 1", c.saves.Load())
}
time.Sleep(50 * time.Millisecond)
if c.saves.Load() != 1 || c.rebuilds.Load() != 0 {
t.Fatalf("saves=%d rebuilds=%d after the delay, want 1 and 0", c.saves.Load(), c.rebuilds.Load())
}
}
func TestRecentFlushSaveNowIgnoresAnOpenInFlight(t *testing.T) {
f, c := newCountingFlusher(time.Hour, time.Hour, 0)
f.OpenStarted()
f.SaveNow()
if c.saves.Load() != 1 || c.rebuilds.Load() != 1 {
t.Fatalf("saves=%d rebuilds=%d, want 1 and 1", c.saves.Load(), c.rebuilds.Load())
}
}
func TestRecentFlushRebuildReachingTheUIThreadAfterCloseDoesNothing(t *testing.T) {
c := &flushCounts{}
var queued func()
f := newRecentFlusher(time.Hour, time.Hour, func() { c.saves.Add(1) }, func() { c.rebuilds.Add(1) }, func(fn func()) { queued = fn })
f.SaveNow()
f.Close()
queued()
if c.rebuilds.Load() != 0 {
t.Fatalf("rebuilds=%d after Close, want 0", c.rebuilds.Load())
}
}
func TestRecentFlushCloseWaitsForASaveInProgress(t *testing.T) {
f, c, started, release := newBlockingFlusher(time.Millisecond)
f.Record()
<-started
done, returned := returnsWhileBlocked(f.Close)
if returned {
t.Fatal("Close returned with a save in progress")
}
close(release)
waitDone(t, done)
if c.saves.Load() != 1 {
t.Fatalf("saves=%d after Close, want 1", c.saves.Load())
}
time.Sleep(10 * time.Millisecond)
if c.rebuilds.Load() != 0 {
t.Fatalf("menu rebuilt %d times after Close, want 0", c.rebuilds.Load())
}
}
func TestRecentFlushSavesAnOpenRecordedDuringASave(t *testing.T) {
f, c, started, release := newBlockingFlusher(time.Millisecond)
f.Record()
<-started
f.Record()
close(release)
waitFor(t, func() bool { return c.saves.Load() == 2 })
}
func TestRecentFlushSaveNowReturnsOnlyAfterASaveThatStartedAfterIt(t *testing.T) {
f, c, started, release := newBlockingFlusher(time.Millisecond)
f.Record()
<-started
done, returned := returnsWhileBlocked(f.SaveNow)
if returned {
t.Fatal("SaveNow returned while the save that began before it was in progress")
}
close(release)
waitDone(t, done)
if c.saves.Load() != 2 {
t.Fatalf("saves=%d when SaveNow returned, want 2", c.saves.Load())
}
}