-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy paththread_test.go
More file actions
43 lines (34 loc) · 785 Bytes
/
thread_test.go
File metadata and controls
43 lines (34 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package queue
import (
"sync/atomic"
"testing"
"time"
)
func TestRoutineGroupRun(t *testing.T) {
t.Run("execute single function", func(t *testing.T) {
g := newRoutineGroup()
var counter int32
g.Run(func() {
atomic.AddInt32(&counter, 1)
})
g.Wait()
if atomic.LoadInt32(&counter) != 1 {
t.Errorf("expected counter to be 1, got %d", counter)
}
})
t.Run("execute multiple functions", func(t *testing.T) {
g := newRoutineGroup()
var counter int32
numRoutines := 10
for i := 0; i < numRoutines; i++ {
g.Run(func() {
atomic.AddInt32(&counter, 1)
time.Sleep(10 * time.Millisecond)
})
}
g.Wait()
if atomic.LoadInt32(&counter) != int32(numRoutines) {
t.Errorf("expected counter to be %d, got %d", numRoutines, counter)
}
})
}