-
Notifications
You must be signed in to change notification settings - Fork 35
/
placement_test.go
76 lines (65 loc) · 1.56 KB
/
placement_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
package beehive
import (
"strconv"
"testing"
"github.com/kandoo/beehive/Godeps/_workspace/src/github.com/golang/glog"
)
type testNonLocalPlacementMethod struct{}
func (m testNonLocalPlacementMethod) Place(cells MappedCells, thisHive Hive,
liveHives []HiveInfo) HiveInfo {
for _, h := range liveHives {
if h.ID != thisHive.ID() {
return h
}
}
glog.Fatal("cannot find a non-local hive")
return HiveInfo{}
}
type testPlacementRes struct {
hive uint64
msg int
}
func registerPlacementApp(h Hive, ch chan testPlacementRes) App {
a := h.NewApp("placementapp", NonTransactional(),
Placement(testNonLocalPlacementMethod{}))
mf := func(msg Msg, ctx MapContext) MappedCells {
return MappedCells{{"D", strconv.Itoa(msg.Data().(int))}}
}
rf := func(msg Msg, ctx RcvContext) error {
ctx.Hive().ID()
ch <- testPlacementRes{
hive: ctx.Hive().ID(),
msg: msg.Data().(int),
}
return nil
}
a.HandleFunc(int(0), mf, rf)
return a
}
func TestPlacement(t *testing.T) {
// TODO(soheil): refactor all these into a single test utility method.
ch := make(chan testPlacementRes)
var hives []Hive
nh := 2
for i := 0; i < nh; i++ {
var h Hive
if i == 0 {
h = newHiveForTest()
} else {
h = newHiveForTest(PeerAddrs(hives[0].(*hive).config.Addr))
}
hives = append(hives, h)
registerPlacementApp(hives[i], ch)
go hives[i].Start()
waitTilStareted(hives[i])
}
for i := 0; i < nh; i++ {
go hives[i].Emit(i)
}
for i := 0; i < nh; i++ {
res := <-ch
if res.hive == hives[res.msg].ID() {
t.Errorf("Got the message from the same hive")
}
}
}