Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit f57fa10

Browse files
author
David Chung
authored
Manager flaky test (#320)
Signed-off-by: David Chung <[email protected]>
1 parent eab86f0 commit f57fa10

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

pkg/manager/manager.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ func (m *manager) Start() (<-chan struct{}, error) {
126126
log.Infoln("Manager stopped.")
127127
return
128128

129-
case leader := <-notify:
129+
case leader, open := <-notify:
130+
131+
if !open {
132+
return
133+
}
130134

131135
// This channel has data only when there's been a leadership change.
132136

@@ -150,11 +154,17 @@ func (m *manager) Start() (<-chan struct{}, error) {
150154
case <-m.stop:
151155

152156
log.Infoln("Stopping..")
157+
m.stop = nil
153158
close(stopWorkQueue)
154159
close(notify)
155160
return
156161

157-
case evt := <-leaderChan:
162+
case evt, open := <-leaderChan:
163+
164+
if !open {
165+
return
166+
}
167+
158168
// This here handles possible duplicated events about leadership and fires only when there
159169
// is a change.
160170

@@ -198,8 +208,8 @@ func (m *manager) Stop() {
198208
if m.stop == nil {
199209
return
200210
}
201-
m.leader.Stop()
202211
close(m.stop)
212+
m.leader.Stop()
203213
}
204214

205215
func (m *manager) getCurrentState() (GlobalSpec, error) {

pkg/manager/manager_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ func testToStruct(m *json.RawMessage) interface{} {
121121
return &o
122122
}
123123

124+
func testCloseAll(c []chan string) {
125+
for _, cc := range c {
126+
close(cc)
127+
}
128+
}
129+
124130
func TestNoCallsToGroupWhenNoLeader(t *testing.T) {
125131
ctrl := gomock.NewController(t)
126132
defer ctrl.Finish()
@@ -145,11 +151,15 @@ func TestNoCallsToGroupWhenNoLeader(t *testing.T) {
145151
manager1.Start()
146152
manager2.Start()
147153

154+
testSetLeader(t, leaderChans, "nobody")
155+
148156
manager1.Stop()
149157
manager2.Stop()
150158

151159
stoppable1.Stop()
152160
stoppable2.Stop()
161+
162+
testCloseAll(leaderChans)
153163
}
154164

155165
func TestStartOneLeader(t *testing.T) {
@@ -209,6 +219,7 @@ func TestStartOneLeader(t *testing.T) {
209219
stoppable1.Stop()
210220
stoppable2.Stop()
211221

222+
testCloseAll(leaderChans)
212223
}
213224

214225
func TestChangeLeadership(t *testing.T) {
@@ -306,4 +317,6 @@ func TestChangeLeadership(t *testing.T) {
306317

307318
stoppable1.Stop()
308319
stoppable2.Stop()
320+
321+
testCloseAll(leaderChans)
309322
}

0 commit comments

Comments
 (0)