Skip to content

Commit ee21031

Browse files
committed
test: add comprehensive tests for routine group functionalities
- Add new file `thread_test.go` for testing routine groups - Add test for running a single function in the routine group - Add test for running multiple functions concurrently in the routine group Signed-off-by: appleboy <[email protected]>
1 parent c71c794 commit ee21031

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

thread_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package queue
2+
3+
import (
4+
"sync/atomic"
5+
"testing"
6+
"time"
7+
)
8+
9+
func TestRoutineGroupRun(t *testing.T) {
10+
t.Run("execute single function", func(t *testing.T) {
11+
g := newRoutineGroup()
12+
var counter int32
13+
14+
g.Run(func() {
15+
atomic.AddInt32(&counter, 1)
16+
})
17+
18+
g.Wait()
19+
20+
if atomic.LoadInt32(&counter) != 1 {
21+
t.Errorf("expected counter to be 1, got %d", counter)
22+
}
23+
})
24+
25+
t.Run("execute multiple functions", func(t *testing.T) {
26+
g := newRoutineGroup()
27+
var counter int32
28+
numRoutines := 10
29+
30+
for i := 0; i < numRoutines; i++ {
31+
g.Run(func() {
32+
atomic.AddInt32(&counter, 1)
33+
time.Sleep(10 * time.Millisecond)
34+
})
35+
}
36+
37+
g.Wait()
38+
39+
if atomic.LoadInt32(&counter) != int32(numRoutines) {
40+
t.Errorf("expected counter to be %d, got %d", numRoutines, counter)
41+
}
42+
})
43+
}

0 commit comments

Comments
 (0)