-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_test.go
244 lines (205 loc) · 3.72 KB
/
process_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package process
import (
"errors"
"fmt"
"strings"
"testing"
)
var (
e1 = errors.New("E1")
e2 = errors.New("E2")
)
func TestEmpty(t *testing.T) {
pg := NewGroup()
// expect no runtime error nor deadlock
pg.Wait()
if pg.Err() != nil {
t.Fatalf("expected no error")
}
}
func TestProcessGroup_Go_GoE(t *testing.T) {
pg := NewGroup()
// expect to be able to make more than one pass
for p := 0; p < 100; p++ {
n1, n2, n3, n4, n5 := false, false, false, false, false
pg.Go(func() { n1 = true })
pg.Go(func() { n2 = true })
pg.Go(func() { n3 = true })
pg.GoE(func() error { n4 = true; return e1 })
pg.GoE(func() error { n5 = true; return e2 })
pg.Wait() // no deadlock expected
e := pg.Err()
if !n1 {
t.Errorf("no n1")
}
if !n2 {
t.Errorf("no n2")
}
if !n3 {
t.Errorf("no n3")
}
if !n4 {
t.Errorf("no n5")
}
if !n5 {
t.Errorf("no n5")
}
if e == nil {
t.Fatalf("no err")
}
em := e.Error()
if len(em) < 5 {
t.Errorf("no count: %q", em)
}
if strings.Index(em, "E1") < 0 {
t.Errorf("no E1: %q", em)
}
if strings.Index(em, "E2") < 0 {
t.Errorf("no E2: %q", em)
}
if s := pg.Size(); s != 0 {
t.Errorf("size is %d", s)
}
}
}
// FWIW, the equivalent Occam communication patterns are shown as comments.
// -------------------------------------------------------------------------------------------------
// CHAN INT c:
// INT x:
// SEQ
//
// PAR
// c ! 1
// c ! 1
// c ? x
// c ? x
func TestProcessGroup_GoN(t *testing.T) {
c := make(chan int)
pg := NewGroup()
// expect to be able to make more than one pass
for p := 0; p < 100; p++ {
pg.GoN(5, func(i int) { c <- i })
if s := pg.Size(); s == 0 {
t.Errorf("size is 0")
}
var sum int
sum += <-c
sum += <-c
sum += <-c
sum += <-c
sum += <-c
if sum != 10 {
t.Errorf("Got %d", sum)
}
pg.Wait() // no deadlock expected
if s := pg.Size(); s != 0 {
t.Errorf("size is %d", s)
}
}
}
func TestProcessGroup_GoNE(t *testing.T) {
c := make(chan int)
pg := NewGroup()
// expect to be able to make more than one pass
for p := 0; p < 100; p++ {
pg.GoNE(5, func(i int) error { c <- i; return e1 })
var sum int
sum += <-c
sum += <-c
sum += <-c
sum += <-c
sum += <-c
if sum != 10 {
t.Errorf("Got %d", sum)
}
pg.Wait() // no deadlock expected
e := pg.Err()
if e == nil {
t.Fatalf("no err")
}
em := e.Error()
if em != "E1\nE1\nE1\nE1\nE1" {
t.Errorf("bad errors: %q", em)
}
}
}
// -------------------------------------------------------------------------------------------------
// CHAN INT c1:
// INT x, y:
// SEQ
//
// PAR
// c1 ! 1
// c1 ! 1
// SEQ
// PAR
// c2 ! 1
// c2 ! 1
// c2 ? y
// c2 ? y
// c2 ! 1
// c1 ? x
// c1 ? x
// c1 ? x
func TestProcessGroupNested(t *testing.T) {
c1 := make(chan int)
c2 := make(chan int)
pg := NewGroup()
inner := NewGroup()
// expect to be able to make more than one pass
for p := 0; p < 100; p++ {
pg.Go(func() {
c1 <- 1
})
pg.Go(func() {
c1 <- 1
})
pg.Go(func() {
c1 <- 1
<-c2
<-c2
inner.Wait()
})
inner.Go(func() {
c2 <- 1
})
inner.Go(func() {
c2 <- 1
})
<-c1
<-c1
<-c1
pg.Wait() // no deadlock expected
}
}
func TestLongChannel(t *testing.T) {
in, out := WorkQueue[int](16)
end := make(chan bool)
in <- 1
if <-out != 1 {
t.Fatalf("expected 1")
}
in <- 2
in <- 3
in <- 4
in <- 5
go func() {
defer func() { end <- true }()
var act int
exp := 2
for act = range out {
if act != exp {
panic(fmt.Sprintf("got %d, expected %d", act, exp))
}
exp++
}
if act != 999 {
panic(fmt.Sprintf("ended with %d; expected 999", act))
}
}()
for i := 6; i < 1000; i++ {
in <- i
}
close(in)
<-end
}