-
Notifications
You must be signed in to change notification settings - Fork 25
/
statemachine.go
187 lines (159 loc) · 4.37 KB
/
statemachine.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
// Copyright 2019 Gregory Petrosyan <gregory.petrosyan@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package rapid
import (
"reflect"
"sort"
"strings"
)
const (
actionLabel = "action"
validActionTries = 100 // hack, but probably good enough for now
initMethodPrefix = "Init"
checkMethodName = "Check"
cleanupMethodName = "Cleanup"
noValidActionsMsg = "can't find a valid action"
)
type StateMachine interface {
// Check is ran after every action and should contain invariant checks.
//
// Other public methods are treated as follows:
// - Init(t *rapid.T), InitAnySuffixHere(t *rapid.T), if present,
// are used as "initializer" actions; exactly one is ran at the beginning
// of each test case;
// - Cleanup(), if present, is called at the end of each test case;
// - All other public methods should have a form ActionName(t *rapid.T)
// and are used as possible actions. At least one action has to be specified.
Check(*T)
}
// Run is a convenience function for defining "state machine" tests,
// to be run by Check or MakeCheck.
//
// State machine test is a pattern for testing stateful systems that looks
// like this:
//
// m := new(StateMachineType)
// m.RandomInitAction(t) // optional
// defer m.Cleanup() // optional
// m.Check(t)
// for {
// m.RandomAction(t)
// m.Check(t)
// }
//
// Run synthesizes such test from the type of m, which must be a pointer.
// Note that for each test case, new state machine instance is created
// via reflection; any data inside m is ignored.
func Run(m StateMachine) func(*T) {
typ := reflect.TypeOf(m)
return func(t *T) {
t.Helper()
repeat := newRepeat(0, *steps, maxInt)
sm := newStateMachine(typ)
sm.init(t)
defer sm.cleanup()
sm.check(t)
for repeat.more(t.s, typ.String()) {
ok := sm.executeAction(t)
if ok {
sm.check(t)
} else {
repeat.reject()
}
}
}
}
type stateMachine struct {
inits map[string]func(*T)
actions map[string]func(*T)
initKeys *Generator
actionKeys *Generator
check func(*T)
cleanup_ func()
}
func newStateMachine(typ reflect.Type) *stateMachine {
assertf(typ.Kind() == reflect.Ptr, "state machine type should be a pointer, not %v", typ.Kind())
var (
v = reflect.New(typ.Elem())
n = typ.NumMethod()
inits = map[string]func(*T){}
actions = map[string]func(*T){}
initKeys []string
actionKeys []string
cleanup func()
)
for i := 0; i < n; i++ {
name := typ.Method(i).Name
m, ok := v.Method(i).Interface().(func(*T))
if ok {
if strings.HasPrefix(name, initMethodPrefix) {
inits[name] = m
initKeys = append(initKeys, name)
} else if name != checkMethodName {
actions[name] = m
actionKeys = append(actionKeys, name)
}
} else if name == cleanupMethodName {
m, ok := v.Method(i).Interface().(func())
assertf(ok, "method %v should have type func(), not %v", cleanupMethodName, v.Method(i).Type())
cleanup = m
}
}
assertf(len(actions) > 0, "state machine of type %v has no actions specified", typ)
sort.Strings(initKeys)
sort.Strings(actionKeys)
sm := &stateMachine{
inits: inits,
actions: actions,
actionKeys: SampledFrom(actionKeys),
check: v.Interface().(StateMachine).Check,
cleanup_: cleanup,
}
if len(initKeys) > 0 {
sm.initKeys = SampledFrom(initKeys)
}
return sm
}
func (sm *stateMachine) init(t *T) {
if sm.initKeys != nil {
t.Helper()
sm.inits[sm.initKeys.Draw(t, "initializer").(string)](t)
}
}
func (sm *stateMachine) cleanup() {
if sm.cleanup_ != nil {
sm.cleanup_()
}
}
func (sm *stateMachine) executeAction(t *T) bool {
t.Helper()
for n := 0; n < validActionTries; n++ {
i := t.s.beginGroup(actionLabel, false)
action := sm.actions[sm.actionKeys.Draw(t, "action").(string)]
invalid, skipped := runAction(t, action)
t.s.endGroup(i, false)
if skipped {
continue
} else {
return !invalid
}
}
panic(stopTest(noValidActionsMsg))
}
func runAction(t *T, action func(*T)) (invalid bool, skipped bool) {
defer func(draws int) {
if r := recover(); r != nil {
if _, ok := r.(invalidData); ok {
invalid = true
skipped = t.draws == draws
} else {
panic(r)
}
}
}(t.draws)
action(t)
return false, false
}