This repository was archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmcset_test.go
106 lines (82 loc) · 2.13 KB
/
mcset_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
package mcset
import (
"net"
"github.com/strava/go.serversets/fixedset"
"testing"
)
func TestNew(t *testing.T) {
fs := fixedset.New([]string{"localhost:2181"})
mcset := New(fs)
if l := len(mcset.Endpoints()); l != 1 {
t.Errorf("should have one endpoint but got %v", l)
}
fs.SetEndpoints([]string{"localhost:2181", "localhost:2182"})
<-mcset.Event()
if len(mcset.Endpoints()) != 2 {
t.Errorf("should have two endpoint but got %v", mcset.Endpoints())
}
}
func TestCloseWatch(t *testing.T) {
count := 0
event := make(chan struct{}, 1)
watcherClosed = func() {
count++
event <- struct{}{}
}
fs := fixedset.New(nil)
New(fs)
fs.Close()
// little event channel to allow the other goroutine to run and quit as it should
<-event
if count != 1 {
t.Error("should close watching goroutine on watcher channel close")
}
}
func TestNewWithoutWatcher(t *testing.T) {
New(nil)
// should not panic or anything
}
func TestMCSetPickServer(t *testing.T) {
mcset := New(nil)
mcset.SetEndpoints([]string{"localhost:2181", "localhost:2182", "localhost:2183", "localhost:2184"})
server1, _ := mcset.PickServer("foo")
mcset.SetEndpoints([]string{"localhost:2181", "localhost:2182", "localhost:2183"})
server2, _ := mcset.PickServer("foo")
if server1.String() != server2.String() {
t.Errorf("should be consistent %v != %v", server1, server2)
}
}
func TestMCSetEach(t *testing.T) {
count := 0
f := func(net.Addr) error {
count++
return nil
}
mcset := New(nil)
mcset.SetEndpoints([]string{"localhost:2181", "localhost:2182", "localhost:2183", "localhost:2184"})
mcset.Each(f)
if count != len(mcset.Endpoints()) {
t.Errorf("should run for all endpoints, but run %d time(s)", count)
}
count = 0
f = func(net.Addr) error {
if count == 2 {
return ErrNoServers
}
count++
return nil
}
mcset.Each(f)
if count != 2 {
t.Errorf("should quite once there is an error, but run %d time(s)", count)
}
}
func TestMCSetTriggerEvent(t *testing.T) {
mcset := New(nil)
mcset.triggerEvent()
mcset.triggerEvent()
mcset.triggerEvent()
if mcset.EventCount != 3 {
t.Errorf("event count not right, got %v", mcset.EventCount)
}
}