-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpool_test.go
More file actions
66 lines (49 loc) · 1.11 KB
/
pool_test.go
File metadata and controls
66 lines (49 loc) · 1.11 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
package workers
import (
"errors"
"fmt"
"testing"
"time"
assert "github.com/stretchr/testify/assert"
)
var errs []error
var errChan = make(chan error)
var wrap = make(chan struct{})
var done = make(chan struct{})
var mypool = GetPool(5, errChan, wrap, done, 2)
func init() {
go func() {
var err error
for err = range errChan {
errs = append(errs, err)
}
}()
}
func TestEmptyPool(t *testing.T) {
assert.Equal(t, 0, len(errs))
assert.Equal(t, []error(nil), errs)
}
func TestWithWork(t *testing.T) {
tasks := []*Task{
NewTask(func() error { return nil }),
NewTask(func() error { return nil }),
NewTask(func() error { return nil }),
}
mypool.AssignTasks(tasks)
assert.Equal(t, []error(nil), errs)
assert.Equal(t, 0, len(errs))
}
func TestWithError(t *testing.T) {
tasks := []*Task{
NewTask(func() error { return fmt.Errorf("error") }),
NewTask(func() error { return nil }),
NewTask(func() error { return nil }),
}
mypool.AssignTasks(tasks)
time.Sleep(time.Second)
fmt.Println(errs)
assert.Equal(t, errors.New("error"), errs[0])
assert.Equal(t, 1, len(errs))
close(wrap)
<-done
}