-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkqueue_test.go
119 lines (105 loc) · 3.45 KB
/
workqueue_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
111
112
113
114
115
116
117
118
119
package workqueue
import (
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type MockWorkItem struct {
sleep time.Duration
excecuted bool
}
func (m *MockWorkItem) Exec() {
time.Sleep(m.sleep)
m.excecuted = true
}
func TestAddToQueue(t *testing.T) {
work := &WorkQueue[*MockWorkItem]{
maxThreads: 1,
queue: make([]*MockWorkItem, 0),
processingCount: 0,
addQueueChan: make(chan *MockWorkItem),
endJobChan: make(chan bool),
stopWorkerChan: make(chan bool),
}
work.addToQueue(&MockWorkItem{sleep: 2 * time.Second})
assert.Equal(t, work.GetQueueSize(), 1, "This should be 5")
}
func TestGetNestTask(t *testing.T) {
work := &WorkQueue[*MockWorkItem]{
maxThreads: 1,
queue: make([]*MockWorkItem, 0),
processingCount: 0,
addQueueChan: make(chan *MockWorkItem),
endJobChan: make(chan bool),
stopWorkerChan: make(chan bool),
}
_, err := work.getNextTask()
assert.Error(t, err)
assert.Equal(t, err, errors.New(EMPTY_QUEUE_ERROR_MSG))
work.addToQueue(&MockWorkItem{sleep: 2 * time.Second})
assert.Equal(t, work.GetQueueSize(), 1, "This should be 1")
_, err = work.getNextTask()
assert.Equal(t, work.GetQueueSize(), 0, "This should be 0")
assert.Equal(t, err, nil)
}
func TestTryExecNextTask(t *testing.T) {
work := &WorkQueue[*MockWorkItem]{
maxThreads: 1,
queue: make([]*MockWorkItem, 0),
processingCount: 0,
addQueueChan: make(chan *MockWorkItem),
endJobChan: make(chan bool),
stopWorkerChan: make(chan bool),
}
excecuted := work.tryToExecNextTask()
assert.Equal(t, excecuted, false, "This should by false")
work.addToQueue(&MockWorkItem{sleep: 2 * time.Second})
excecuted = work.tryToExecNextTask()
assert.Equal(t, excecuted, true, "This should be true")
}
func TestAddWorkToWorkQueue(t *testing.T) {
work := New[*MockWorkItem](0)
go work.StartWorker()
work.AddWork(&MockWorkItem{sleep: 1 * time.Second})
time.Sleep(1 * time.Second)
assert.Equal(t, work.GetQueueSize(), 1, "This should be 5")
}
func TestNoneThreadCapacity(t *testing.T) {
work := New[*MockWorkItem](0)
go work.StartWorker()
assert.Equal(t, work.HasThreadsCapacity(), false, "This must be false")
assert.Equal(t, work.GetProcessingCount(), 0, "This should be 0")
assert.Equal(t, work.GetThreadsOccupation(), 1.0, "This should be 1.0")
for i := 0; i < 5; i++ {
work.AddWork(&MockWorkItem{sleep: 2 * time.Second})
}
time.Sleep(1 * time.Second)
assert.Equal(t, work.GetProcessingCount(), 0, "This should be 0")
assert.Equal(t, work.GetThreadsOccupation(), 1.0, "This should be 1.0")
assert.Equal(t, work.GetQueueSize(), 5, "This should be 5")
}
func TestProcessingWithSingleThread(t *testing.T) {
work := New[*MockWorkItem](1)
go work.StartWorker()
assert.Equal(t, work.HasThreadsCapacity(), true, "This must be true")
assert.Equal(t, work.GetProcessingCount(), 0, "This should be 0")
assert.Equal(t, work.GetThreadsOccupation(), 0.0, "This should be 0")
jobs := []*MockWorkItem{
{sleep: 2 * time.Nanosecond},
{sleep: 3 * time.Nanosecond},
{sleep: 4 * time.Nanosecond},
{sleep: 5 * time.Nanosecond},
{sleep: 6 * time.Nanosecond},
{sleep: 7 * time.Nanosecond},
}
for _, job := range jobs {
work.AddWork(job)
}
time.Sleep(1 * time.Second)
assert.Equal(t, work.GetProcessingCount(), 0, "This should be 0")
assert.Equal(t, work.GetThreadsOccupation(), 0.0, "This should be 0")
for _, job := range jobs {
assert.Equal(t, job.excecuted, true, "This should be true")
}
}