forked from pingcap/failpoint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfailpoint_test.go
75 lines (64 loc) · 1.84 KB
/
failpoint_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
package failpoint_test
import (
"context"
"sync"
"testing"
"time"
. "github.com/pingcap/check"
"github.com/pingcap/failpoint"
)
func TestT(t *testing.T) {
TestingT(t)
}
var _ = Suite(&failpointSuite{})
type failpointSuite struct{}
func (s *failpointSuite) TestWithHook(c *C) {
err := failpoint.Enable("TestWithHook-test-0", "return(1)")
c.Assert(err, IsNil)
val, err := failpoint.EvalContext(context.Background(), "TestWithHook-test-0")
c.Assert(val, IsNil)
c.Assert(err, NotNil)
val, err = failpoint.EvalContext(nil, "TestWithHook-test-0")
c.Assert(val, IsNil)
c.Assert(err, NotNil)
ctx := failpoint.WithHook(context.Background(), func(ctx context.Context, fpname string) bool {
return false
})
val, err = failpoint.EvalContext(ctx, "unit-test")
c.Assert(err, NotNil)
c.Assert(val, IsNil)
ctx = failpoint.WithHook(context.Background(), func(ctx context.Context, fpname string) bool {
return true
})
err = failpoint.Enable("TestWithHook-test-1", "return(1)")
c.Assert(err, IsNil)
defer func() {
err := failpoint.Disable("TestWithHook-test-1")
c.Assert(err, IsNil)
}()
val, err = failpoint.EvalContext(ctx, "TestWithHook-test-1")
c.Assert(err, IsNil)
c.Assert(val.(int), Equals, 1)
}
func (s *failpointSuite) TestConcurrent(c *C) {
err := failpoint.Enable("TestWithHook-test-2", "pause")
c.Assert(err, IsNil)
var wg sync.WaitGroup
wg.Add(1)
go func() {
ctx := failpoint.WithHook(context.Background(), func(ctx context.Context, fpname string) bool {
return true
})
val, _ := failpoint.EvalContext(ctx, "TestWithHook-test-2")
c.Assert(val, IsNil)
wg.Done()
}()
time.Sleep(1 * time.Second)
err = failpoint.Enable("TestWithHook-test-3", "return(1)")
c.Assert(err, IsNil)
err = failpoint.Disable("TestWithHook-test-3")
c.Assert(err, IsNil)
err = failpoint.Disable("TestWithHook-test-2")
c.Assert(err, IsNil)
wg.Wait()
}