forked from vanadium/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tester.go
238 lines (218 loc) · 6.26 KB
/
tester.go
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
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package concurrency
import (
"sync"
"time"
)
// globalT stores a pointer to the global instance of the tester.
var (
globalT *Tester = nil
)
// T returns the global instance of the tester.
func T() *Tester {
return globalT
}
// Setup sets up a new instance of the tester.
func Init(setup, body, cleanup func()) *Tester {
tree := newState(nil, 0)
tree.visited = true
seeds := &stack{}
seeds.Push(tree)
tree.seeded = true
globalT = &Tester{
tree: tree,
seeds: seeds,
setup: setup,
body: body,
cleanup: cleanup,
}
return globalT
}
// Cleanup destroys the existing instance of the tester.
func Finish() {
globalT = nil
}
// Tester represents an instance of the systematic test.
type Tester struct {
// enabled records whether the tester is to be used or not.
enabled bool
// execution represents the currently explored execution.
execution *execution
// tree represents the current state of the exploration of the space
// of all possible interleavings of concurrent transitions.
tree *state
// seeds is records the collection of scheduling alternatives to be
// explored in the future.
seeds *stack
// setup is a function that is executed before an instance of the
// test is started. It is assumed to always produce the same initial
// state.
setup func()
// body is a function that implements the body of the test.
body func()
// cleanup is a function that is executed after an instance of a
// test instance terminates.
cleanup func()
}
// Explore explores the space of possible test schedules until the
// state space is fully exhausted.
func (t *Tester) Explore() (int, error) {
return t.explore(0, 0)
}
// ExploreFor explores the space of possible test schedules until the
// state space is fully exhausted or the given duration elapses,
// whichever occurs first.
func (t *Tester) ExploreFor(d time.Duration) (int, error) {
return t.explore(0, d)
}
// ExploreN explores the space of possible test schedules until the
// state space is fully exhausted or the given number of schedules is
// explored, whichever occurs first.
func (t *Tester) ExploreN(n int) (int, error) {
return t.explore(n, 0)
}
// MutexLock implements the logic related to modeling and scheduling
// an execution of "m.Lock()".
func (t *Tester) MutexLock(m *sync.Mutex) {
if t.enabled {
done := make(chan struct{})
request := mutexLockRequest{defaultRequest{done: done}, m}
t.execution.requests <- request
<-done
} else {
m.Lock()
}
}
// MutexUnlock implements the logic related to modeling and scheduling
// an execution of "m.Unlock()".
func (t *Tester) MutexUnlock(m *sync.Mutex) {
if t.enabled {
done := make(chan struct{})
request := mutexUnlockRequest{defaultRequest{done: done}, m}
t.execution.requests <- request
<-done
} else {
m.Unlock()
}
}
// RWMutexLock implements the logic related to modeling and scheduling
// an execution of "rw.Lock()".
func (t *Tester) RWMutexLock(rw *sync.RWMutex) {
if t.enabled {
done := make(chan struct{})
request := rwMutexLockRequest{defaultRequest{done: done}, false, rw}
t.execution.requests <- request
<-done
} else {
rw.Lock()
}
}
// RWMutexRLock implements the logic related to modeling and
// scheduling an execution of "rw.RLock()".
func (t *Tester) RWMutexRLock(rw *sync.RWMutex) {
if t.enabled {
done := make(chan struct{})
request := rwMutexLockRequest{defaultRequest{done: done}, true, rw}
t.execution.requests <- request
<-done
} else {
rw.RLock()
}
}
// RWMutexRUnlock implements the logic related to modeling and
// scheduling an execution of "rw.RUnlock()".
func (t *Tester) RWMutexRUnlock(rw *sync.RWMutex) {
if t.enabled {
done := make(chan struct{})
request := rwMutexUnlockRequest{defaultRequest{done: done}, true, rw}
t.execution.requests <- request
<-done
} else {
rw.RUnlock()
}
}
// RWMutexUnlock implements the logic related to modeling and
// scheduling an execution of "rw.Unlock()".
func (t *Tester) RWMutexUnlock(rw *sync.RWMutex) {
if t.enabled {
done := make(chan struct{})
request := rwMutexUnlockRequest{defaultRequest{done: done}, false, rw}
t.execution.requests <- request
<-done
} else {
rw.Unlock()
}
}
// Start implements the logic related to modeling and scheduling an
// execution of "go fn()".
func Start(fn func()) {
t := globalT
if t != nil && t.enabled {
done1 := make(chan struct{})
reply := make(chan TID)
request1 := goRequest{defaultRequest{done: done1}, reply}
t.execution.requests <- request1
tid := <-reply
<-done1
go t.startHelper(tid, fn)
done2 := make(chan struct{})
request2 := goParentRequest{defaultRequest{done: done2}}
t.execution.requests <- request2
<-done2
} else {
fn()
}
}
// Exit implements the logic related to modeling and scheduling thread
// termination.
func Exit() {
t := globalT
if t != nil && t.enabled {
done := make(chan struct{})
request := goExitRequest{defaultRequest{done: done}}
t.execution.requests <- request
<-done
}
}
// startHelper is a wrapper used by the implementation of Start() to
// make sure the child thread is registered with the correct
// identifier.
func (t *Tester) startHelper(tid TID, fn func()) {
done := make(chan struct{})
request := goChildRequest{defaultRequest{done: done}, tid}
t.execution.requests <- request
<-done
fn()
}
func (t *Tester) explore(n int, d time.Duration) (int, error) {
niterations := 0
start := time.Now()
for !t.seeds.Empty() &&
(n == 0 || niterations < n) &&
(d == 0 || time.Since(start) < d) {
t.setup()
seed, err := t.seeds.Pop()
if err != nil {
panic("Corrupted stack.\n")
}
strategy := seed.generateStrategy()
t.execution = newExecution(strategy)
t.enabled = true
if err := t.tree.addBranch(t.execution.Run(t.body), t.seeds); err != nil {
t.enabled = false
return niterations, err
}
t.enabled = false
// Sort the seeds because dynamic partial order reduction might
// have added elements that violate the depth-first ordering of
// seeds. The depth-first ordering is used for space-efficient
// (O(d) where d is the depth of the execution tree) exploration
// of the execution tree.
t.seeds.Sort()
t.cleanup()
niterations++
}
return niterations, nil
}