-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelection_test.go
334 lines (261 loc) · 8.46 KB
/
election_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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package prophet
import (
"context"
"fmt"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.etcd.io/etcd/clientv3"
)
type electorTester struct {
sync.Mutex
group uint64
id string
leader bool
elector Elector
cancel context.CancelFunc
ctx context.Context
startedC chan interface{}
once sync.Once
blockTime time.Duration
}
func newElectorTester(group uint64, id string, elector Elector) *electorTester {
ctx, cancel := context.WithCancel(context.Background())
return &electorTester{
startedC: make(chan interface{}),
group: group,
id: id,
elector: elector,
ctx: ctx,
cancel: cancel,
}
}
func (t *electorTester) start() {
go t.elector.ElectionLoop(t.ctx, t.group, t.id, t.becomeLeader, t.becomeFollower)
<-t.startedC
}
func (t *electorTester) stop(blockTime time.Duration) {
t.blockTime = blockTime
t.cancel()
}
func (t *electorTester) isLeader() bool {
t.Lock()
defer t.Unlock()
return t.leader
}
func (t *electorTester) becomeLeader() {
t.Lock()
defer t.Unlock()
t.leader = true
t.notifyStarted()
if t.blockTime > 0 {
<-time.After(t.blockTime)
}
}
func (t *electorTester) becomeFollower() {
t.Lock()
defer t.Unlock()
t.leader = false
t.notifyStarted()
if t.blockTime > 0 {
<-time.After(t.blockTime)
}
}
func (t *electorTester) notifyStarted() {
t.once.Do(func() {
if t.startedC != nil {
close(t.startedC)
t.startedC = nil
}
})
}
func TestElectionLoop(t *testing.T) {
stopC, port, err := startTestSingleEtcd(t)
if err != nil {
assert.FailNowf(t, "start embed etcd failed", "error: %+v", err)
}
defer close(stopC)
client, err := clientv3.New(clientv3.Config{
Endpoints: strings.Split(fmt.Sprintf("http://127.0.0.1:%d", port), ","),
DialTimeout: DefaultTimeout,
})
assert.Nil(t, err, "create etcd client failed")
defer client.Close()
elector, err := NewElector(client, WithLeaderLeaseSeconds(1))
assert.Nil(t, err, "create elector failed")
defer elector.Stop(0)
value1 := newElectorTester(0, "1", elector)
value2 := newElectorTester(0, "2", elector)
value1.start()
value2.start()
defer value1.stop(0)
defer value2.stop(0)
assert.True(t, value1.isLeader(), "value1 must be leader")
assert.False(t, value2.isLeader(), "value2 must be follower")
value3 := newElectorTester(0, "", elector)
value3.start()
defer value3.stop(0)
assert.False(t, value3.isLeader(), "value3 must be follower")
value1.stop(0)
time.Sleep(time.Second + time.Millisecond*200)
assert.True(t, value2.isLeader(), "value2 must be leader")
value2.stop(0)
time.Sleep(time.Second + time.Millisecond*200)
assert.False(t, value2.isLeader(), "value2 must be follower")
assert.False(t, value3.isLeader(), "value3 must be follower")
}
func TestElectionLoopWithDistributedLock(t *testing.T) {
stopC, port, err := startTestSingleEtcd(t)
if err != nil {
assert.FailNowf(t, "start embed etcd failed", "error: %+v", err)
}
defer close(stopC)
client, err := clientv3.New(clientv3.Config{
Endpoints: strings.Split(fmt.Sprintf("http://127.0.0.1:%d", port), ","),
DialTimeout: DefaultTimeout,
})
assert.Nil(t, err, "create etcd client failed")
defer client.Close()
elector, err := NewElector(client,
WithLeaderLeaseSeconds(1),
WithLockIfBecomeLeader(true))
assert.Nil(t, err, "create elector failed")
defer elector.Stop(0)
value1 := newElectorTester(0, "1", elector)
value2 := newElectorTester(0, "2", elector)
value1.start()
value2.start()
defer value1.stop(0)
defer value2.stop(0)
assert.True(t, value1.isLeader(), "value1 must be leader")
assert.False(t, value2.isLeader(), "value2 must be follower")
value1.stop(time.Second * 2)
time.Sleep(time.Second + time.Millisecond*200)
assert.False(t, value2.isLeader(), "value2 must be follower before distributed lock released")
time.Sleep(time.Second + time.Millisecond*200)
assert.True(t, value2.isLeader(), "value2 must be leader after distributed lock released")
}
func TestChangeLeaderTo(t *testing.T) {
stopC, port, err := startTestSingleEtcd(t)
if err != nil {
assert.FailNowf(t, "start embed etcd failed", "error: %+v", err)
}
defer close(stopC)
client, err := clientv3.New(clientv3.Config{
Endpoints: strings.Split(fmt.Sprintf("http://127.0.0.1:%d", port), ","),
DialTimeout: DefaultTimeout,
})
assert.Nil(t, err, "create etcd client failed")
defer client.Close()
elector, err := NewElector(client,
WithLeaderLeaseSeconds(1))
assert.Nil(t, err, "create elector failed")
defer elector.Stop(0)
value1 := newElectorTester(0, "1", elector)
value2 := newElectorTester(0, "2", elector)
value1.start()
value2.start()
defer value1.stop(0)
defer value2.stop(0)
err = elector.ChangeLeaderTo(0, "2", "3")
assert.NotNil(t, err, "only leader node can transfer leader")
err = elector.ChangeLeaderTo(0, "1", "2")
assert.Nil(t, err, "change leader failed")
time.Sleep(time.Second + time.Millisecond*200)
assert.False(t, value1.isLeader(), "value1 must be follower")
assert.True(t, value2.isLeader(), "value2 must be leader")
}
func TestCurrentLeader(t *testing.T) {
stopC, port, err := startTestSingleEtcd(t)
if err != nil {
assert.FailNowf(t, "start embed etcd failed", "error: %+v", err)
}
defer close(stopC)
client, err := clientv3.New(clientv3.Config{
Endpoints: strings.Split(fmt.Sprintf("http://127.0.0.1:%d", port), ","),
DialTimeout: DefaultTimeout,
})
assert.Nil(t, err, "create etcd client failed")
defer client.Close()
elector, err := NewElector(client,
WithLeaderLeaseSeconds(1))
assert.Nil(t, err, "create elector failed")
defer elector.Stop(0)
value1 := newElectorTester(0, "1", elector)
value2 := newElectorTester(0, "2", elector)
value1.start()
value2.start()
defer value1.stop(0)
defer value2.stop(0)
data, err := elector.CurrentLeader(0)
assert.Nil(t, err, "get current leader failed")
assert.Equal(t, "1", string(data), "current leader failed")
elector.ChangeLeaderTo(0, "1", "2")
assert.Nil(t, err, "get current leader failed")
time.Sleep(time.Second + time.Millisecond*200)
data, err = elector.CurrentLeader(0)
assert.Nil(t, err, "get current leader failed")
assert.Equalf(t, "2", string(data), "current leader failed, %+v", value2.isLeader())
}
func TestStop(t *testing.T) {
stopC, port, err := startTestSingleEtcd(t)
if err != nil {
assert.FailNowf(t, "start embed etcd failed", "error: %+v", err)
}
defer close(stopC)
client, err := clientv3.New(clientv3.Config{
Endpoints: strings.Split(fmt.Sprintf("http://127.0.0.1:%d", port), ","),
DialTimeout: DefaultTimeout,
})
assert.Nil(t, err, "create etcd client failed")
defer client.Close()
e, err := NewElector(client,
WithLeaderLeaseSeconds(1))
assert.Nil(t, err, "create elector failed")
defer e.Stop(0)
value1 := newElectorTester(0, "1", e)
value2 := newElectorTester(0, "2", e)
value1.start()
value2.start()
defer value1.stop(0)
defer value2.stop(0)
e.Stop(0)
assert.Equal(t, 0, len(e.(*elector).watchers), "watchers must be clear")
assert.Equal(t, 0, len(e.(*elector).leasors), "leasors must be clear")
}
func TestDoIfLeader(t *testing.T) {
stopC, port, err := startTestSingleEtcd(t)
if err != nil {
assert.FailNowf(t, "start embed etcd failed", "error: %+v", err)
}
defer close(stopC)
client, err := clientv3.New(clientv3.Config{
Endpoints: strings.Split(fmt.Sprintf("http://127.0.0.1:%d", port), ","),
DialTimeout: DefaultTimeout,
})
assert.Nil(t, err, "create etcd client failed")
defer client.Close()
elector, err := NewElector(client,
WithLeaderLeaseSeconds(1))
assert.Nil(t, err, "create elector failed")
value1 := newElectorTester(0, "1", elector)
value2 := newElectorTester(0, "2", elector)
value1.start()
value2.start()
defer value1.stop(0)
defer value2.stop(0)
ok, err := elector.DoIfLeader(0, "1", nil, clientv3.OpPut("/key1", "value1"))
assert.Nil(t, err, "check do if leader failed")
assert.True(t, ok, "check do if leader failed")
ok, err = elector.DoIfLeader(0, "2", nil, clientv3.OpPut("/key2", "value2"))
assert.Nil(t, err, "check do if leader failed")
assert.False(t, ok, "check do if leader failed")
ok, err = elector.DoIfLeader(0, "1", []clientv3.Cmp{clientv3.Value("value2")}, clientv3.OpPut("/key1", "value2"))
assert.Nil(t, err, "check do if leader failed")
assert.False(t, ok, "check do if leader failed")
ok, err = elector.DoIfLeader(0, "1", []clientv3.Cmp{clientv3.Compare(clientv3.Value("/key1"), "=", "value1")}, clientv3.OpPut("/key1", "value2"))
assert.Nil(t, err, "check do if leader failed")
assert.True(t, ok, "check do if leader failed")
}