Skip to content

Commit 4935078

Browse files
committed
Prevent deadlock in TestPersistableChannelQueue
There is a potential deadlock in TestPersistableChannelQueue due to attempting to shutdown the test queue before it is ready. Signed-off-by: Andrew Thornton <[email protected]>
1 parent e1d6559 commit 4935078

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

modules/queue/queue_disk_channel_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,26 @@ func TestPersistableChannelQueue(t *testing.T) {
4242
}, &testData{})
4343
assert.NoError(t, err)
4444

45+
readyForShutdown := make(chan struct{})
46+
readyForTerminate := make(chan struct{})
47+
4548
go queue.Run(func(shutdown func()) {
4649
lock.Lock()
4750
defer lock.Unlock()
51+
select {
52+
case <-readyForShutdown:
53+
default:
54+
close(readyForShutdown)
55+
}
4856
queueShutdown = append(queueShutdown, shutdown)
4957
}, func(terminate func()) {
5058
lock.Lock()
5159
defer lock.Unlock()
60+
select {
61+
case <-readyForTerminate:
62+
default:
63+
close(readyForTerminate)
64+
}
5265
queueTerminate = append(queueTerminate, terminate)
5366
})
5467

@@ -74,6 +87,7 @@ func TestPersistableChannelQueue(t *testing.T) {
7487
err = queue.Push(test1)
7588
assert.Error(t, err)
7689

90+
<-readyForShutdown
7791
// Now shutdown the queue
7892
lock.Lock()
7993
callbacks := make([]func(), len(queueShutdown))
@@ -97,6 +111,7 @@ func TestPersistableChannelQueue(t *testing.T) {
97111
}
98112

99113
// terminate the queue
114+
<-readyForTerminate
100115
lock.Lock()
101116
callbacks = make([]func(), len(queueTerminate))
102117
copy(callbacks, queueTerminate)
@@ -123,13 +138,26 @@ func TestPersistableChannelQueue(t *testing.T) {
123138
}, &testData{})
124139
assert.NoError(t, err)
125140

141+
readyForShutdown = make(chan struct{})
142+
readyForTerminate = make(chan struct{})
143+
126144
go queue.Run(func(shutdown func()) {
127145
lock.Lock()
128146
defer lock.Unlock()
147+
select {
148+
case <-readyForShutdown:
149+
default:
150+
close(readyForShutdown)
151+
}
129152
queueShutdown = append(queueShutdown, shutdown)
130153
}, func(terminate func()) {
131154
lock.Lock()
132155
defer lock.Unlock()
156+
select {
157+
case <-readyForTerminate:
158+
default:
159+
close(readyForTerminate)
160+
}
133161
queueTerminate = append(queueTerminate, terminate)
134162
})
135163

@@ -141,13 +169,15 @@ func TestPersistableChannelQueue(t *testing.T) {
141169
assert.Equal(t, test2.TestString, result4.TestString)
142170
assert.Equal(t, test2.TestInt, result4.TestInt)
143171

172+
<-readyForShutdown
144173
lock.Lock()
145174
callbacks = make([]func(), len(queueShutdown))
146175
copy(callbacks, queueShutdown)
147176
lock.Unlock()
148177
for _, callback := range callbacks {
149178
callback()
150179
}
180+
<-readyForTerminate
151181
lock.Lock()
152182
callbacks = make([]func(), len(queueTerminate))
153183
copy(callbacks, queueTerminate)

0 commit comments

Comments
 (0)