-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunnel_test.go
65 lines (59 loc) · 1.56 KB
/
funnel_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
package funnel
import (
"fmt"
"testing"
"time"
)
func TestFanOut_ZeroWorkers_Panics(t *testing.T) {
defer func() { recover() }()
FanOut(make(chan int), 0, func(int) int { return 0 })
t.Error("should have panicked...")
}
func TestFanOut_TooManyWorkers_Panics(t *testing.T) {
defer func() { recover() }()
FanOut(make(chan int), maxWorkerCount+1, func(int) int { return 0 })
t.Error("should have panicked...")
}
func TestFanOut_NilInput_Panics(t *testing.T) {
defer func() { recover() }()
FanOut(nil, 1, func(int) int { return 0 })
t.Error("should have panicked...")
}
func TestFanOut_NilCallback_Panics(t *testing.T) {
defer func() { recover() }()
FanOut(make(chan int), 1, nil)
t.Error("should have panicked...")
}
func TestFanOut(t *testing.T) {
if testing.Short() {
t.Skip("skipping long-running test")
}
var (
workItemCount = 100
workerCount = 10
expectedDurationInSeconds = workItemCount / workerCount
)
input := make(chan string)
go func() {
defer close(input)
for x := 0; x < workItemCount; x++ {
input <- fmt.Sprint(time.Now().Second())
}
}()
started := time.Now()
output := FanOut(input, workerCount, func(s string) string {
time.Sleep(time.Second) // simulate long-running process
return s + " " + fmt.Sprint(time.Now().Second())
})
for item := range output {
t.Log(item)
}
actualDurationInSeconds := int(time.Since(started).Seconds())
if actualDurationInSeconds != expectedDurationInSeconds {
t.Errorf("\n"+
"got: %d\n"+
"want: %d",
expectedDurationInSeconds,
actualDurationInSeconds)
}
}