-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
83 lines (65 loc) · 1.5 KB
/
main_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
package workerpool
import (
"testing"
"time"
)
type dummyTask struct {
done bool
taskDuration time.Duration
}
func (d *dummyTask) Execute() {
time.Sleep(d.taskDuration)
d.done = true
}
func TestCreateService(t *testing.T) {
// given
taskTime := time.Duration(100 * time.Millisecond)
task := dummyTask{done: false, taskDuration: taskTime}
workers := 1
queueDepth := 1
// when
undertest := NewService(workers, queueDepth)
defer undertest.Exit()
undertest.Run()
undertest.Send(&task)
time.Sleep(120 * time.Millisecond) // wait taskTime +20ms
// then
if !task.done {
t.Errorf("task incomplete")
}
}
func TestDelayedRunDispacher(t *testing.T) {
// given
numOfTaks := 4
taskDuration := time.Duration(100 * time.Millisecond)
workers := 1
queueDepth := 0 //unbuffered channel
// when
undertest := NewService(workers, queueDepth)
defer undertest.Exit()
// run dispacher after 120 ms.
go func(wp *Service) {
wait := time.After(120 * time.Millisecond)
<-wait
wp.Run()
}(undertest)
allTasks := make([]*dummyTask, numOfTaks)
go func(at []*dummyTask) {
for i := 0; i < numOfTaks; i++ {
task := dummyTask{done: false, taskDuration: taskDuration}
at[i] = &task
undertest.Send(&task)
}
}(allTasks)
// after 420ms all results must be completed.
time.Sleep(420 * time.Millisecond)
doneCount := 0
for i := range allTasks {
if allTasks[i].done {
doneCount++
}
}
if doneCount == numOfTaks {
t.Errorf("must be at least one task incomplete. Count: %d", doneCount)
}
}