-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathhive_test.go
150 lines (116 loc) · 2.46 KB
/
hive_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
package beehive
import (
"fmt"
"runtime"
"strconv"
"testing"
"time"
)
const (
handlers int = 1
msgs = 2
)
var testHiveCh = make(chan interface{})
type MyMsg int
type testHiveHandler struct{}
func (h *testHiveHandler) Map(m Msg, c MapContext) MappedCells {
v := int(m.Data().(MyMsg))
return MappedCells{{"D", strconv.Itoa(v % handlers)}}
}
func (h *testHiveHandler) Rcv(m Msg, c RcvContext) error {
hash := int(m.Data().(MyMsg)) % handlers
d := c.Dict("D")
k := strconv.Itoa(hash)
v, err := d.Get(k)
i := 1
if err == nil {
i += v.(int)
}
if err = d.Put(k, i); err != nil {
panic(fmt.Sprintf("cannot change the key: %v", err))
}
id := c.ID() % uint64(handlers)
if id != uint64(hash) {
panic(fmt.Sprintf("invalid message %v received in %v.", m, id))
}
if i == msgs/handlers {
testHiveCh <- true
}
return nil
}
func runHiveTest(t *testing.T, opts ...HiveOption) {
runtime.GOMAXPROCS(4)
defer runtime.GOMAXPROCS(1)
testHiveCh = make(chan interface{})
defer func() {
close(testHiveCh)
testHiveCh = nil
}()
hive := newHiveForTest(opts...)
app := hive.NewApp("TestHiveApp")
app.Handle(MyMsg(0), &testHiveHandler{})
go hive.Start()
for i := 1; i <= msgs; i++ {
hive.Emit(MyMsg(i))
}
for i := 0; i < handlers; i++ {
<-testHiveCh
}
if err := hive.Stop(); err != nil {
t.Errorf("cannot stop the hive %v", err)
}
}
func TestHiveStart(t *testing.T) {
runHiveTest(t)
}
func TestHiveRestart(t *testing.T) {
runHiveTest(t)
time.Sleep(1 * time.Second)
runHiveTest(t)
}
func TestHiveCluster(t *testing.T) {
h1 := newHiveForTest()
go h1.Start()
waitTilStareted(h1)
h1a := h1.Config().Addr
h2 := newHiveForTest(PeerAddrs(h1a))
go h2.Start()
h3 := newHiveForTest(PeerAddrs(h1a))
go h3.Start()
waitTilStareted(h2)
waitTilStareted(h3)
h3.Stop()
h2.Stop()
h1.Stop()
}
func TestHiveFailure(t *testing.T) {
h1 := newHiveForTest()
go h1.Start()
waitTilStareted(h1)
cfg1 := h1.Config()
h2 := newHiveForTest(PeerAddrs(cfg1.Addr))
go h2.Start()
h3 := newHiveForTest(PeerAddrs(cfg1.Addr))
go h3.Start()
waitTilStareted(h2)
waitTilStareted(h3)
h1.Stop()
elect := cfg1.RaftElectTimeout()
time.Sleep(3 * elect)
for {
if _, err := h2.(*hive).processCmd(cmdSync{}); err == nil {
break
}
t.Logf("cannot sync %v, retrying", h2)
time.Sleep(elect)
}
for {
if _, err := h3.(*hive).processCmd(cmdSync{}); err == nil {
break
}
t.Logf("cannot sync %v, retrying", h3)
time.Sleep(elect)
}
h3.Stop()
h2.Stop()
}