forked from VSoftTechnologies/DUnitX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDUnitX.Timeout.pas
175 lines (146 loc) · 4.9 KB
/
DUnitX.Timeout.pas
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
{***************************************************************************}
{ }
{ DUnitX }
{ }
{ Copyright (C) 2015 Vincent Parrett & Contributors }
{ }
{ http://www.finalbuilder.com }
{ }
{ }
{***************************************************************************}
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{***************************************************************************}
unit DUnitX.Timeout;
interface
{$I DUnitX.inc}
uses
{$IFDEF USE_NS}
System.Classes;
{$ELSE}
Classes;
{$ENDIF}
//TODO : This is currently only supported on Windows, need to investigate osx etc.
type
ITimeout = interface(IUnknown)
['{0A380F7B-9CEE-4FD7-9D86-60CE05B97C1A}']
procedure Stop;
end;
function InitialiseTimeout(const ATime: cardinal): ITimeout;
implementation
uses
{$IFDEF USE_NS}
WinAPI.Windows,
System.Diagnostics,
{$ELSE}
Windows,
Diagnostics,
{$ENDIF}
DUnitX.ResStrs,
DUnitX.TestFramework,
DUnitX.Exceptions;
// The following TimeOut code is based on the code found at
// https://code.google.com/p/delphitimeouts/
// DelphiTimeouts version 1.1
// Copyright (c) 2007-2008 Szymon Jachim
type
TTimeoutThread = class(TThread)
private
procedure TimeoutThread;
public
ThreadHandle: Cardinal;
Timeout: Cardinal;
procedure Execute; override;
end;
TTimeout = class(TInterfacedObject, ITimeout)
private
FTimeoutThread: TTimeoutThread;
public
constructor Create(const ATimeout: Cardinal; AThreadHandle: THandle);
destructor Destroy; override;
procedure Stop;
end;
function InitialiseTimeout(const ATime: cardinal): ITimeout;
var
ThisThreadHandle: THandle;
begin
DuplicateHandle(GetCurrentProcess, GetCurrentThread, GetCurrentProcess, @ThisThreadHandle, 0, True, DUPLICATE_SAME_ACCESS);
Result := TTimeout.Create(ATime, ThisThreadHandle);
end;
procedure RaiseTimeOutException;
begin
raise ETimedOut.Create(SOperationTimedOut);
end;
procedure TTimeoutThread.TimeoutThread;
var
Ctx: _CONTEXT;
begin
SuspendThread(ThreadHandle);
Ctx.ContextFlags := CONTEXT_FULL;
GetThreadContext(ThreadHandle, Ctx);
{$IFDEF CPUX64}
Ctx.Rip := Cardinal(@RaiseTimeOutException);
{$ELSE}
Ctx.Eip := Cardinal(@RaiseTimeOutException);
{$ENDIF}
SetThreadContext(ThreadHandle, Ctx);
ResumeThread(ThreadHandle);
end;
{ TTimeout }
procedure TTimeout.Stop;
begin
FTimeoutThread.Terminate;
end;
constructor TTimeout.Create(const ATimeout: Cardinal; AThreadHandle: THandle);
begin
FTimeoutThread := TTimeoutThread.Create(true);
FTimeoutThread.FreeOnTerminate := false;
FTimeoutThread.ThreadHandle := AThreadHandle;
FTimeoutThread.Timeout := ATimeout;
FTimeoutThread.Start;
end;
destructor TTimeout.Destroy;
begin
//Unwinding and we need to stop the thread, as it may still raise an exception
Stop;
FTimeoutThread.WaitFor;
FTimeoutThread.Free;
inherited;
end;
{ TTimeoutThread }
procedure TTimeoutThread.Execute;
var
elapsedTime : Int64;
stopwatch : TStopWatch;
begin
inherited;
stopwatch := TStopWatch.Create;
stopwatch.Reset;
stopwatch.Start;
elapsedTime := 0;
if Terminated then
exit;
repeat
//Give some time back to the system to process the test.
Sleep(20);
if Terminated then
Break;
elapsedTime := stopwatch.ElapsedMilliseconds;
until (elapsedTime >= Timeout);
//If we haven't been terminated then we have timed out.
if not Terminated then
TimeoutThread;
end;
end.