This repository has been archived by the owner on Oct 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks_test.go
267 lines (230 loc) · 8 KB
/
tasks_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package minerva
import (
"errors"
"strconv"
"sync"
"testing"
"time"
"github.com/rs/zerolog"
)
// Define a simple Task implementation for testing
type MockTask struct {
ID string `json:"id"`
Priority int `json:"priority"`
Retries int `json:"retries"`
MaxRetries int `json:"max_retries"`
CreatedAt time.Time `json:"created_at"`
LastError string `json:"last_error"`
Success bool `json:"success"`
MockProvider *MockProvider `json:"provider"`
WillFail bool `json:"will_fail"`
Failed bool `json:"failed"`
Completed bool `json:"completed"`
HandleCalls int `json:"handle_calls"`
WillFailOnce bool `json:"will_fail_once"`
HandleServer string `json:"handle_server"`
TestCase string `json:"test_case"`
Mutex *sync.Mutex
}
func (m *MockTask) MarkAsSuccess() {
m.Mutex.Lock()
defer m.Mutex.Unlock()
m.Success = true
m.Completed = true
}
func (m *MockTask) MarkAsFailed(err error) {
m.Mutex.Lock()
defer m.Mutex.Unlock()
m.LastError = err.Error()
m.Failed = true
m.Completed = true
}
func (m *MockTask) GetPriority() int { return m.Priority }
func (m *MockTask) GetMutex() *sync.Mutex { return m.Mutex }
func (m *MockTask) GetID() string { return m.ID }
func (m *MockTask) GetMaxRetries() int { return m.MaxRetries }
func (m *MockTask) GetRetries() int { return m.Retries }
func (m *MockTask) GetCreatedAt() time.Time { return m.CreatedAt }
func (m *MockTask) GetTaskGroup() ITaskGroup { return nil }
func (m *MockTask) GetProvider() IProvider { return m.MockProvider }
func (m *MockTask) UpdateRetries(newRetries int) error { m.Retries = newRetries; return nil }
func (m *MockTask) GetTimeout() time.Duration { return 5 * time.Second }
func (m *MockTask) UpdateLastError(err string) error { m.LastError = err; return nil }
func (m *MockTask) OnComplete() {}
func (m *MockTask) OnStart() {}
// Define a simple Provider implementation for testing
type MockProvider struct {
name string
}
func (mp *MockProvider) Handle(task ITask, server string) error {
t := task.(*MockTask)
t.Mutex.Lock()
defer t.Mutex.Unlock()
t.HandleServer = server
if t.WillFail && !t.WillFailOnce || (t.WillFailOnce && t.HandleCalls == 0) {
t.HandleCalls++
//log.Printf("Task %s fails, HandleCalls: %d\n", t.ID, t.HandleCalls)
return errors.New("intentional failure")
}
t.HandleCalls++
//log.Printf("Task %s succeeds, HandleCalls: %d\n", t.ID, t.HandleCalls)
return nil
}
func (mp *MockProvider) Name() string { return mp.name }
// Setting up the test environment
func TestTaskQueueManager(t *testing.T) {
logger := zerolog.New(nil).Level(zerolog.Disabled)
mockProvider1 := &MockProvider{name: "Provider1"}
mockProvider2 := &MockProvider{name: "Provider2"}
servers := map[string][]string{
"Provider1": {"Server1", "Server2"},
"Provider2": {"Server3", "Server4", "Server5"},
}
manager := NewTaskQueueManager(&logger, &[]IProvider{
mockProvider1, mockProvider2}, servers, 2*time.Second, true)
var tasks []ITask
for i := 0; i < 10000; i++ {
provider := mockProvider1
if i%2 == 0 {
provider = mockProvider2
}
maxRetries := 1
willFail := false
willFailOnce := false
testCase := ""
switch i % 4 {
case 0: // Case 1: 1 retry, succeed after retry
maxRetries = 1
willFailOnce = true
testCase = "Case 1: 1 retry, succeed after retry"
case 1: // Case 2: 3 retries, always fail
maxRetries = 3
willFail = true
testCase = "Case 2: 3 retries, always fail"
case 2: // Case 3: 3 retries, fail once then succeed
maxRetries = 3
willFailOnce = true
testCase = "Case 3: 3 retries, fail once then succeed"
case 3: // Case 4: 1 retry, fail always
maxRetries = 1
willFail = true
testCase = "Case 4: 1 retry, fail always"
}
task := &MockTask{
ID: "task" + strconv.Itoa(i+1),
Priority: 1,
CreatedAt: time.Now(),
MaxRetries: maxRetries,
MockProvider: provider,
WillFail: willFail,
WillFailOnce: willFailOnce,
TestCase: testCase,
Mutex: &sync.Mutex{},
}
tasks = append(tasks, task)
}
// Running the manager and tasks
go manager.Start(tasks)
time.Sleep(6 * time.Second)
// Test scenarios
// Evaluate results
for i, item := range tasks {
task := item.(*MockTask)
task.Mutex.Lock()
var expectedHandleCalls int
if task.WillFail && !task.WillFailOnce {
expectedHandleCalls = task.MaxRetries + 1 // Continues to fail through all retries
} else if task.WillFailOnce {
expectedHandleCalls = 2 // Fails once, then should succeed the next time
} else {
expectedHandleCalls = 1 // No failure, succeeds on the first attempt
}
if task.HandleCalls != expectedHandleCalls {
t.Errorf("Task %d [%s] expected %d handle calls, got %d", i+1, task.TestCase, expectedHandleCalls, task.HandleCalls)
}
if (!task.WillFail && !task.WillFailOnce && !task.Success) || (task.WillFailOnce && task.HandleCalls == 2 && !task.Success) {
t.Errorf("Task %d [%s] expected to succeed but did not", i+1, task.TestCase)
}
if task.WillFail && !task.WillFailOnce && task.HandleCalls > task.MaxRetries && !task.Failed {
t.Errorf("Task %d [%s] expected to fail but did not", i+1, task.TestCase)
}
task.Mutex.Unlock()
}
// check if GetTotalTasks returns 0
if manager.GetTotalTasks() != 0 {
t.Errorf("Expected GetTotalTasks to return 0, got %d", manager.GetTotalTasks())
}
t.Logf("All tests passed (10000 tasks with 2 providers)")
tasks = []ITask{}
for i := 0; i < 10000; i++ {
provider := mockProvider1
if i%2 == 0 {
provider = mockProvider2
}
maxRetries := 1
willFail := false
willFailOnce := false
testCase := ""
switch i % 4 {
case 0: // Case 1: 1 retry, succeed after retry
maxRetries = 1
willFailOnce = true
testCase = "Case 1: 1 retry, succeed after retry"
case 1: // Case 2: 3 retries, always fail
maxRetries = 3
willFail = true
testCase = "Case 2: 3 retries, always fail"
case 2: // Case 3: 3 retries, fail once then succeed
maxRetries = 3
willFailOnce = true
testCase = "Case 3: 3 retries, fail once then succeed"
case 3: // Case 4: 1 retry, fail always
maxRetries = 1
willFail = true
testCase = "Case 4: 1 retry, fail always"
}
task := &MockTask{
ID: "task" + strconv.Itoa(i+1),
Priority: 1,
CreatedAt: time.Now(),
MaxRetries: maxRetries,
MockProvider: provider,
WillFail: willFail,
WillFailOnce: willFailOnce,
TestCase: testCase,
Mutex: &sync.Mutex{},
}
tasks = append(tasks, task)
}
manager.Restart(tasks)
time.Sleep(6 * time.Second)
// Test scenarios
// Evaluate results
for i, item := range tasks {
task := item.(*MockTask)
task.Mutex.Lock()
var expectedHandleCalls int
if task.WillFail && !task.WillFailOnce {
expectedHandleCalls = task.MaxRetries + 1 // Continues to fail through all retries
} else if task.WillFailOnce {
expectedHandleCalls = 2 // Fails once, then should succeed the next time
} else {
expectedHandleCalls = 1 // No failure, succeeds on the first attempt
}
if task.HandleCalls != expectedHandleCalls {
t.Errorf("Task %d [%s] expected %d handle calls, got %d", i+1, task.TestCase, expectedHandleCalls, task.HandleCalls)
}
if (!task.WillFail && !task.WillFailOnce && !task.Success) || (task.WillFailOnce && task.HandleCalls == 2 && !task.Success) {
t.Errorf("Task %d [%s] expected to succeed but did not", i+1, task.TestCase)
}
if task.WillFail && !task.WillFailOnce && task.HandleCalls > task.MaxRetries && !task.Failed {
t.Errorf("Task %d [%s] expected to fail but did not", i+1, task.TestCase)
}
task.Mutex.Unlock()
}
// check if GetTotalTasks returns 0
if manager.GetTotalTasks() != 0 {
t.Errorf("Expected GetTotalTasks to return 0, got %d", manager.GetTotalTasks())
}
t.Logf("All tests passed (10000 tasks with 2 providers)")
}