-
Notifications
You must be signed in to change notification settings - Fork 0
/
racy_test.go
132 lines (111 loc) · 4.09 KB
/
racy_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
//go:build racy
package goomerang_test
import (
"context"
"sync"
"testing"
"time"
"go.eloylp.dev/kit/exec"
"go.eloylp.dev/goomerang/client"
"go.eloylp.dev/goomerang/internal/test"
"go.eloylp.dev/goomerang/server"
)
// TestNoRaces tries to find nasty data races in the code.
// It works by stressing out the system on all its possible
// execution paths at the same time.
//
// IMPORTANT NOTE, this test assumes that the rest of the
// tests are already passing. Its not the target of this
// test to check functionality. Only data races. It must
// be executed in isolation.
func TestNoRaces(t *testing.T) {
t.Parallel()
// Bring up 2 clients and one server.
s, run := Server(t)
s.Handle(defaultMsg().Payload, echoHandler)
run()
c, connect := Client(t, client.WithServerAddr(s.Addr()))
c.Handle(defaultMsg().Payload, nilHandler)
connect()
failIfErr(t, c.Subscribe("topic.a"))
c2, connect2 := Client(t, client.WithServerAddr(s.Addr()))
c2.Handle(defaultMsg().Payload, nilHandler)
connect2()
failIfErr(t, c2.Subscribe("topic.a"))
// Set test duration of 10 seconds.
ctx, cancl := context.WithTimeout(defaultCtx, 10*time.Second)
defer cancl()
wg := &sync.WaitGroup{}
// Use the arbiter to register successes. Have at least an idea of whats happening.
arbiter := test.NewArbiter(t)
// Stress all parts of the system through the public API. All uses cases at the same time.
const maxConcurrent = 10
go exec.Parallelize(ctx, wg, maxConcurrent, func() {
if _, err := s.Broadcast(ctx, defaultMsg()); err != nil && err != server.ErrNotRunning {
arbiter.ErrorHappened(err)
return
}
arbiter.ItsAFactThat("s.Broadcast()")
})
go exec.Parallelize(ctx, wg, maxConcurrent, func() {
if err := s.Publish("topic.a", defaultMsg()); err != nil && err != server.ErrNotRunning {
arbiter.ErrorHappened(err)
return
}
arbiter.ItsAFactThat("s.Publish()")
})
// Warm up client execution paths.
execClientSend(ctx, wg, maxConcurrent, c, arbiter)
execClientSendSync(ctx, wg, maxConcurrent, c, arbiter)
execClientBroadcast(ctx, wg, maxConcurrent, c, arbiter)
execClientPublish(ctx, wg, maxConcurrent, c, arbiter)
execClientSend(ctx, wg, maxConcurrent, c2, arbiter)
execClientSendSync(ctx, wg, maxConcurrent, c2, arbiter)
execClientBroadcast(ctx, wg, maxConcurrent, c2, arbiter)
execClientPublish(ctx, wg, maxConcurrent, c2, arbiter)
<-ctx.Done()
// Call shutdown functions and hopefully find some data races.
arbiter.ErrorHappened(c.Close(defaultCtx))
arbiter.ErrorHappened(c2.Close(defaultCtx))
arbiter.ErrorHappened(s.Shutdown(defaultCtx))
wg.Wait()
// Have minimum feedback of what happened.
t.Logf("Registered errors: %v", arbiter.Errors())
t.Logf("Registered events: %v", arbiter.EvCount())
}
func execClientSend(ctx context.Context, wg *sync.WaitGroup, maxConcurrent int, c *client.Client, arbiter *test.Arbiter) {
go exec.Parallelize(ctx, wg, maxConcurrent, func() {
if _, err := c.Send(defaultMsg()); err != nil && err != client.ErrNotRunning {
arbiter.ErrorHappened(err)
return
}
arbiter.ItsAFactThat("c.Send()")
})
}
func execClientSendSync(ctx context.Context, wg *sync.WaitGroup, maxConcurrent int, c *client.Client, arbiter *test.Arbiter) {
go exec.Parallelize(ctx, wg, maxConcurrent, func() {
if _, _, err := c.SendSync(ctx, defaultMsg()); err != nil && err != client.ErrNotRunning {
arbiter.ErrorHappened(err)
return
}
arbiter.ItsAFactThat("c.SendSync()")
})
}
func execClientBroadcast(ctx context.Context, wg *sync.WaitGroup, maxConcurrent int, c *client.Client, arbiter *test.Arbiter) {
go exec.Parallelize(ctx, wg, maxConcurrent, func() {
if _, err := c.Broadcast(defaultMsg()); err != nil && err != client.ErrNotRunning {
arbiter.ErrorHappened(err)
return
}
arbiter.ItsAFactThat("c.Broadcast()")
})
}
func execClientPublish(ctx context.Context, wg *sync.WaitGroup, maxConcurrent int, c *client.Client, arbiter *test.Arbiter) {
go exec.Parallelize(ctx, wg, maxConcurrent, func() {
if _, err := c.Publish("topic.a", defaultMsg()); err != nil && err != client.ErrNotRunning {
arbiter.ErrorHappened(err)
return
}
arbiter.ItsAFactThat("c.Publish()")
})
}