-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhashring_test.go
112 lines (84 loc) · 2.77 KB
/
hashring_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
package ringman
import (
"testing"
director "github.com/relistan/go-director"
. "github.com/smartystreets/goconvey/convey"
)
func Test_NewHashRingManager(t *testing.T) {
Convey("NewHashRingManager()", t, func() {
hostList := []string{"njal", "kjartan"}
ringMgr := NewHashRingManager(hostList)
Convey("returns a properly configured HashRingManager", func() {
So(ringMgr.cmdChan, ShouldNotBeNil)
So(ringMgr.HashRing, ShouldNotBeNil)
})
})
}
func Test_Run(t *testing.T) {
Convey("Run()", t, func() {
hostList := []string{"njal", "kjartan"}
ringMgr := NewHashRingManager(hostList)
go ringMgr.Run(director.NewFreeLooper(director.ONCE, nil))
Convey("ringMgr.Ping() returns true when running", func() {
So(ringMgr.Ping(), ShouldBeTrue)
})
Convey("ringMgr can stop", func() {
So(ringMgr.Ping(), ShouldBeTrue)
ringMgr.Stop()
So(ringMgr.cmdChan, ShouldBeNil)
So(ringMgr.Ping(), ShouldBeFalse)
})
Convey("doesn't blow up on a nil receiver", func() {
var broken *HashRingManager
So(func() { broken.Run(nil) }, ShouldNotPanic)
})
})
}
func Test_Commands(t *testing.T) {
Convey("Running commands", t, func() {
ringMgr := NewHashRingManager([]string{"kjartan"})
Convey("AddNode adds a node which is returned from GetNode", func() {
go ringMgr.Run(director.NewFreeLooper(3, nil))
// Make sure the RingManager is started
So(ringMgr.Ping(), ShouldBeTrue)
err := ringMgr.AddNode("njal")
So(err, ShouldBeNil)
node, err := ringMgr.GetNode("foo")
So(err, ShouldBeNil)
So(node, ShouldEqual, "njal")
})
Convey("RemoveNode removes a node", func() {
go ringMgr.Run(director.NewFreeLooper(3, nil))
// Make sure the RingManager is started
So(ringMgr.Ping(), ShouldBeTrue)
ringMgr.RemoveNode("kjartan")
node, err := ringMgr.GetNode("foo")
So(err, ShouldNotBeNil)
So(node, ShouldEqual, "")
})
Convey("Ping responds as up, in a timely manner", func() {
go ringMgr.Run(director.NewFreeLooper(director.ONCE, nil))
result := ringMgr.Ping()
So(result, ShouldBeTrue)
})
Convey("Ping fails when the manager is not running", func() {
go ringMgr.Run(director.NewFreeLooper(director.ONCE, nil))
// Make sure the RingManager is started
So(ringMgr.Ping(), ShouldBeTrue)
ringMgr.Stop()
So(ringMgr.Ping(), ShouldBeFalse)
})
Convey("With error conditions", func() {
Convey("does not blow up on nil receiver", func() {
var broken *HashRingManager
So(func() { broken.AddNode("junk") }, ShouldNotPanic)
So(func() { broken.RemoveNode("junk") }, ShouldNotPanic)
})
Convey("does not try to run if not started", func() {
broken := &HashRingManager{}
So(func() { broken.AddNode("junk") }, ShouldNotPanic)
So(func() { broken.RemoveNode("junk") }, ShouldNotPanic)
})
})
})
}