-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_test.go
130 lines (107 loc) · 1.68 KB
/
async_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
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
124
125
126
127
128
129
130
package async
import (
"errors"
"sync/atomic"
"testing"
"time"
)
func Test_RunCount(t *testing.T) {
var count uint64
op1 := func() error {
atomic.AddUint64(&count, 1)
return nil
}
op2 := func() error {
atomic.AddUint64(&count, 2)
return nil
}
op3 := func() error {
atomic.AddUint64(&count, 4)
return nil
}
ch := make(chan error)
q := New(op1, op2)
q.Add(op3)
q.Run(ch)
for {
select {
case <-ch:
if count != 7 {
t.Error("All three channels didn't run properly. count = ", count)
}
return
}
}
}
func Test_Run(t *testing.T) {
var a, b int
op1 := func() error {
a = 13
return nil
}
op2 := func() error {
b = 37
return nil
}
ch := make(chan error)
New(op1, op2).Run(ch)
for {
select {
case err := <-ch:
if err != nil {
t.Error("no error expected")
}
if a != 13 {
t.Error("'a' value is not 13, but is", a)
}
if b != 37 {
t.Error("'b' value is not 37, but is", b)
}
return
}
}
}
func Test_RunWithError(t *testing.T) {
var err error
op1 := func() error {
return nil
}
op2 := func() error {
return errors.New("OMG FAIL")
}
ch := make(chan error)
New(op1, op2).Run(ch)
LOOP:
for {
select {
case err = <-ch:
if err != nil {
break LOOP
}
}
}
if err == nil {
t.Error("error expected")
}
}
func TestRunWithTimeout(t *testing.T) {
var err error
longOp := func() error {
time.Sleep(10 * time.Second)
return nil
}
ch := make(chan error)
New(longOp).RunWithTimeout(ch, time.Millisecond*100)
LOOP:
for {
select {
case err = <-ch:
break LOOP
case <-time.Tick(time.Second):
break LOOP
}
}
if err != ErrTimeout {
t.Error("timeout error expected")
}
}