-
Notifications
You must be signed in to change notification settings - Fork 1
/
runner_test.go
110 lines (82 loc) · 1.85 KB
/
runner_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
package eventbus
import (
"testing"
"time"
)
func TestNewRunnerFn(t *testing.T) {
t.Run("Not nil", func(t *testing.T) {
if got := NewRunnerFn(time.Second, 1); got == nil {
t.Errorf("NewRunnerFn() got nil")
}
})
}
func TestPorcessFn_Stop(t *testing.T) {
maxTime := time.Millisecond * 10
pf := NewRunnerFn(maxTime, 5)
for i := 0; i < 5; i++ {
pf.Next(func() {
time.Sleep(maxTime * 2)
})
}
pf.Stop()
time.Sleep(maxTime * 4)
if pf.currentNrFnWorkers != 0 {
t.Errorf("Runner.Stop() current number of workers should be 0 but is %d", pf.currentNrFnWorkers)
}
}
func TestPorcessFn_Next(t *testing.T) {
isFinished := false
maxTime := time.Millisecond * 1
limit := 3
pf := NewRunnerFn(maxTime, limit)
t1 := time.Now()
pf.Next(func() {
time.Sleep(maxTime * 2)
})
elapsed := time.Since(t1)
if elapsed > maxTime+time.Millisecond {
t.Errorf("Runner.Next() dosen't free blocking after maxTime")
}
if pf.currentNrFnWorkers < 0 {
t.Errorf("Runner.Next() dosen't count each worker")
}
time.Sleep(maxTime * 2)
if pf.currentNrFnWorkers < 0 {
t.Errorf("Runner.Next() releases last worker to early")
}
t1 = time.Now()
go func() {
time.Sleep(maxTime / 2)
if pf.currentNrFnWorkers > 1 {
t.Errorf("Runner.Next() increases workers unecessary")
}
}()
pf.Next(func() {
time.Sleep(maxTime)
})
time.Sleep(maxTime)
go func() {
for !isFinished {
time.Sleep(time.Nanosecond)
if pf.currentNrFnWorkers > int32(limit) {
t.Errorf("Runner.Next() increases workers over the limit")
return
}
}
}()
for i := 0; i < limit+1; i++ {
if i == limit {
t1 = time.Now()
}
pf.Next(func() {
time.Sleep(maxTime * time.Duration(limit) * 5)
})
if i == limit {
elapsed = time.Since(t1)
}
}
if elapsed < maxTime*2 {
t.Errorf("Runner.Next() dosen't block when limit is reached")
}
isFinished = true
}