-
Notifications
You must be signed in to change notification settings - Fork 0
/
candidate_test.go
71 lines (55 loc) · 1.84 KB
/
candidate_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
package leadership
import (
"context"
"testing"
"time"
"github.com/abronan/leadership/mockstore"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestCandidate(t *testing.T) {
kv, err := mockstore.New([]string{}, nil)
assert.NoError(t, err)
assert.NotNil(t, kv)
mockStore := kv.(*mockstore.Mock)
mockLock := &mockstore.Lock{}
mockStore.On("NewLock", context.Background(), mock.Anything, mock.AnythingOfType("*store.LockOptions")).Return(mockLock, nil)
// Lock and unlock always succeeds.
lostCh := make(chan struct{})
var mockLostCh <-chan struct{} = lostCh
mockLock.On("Lock", context.Background()).Return(mockLostCh, nil)
mockLock.On("Unlock", context.Background()).Return(nil)
candidate := NewCandidate(kv, "test_key", "test_node", 0)
// Calling stop when not running for the election should not panic and should
// result in no side effects observed when starting to run for the election.
candidate.Stop()
electedCh, _ := candidate.RunForElection()
// Should issue a false upon start, no matter what.
assert.False(t, <-electedCh)
// Since the lock always succeeeds, we should get elected.
assert.True(t, <-electedCh)
assert.True(t, candidate.IsLeader())
// Signaling a lost lock should get us de-elected...
close(lostCh)
assert.False(t, <-electedCh)
// And we should attempt to get re-elected again.
assert.True(t, <-electedCh)
// When we resign, unlock will get called, we'll be notified of the
// de-election and we'll try to get the lock again.
go candidate.Resign()
assert.False(t, <-electedCh)
assert.True(t, <-electedCh)
candidate.Stop()
// Ensure that the chan closes after some time
for {
select {
case _, open := <-electedCh:
if !open {
mockStore.AssertExpectations(t)
return
}
case <-time.After(1 * time.Second):
t.Fatalf("electedCh not closed correctly")
}
}
}