-
Notifications
You must be signed in to change notification settings - Fork 3
/
command_test.go
107 lines (89 loc) · 2 KB
/
command_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
package patrol
import (
"context"
"testing"
"time"
"github.com/oklog/run"
"github.com/tsenart/vegeta/lib"
"go.uber.org/zap"
)
func TestCommand(t *testing.T) {
ctx := context.Background()
apis := []string{
"127.0.0.1:12000",
"127.0.0.1:12001",
"127.0.0.1:12002",
}
nodes := []string{
"127.0.0.1:16000",
"127.0.0.1:16001",
"127.0.0.1:16002",
}
peers := func(node string, nodes []string) []string {
var peers []string
for i := range nodes {
if nodes[i] != node {
peers = append(peers, node)
}
}
return peers
}
var g run.Group
logger, err := zap.NewDevelopment()
if err != nil {
t.Fatal(err)
}
for i, node := range nodes {
offset := time.Duration(i) * time.Minute
cmd := Command{
Log: logger,
APIAddr: apis[i],
NodeAddr: node,
PeerAddrs: peers(node, nodes),
Clock: func() time.Time {
// Test that unsynchronized clocks don't affect results.
return time.Now().UTC().Add(offset)
},
ShutdownTimeout: 5 * time.Second,
}
ctx, cancel := context.WithCancel(ctx)
g.Add(func() error {
return cmd.Run(ctx)
}, func(error) {
cancel()
})
}
// Integration test
g.Add(func() error {
testCommand(t, apis)
return nil
}, func(error) {
// testCommand is not interruptable, it governs the run.Group
})
if err := g.Run(); err != nil {
t.Fatal(err)
}
}
func testCommand(t *testing.T, nodes []string) {
a := vegeta.NewAttacker(vegeta.H2C(true))
targets := make([]vegeta.Target, len(nodes))
for i, node := range nodes {
targets[i] = vegeta.Target{
Method: "POST",
URL: "http://" + node + "/take/foobar?rate=10:s&count=1",
}
}
// Wait until APIs are serving.
time.Sleep(time.Second)
tr := vegeta.NewStaticTargeter(targets...)
rate := vegeta.Rate{Freq: 100, Per: time.Second} // > 10/s
results := a.Attack(tr, rate, 5*time.Second, "Patrol test")
var m vegeta.Metrics
for r := range results {
m.Add(r)
}
m.Close()
if m.Success > 0.9 {
t.Errorf("success rate should be below 0.9: got %f", m.Success)
}
}