Repository navigation
Expand file tree
/
Copy pathiris_builder_worker_internal_test.go
More file actions
141 lines (109 loc) · 2.88 KB
/
Copy pathiris_builder_worker_internal_test.go
File metadata and controls
141 lines (109 loc) · 2.88 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
package iris
import (
stdContext "context"
"sync"
"testing"
"time"
)
// interleavingWorker records whether Close ever ran while Run was live, which
// is the failure the start/stop mutual exclusion exists to prevent.
type interleavingWorker struct {
mu sync.Mutex
running bool
badness int
closes int
}
func (w *interleavingWorker) Run(ctx stdContext.Context) error {
w.mu.Lock()
w.running = true
w.mu.Unlock()
<-ctx.Done()
w.mu.Lock()
w.running = false
w.mu.Unlock()
return nil
}
func (w *interleavingWorker) Stop(stdContext.Context) error { return nil }
func (w *interleavingWorker) Close() error {
w.mu.Lock()
defer w.mu.Unlock()
w.closes++
if w.running {
w.badness++
}
return nil
}
func newWorkerTestBuilder() *builder {
return &builder{app: New(), once: new(sync.Once)}
}
// The shutdown step and startWorker cannot interleave: while the start
// decision is being taken, the stop decision waits. Holding startMu from the
// test is what makes this deterministic; the window it closes is otherwise
// only reachable when a signal arrives inside Build.
func TestWorkerStartAndStopAreMutuallyExclusive(t *testing.T) {
g := newWorkerTestBuilder()
w := &interleavingWorker{}
g.registerWorker(w)
reg := g.workers[0]
reg.startMu.Lock() // stand in for startWorker having taken the decision.
stopped := make(chan struct{})
go func() {
_ = g.stopWorker(reg)(stdContext.Background())
close(stopped)
}()
select {
case <-stopped:
t.Fatal("expected the shutdown step to wait for the start decision")
case <-time.After(100 * time.Millisecond):
}
reg.startMu.Unlock()
select {
case <-stopped:
case <-time.After(5 * time.Second):
t.Fatal("timeout waiting for the shutdown step")
}
// The stop decision won, so a later start must launch nothing: a goroutine
// started here would run against a worker which is already closed.
g.startWorker(reg)
if reg.started.Load() {
t.Fatal("expected a stopped worker not to be started")
}
w.mu.Lock()
defer w.mu.Unlock()
if w.closes != 1 {
t.Fatalf("expected exactly one Close, got: %d", w.closes)
}
if w.badness != 0 {
t.Fatalf("Close ran while Run was live %d time(s)", w.badness)
}
}
// The same invariant under contention, which is the shape a signal handler
// racing Build actually produces.
func TestWorkerStartAndStopUnderContention(t *testing.T) {
for range 200 {
g := newWorkerTestBuilder()
w := &interleavingWorker{}
g.registerWorker(w)
reg := g.workers[0]
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
g.startWorker(reg)
}()
go func() {
defer wg.Done()
_ = g.stopWorker(reg)(stdContext.Background())
}()
wg.Wait()
if reg.started.Load() {
<-reg.done // the goroutine was launched: it must return on its own.
}
w.mu.Lock()
badness := w.badness
w.mu.Unlock()
if badness != 0 {
t.Fatalf("Close ran while Run was live %d time(s)", badness)
}
}
}