-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
default_test.go
83 lines (58 loc) · 1.22 KB
/
default_test.go
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package pond
import (
"errors"
"sync/atomic"
"testing"
"github.com/alitto/pond/v2/internal/assert"
)
func TestSubmit(t *testing.T) {
done := make(chan int, 1)
task := Submit(func() {
done <- 10
})
err := task.Wait()
assert.Equal(t, nil, err)
assert.Equal(t, 10, <-done)
}
func TestSubmitWithError(t *testing.T) {
task := SubmitErr(func() error {
return errors.New("sample error")
})
err := task.Wait()
assert.Equal(t, "sample error", err.Error())
}
func TestSubmitWithPanic(t *testing.T) {
task := Submit(func() {
panic("dummy panic")
})
err := task.Wait()
assert.True(t, errors.Is(err, ErrPanic))
assert.Equal(t, "task panicked: dummy panic", err.Error())
}
func TestNewGroup(t *testing.T) {
group := NewGroup()
count := 10
var done atomic.Int32
for i := 0; i < count; i++ {
group.SubmitErr(func() error {
done.Add(1)
return nil
})
}
err := group.Wait()
assert.Equal(t, nil, err)
assert.Equal(t, count, int(done.Load()))
}
func TestNewSubpool(t *testing.T) {
pool := NewSubpool(10)
count := 10
var done atomic.Int32
for i := 0; i < count; i++ {
pool.SubmitErr(func() error {
done.Add(1)
return nil
})
}
pool.StopAndWait()
assert.Equal(t, count, int(done.Load()))
}