-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathintegration_test.go
160 lines (137 loc) · 6.23 KB
/
integration_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
package leaderelection
import (
"context"
mockleaderelection "github.com/ahmagdy/leaderelection/mocks"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
"go.uber.org/zap/zaptest"
"testing"
)
func TestMain(m *testing.M) {
opts := []goleak.Option{
goleak.IgnoreTopFunction("go.etcd.io/etcd/raft/v3.(*node).run"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/server/v3/mvcc/backend.(*backend).run"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/server/v3/wal.(*filePipeline).run"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/server/v3/lease.(*lessor).runLoop"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/fileutil.purgeFile.func1"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/pkg/v3/schedule.(*fifo).run"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/server/v3/mvcc.(*watchableStore).syncWatchersLoop"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/client/v3.(*watchGrpcStream).serveSubstream"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/server/v3/proxy/grpcproxy/adapter.(*chanStream).RecvMsg"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/client/v3.(*lessor).sendKeepAliveLoop"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/client/v3.(*lessor).deadlineLoop"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/client/v3.(*lessor).keepAliveCtxCloser"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/client/v3/concurrency.NewSession.func1"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/server/v3/etcdserver/api/v3rpc.(*serverWatchStream).sendLoop"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/server/v3/etcdserver/api/v3rpc.(*watchServer).Watch"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/client/v3.(*watchGrpcStream).run"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/client/v3/concurrency.(*Election).observe"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/server/v3/etcdserver/api/v3rpc.(*LeaseServer).LeaseKeepAlive"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/client/v3.(*lessor).recvKeepAliveLoop"),
}
goleak.VerifyTestMain(m, opts...)
}
func Test_Leadership(t *testing.T) {
const _fooInstance = "foo"
const _barInstance = "bar"
t.Run("onGainedLeadership is called when there is no participants", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
ctrl := gomock.NewController(t)
logger := zaptest.NewLogger(t)
e := &Embedded{logger: logger}
leadershipEventsWatcher := mockleaderelection.NewMockEventsWatcher(ctrl)
leadershipEventsWatcher.EXPECT().OnGainedLeadership()
e.Start(t)
t.Cleanup(func() {
e.CleanDs()
e.Stop()
cancel()
})
leaderElection, err := New(e.Client(), logger, _fooInstance, leadershipEventsWatcher)
require.NoError(t, err)
require.NoError(t, leaderElection.Start(ctx))
})
t.Run("when a new participant joins, and the leader is healthy, no new election occurs", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
ctrl := gomock.NewController(t)
logger := zaptest.NewLogger(t)
e := &Embedded{logger: logger}
fooLeadershipEventsWatcher := mockleaderelection.NewMockEventsWatcher(ctrl)
barLeadershipEventsWatcher := mockleaderelection.NewMockEventsWatcher(ctrl)
e.Start(t)
t.Cleanup(func() {
e.Stop()
cancel()
})
fooLeadershipEventsWatcher.EXPECT().OnGainedLeadership()
fooSvcLeaderElection, err := New(e.Client(), logger, _fooInstance, fooLeadershipEventsWatcher)
require.NoError(t, err)
require.NoError(t, fooSvcLeaderElection.Start(ctx))
barSvcLeaderElection, err := New(e.Client(), logger, _barInstance, barLeadershipEventsWatcher)
require.NoError(t, err)
require.NoError(t, barSvcLeaderElection.Start(ctx))
})
t.Run("when the leader stops, a new leader from the participants gets elected", func(t *testing.T) {
// Note, there is a potential raise in this test
ctx, cancel := context.WithCancel(context.Background())
ctrl := gomock.NewController(t)
logger := zaptest.NewLogger(t)
wait := make(chan struct{}, 2)
e := &Embedded{logger: logger}
fooLeadershipEventsWatcher := mockleaderelection.NewMockEventsWatcher(ctrl)
barLeadershipEventsWatcher := mockleaderelection.NewMockEventsWatcher(ctrl)
e.Start(t)
t.Cleanup(func() {
e.CleanDs()
e.Stop()
cancel()
})
fooLeadershipEventsWatcher.EXPECT().OnGainedLeadership()
fooLeadershipEventsWatcher.EXPECT().OnLostLeadership().Do(func() { wait <- struct{}{} })
barLeadershipEventsWatcher.EXPECT().OnGainedLeadership().Do(func() { wait <- struct{}{} })
fooSvcLeaderElection, err := New(e.Client(), logger, _fooInstance, fooLeadershipEventsWatcher)
require.NoError(t, err)
require.NoError(t, fooSvcLeaderElection.Start(ctx))
barSvcLeaderElection, err := New(e.Client(), logger, _barInstance, barLeadershipEventsWatcher)
require.NoError(t, err)
require.NoError(t, barSvcLeaderElection.Start(ctx))
require.NoError(t, fooSvcLeaderElection.Stop(ctx))
// block both OnLostLeadership and second OnGainedLeadership
// so the test doesn't exist before they are satisfied
<-wait
<-wait
})
t.Run("when the leader resigns, a new leader from the participants gets elected", func(t *testing.T) {
// setup
ctx, cancel := context.WithCancel(context.Background())
ctrl := gomock.NewController(t)
logger := zaptest.NewLogger(t)
wait := make(chan struct{}, 2)
e := &Embedded{logger: logger}
fooLeadershipEventsWatcher := mockleaderelection.NewMockEventsWatcher(ctrl)
barLeadershipEventsWatcher := mockleaderelection.NewMockEventsWatcher(ctrl)
e.Start(t)
t.Cleanup(func() {
e.CleanDs()
e.Stop()
cancel()
})
// expect
fooLeadershipEventsWatcher.EXPECT().OnGainedLeadership()
fooLeadershipEventsWatcher.EXPECT().OnLostLeadership().Do(func() { wait <- struct{}{} })
barLeadershipEventsWatcher.EXPECT().OnGainedLeadership().Do(func() { wait <- struct{}{} })
// assert
fooSvcLeaderElection, err := New(e.Client(), logger, _fooInstance, fooLeadershipEventsWatcher)
require.NoError(t, err)
require.NoError(t, fooSvcLeaderElection.Start(ctx))
barSvcLeaderElection, err := New(e.Client(), logger, _barInstance, barLeadershipEventsWatcher)
require.NoError(t, err)
require.NoError(t, barSvcLeaderElection.Start(ctx))
require.NoError(t, fooSvcLeaderElection.Resign(ctx))
// block both OnLostLeadership and second OnGainedLeadership
// so the test doesn't exist before they are satisfied
<-wait
<-wait
})
}