-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_scheduler_test.go
153 lines (128 loc) · 3.46 KB
/
dynamic_scheduler_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package gowq
import (
"context"
"errors"
"fmt"
"math/rand"
"slices"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestDynamicWQ(t *testing.T) {
t.Run("basic setup", func(t *testing.T) {
wq := New[*FakeResult](3)
var startWait sync.WaitGroup
startWait.Add(1)
go func() {
wq.Start(context.TODO())
startWait.Done()
}()
checkvalue := 0
mtx := sync.Mutex{}
time.Sleep(1 * time.Second)
for i := 0; i < 100; i++ {
wq.Schedule(func(ctx context.Context) (*FakeResult, error) {
mtx.Lock()
checkvalue++
mtx.Unlock()
return nil, nil
})
}
terminated := wq.Shutdown()
require.True(t, terminated, "Start has not terminated yet")
mtx.Lock()
require.Equal(t, 100, checkvalue, "Unexpected number of jobs executed")
mtx.Unlock()
})
t.Run("error management", func(t *testing.T) {
wq := New[*FakeResult](10)
var startWait sync.WaitGroup
startWait.Add(1)
go func() {
wq.Start(context.TODO())
startWait.Done()
}()
checkvalue := 0
mtx := sync.Mutex{}
time.Sleep(1 * time.Second)
for i := 0; i < 100; i++ {
j := i
wq.Schedule(func(ctx context.Context) (*FakeResult, error) {
time.Sleep(time.Duration(rand.Intn(10)*10) * time.Millisecond)
if j%2 == 0 {
return nil, fmt.Errorf("error %d", j)
}
mtx.Lock()
checkvalue++
mtx.Unlock()
return &FakeResult{id: j}, nil
})
}
terminated := wq.Shutdown()
time.Sleep(1 * time.Second)
require.True(t, terminated, "Start has not terminated yet")
mtx.Lock()
require.Equal(t, 50, checkvalue, "Unexpected number of jobs executed")
mtx.Unlock()
errorsList := wq.GetErrors(false)
require.Equal(t, 50, len(errorsList), "Unexpected number of errors")
for i, err := range errorsList {
require.True(t, errors.Is(err, ErrJobFailed), "Unexpected error type for error[%d]: %s", i, err.Error())
}
resultsList := wq.GetResults()
require.Len(t, resultsList, 50)
expectedResults := make([]int, 0)
for i := 0; i < 100; i++ {
if i%2 != 0 {
expectedResults = append(expectedResults, i)
}
}
resultIds := make([]int, 0)
for _, result := range resultsList {
resultIds = append(resultIds, result.id)
}
slices.Sort(resultIds)
require.Equal(t, expectedResults, resultIds)
})
}
func TestSchedule(t *testing.T) {
t.Run("can't Schedule before Start", func(t *testing.T) {
var panicOccurred bool
var panicError error
defer func() {
if r := recover(); r != nil {
panicOccurred = true
panicError = r.(error)
}
}()
wq := New[*FakeResult](10)
wq.Schedule(fakeJob)
require.True(t, panicOccurred, "A panic was expected")
require.True(t, errors.Is(panicError, ErrQueueNotStarted), "Unexpected error type")
})
t.Run("Job is actually enqueued", func(t *testing.T) {
wq := New[*FakeResult](10).(*workQueue[*FakeResult])
simulateStart(wq, 1)
require.Equal(t, len(wq.dynamicJobQueue), 0, "Unexpected job queue before enqueue")
wq.Schedule(fakeJob)
require.Equal(t, len(wq.dynamicJobQueue), 1, "Unexpected job queue after enqueue")
})
}
func TestShutdown(t *testing.T) {
t.Run("can't Shutdown before Start", func(t *testing.T) {
var panicOccurred bool
var panicError error
defer func() {
if r := recover(); r != nil {
panicOccurred = true
panicError = r.(error)
}
}()
wq := New[*FakeResult](10)
wq.Shutdown()
require.True(t, panicOccurred, "A panic was expected")
require.True(t, errors.Is(panicError, ErrQueueNotStarted), "Unexpected error type")
})
}