-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_test.go
74 lines (59 loc) · 1.13 KB
/
stream_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 easyworker
import (
"testing"
"time"
)
func TestStreamStopWithOutRun(t *testing.T) {
inCh := make(chan []any, 1)
outCh := make(chan any)
// test with stream.
eWorker, err := NewStream(defaultConfig(strId), inCh, outCh)
if err != nil {
t.Error("create EasyWorker failed, ", err)
}
err = eWorker.Stop()
if err == nil {
t.Error("StopStream malfunction")
}
}
func TestStream(t *testing.T) {
inCh := make(chan []any, 1)
outCh := make(chan any)
// test with stream.
eWorker, err := NewStream(defaultConfig(strId), inCh, outCh)
if err != nil {
t.Error("create EasyStream failed, ", err)
}
e := eWorker.Run()
if e != nil {
t.Error("run stream task failed, ", e)
}
num := 5
go func() {
for i := 0; i < num; i++ {
input := []any{i, "hello"}
// send task to stream
inCh <- input
}
}()
counterCh := make(chan int)
go func() {
counter := 0
l:
for {
select {
// get result from stream
case <-outCh:
counter++
case <-time.After(time.Millisecond * 200):
break l
}
}
counterCh <- counter
}()
out := <-counterCh
if out != num {
t.Error("wrong result")
}
eWorker.Stop()
}