4
4
package queue
5
5
6
6
import (
7
- "os"
8
7
"strconv"
9
8
"sync"
9
+ "sync/atomic"
10
10
"testing"
11
11
"time"
12
12
@@ -16,10 +16,7 @@ import (
16
16
)
17
17
18
18
func TestPersistableChannelUniqueQueue (t * testing.T ) {
19
- if os .Getenv ("CI" ) != "" {
20
- t .Skip ("Skipping because test is flaky on CI" )
21
- }
22
-
19
+ // Create a temporary directory for the queue
23
20
tmpDir := t .TempDir ()
24
21
_ = log .NewLogger (1000 , "console" , "console" , `{"level":"warn","stacktracelevel":"NONE","stderr":true}` )
25
22
@@ -100,7 +97,7 @@ func TestPersistableChannelUniqueQueue(t *testing.T) {
100
97
executedInitial := map [string ][]string {}
101
98
hasInitial := map [string ][]string {}
102
99
103
- fillQueue := func (name string , done chan struct {} ) {
100
+ fillQueue := func (name string , done chan int64 ) {
104
101
t .Run ("Initial Filling: " + name , func (t * testing.T ) {
105
102
lock := sync.Mutex {}
106
103
@@ -157,33 +154,39 @@ func TestPersistableChannelUniqueQueue(t *testing.T) {
157
154
assert .Equal (t , 101 , len (executedInitial [name ])+ len (hasInitial [name ]))
158
155
mapLock .Unlock ()
159
156
})
157
+ mapLock .Lock ()
158
+ count := int64 (len (hasInitial [name ]))
159
+ mapLock .Unlock ()
160
+ done <- count
160
161
close (done )
161
162
}
162
163
163
- doneA := make (chan struct {} )
164
- doneB := make (chan struct {} )
164
+ hasQueueAChan := make (chan int64 )
165
+ hasQueueBChan := make (chan int64 )
165
166
166
- go fillQueue ("QueueA" , doneA )
167
- go fillQueue ("QueueB" , doneB )
167
+ go fillQueue ("QueueA" , hasQueueAChan )
168
+ go fillQueue ("QueueB" , hasQueueBChan )
168
169
169
- <- doneA
170
- <- doneB
170
+ hasA := <- hasQueueAChan
171
+ hasB := <- hasQueueBChan
171
172
172
173
executedEmpty := map [string ][]string {}
173
174
hasEmpty := map [string ][]string {}
174
- emptyQueue := func (name string , done chan struct {}) {
175
+ emptyQueue := func (name string , numInQueue int64 , done chan struct {}) {
175
176
t .Run ("Empty Queue: " + name , func (t * testing.T ) {
176
177
lock := sync.Mutex {}
177
178
stop := make (chan struct {})
178
179
179
180
// collect the tasks that have been executed
181
+ atomicCount := int64 (0 )
180
182
handle := func (data ... Data ) []Data {
181
183
lock .Lock ()
182
184
for _ , datum := range data {
183
185
mapLock .Lock ()
184
186
executedEmpty [name ] = append (executedEmpty [name ], datum .(string ))
185
187
mapLock .Unlock ()
186
- if datum .(string ) == "final" {
188
+ count := atomic .AddInt64 (& atomicCount , 1 )
189
+ if count >= numInQueue {
187
190
close (stop )
188
191
}
189
192
}
@@ -217,11 +220,11 @@ func TestPersistableChannelUniqueQueue(t *testing.T) {
217
220
close (done )
218
221
}
219
222
220
- doneA = make (chan struct {})
221
- doneB = make (chan struct {})
223
+ doneA : = make (chan struct {})
224
+ doneB : = make (chan struct {})
222
225
223
- go emptyQueue ("QueueA" , doneA )
224
- go emptyQueue ("QueueB" , doneB )
226
+ go emptyQueue ("QueueA" , hasA , doneA )
227
+ go emptyQueue ("QueueB" , hasB , doneB )
225
228
226
229
<- doneA
227
230
<- doneB
@@ -237,20 +240,20 @@ func TestPersistableChannelUniqueQueue(t *testing.T) {
237
240
hasEmpty = map [string ][]string {}
238
241
mapLock .Unlock ()
239
242
240
- doneA = make (chan struct {} )
241
- doneB = make (chan struct {} )
243
+ hasQueueAChan = make (chan int64 )
244
+ hasQueueBChan = make (chan int64 )
242
245
243
- go fillQueue ("QueueA" , doneA )
244
- go fillQueue ("QueueB" , doneB )
246
+ go fillQueue ("QueueA" , hasQueueAChan )
247
+ go fillQueue ("QueueB" , hasQueueBChan )
245
248
246
- <- doneA
247
- <- doneB
249
+ hasA = <- hasQueueAChan
250
+ hasB = <- hasQueueBChan
248
251
249
252
doneA = make (chan struct {})
250
253
doneB = make (chan struct {})
251
254
252
- go emptyQueue ("QueueA" , doneA )
253
- go emptyQueue ("QueueB" , doneB )
255
+ go emptyQueue ("QueueA" , hasA , doneA )
256
+ go emptyQueue ("QueueB" , hasB , doneB )
254
257
255
258
<- doneA
256
259
<- doneB
0 commit comments