-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool_test.go
63 lines (47 loc) · 947 Bytes
/
pool_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
package workpool
import (
"fmt"
"math/rand"
"testing"
)
func RandomString(n int) string {
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
s := make([]rune, n)
for i := range s {
s[i] = letters[rand.Intn(len(letters))]
}
return string(s)
}
func newWorkerPool() WorkerPool {
return Create(50)
}
func TestMap(t *testing.T) {
in := make(chan string, 24)
pool := newWorkerPool()
for i := 0; i < 20; i++ {
in <- RandomString(40)
}
close(in)
f := func(x string) string {
return x + "_" + x
}
out := Map(&pool, in, f)
for x := range out {
fmt.Println(x)
}
}
func runN(f func(), n int) {
for i := 0; i < n; i++ {
f()
}
}
func TestWait(t *testing.T) {
pool := newWorkerPool()
f := func() { fmt.Println("hello") }
runN(func() { pool.Run(f) }, 5)
randStr := func() string { return RandomString(40) }
o := Spawn(&pool, randStr)
x := <-o
pool.Wait()
fmt.Println(x)
}