-
Notifications
You must be signed in to change notification settings - Fork 1
/
ephemeral_test.go
273 lines (227 loc) · 6.92 KB
/
ephemeral_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package ephemeral
import (
"fmt"
"path"
"time"
pf "github.com/csigo/portforward"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"golang.org/x/net/context"
)
type ephemeralSuite struct {
suite.Suite
newEphemeral func(int) (Ephemeral, error)
startFunc func() (int, error)
stopFunc func()
ephemeral Ephemeral
testZK bool
testETCD bool
port int
}
func (s *ephemeralSuite) SetupSuite() {
var err error
s.port, err = s.startFunc()
assert.NoError(s.T(), err)
}
func (s *ephemeralSuite) TearDownSuite() {
s.stopFunc()
}
func (s *ephemeralSuite) SetupTest() {
var err error
s.ephemeral, err = s.newEphemeral(s.port)
assert.NoError(s.T(), err)
}
func (s *ephemeralSuite) TearDownTest() {
assert.NoError(s.T(), s.ephemeral.Close())
}
func (s *ephemeralSuite) TestAddList() {
testDir := "/add-list"
testNode := testDir + "/add-node"
// testDir should be empty at the beginning
receiver := s.ephemeral.List(context.Background(), testDir, false)
resp := <-receiver
assert.Error(s.T(), resp.Err)
assert.Equal(s.T(), []string(nil), resp.Children)
_, ok := <-receiver
assert.False(s.T(), ok)
// Now add testDir
err := s.ephemeral.AddDir(context.Background(), testDir)
assert.NoError(s.T(), err)
// Get again. Now should be available.
receiver = s.ephemeral.List(context.Background(), testDir, false)
resp = <-receiver
assert.NoError(s.T(), resp.Err)
assert.Equal(s.T(), []string{}, resp.Children)
// resp should be closed.
_, ok = <-receiver
assert.False(s.T(), ok)
// Now add testNode
err = s.ephemeral.AddKey(context.Background(), testNode, "")
assert.NoError(s.T(), err)
// Get again. Now should get testNode.
receiver = s.ephemeral.List(context.Background(), testDir, false)
resp = <-receiver
assert.NoError(s.T(), resp.Err)
assert.Equal(s.T(), []string{"add-node"}, resp.Children)
// resp should be closed.
_, ok = <-receiver
assert.False(s.T(), ok)
}
func (s *ephemeralSuite) TestListWatch() {
testDir := "/list-watch"
// Add testDir
err := s.ephemeral.AddDir(context.Background(), testDir)
assert.NoError(s.T(), err)
// Get dir and watch
ctx, cancel := context.WithCancel(context.Background())
receiver := s.ephemeral.List(ctx, testDir, true)
n := <-receiver
assert.NoError(s.T(), n.Err)
assert.Equal(s.T(), []string{}, n.Children)
// nothing else should pop up from receiver side
select {
case <-receiver:
assert.Fail(s.T(), "shouldn't receive")
case <-time.After(time.Second):
}
// Add a children
err = s.ephemeral.AddKey(context.Background(), testDir+"/node1", "")
assert.NoError(s.T(), err)
// We should receive node from receiver.
select {
case nodes := <-receiver:
assert.NoError(s.T(), nodes.Err)
assert.Equal(s.T(), []string{"node1"}, nodes.Children)
case <-time.After(time.Second):
assert.Fail(s.T(), "should receive")
}
// Cancel the watch request
cancel()
time.Sleep(3 * time.Second)
// make some changes
err = s.ephemeral.AddKey(context.Background(), testDir+"/node2", "")
assert.NoError(s.T(), err)
// receiver should be closed.
select {
case r, ok := <-receiver:
assert.True(s.T(), ok)
assert.Equal(s.T(), ErrWatchStoppedByUser, r.Err)
case <-time.After(time.Second):
assert.Fail(s.T(), "should receive")
}
}
func (s *ephemeralSuite) TestListWatchFailed() {
testDir := "/list-watch-failed"
// testdir should be empty
receiver := s.ephemeral.List(context.Background(), testDir, true)
n := <-receiver
assert.Error(s.T(), n.Err)
assert.Equal(s.T(), []string(nil), n.Children)
// nothing should pop up from receiver side
select {
case _, ok := <-receiver:
assert.False(s.T(), ok)
case <-time.After(time.Second):
assert.Fail(s.T(), "should receive")
}
}
func (s *ephemeralSuite) TestWatchDisconnect() {
if s.testZK {
fmt.Println("this is skipped in zk due to samuel library issue.")
return
}
testDir := "/watch-disconnect"
// Make a node
assert.NoError(s.T(), s.ephemeral.AddDir(context.Background(), testDir))
// Configure port forwarding to test network disconnection
portForward := 4444
stop, err := pf.PortForward(fmt.Sprintf(":%d", portForward), fmt.Sprintf(":%d", s.port))
assert.NoError(s.T(), err, "no error")
// Make a new storer to be disconnected later.
eph2, err := s.newEphemeral(portForward)
assert.NoError(s.T(), err)
// List with watch. It should return current children
children := eph2.List(context.Background(), testDir, true)
c := <-children
assert.NoError(s.T(), c.Err)
assert.Equal(s.T(), []string{}, c.Children)
// make sure that etcd watch has started
time.Sleep(3 * time.Second)
// Now disconnect
close(stop)
// list channel should get error and close.
select {
case c := <-children:
assert.Error(s.T(), c.Err)
assert.Equal(s.T(), []string(nil), c.Children)
case <-time.After(5 * time.Second):
assert.Fail(s.T(), "should receive")
}
select {
case _, ok := <-children:
assert.False(s.T(), ok)
case <-time.After(5 * time.Second):
assert.Fail(s.T(), "should receive close")
}
}
func (s *ephemeralSuite) TestGet() {
testPath := "/test"
testValue := "studio"
// create node with value
err := s.ephemeral.AddKey(context.Background(), testPath, testValue)
assert.NoError(s.T(), err)
// test get if works
v, err := s.ephemeral.Get(context.Background(), testPath)
assert.NoError(s.T(), err)
assert.Equal(s.T(), testValue, v)
}
func (s *ephemeralSuite) TestListWatchClose() {
testDir := "/list-watch-close"
testNode := "node1"
// Add testDir
err := s.ephemeral.AddDir(context.Background(), testDir)
assert.NoError(s.T(), err)
receiver := s.ephemeral.List(context.Background(), testDir, true)
select {
case n, ok := <-receiver:
assert.True(s.T(), ok)
assert.NoError(s.T(), n.Err)
assert.Equal(s.T(), []string{}, n.Children)
case <-time.After(5 * time.Second):
assert.Fail(s.T(), "should receive")
}
// create node with value
eph2, err := s.newEphemeral(s.port)
assert.NoError(s.T(), err)
err = eph2.AddKey(context.Background(), path.Join(testDir, testNode), "")
assert.NoError(s.T(), err)
n := <-receiver
assert.NoError(s.T(), n.Err)
assert.Equal(s.T(), []string{testNode}, n.Children)
// Close ephemeral and wait
assert.NoError(s.T(), eph2.Close())
select {
case n, ok := <-receiver:
assert.True(s.T(), ok)
assert.NoError(s.T(), n.Err)
assert.Equal(s.T(), []string{}, n.Children)
case <-time.After(5 * time.Second):
assert.Fail(s.T(), "should receive")
}
}
func (s *ephemeralSuite) TestGetDir() {
testPath := "/test"
// create a dir, sleep for a while to wait for previous test end
time.Sleep(time.Duration(2*DefaultEphemeralTTL) * time.Second)
err := s.ephemeral.AddDir(context.Background(), testPath)
assert.NoError(s.T(), err)
// get dir should return error
_, err = s.ephemeral.Get(context.Background(), testPath)
assert.Error(s.T(), err)
}
func (s *ephemeralSuite) TestGetNonExistPath() {
testPath := "/NONEXIST-haha"
// get a non-exist node
_, err := s.ephemeral.Get(context.Background(), testPath)
assert.Equal(s.T(), ErrPathNotFound, err)
}