-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool_test.go
More file actions
123 lines (96 loc) · 1.74 KB
/
pool_test.go
File metadata and controls
123 lines (96 loc) · 1.74 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package grpool
import (
"context"
"errors"
"sync/atomic"
"testing"
)
type (
adder struct {
i atomic.Int32
}
errorer struct {
result bool
}
panicer struct {
result bool
}
)
func (a *adder) Run() error {
a.i.Add(1)
return nil
}
var ErrorErr = errors.New("error")
func (e *errorer) Run() error {
return ErrorErr
}
func (e *errorer) Handle(err error) {
if err == ErrorErr {
e.result = true
}
}
func (p *panicer) Run() error {
panic("panic")
}
func (p *panicer) Handle(err error) {
var rpe *RunnablePanicError
if errors.As(err, &rpe) {
if r, ok := rpe.Value().(string); ok {
if r == "panic" {
p.result = true
}
}
}
}
func TestNewPoolBasic(t *testing.T) {
expected := 5
p := NewPool(context.Background(), 1, expected)
a := &adder{}
for i := 0; i < expected; i++ {
p.Run(a)
}
p.Close(true)
res := a.i.Load()
if res != int32(expected) {
t.Errorf("got %d, expected %d", res, expected)
}
}
func TestNewPoolAdvanced(t *testing.T) {
expected := 1000
p := NewPool(context.Background(), 10, expected)
a := &adder{}
for i := 0; i < expected; i++ {
p.Run(a)
}
p.Close(true)
res := a.i.Load()
if res != int32(expected) {
t.Errorf("got %d, expected %d", res, expected)
}
}
func TestErrorOnClosedChannel(t *testing.T) {
p := NewPool(context.Background(), 1, 1)
a := &adder{}
p.Close(true)
if err := p.Run(a); err == nil {
t.Error("expected error on Run()")
}
}
func TestHandleError(t *testing.T) {
p := NewPool(context.Background(), 1, 1)
e := &errorer{}
p.Run(e)
p.Close(true)
if !e.result {
t.Error("result not set")
}
}
func TestHandlePanic(t *testing.T) {
p := NewPool(context.Background(), 1, 1)
e := &panicer{}
p.Run(e)
p.Close(true)
if !e.result {
t.Error("result not set")
}
}