-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtask_wait_semantic.dpr
More file actions
371 lines (348 loc) · 9.07 KB
/
Copy pathtask_wait_semantic.dpr
File metadata and controls
371 lines (348 loc) · 9.07 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
program task_wait_semantic;
{$mode delphi}{$H+}
uses
mormot.core.fpcx64mm,
{$ifdef UNIX}
cthreads,
cwstring,
{$endif UNIX}
System.SysUtils,
System.Classes,
System.SyncObjs,
System.Diagnostics,
System.TimeSpan,
System.Threading;
procedure Check(ACondition: Boolean; const AMessage: string);
begin
if not ACondition then
raise Exception.Create('TASK_WAIT_FAIL: '+AMessage);
end;
procedure CheckEmptyAndNil;
var
NilTask: ITask;
Raised: Boolean;
begin
Check(TTask.WaitForAll([]),'empty WaitForAll');
Check(TTask.WaitForAny([])=-1,'empty WaitForAny');
NilTask:=Nil;
Raised:=False;
try
TTask.WaitForAll([NilTask]);
except
on EArgumentNilException do
Raised:=True;
end;
Check(Raised,'WaitForAll nil task');
Raised:=False;
try
TTask.WaitForAny([NilTask]);
except
on EArgumentNilException do
Raised:=True;
end;
Check(Raised,'WaitForAny nil task');
end;
procedure CheckCompletedAndMixed(APool: TThreadPool);
var
DoneTask, PendingTask: ITask;
Gate: TEvent;
begin
DoneTask:=TTask.Run(procedure begin end,APool);
Check(DoneTask.Wait(2000),'complete task');
Check(TTask.WaitForAny([DoneTask])=0,'already-complete WaitForAny');
Check(TTask.WaitForAll([DoneTask]),'already-complete WaitForAll');
Gate:=TEvent.Create(Nil,True,False,'');
try
PendingTask:=TTask.Run(
procedure
begin
if Gate.WaitFor(2000)<>wrSignaled then
raise Exception.Create('pending task gate timeout');
end,APool);
Check(not TTask.WaitForAll([DoneTask,PendingTask],0),
'mixed WaitForAll zero timeout');
Gate.SetEvent;
Check(TTask.WaitForAll([DoneTask,PendingTask],2000),
'mixed WaitForAll completion');
finally
Gate.SetEvent;
if PendingTask<>Nil then
PendingTask.Wait(2000);
Gate.Free;
end;
end;
procedure CheckFiniteAndInfinite(APool: TThreadPool);
var
SlowTask, FastTask: ITask;
Gate: TEvent;
Watch: TStopwatch;
Index: Integer;
begin
Gate:=TEvent.Create(Nil,True,False,'');
try
SlowTask:=TTask.Run(
procedure
begin
if Gate.WaitFor(2000)<>wrSignaled then
raise Exception.Create('slow task gate timeout');
end,APool);
FastTask:=TTask.Run(
procedure
begin
TThread.Sleep(40);
end,APool);
Watch:=TStopwatch.StartNew;
Index:=TTask.WaitForAny([SlowTask,FastTask],1000);
Watch.Stop;
Check(Index=1,'finite WaitForAny index');
Check(Watch.ElapsedMilliseconds>=10,'finite WaitForAny waited');
Gate.SetEvent;
Check(TTask.WaitForAll([SlowTask,FastTask],2000),
'finite WaitForAll completion');
finally
Gate.SetEvent;
if SlowTask<>Nil then
SlowTask.Wait(2000);
if FastTask<>Nil then
FastTask.Wait(2000);
Gate.Free;
end;
FastTask:=TTask.Run(
procedure
begin
TThread.Sleep(20);
end,APool);
Check(TTask.WaitForAny([FastTask])=0,'infinite WaitForAny');
Check(TTask.WaitForAll([FastTask]),'infinite WaitForAll');
end;
procedure CheckTimeout(APool: TThreadPool);
var
Task1, Task2: ITask;
Gate: TEvent;
Watch: TStopwatch;
begin
Gate:=TEvent.Create(Nil,True,False,'');
try
Task1:=TTask.Run(procedure begin Gate.WaitFor(2000); end,APool);
Task2:=TTask.Run(procedure begin Gate.WaitFor(2000); end,APool);
Watch:=TStopwatch.StartNew;
Check(TTask.WaitForAny([Task1,Task2],40)=-1,'WaitForAny timeout');
Watch.Stop;
Check(Watch.ElapsedMilliseconds>=10,'WaitForAny timeout not immediate');
Check(not TTask.WaitForAll([Task1,Task2],0),'WaitForAll timeout');
Gate.SetEvent;
Check(TTask.WaitForAll([Task1,Task2],2000),'tasks after timeout');
finally
Gate.SetEvent;
if Task1<>Nil then
Task1.Wait(2000);
if Task2<>Nil then
Task2.Wait(2000);
Gate.Free;
end;
end;
procedure CheckExceptionsAndCancellation(APool: TThreadPool);
var
Task: ITask;
Raised: Boolean;
begin
Task:=TTask.Run(
procedure
begin
raise Exception.Create('expected task failure');
end,APool);
WriteLn('task-wait: WaitForAll exception');
Raised:=False;
try
TTask.WaitForAll([Task]);
except
on EAggregateException do
Raised:=True;
end;
Check(Raised,'WaitForAll aggregate exception');
Task:=TTask.Run(
procedure
begin
raise Exception.Create('expected task failure');
end,APool);
WriteLn('task-wait: WaitForAny exception');
Raised:=False;
try
TTask.WaitForAny([Task]);
except
on EAggregateException do
Raised:=True;
end;
Check(Raised,'WaitForAny aggregate exception');
Task:=TTask.Create(procedure begin end,APool);
Task.Cancel;
WriteLn('task-wait: WaitForAll cancellation');
Raised:=False;
try
TTask.WaitForAll([Task]);
except
on EOperationCancelled do
Raised:=True;
end;
Check(Raised,'WaitForAll cancellation');
Task:=TTask.Create(procedure begin end,APool);
Task.Cancel;
WriteLn('task-wait: WaitForAny cancellation');
Raised:=False;
try
TTask.WaitForAny([Task]);
except
on EOperationCancelled do
Raised:=True;
end;
Check(Raised,'WaitForAny cancellation');
end;
procedure CheckInteractive;
var
Pool: TThreadPool;
Task: ITask;
OldInteractive: Boolean;
begin
Pool:=TThreadPool.Default;
OldInteractive:=Pool.Interactive;
try
Pool.Interactive:=True;
Task:=TTask.Run(
procedure
begin
TThread.Sleep(20);
end,Pool);
Check(Task.Wait(2000),'interactive task Wait');
Task:=TTask.Run(
procedure
begin
TThread.Sleep(20);
end,Pool);
Check(TTask.WaitForAll([Task],2000),'interactive WaitForAll');
Task:=TTask.Run(
procedure
begin
TThread.Sleep(20);
end,Pool);
Check(TTask.WaitForAny([Task],2000)=0,'interactive WaitForAny');
finally
Pool.Interactive:=OldInteractive;
end;
end;
procedure CheckMixedInteractiveOrder(AWaitAll, AInteractiveFirst: Boolean);
var
NonInteractivePool, InteractivePool: TThreadPool;
NonInteractiveTask, InteractiveTask: ITask;
Gate: TEvent;
WaitResult: Boolean;
WaitIndex: Integer;
Value: Integer;
begin
NonInteractivePool:=TThreadPool.Create;
InteractivePool:=TThreadPool.Create;
Gate:=TEvent.Create(Nil,True,False,'');
try
InteractivePool.Interactive:=True;
Value:=0;
WaitResult:=False;
NonInteractiveTask:=TTask.Run(
procedure
begin
if Gate.WaitFor(2000)<>wrSignaled then
raise Exception.Create('mixed-pool gate timeout');
end,NonInteractivePool);
InteractiveTask:=TTask.Run(
procedure
begin
TThread.Synchronize(Nil,
procedure
begin
Value:=42;
Gate.SetEvent;
end);
end,InteractivePool);
if AWaitAll then
begin
if AInteractiveFirst then
WaitResult:=TTask.WaitForAll(
[InteractiveTask,NonInteractiveTask],1000)
else
WaitResult:=TTask.WaitForAll(
[NonInteractiveTask,InteractiveTask],1000);
end
else
begin
if AInteractiveFirst then
WaitIndex:=TTask.WaitForAny(
[InteractiveTask,NonInteractiveTask],1000)
else
WaitIndex:=TTask.WaitForAny(
[NonInteractiveTask,InteractiveTask],1000);
WaitResult:=WaitIndex>=0;
end;
finally
Gate.SetEvent;
if (InteractiveTask<>Nil) and
(InteractiveTask.Status in
[TTaskStatus.Created,TTaskStatus.WaitingToRun,TTaskStatus.Running,
TTaskStatus.WaitingForChildren]) then
CheckSynchronize(2000);
if NonInteractiveTask<>Nil then
NonInteractiveTask.Wait(2000);
if InteractiveTask<>Nil then
InteractiveTask.Wait(2000);
Gate.Free;
NonInteractivePool.Free;
InteractivePool.Free;
end;
Check(WaitResult,'mixed-pool wait result');
Check(Value=42,'mixed-pool synchronized callback');
end;
procedure CheckMixedInteractive;
begin
CheckMixedInteractiveOrder(False,False);
CheckMixedInteractiveOrder(False,True);
CheckMixedInteractiveOrder(True,False);
CheckMixedInteractiveOrder(True,True);
end;
procedure CheckTimespan(APool: TThreadPool);
var
Task: ITask;
Raised: Boolean;
begin
Task:=TTask.Run(procedure begin end,APool);
Check(TTask.WaitForAll([Task],TTimeSpan.Create(0,0,0,0,2000)),
'WaitForAll timespan');
Check(TTask.WaitForAny([Task],TTimeSpan.Create(0,0,0,0,2000))=0,
'WaitForAny timespan');
Raised:=False;
try
TTask.WaitForAny([Task],TTimeSpan.FromMilliseconds(-1));
except
on EArgumentOutOfRangeException do
Raised:=True;
end;
Check(Raised,'negative timespan');
end;
var
Pool: TThreadPool;
begin
Pool:=TThreadPool.Default;
WriteLn('task-wait: empty/nil');
CheckEmptyAndNil;
WriteLn('task-wait: completed/mixed');
CheckCompletedAndMixed(Pool);
WriteLn('task-wait: finite/infinite');
CheckFiniteAndInfinite(Pool);
WriteLn('task-wait: timeout');
CheckTimeout(Pool);
WriteLn('task-wait: exceptions/cancellation');
CheckExceptionsAndCancellation(Pool);
WriteLn('task-wait: timespan');
CheckTimespan(Pool);
WriteLn('task-wait: interactive');
CheckInteractive;
WriteLn('task-wait: mixed interactive pools');
CheckMixedInteractive;
WriteLn('TASK_WAIT_SEMANTIC_PASS');
end.