-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathintegration_test.go
117 lines (95 loc) · 2.08 KB
/
integration_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
package main
import (
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"os"
"runtime"
"strconv"
"testing"
)
const (
servers = 5
)
func makeRaftAddr(server int) string {
return fmt.Sprintf("127.0.0.1:%d", 6346+server)
}
func makeHttpAddr(server int) string {
return fmt.Sprintf("127.0.0.1:%d", 8080+server)
}
func makeDataDir(server int) string {
dir, err := os.Getwd()
if err != nil {
panic(err.Error())
}
return fmt.Sprintf("%s/goflake-data/%d", dir, server+1)
}
// fire up five servers and hit 'em
func TestIntegration(t *testing.T) {
// just make super-sure we're in a multi-threaded environmet
runtime.GOMAXPROCS(runtime.NumCPU())
tester := newIntegrityTester()
cluster := make([]string, 0, servers)
for i := 0; i < servers; i++ {
cluster = append(cluster, makeRaftAddr(i))
}
// start up all our servers
for i := 0; i < servers; i++ {
cfg := &Config{
Addr: makeHttpAddr(i),
Raft: RaftConfig{
Addr: makeRaftAddr(i),
DataDir: makeDataDir(i),
Cluster: cluster,
ClusterState: ClusterStateNew,
},
}
s, err := newServer(cfg)
if err != nil {
panic(err.Error())
}
go s.start()
}
done := make(chan bool)
run := func() {
defer func() {
done <- true
}()
for range getTicker(100, 1000) {
resp, err := http.Get("http://" + makeHttpAddr(int(rand.Int31n(servers))) + "/next")
if err != nil {
t.Error(err.Error())
continue
}
if resp.StatusCode != 200 {
t.Error("response not 200")
continue
}
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Error("could not read response body")
resp.Body.Close()
continue
}
id, err := strconv.ParseUint(string(content), 10, 64)
if err != nil {
t.Error("could not parse response body: %v", string(content))
resp.Body.Close()
continue
}
if err := tester.check(id); err != nil {
t.Error(err.Error())
}
resp.Body.Close()
}
}
// hit them from different threads
for i := 0; i < runtime.NumCPU(); i++ {
go run()
}
// wait for them to finish
for i := 0; i < runtime.NumCPU(); i++ {
<-done
}
}