-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpond_test.go
89 lines (63 loc) · 1.52 KB
/
pond_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
package koi_test
import (
"errors"
"sync"
"testing"
"time"
"github.com/1995parham/koi"
)
const (
queueSize = 0
concurrentCount = 10
)
func TestNoReturn(t *testing.T) {
t.Parallel()
pond := koi.NewPond[int, koi.NoReturn]()
var wg sync.WaitGroup
printer := func(_ int) koi.NoReturn {
time.Sleep(1 * time.Second)
wg.Done()
return koi.None
}
printWorker := koi.MustNewWoker(printer, queueSize, concurrentCount)
pond.MustRegisterWorker("printer", printWorker)
for i := 0; i < concurrentCount; i++ {
wg.Add(1)
if _, err := pond.AddWork("printer", i); err != nil {
t.Errorf("error while adding job: %s", err)
}
}
wg.Wait()
}
func TestWorkerNotFound(t *testing.T) {
t.Parallel()
pond := koi.NewPond[int, koi.NoReturn]()
if _, err := pond.AddWork("printer", 1); !errors.Is(err, koi.ErrWorkerNotFound) {
t.Error("expects not found error")
}
}
func TestReturn(t *testing.T) {
t.Parallel()
pond := koi.NewPond[int, int]()
square := func(i int) int {
return i * i
}
printWorker := koi.MustNewWoker(square, queueSize, concurrentCount)
pond.MustRegisterWorker("square", printWorker)
for i := 0; i < concurrentCount; i++ {
if _, err := pond.AddWork("square", i); err != nil {
t.Errorf("error while adding job: %s", err)
}
}
ch := pond.ResultChan("square")
results := make(map[int]bool)
for i := 0; i < concurrentCount; i++ {
r := <-ch
results[r] = true
}
for i := 0; i < concurrentCount; i++ {
if _, ok := results[i*i]; !ok {
t.Errorf("cannot find result for %d", i)
}
}
}