Skip to content

Commit 02d0ff2

Browse files
Fix race condition in TestWorkerPool (#482)
Co-authored-by: Péter Garamvölgyi <[email protected]>
1 parent a70c317 commit 02d0ff2

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

Diff for: common/utils/workerpool/workerpool_test.go

+24-3
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,35 @@ func TestWorkerPool(t *testing.T) {
2222
atomic.AddInt32(&cnt, -1)
2323
}
2424

25-
go vwp.AddTask(task)
26-
go vwp.AddTask(task)
27-
go vwp.AddTask(task)
25+
vwp.AddTask(task)
26+
vwp.AddTask(task)
2827

2928
time.Sleep(600 * time.Millisecond)
3029
as.Equal(int32(1), atomic.LoadInt32(&cnt))
30+
vwp.AddTask(task)
3131
vwp.Stop()
3232
as.Equal(int32(0), atomic.LoadInt32(&cnt))
33+
}
34+
35+
func TestWorkerPoolMaxWorkers(t *testing.T) {
36+
as := assert.New(t)
37+
38+
vwp := workerpool.NewWorkerPool(2)
39+
vwp.Run()
40+
var cnt int32 = 3
41+
42+
task := func() {
43+
time.Sleep(500 * time.Millisecond)
44+
atomic.AddInt32(&cnt, -1)
45+
}
46+
47+
time1 := time.Now()
48+
vwp.AddTask(task)
49+
vwp.AddTask(task)
50+
vwp.AddTask(task)
51+
vwp.Stop()
52+
time2 := time.Now()
53+
as.Greater(time2.Sub(time1), time.Second*1)
3354

3455
}
3556

0 commit comments

Comments
 (0)