-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler_test.go
173 lines (137 loc) · 3.99 KB
/
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package scheduler
import (
"errors"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestScheduler_SlowTask(t *testing.T) {
const slowTaskInterval = 2 * time.Second
const slowTaskDuration = 3 * time.Second
taskExecutionCounter := int32(0)
taskUnsuccessfulCounter := int32(0)
action := func(payload Payload) error {
atomic.AddInt32(&taskExecutionCounter, 1)
time.Sleep(slowTaskDuration)
return errors.New("Task unsuccessful")
}
taskMiddleware := func(next ActionFunc) ActionFunc {
return func(payload Payload) error {
err := next(payload)
if err != nil {
atomic.AddInt32(&taskUnsuccessfulCounter, 1)
}
return err
}
}
cfg := TaskConfig{
Name: "slow_task",
Interval: slowTaskInterval,
Action: action,
Deadline: slowTaskDuration,
Middlewares: []Middleware{taskMiddleware},
}
s := New()
s.RegisterTask(cfg)
// Use sync.WaitGroup for synchronization
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
s.Start()
}()
// Wait for a few task intervals to pass
time.Sleep(7 * time.Second)
s.Stop()
wg.Wait()
executionCount := atomic.LoadInt32(&taskExecutionCounter)
unsuccessfulCount := atomic.LoadInt32(&taskUnsuccessfulCounter)
assert.GreaterOrEqual(t, executionCount, int32(2), "Expected at least 2 task executions")
assert.Equal(t, executionCount, unsuccessfulCount, "Expected unsuccessful task executions to be equal to the total task executions")
}
func TestScheduler_SuccessfulTask(t *testing.T) {
const taskInterval = 1 * time.Second
const taskDuration = 500 * time.Millisecond
taskExecutionCounter := int32(0)
taskSuccessfulCounter := int32(0)
action := func(payload Payload) error {
atomic.AddInt32(&taskExecutionCounter, 1)
time.Sleep(taskDuration)
return nil
}
taskMiddleware := func(next ActionFunc) ActionFunc {
return func(payload Payload) error {
err := next(payload)
if err == nil {
atomic.AddInt32(&taskSuccessfulCounter, 1)
}
return err
}
}
cfg := TaskConfig{
Name: "successful_task",
Interval: taskInterval,
Action: action,
Deadline: 2 * taskDuration,
Middlewares: []Middleware{taskMiddleware},
}
s := New()
s.RegisterTask(cfg)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
s.Start()
}()
time.Sleep(5 * time.Second)
s.Stop()
wg.Wait()
executionCount := atomic.LoadInt32(&taskExecutionCounter)
successfulCount := atomic.LoadInt32(&taskSuccessfulCounter)
assert.GreaterOrEqual(t, executionCount, int32(4), "Expected at least 4 task executions")
assert.Equal(t, executionCount, successfulCount, "Expected successful task executions to be equal to the total task executions")
}
func TestScheduler_DeadlineExceeded(t *testing.T) {
const taskInterval = 200 * time.Millisecond
const taskDuration = 300 * time.Millisecond
taskExecutionCounter := int32(0)
taskDeadlineExceededCounter := int32(0)
action := func(payload Payload) error {
atomic.AddInt32(&taskExecutionCounter, 1)
time.Sleep(taskDuration)
return nil
}
taskMiddleware := func(next ActionFunc) ActionFunc {
return func(payload Payload) error {
err := next(payload)
if errors.Is(err, ErrDeadlineExceeded) {
atomic.AddInt32(&taskDeadlineExceededCounter, 1)
}
return err
}
}
cfg := TaskConfig{
Name: "deadline_exceeded_task",
Interval: taskInterval,
Action: action,
Deadline: taskDuration - 100*time.Millisecond,
Middlewares: []Middleware{taskMiddleware},
}
s := New()
s.RegisterTask(cfg)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
s.Start()
}()
time.Sleep(700 * time.Millisecond)
s.Stop()
wg.Wait()
executionCount := atomic.LoadInt32(&taskExecutionCounter)
deadlineExceededCount := atomic.LoadInt32(&taskDeadlineExceededCounter)
assert.GreaterOrEqual(t, executionCount, int32(3), "Expected at least 3 task executions")
assert.Equal(t, executionCount, deadlineExceededCount, "Expected deadline exceeded task executions to be equal to the total task executions")
}