-
Notifications
You must be signed in to change notification settings - Fork 25
/
statemachine.go
171 lines (147 loc) · 3.99 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
// 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"
)
const (
actionLabel = "action"
validActionTries = 100 // hack, but probably good enough for now
initMethodName = "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), if present, is ran at the beginning of each test case
// to initialize the state machine instance;
// - 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.Init(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)
if sm.init != nil {
sm.init(t)
t.failOnError()
}
if sm.cleanup != nil {
defer sm.cleanup()
}
sm.check(t)
t.failOnError()
for repeat.more(t.s, typ.String()) {
ok := sm.executeAction(t)
if ok {
sm.check(t)
t.failOnError()
} else {
repeat.reject()
}
}
}
}
type stateMachine struct {
init func(*T)
cleanup func()
check func(*T)
actionKeys *Generator
actions map[string]func(*T)
}
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()
init func(*T)
cleanup func()
actionKeys []string
actions = map[string]func(*T){}
)
for i := 0; i < n; i++ {
name := typ.Method(i).Name
m, ok := v.Method(i).Interface().(func(*T))
if ok {
if name == initMethodName {
init = m
} else if name != checkMethodName {
actionKeys = append(actionKeys, name)
actions[name] = m
}
} 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(actionKeys)
return &stateMachine{
init: init,
cleanup: cleanup,
check: v.Interface().(StateMachine).Check,
actionKeys: SampledFrom(actionKeys),
actions: actions,
}
}
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)
t.failOnError()
return false, false
}