-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworkerpool_test.go
74 lines (52 loc) · 1.6 KB
/
workerpool_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
package twerk
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestThatNonFunctionReturnsAnError(t *testing.T) {
pool, err := New(123, DefaultConfig)
assert.Error(t, err)
assert.Nil(t, pool)
}
func TestThatInvalidConfigReturnsAnError(t *testing.T) {
pool, err := New(func() {}, Config{})
assert.Error(t, err)
assert.Nil(t, pool)
}
func TestThatFunctionWithoutReturnValuesHasNilChannel(t *testing.T) {
pool, err := New(func() {}, DefaultConfig)
assert.NoError(t, err)
assert.NotNil(t, pool)
resChan, err := pool.Work()
assert.NoError(t, err)
assert.Nil(t, resChan)
}
func TestThatCallingFunctionWithIncorrectArgumentsReturnsAnError(t *testing.T) {
pool, _ := New(func(a int) {}, DefaultConfig)
resChan, err := pool.Work("this is not an int")
assert.Error(t, err)
assert.Nil(t, resChan)
}
func TestThatCallingWorkWithWrongNumberOfArgumentsReturnsAnError(t *testing.T) {
pool, _ := New(func(a, b int) {}, DefaultConfig)
resChan, err := pool.Work(1)
assert.Error(t, err)
assert.Nil(t, resChan)
}
func TestThatCallingAFunctionWithNoArgumentsWithArgumentsReturnsAnError(t *testing.T) {
pool, _ := New(func() {}, DefaultConfig)
resChan, err := pool.Work(1)
assert.Error(t, err)
assert.Nil(t, resChan)
}
func TestThatWorkerActuallyReturnsTheSameNumberOfReturnValues(t *testing.T) {
pool, _ := New(func() (int, int, int) { return 1, 2, 3 }, DefaultConfig)
resChan, err := pool.Work()
assert.NoError(t, err)
assert.NotNil(t, resChan)
results := <-resChan
assert.Len(t, results, 3)
assert.Equal(t, 1, results[0])
assert.Equal(t, 2, results[1])
assert.Equal(t, 3, results[2])
}