-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathfailpoints_test.go
242 lines (212 loc) · 6.48 KB
/
failpoints_test.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
239
240
241
242
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package failpoint_test
import (
"io/ioutil"
"os"
"testing"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/stretchr/testify/require"
)
func TestFailpoints(t *testing.T) {
var fps failpoint.Failpoints
err := fps.Enable("failpoints-test-1", "return(1)")
require.NoError(t, err)
val, err := fps.Eval("failpoints-test-1")
require.NoError(t, err)
require.Equal(t, 1, val.(int))
err = fps.Enable("failpoints-test-2", "invalid")
require.EqualError(t, err, `error on failpoints-test-2: failpoint: failed to parse "invalid" past "invalid"`)
val, err = fps.Eval("failpoints-test-2")
require.Error(t, err)
require.Nil(t, val)
err = fps.Disable("failpoints-test-1")
require.NoError(t, err)
val, err = fps.Eval("failpoints-test-1")
require.Error(t, err)
require.Nil(t, val)
err = fps.Disable("failpoints-test-1")
require.NoError(t, err)
err = fps.Enable("failpoints-test-1", "return(1)")
require.NoError(t, err)
status, err := fps.Status("failpoints-test-1")
require.NoError(t, err)
require.Equal(t, "return(1)", status)
err = fps.Enable("failpoints-test-3", "return(2)")
require.NoError(t, err)
ch := make(chan struct{})
go func() {
time.Sleep(time.Second)
err := fps.Disable("gofail/testPause")
require.NoError(t, err)
close(ch)
}()
err = fps.Enable("gofail/testPause", "pause")
require.NoError(t, err)
start := time.Now()
v, err := fps.Eval("gofail/testPause")
require.NoError(t, err)
require.Nil(t, v)
require.True(t, time.Since(start) > 100*time.Millisecond)
<-ch
err = fps.Enable("failpoints-test-4", "50.0%return(5)")
require.NoError(t, err)
var succ int
for i := 0; i < 1000; i++ {
val, err = fps.Eval("failpoints-test-4")
if err == nil && val != nil {
succ++
require.Equal(t, 5, val.(int))
}
}
if succ < 450 || succ > 550 {
require.FailNow(t, "prop failure: %v", succ)
}
err = fps.Enable("failpoints-test-5", "50*return(5)")
require.NoError(t, err)
for i := 0; i < 50; i++ {
val, err = fps.Eval("failpoints-test-5")
require.NoError(t, err)
require.Equal(t, 5, val.(int))
}
val, err = fps.Eval("failpoints-test-5")
require.Equal(t, failpoint.ErrNotAllowed, errors.Cause(err))
require.Nil(t, val)
points := map[string]struct{}{}
for _, fp := range fps.List() {
points[fp] = struct{}{}
}
require.Contains(t, points, "failpoints-test-1")
require.Contains(t, points, "failpoints-test-2")
require.Contains(t, points, "failpoints-test-3")
require.Contains(t, points, "failpoints-test-4")
require.Contains(t, points, "failpoints-test-5")
err = fps.Enable("failpoints-test-6", "50*return(5)->1*return(true)->1*return(false)->10*return(20)")
require.NoError(t, err)
// 50*return(5)
for i := 0; i < 50; i++ {
val, err = fps.Eval("failpoints-test-6")
require.NoError(t, err)
require.Equal(t, 5, val.(int))
}
// 1*return(true)
val, err = fps.Eval("failpoints-test-6")
require.NoError(t, err)
require.True(t, val.(bool))
// 1*return(false)
val, err = fps.Eval("failpoints-test-6")
require.NoError(t, err)
require.False(t, val.(bool))
// 10*return(20)
for i := 0; i < 10; i++ {
val, err = fps.Eval("failpoints-test-6")
require.NoError(t, err)
require.Equal(t, 20, val.(int))
}
val, err = fps.Eval("failpoints-test-6")
require.Equal(t, failpoint.ErrNotAllowed, errors.Cause(err))
require.Nil(t, val)
val, err = fps.Eval("failpoints-test-7")
require.Equal(t, failpoint.ErrNotExist, errors.Cause(err))
require.Nil(t, val)
val, err = failpoint.Eval("failpoint-env1")
require.NoError(t, err)
require.Equal(t, 10, val.(int))
val, err = failpoint.Eval("failpoint-env2")
require.NoError(t, err)
require.True(t, val.(bool))
// Tests for sleep
ch = make(chan struct{})
go func() {
defer close(ch)
time.Sleep(time.Second)
err := failpoint.Disable("gofail/test-sleep")
require.NoError(t, err)
}()
err = failpoint.Enable("gofail/test-sleep", "sleep(100)")
require.NoError(t, err)
start = time.Now()
v, err = failpoint.Eval("gofail/test-sleep")
require.NoError(t, err)
require.Nil(t, v)
require.GreaterOrEqual(t, time.Since(start).Milliseconds(), int64(90))
<-ch
// Tests for sleep duration
ch = make(chan struct{})
go func() {
defer close(ch)
time.Sleep(time.Second)
err := failpoint.Disable("gofail/test-sleep2")
require.NoError(t, err)
}()
err = failpoint.Enable("gofail/test-sleep2", `sleep("100ms")`)
require.NoError(t, err)
start = time.Now()
v, err = failpoint.Eval("gofail/test-sleep2")
require.NoError(t, err)
require.Nil(t, v)
require.GreaterOrEqual(t, time.Since(start).Milliseconds(), int64(90))
<-ch
// Tests for print
oldStdio := os.Stdout
r, w, err := os.Pipe()
require.NoError(t, err)
os.Stdout = w
err = fps.Enable("test-print", `print("hello world")`)
require.NoError(t, err)
val, err = fps.Eval("test-print")
require.NoError(t, err)
require.Nil(t, val)
outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
go func() {
defer close(outC)
s, err := ioutil.ReadAll(r)
require.NoError(t, err)
outC <- string(s)
}()
require.NoError(t, w.Close())
os.Stdout = oldStdio
out := <-outC
require.Equal(t, "failpoint print: hello world\n", out)
// Tests for panic
require.PanicsWithValue(t, "failpoint panic: {}", testPanic)
err = fps.Enable("failpoints-test-7", `return`)
require.NoError(t, err)
val, err = fps.Eval("failpoints-test-7")
require.NoError(t, err)
require.Equal(t, struct{}{}, val)
err = fps.Enable("failpoints-test-8", `return()`)
require.NoError(t, err)
val, err = fps.Eval("failpoints-test-8")
require.NoError(t, err)
require.Equal(t, struct{}{}, val)
}
func testPanic() {
_ = failpoint.Enable("test-panic", `panic`)
_, _ = failpoint.Eval("test-panic")
}
func TestCall(t *testing.T) {
var capturedArg int
require.NoError(t, failpoint.EnableCall("test", func(a int) {
capturedArg = a
}))
t.Cleanup(func() {
require.NoError(t, failpoint.Disable("test"))
})
failpoint.Call("test", 123)
require.Equal(t, 123, capturedArg)
}