forked from linagora/twake-on-matrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker_queue_test.dart
319 lines (290 loc) · 10.4 KB
/
worker_queue_test.dart
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import 'package:fluffychat/utils/manager/download_manager/downloading_worker_queue.dart';
import 'package:fluffychat/utils/task_queue/task.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:matrix/matrix.dart';
void main() {
// Helper function to generate tasks
Task generateTask(
String taskId,
Future Function() runnable, {
void Function()? onTaskCompleted,
}) {
return Task(
id: taskId,
runnable: runnable,
onTaskCompleted: onTaskCompleted,
);
}
// Testing group for worker queue
group("worker queue test", () {
// Test case for adding tasks without cancelation or error
test("""
WHEN add 4 tasks without any cancel or error in worker queue,
SHOULD completed tasks should be order and in time
""", () async {
final completedTasks = <int>[];
final alreadyRunTasks = <int>[];
final tasks = List.generate(4, (index) {
return generateTask(
'${index + 1}',
() async => await Future.delayed(const Duration(seconds: 2), () {
alreadyRunTasks.add(index + 1);
return index + 1;
}),
onTaskCompleted: () {
Logs().i('task${index + 1} completed');
completedTasks.add(index + 1);
},
);
});
final workerQueue = DownloadWorkerQueue();
tasks.forEach(workerQueue.addTask);
await Future.delayed(const Duration(seconds: 1));
expect(workerQueue.queue.length, 3);
await Future.delayed(const Duration(seconds: 1));
expect(workerQueue.queue.length, 2);
await Future.delayed(const Duration(seconds: 7));
// Verify that all tasks completed in the expected order
expect(completedTasks, [1, 2, 3, 4]);
expect(alreadyRunTasks, [1, 2, 3, 4]);
});
test("""
WHEN add 4 tasks consecutively,
THEN there is an error task while the other task is running
SHOULD completed tasks should be order
""", () async {
final completedTasks = <int>[];
final alreadyRunTasks = <int>[];
final errorTasks = <String>[];
final tasks = [
generateTask(
'1',
() async => await Future.delayed(const Duration(seconds: 2), () {
alreadyRunTasks.add(1);
return 1;
}),
onTaskCompleted: () {
Logs().i('task1 completed');
completedTasks.add(1);
},
),
generateTask(
'2',
() async {
await Future.delayed(const Duration(seconds: 1));
errorTasks.add('task2 error');
throw Exception('task2 error');
},
onTaskCompleted: () {
Logs().i('task2 completed');
completedTasks.add(2);
},
),
generateTask(
'3',
() async => await Future.delayed(const Duration(seconds: 2), () {
alreadyRunTasks.add(3);
return 3;
}),
onTaskCompleted: () {
Logs().i('task3 completed');
completedTasks.add(3);
},
),
generateTask(
'4',
() async => await Future.delayed(const Duration(seconds: 2), () {
alreadyRunTasks.add(4);
return 4;
}),
onTaskCompleted: () {
Logs().i('task4 completed');
completedTasks.add(4);
},
),
];
final workerQueue = DownloadWorkerQueue();
try {
for (final task in tasks) {
workerQueue.addTask(task);
}
} catch (e) {
Logs().e('workerQueue.addTask(): $e');
}
await Future.delayed(const Duration(seconds: 10));
// Verify that completedTasks includes tasks 1, 3, and 4 but not the errored task 2
expect(completedTasks, containsAllInOrder([1, 3, 4]));
expect(alreadyRunTasks, containsAllInOrder([1, 3, 4]));
expect(
errorTasks,
['task2 error'],
); // Verify that task 2's error is recorded
});
test("""
WHEN a task is processing in queue with 4 tasks
THEN add a new task to the queue
SHOULD the new task should be added to the end of the queue
""", () async {
final completedTasks = <int>[];
final alreadyRunTasks = <int>[];
final tasks = List.generate(4, (index) {
return generateTask(
'${index + 1}',
() async => await Future.delayed(const Duration(seconds: 2), () {
alreadyRunTasks.add(index + 1);
return index + 1;
}),
onTaskCompleted: () {
Logs().i('task${index + 1} completed');
completedTasks.add(index + 1);
},
);
});
final workerQueue = DownloadWorkerQueue();
tasks.forEach(workerQueue.addTask);
await Future.delayed(const Duration(seconds: 1));
expect(workerQueue.queue.length, 3);
workerQueue.addTask(
Task(
id: '5',
runnable: () async =>
await Future.delayed(const Duration(seconds: 2), () {
alreadyRunTasks.add(5);
return 5;
}),
onTaskCompleted: () {
Logs().i('task5 completed');
completedTasks.add(5);
},
),
);
expect(workerQueue.queue.length, 4);
await Future.delayed(const Duration(seconds: 10));
// Verify that all tasks completed in the expected order
expect(completedTasks, containsAllInOrder([1, 2, 3, 4]));
expect(alreadyRunTasks, containsAllInOrder([1, 2, 3, 4]));
});
test("""
WHEN add 4 tasks with the same task id to the worker queue,
SHOULD the queue executes task in order and consider the duplicate task id as normal task
""", () async {
final completedTasks = <int>[];
final alreadyRunTasks = <int>[];
final tasks = List.generate(4, (index) {
return generateTask(
'1',
() async => await Future.delayed(const Duration(seconds: 2), () {
alreadyRunTasks.add(index + 1);
return index + 1;
}),
onTaskCompleted: () {
Logs().i('task${index + 1} completed');
completedTasks.add(index + 1);
},
);
});
final workerQueue = DownloadWorkerQueue();
tasks.forEach(workerQueue.addTask);
await Future.delayed(const Duration(seconds: 1));
expect(workerQueue.queue.length, 3);
expect(workerQueue.queue.first.id, '1');
await Future.delayed(const Duration(seconds: 1));
expect(workerQueue.queue.length, 2);
await Future.delayed(const Duration(seconds: 7));
// Verify that all tasks completed in the expected order
expect(completedTasks, containsAllInOrder([1, 2, 3, 4]));
expect(alreadyRunTasks, containsAllInOrder([1, 2, 3, 4]));
});
test("""
WHEN add 4 tasks with the same task id to the worker queue,
THEN in task complete, there is async task
SHOULD async task do not block any task in the queue
""", () async {
final completedTasks = <int>[];
final alreadyRunTasks = <int>[];
final tasks = List.generate(4, (index) {
return generateTask(
'1',
() async => await Future.delayed(const Duration(seconds: 2), () {
alreadyRunTasks.add(index + 1);
return index + 1;
}),
onTaskCompleted: () async {
Logs().i('task${index + 1} completed');
await Future.delayed(const Duration(seconds: 1));
completedTasks.add(index + 1);
},
);
});
final workerQueue = DownloadWorkerQueue();
tasks.forEach(workerQueue.addTask);
await Future.delayed(const Duration(seconds: 9));
// Verify that all tasks completed in the expected order
expect(alreadyRunTasks, containsAllInOrder([1, 2, 3, 4]));
await Future.delayed(const Duration(seconds: 4));
expect(completedTasks, containsAllInOrder([1, 2, 3, 4]));
});
test("""
WHEN add 4 tasks at once to the queue using Future.wait
SHOULD the tasks are executed in order and the queue is empty after all tasks are completed
""", () async {
final completedTasks = <int>[];
final alreadyRunTasks = <int>[];
final tasks = List.generate(4, (index) {
return generateTask(
'1',
() async => await Future.delayed(const Duration(seconds: 2), () {
alreadyRunTasks.add(index + 1);
return index + 1;
}),
onTaskCompleted: () async {
Logs().i('task${index + 1} completed');
completedTasks.add(index + 1);
},
);
});
final workerQueue = DownloadWorkerQueue();
Future.wait(tasks.map((task) => workerQueue.addTask(task)));
await Future.delayed(const Duration(seconds: 1));
expect(workerQueue.queue.length, 3);
expect(workerQueue.queue.first.id, '1');
await Future.delayed(const Duration(seconds: 9));
// Verify that all tasks completed in the expected order
expect(alreadyRunTasks, containsAllInOrder([1, 2, 3, 4]));
expect(completedTasks, containsAllInOrder([1, 2, 3, 4]));
});
test("""
WHEN add 4 tasks to the worker queue
THEN task 1 is processing
THEN remove the last task from the queue
SHOULD the queue executes task except the removed task
""", () async {
final completedTasks = <int>[];
final alreadyRunTasks = <int>[];
final tasks = List.generate(4, (index) {
return generateTask(
'1',
() async => await Future.delayed(const Duration(seconds: 2), () {
alreadyRunTasks.add(index + 1);
return index + 1;
}),
onTaskCompleted: () async {
Logs().i('task${index + 1} completed');
completedTasks.add(index + 1);
},
);
});
final workerQueue = DownloadWorkerQueue();
Future.wait(tasks.map((task) => workerQueue.addTask(task)));
await Future.delayed(const Duration(seconds: 1));
workerQueue.queue.removeLast();
// Verify that the worker queue has 2 remaining tasks and the first task is being processed
expect(workerQueue.queue.length, 2);
expect(workerQueue.queue.first.id, '1');
await Future.delayed(const Duration(seconds: 7));
// Verify that all tasks completed in the expected order
expect(alreadyRunTasks, containsAllInOrder([1, 2, 3]));
expect(completedTasks, containsAllInOrder([1, 2, 3]));
});
});
}