Skip to content

Commit

Permalink
test: fix race-condition into unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeamon committed Nov 4, 2023
1 parent 0740780 commit 0ad3695
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions gorqs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,10 @@ func TestAsyncQueue_Basic(t *testing.T) {
case <-time.After(2 * time.Second):
t.Error("running queue did not exit.")
}

if lg := len(results); lg != 3 {
mu.Lock()
lg := len(results)
mu.Unlock()
if lg != 3 {
t.Fatalf("invalid results length. expected 3 but got %d", lg)
return
}
Expand Down Expand Up @@ -504,11 +506,16 @@ func TestSyncQueue_StopOngoing(t *testing.T) {
t.Fatalf("invalid results length. expected 2 but got %d", lg)
return
}

if results[0] != "job1" {
mu.Lock()
r := results[0]
mu.Unlock()
if r != "job1" {
t.Errorf("expected %q but got %s", "job1", results[0])
}
if results[1] != "job2" {
mu.Lock()
r = results[1]
mu.Unlock()
if r != "job2" {
t.Errorf("expected %q but got %s", "job2", results[1])
}
}
Expand Down Expand Up @@ -559,10 +566,16 @@ func TestSyncQueue_TimeoutOngoing(t *testing.T) {
t.Fatalf("invalid results. expected 2 items but got %d", lg)
return
}
if results[0] != "job1" {
mu.Lock()
r := results[0]
mu.Unlock()
if r != "job1" {
t.Errorf("expected %q but got %s", "job1", results[0])
}
if results[1] != "job2" {
mu.Lock()
r = results[1]
mu.Unlock()
if r != "job2" {
t.Errorf("expected %q but got %s", "job2", results[1])
}
}
Expand Down

0 comments on commit 0ad3695

Please sign in to comment.