blob: 8c0108004868c2719b7fdaca11199f66b400248b [file] [log] [blame]
Jens Geyer7bb44a32014-02-07 22:24:37 +01001(*
Roger Meier3bef8c22012-10-06 06:58:00 +00002 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *)
19
20unit TestClient;
21
Jens Geyer9f7f11e2016-04-14 21:37:11 +020022{$I ../src/Thrift.Defines.inc}
23
Jens Geyer06045cf2013-03-27 20:26:25 +020024{.$DEFINE StressTest} // activate to stress-test the server with frequent connects/disconnects
Jens Geyer17c3ad92017-09-05 20:31:27 +020025{.$DEFINE PerfTest} // activate the performance test
26{$DEFINE Exceptions} // activate the exceptions test (or disable while debugging)
Jens Geyer06045cf2013-03-27 20:26:25 +020027
Jens Geyer14f5d502017-12-09 13:47:09 +010028{$if CompilerVersion >= 28}
29{$DEFINE SupportsAsync}
30{$ifend}
31
Roger Meier3bef8c22012-10-06 06:58:00 +000032interface
33
34uses
Jens Geyerfd1b3582014-12-13 23:42:58 +010035 Windows, SysUtils, Classes, Math,
Jens Geyer14f5d502017-12-09 13:47:09 +010036 {$IFDEF SupportsAsync} System.Threading, {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +000037 DateUtils,
38 Generics.Collections,
39 TestConstants,
Jens Geyer3d556242018-01-24 19:14:32 +010040 ConsoleHelper,
Roger Meier3bef8c22012-10-06 06:58:00 +000041 Thrift,
Jens Geyerf0e63312015-03-01 18:47:49 +010042 Thrift.Protocol.Compact,
Roger Meier3bef8c22012-10-06 06:58:00 +000043 Thrift.Protocol.JSON,
44 Thrift.Protocol,
45 Thrift.Transport.Pipes,
46 Thrift.Transport,
47 Thrift.Stream,
48 Thrift.Test,
Jens Geyerf7904452017-07-26 15:02:12 +020049 Thrift.Utils,
Jens Geyer3d556242018-01-24 19:14:32 +010050 Thrift.Collections;
Roger Meier3bef8c22012-10-06 06:58:00 +000051
52type
53 TThreadConsole = class
54 private
55 FThread : TThread;
56 public
57 procedure Write( const S : string);
58 procedure WriteLine( const S : string);
59 constructor Create( AThread: TThread);
60 end;
61
62 TClientThread = class( TThread )
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020063 private type
64 TTestGroup = (
65 test_Unknown,
66 test_BaseTypes,
67 test_Structs,
68 test_Containers,
69 test_Exceptions
70 // new values here
71 );
72 TTestGroups = set of TTestGroup;
73
Jens Geyer85827152018-01-12 21:20:59 +010074 TTestSize = (
75 Empty, // Edge case: the zero-length empty binary
76 Normal, // Fairly small array of usual size (256 bytes)
77 ByteArrayTest, // THRIFT-4454 Large writes/reads may cause range check errors in debug mode
78 PipeWriteLimit // THRIFT-4372 Pipe write operations across a network are limited to 65,535 bytes per write.
79 );
80
Roger Meier3bef8c22012-10-06 06:58:00 +000081 private
82 FTransport : ITransport;
83 FProtocol : IProtocol;
84 FNumIteration : Integer;
85 FConsole : TThreadConsole;
86
87 // test reporting, will be refactored out into separate class later
88 FTestGroup : string;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020089 FCurrentTest : TTestGroup;
Roger Meier3bef8c22012-10-06 06:58:00 +000090 FSuccesses : Integer;
91 FErrors : TStringList;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020092 FFailed : TTestGroups;
93 FExecuted : TTestGroups;
94 procedure StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +000095 procedure Expect( aTestResult : Boolean; const aTestInfo : string);
96 procedure ReportResults;
Jens Geyerfd1b3582014-12-13 23:42:58 +010097 function CalculateExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +000098
99 procedure ClientTest;
Jens Geyer14f5d502017-12-09 13:47:09 +0100100 {$IFDEF SupportsAsync}
101 procedure ClientAsyncTest;
102 {$ENDIF}
103
Roger Meier3bef8c22012-10-06 06:58:00 +0000104 procedure JSONProtocolReadWriteTest;
Jens Geyer85827152018-01-12 21:20:59 +0100105 function PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes;
Jens Geyer718f6ee2013-09-06 21:02:34 +0200106 {$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200107 procedure StressTest(const client : TThriftTest.Iface);
Jens Geyer718f6ee2013-09-06 21:02:34 +0200108 {$ENDIF}
Jens Geyerf7904452017-07-26 15:02:12 +0200109 {$IFDEF Win64}
110 procedure UseInterlockedExchangeAdd64;
111 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000112 protected
113 procedure Execute; override;
114 public
115 constructor Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
116 destructor Destroy; override;
117 end;
118
119 TTestClient = class
120 private
121 class var
122 FNumIteration : Integer;
123 FNumThread : Integer;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200124
125 class procedure PrintCmdLineHelp;
126 class procedure InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000127 public
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200128 class function Execute( const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000129 end;
130
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200131
Roger Meier3bef8c22012-10-06 06:58:00 +0000132implementation
133
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200134const
135 EXITCODE_SUCCESS = $00; // no errors bits set
136 //
137 EXITCODE_FAILBIT_BASETYPES = $01;
138 EXITCODE_FAILBIT_STRUCTS = $02;
139 EXITCODE_FAILBIT_CONTAINERS = $04;
140 EXITCODE_FAILBIT_EXCEPTIONS = $08;
141
142 MAP_FAILURES_TO_EXITCODE_BITS : array[TClientThread.TTestGroup] of Byte = (
143 EXITCODE_SUCCESS, // no bits here
144 EXITCODE_FAILBIT_BASETYPES,
145 EXITCODE_FAILBIT_STRUCTS,
146 EXITCODE_FAILBIT_CONTAINERS,
147 EXITCODE_FAILBIT_EXCEPTIONS
148 );
149
150
151
Roger Meier3bef8c22012-10-06 06:58:00 +0000152function BoolToString( b : Boolean) : string;
153// overrides global BoolToString()
154begin
155 if b
156 then result := 'true'
157 else result := 'false';
158end;
159
160// not available in all versions, so make sure we have this one imported
161function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent';
162
163{ TTestClient }
164
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200165class procedure TTestClient.PrintCmdLineHelp;
166const HELPTEXT = ' [options]'#10
167 + #10
168 + 'Allowed options:'#10
169 + ' -h [ --help ] produce help message'#10
170 + ' --host arg (=localhost) Host to connect'#10
171 + ' --port arg (=9090) Port number to connect'#10
172 + ' --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift),'#10
173 + ' instead of host and port'#10
174 + ' --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)'#10
175 + ' --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)'#10
176 + ' --transport arg (=sockets) Transport: buffered, framed, http, evhttp'#10
177 + ' --protocol arg (=binary) Protocol: binary, compact, json'#10
178 + ' --ssl Encrypted Transport using SSL'#10
179 + ' -n [ --testloops ] arg (=1) Number of Tests'#10
180 + ' -t [ --threads ] arg (=1) Number of Test threads'#10
181 ;
182begin
183 Writeln( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + HELPTEXT);
184end;
185
186class procedure TTestClient.InvalidArgs;
187begin
188 Console.WriteLine( 'Invalid args.');
189 Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + ' -h for more information');
190 Abort;
191end;
192
193class function TTestClient.Execute(const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000194var
195 i : Integer;
Jens Geyer14f5d502017-12-09 13:47:09 +0100196 threadExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000197 host : string;
198 port : Integer;
Roger Meier3bef8c22012-10-06 06:58:00 +0000199 sPipeName : string;
200 hAnonRead, hAnonWrite : THandle;
201 s : string;
Roger Meier3bef8c22012-10-06 06:58:00 +0000202 threads : array of TThread;
203 dtStart : TDateTime;
204 test : Integer;
205 thread : TThread;
206 trans : ITransport;
207 prot : IProtocol;
208 streamtrans : IStreamTransport;
209 http : IHTTPClient;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200210 protType : TKnownProtocol;
211 endpoint : TEndpointTransport;
212 layered : TLayeredTransports;
213 UseSSL : Boolean; // include where appropriate (TLayeredTransport?)
Jens Geyer0b20cc82013-03-07 20:47:01 +0100214const
215 // pipe timeouts to be used
216 DEBUG_TIMEOUT = 30 * 1000;
Jens Geyer3e8d9272014-09-14 20:10:40 +0200217 RELEASE_TIMEOUT = DEFAULT_THRIFT_TIMEOUT;
Jens Geyer0b20cc82013-03-07 20:47:01 +0100218 TIMEOUT = RELEASE_TIMEOUT;
Roger Meier3bef8c22012-10-06 06:58:00 +0000219begin
Roger Meier3bef8c22012-10-06 06:58:00 +0000220 protType := prot_Binary;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200221 endpoint := trns_Sockets;
222 layered := [];
223 UseSSL := FALSE;
224 host := 'localhost';
225 port := 9090;
226 sPipeName := '';
227 hAnonRead := INVALID_HANDLE_VALUE;
228 hAnonWrite := INVALID_HANDLE_VALUE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000229 try
Roger Meier3bef8c22012-10-06 06:58:00 +0000230 i := 0;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200231 while ( i < Length(args) ) do begin
232 s := args[i];
233 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000234
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200235 if (s = '-h') or (s = '--help') then begin
236 // -h [ --help ] produce help message
237 PrintCmdLineHelp;
238 result := $FF; // all tests failed
239 Exit;
240 end
Jens Geyerb360b652014-09-28 01:55:46 +0200241 else if s = '--host' then begin
242 // --host arg (=localhost) Host to connect
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200243 host := args[i];
244 Inc( i);
245 end
Jens Geyerb360b652014-09-28 01:55:46 +0200246 else if s = '--port' then begin
247 // --port arg (=9090) Port number to connect
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200248 s := args[i];
249 Inc( i);
250 port := StrToIntDef(s,0);
251 if port <= 0 then InvalidArgs;
252 end
Jens Geyerb360b652014-09-28 01:55:46 +0200253 else if s = '--domain-socket' then begin
254 // --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200255 raise Exception.Create('domain-socket not supported');
256 end
Jens Geyerb360b652014-09-28 01:55:46 +0200257 else if s = '--named-pipe' then begin
258 // --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200259 endpoint := trns_NamedPipes;
260 sPipeName := args[i];
261 Inc( i);
262 end
Jens Geyerb360b652014-09-28 01:55:46 +0200263 else if s = '--anon-pipes' then begin
264 // --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200265 endpoint := trns_AnonPipes;
266 hAnonRead := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
267 Inc( i);
268 hAnonWrite := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
269 Inc( i);
270 end
Jens Geyerb360b652014-09-28 01:55:46 +0200271 else if s = '--transport' then begin
272 // --transport arg (=sockets) Transport: buffered, framed, http, evhttp
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200273 s := args[i];
274 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000275
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200276 if s = 'buffered' then Include( layered, trns_Buffered)
277 else if s = 'framed' then Include( layered, trns_Framed)
278 else if s = 'http' then endpoint := trns_Http
Jens Geyera62efa42017-09-07 22:24:33 +0200279 else if s = 'evhttp' then endpoint := trns_EvHttp
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200280 else InvalidArgs;
281 end
Jens Geyerb360b652014-09-28 01:55:46 +0200282 else if s = '--protocol' then begin
283 // --protocol arg (=binary) Protocol: binary, compact, json
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200284 s := args[i];
285 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000286
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200287 if s = 'binary' then protType := prot_Binary
288 else if s = 'compact' then protType := prot_Compact
289 else if s = 'json' then protType := prot_JSON
290 else InvalidArgs;
291 end
Jens Geyerb360b652014-09-28 01:55:46 +0200292 else if s = '--ssl' then begin
293 // --ssl Encrypted Transport using SSL
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200294 UseSSL := TRUE;
295
296 end
297 else if (s = '-n') or (s = '--testloops') then begin
298 // -n [ --testloops ] arg (=1) Number of Tests
299 FNumIteration := StrToIntDef( args[i], 0);
300 Inc( i);
301 if FNumIteration <= 0
302 then InvalidArgs;
303
304 end
305 else if (s = '-t') or (s = '--threads') then begin
306 // -t [ --threads ] arg (=1) Number of Test threads
307 FNumThread := StrToIntDef( args[i], 0);
308 Inc( i);
309 if FNumThread <= 0
310 then InvalidArgs;
311 end
312 else begin
313 InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000314 end;
315 end;
316
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200317
Roger Meier79655fb2012-10-20 20:59:41 +0000318 // In the anonymous pipes mode the client is launched by the test server
319 // -> behave nicely and allow for attaching a debugger to this process
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200320 if (endpoint = trns_AnonPipes) and not IsDebuggerPresent
Roger Meier79655fb2012-10-20 20:59:41 +0000321 then MessageBox( 0, 'Attach Debugger and/or click OK to continue.',
322 'Thrift TestClient (Delphi)',
323 MB_OK or MB_ICONEXCLAMATION);
324
Roger Meier3bef8c22012-10-06 06:58:00 +0000325 SetLength( threads, FNumThread);
326 dtStart := Now;
327
328 for test := 0 to FNumThread - 1 do
329 begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200330 case endpoint of
331 trns_Sockets: begin
Roger Meier3bef8c22012-10-06 06:58:00 +0000332 Console.WriteLine('Using sockets ('+host+' port '+IntToStr(port)+')');
333 streamtrans := TSocketImpl.Create( host, port );
334 end;
335
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200336 trns_Http: begin
337 Console.WriteLine('Using HTTPClient');
338 http := THTTPClientImpl.Create( host);
339 trans := http;
Roger Meier3bef8c22012-10-06 06:58:00 +0000340 end;
341
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200342 trns_EvHttp: begin
343 raise Exception.Create(ENDPOINT_TRANSPORTS[endpoint]+' transport not implemented');
Roger Meier3bef8c22012-10-06 06:58:00 +0000344 end;
345
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200346 trns_NamedPipes: begin
347 Console.WriteLine('Using named pipe ('+sPipeName+')');
Jens Geyerae985dd2016-04-20 21:48:35 +0200348 streamtrans := TNamedPipeTransportClientEndImpl.Create( sPipeName, 0, nil, TIMEOUT, TIMEOUT);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200349 end;
350
351 trns_AnonPipes: begin
352 Console.WriteLine('Using anonymous pipes ('+IntToStr(Integer(hAnonRead))+' and '+IntToStr(Integer(hAnonWrite))+')');
353 streamtrans := TAnonymousPipeTransportImpl.Create( hAnonRead, hAnonWrite, FALSE);
354 end;
355
356 else
357 raise Exception.Create('Unhandled endpoint transport');
358 end;
359 trans := streamtrans;
360 ASSERT( trans <> nil);
361
362 if (trns_Buffered in layered) then begin
363 trans := TBufferedTransportImpl.Create( streamtrans, 32); // small buffer to test read()
364 Console.WriteLine('Using buffered transport');
365 end;
366
367 if (trns_Framed in layered) then begin
368 trans := TFramedTransportImpl.Create( trans );
369 Console.WriteLine('Using framed transport');
370 end;
371
372 if UseSSL then begin
373 raise Exception.Create('SSL not implemented');
Roger Meier3bef8c22012-10-06 06:58:00 +0000374 end;
375
376 // create protocol instance, default to BinaryProtocol
377 case protType of
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200378 prot_Binary : prot := TBinaryProtocolImpl.Create( trans, BINARY_STRICT_READ, BINARY_STRICT_WRITE);
379 prot_JSON : prot := TJSONProtocolImpl.Create( trans);
Jens Geyerf0e63312015-03-01 18:47:49 +0100380 prot_Compact : prot := TCompactProtocolImpl.Create( trans);
Roger Meier3bef8c22012-10-06 06:58:00 +0000381 else
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200382 raise Exception.Create('Unhandled protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000383 end;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200384 ASSERT( trans <> nil);
385 Console.WriteLine(THRIFT_PROTOCOLS[protType]+' protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000386
387 thread := TClientThread.Create( trans, prot, FNumIteration);
388 threads[test] := thread;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200389 thread.Start;
Roger Meier3bef8c22012-10-06 06:58:00 +0000390 end;
391
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200392 result := 0;
393 for test := 0 to FNumThread - 1 do begin
Jens Geyer14f5d502017-12-09 13:47:09 +0100394 threadExitCode := threads[test].WaitFor;
395 result := result or threadExitCode;
Roger Meier3bef8c22012-10-06 06:58:00 +0000396 end;
397
Jens Geyer14f5d502017-12-09 13:47:09 +0100398 for test := 0 to FNumThread - 1 do begin
399 threads[test].Free;
400 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000401
402 Console.Write('Total time: ' + IntToStr( MilliSecondsBetween(Now, dtStart)));
403
404 except
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200405 on E: EAbort do raise;
406 on E: Exception do begin
407 Console.WriteLine( E.Message + #10 + E.StackTrace);
408 raise;
Roger Meier3bef8c22012-10-06 06:58:00 +0000409 end;
410 end;
411
412 Console.WriteLine('');
413 Console.WriteLine('done!');
414end;
415
416{ TClientThread }
417
418procedure TClientThread.ClientTest;
419var
420 client : TThriftTest.Iface;
421 s : string;
422 i8 : ShortInt;
423 i32 : Integer;
424 i64 : Int64;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100425 binOut,binIn : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000426 dub : Double;
427 o : IXtruct;
428 o2 : IXtruct2;
429 i : IXtruct;
430 i2 : IXtruct2;
431 mapout : IThriftDictionary<Integer,Integer>;
432 mapin : IThriftDictionary<Integer,Integer>;
433 strmapout : IThriftDictionary<string,string>;
434 strmapin : IThriftDictionary<string,string>;
435 j : Integer;
436 first : Boolean;
437 key : Integer;
438 strkey : string;
439 listout : IThriftList<Integer>;
440 listin : IThriftList<Integer>;
441 setout : IHashSet<Integer>;
442 setin : IHashSet<Integer>;
443 ret : TNumberz;
444 uid : Int64;
445 mm : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
446 pos : IThriftDictionary<Integer, Integer>;
447 neg : IThriftDictionary<Integer, Integer>;
448 m2 : IThriftDictionary<Integer, Integer>;
449 k2 : Integer;
450 insane : IInsanity;
451 truck : IXtruct;
452 whoa : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
453 key64 : Int64;
454 val : IThriftDictionary<TNumberz, IInsanity>;
455 k2_2 : TNumberz;
456 k3 : TNumberz;
457 v2 : IInsanity;
458 userMap : IThriftDictionary<TNumberz, Int64>;
459 xtructs : IThriftList<IXtruct>;
460 x : IXtruct;
461 arg0 : ShortInt;
462 arg1 : Integer;
463 arg2 : Int64;
464 arg3 : IThriftDictionary<SmallInt, string>;
465 arg4 : TNumberz;
466 arg5 : Int64;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200467 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000468 StartTick : Cardinal;
469 k : Integer;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200470 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000471 hello, goodbye : IXtruct;
472 crazy : IInsanity;
473 looney : IInsanity;
474 first_map : IThriftDictionary<TNumberz, IInsanity>;
475 second_map : IThriftDictionary<TNumberz, IInsanity>;
Jens Geyer540e3462016-12-28 14:25:41 +0100476 pair : TPair<TNumberz, TUserId>;
Jens Geyer85827152018-01-12 21:20:59 +0100477 testsize : TTestSize;
Roger Meier3bef8c22012-10-06 06:58:00 +0000478begin
479 client := TThriftTest.TClient.Create( FProtocol);
480 FTransport.Open;
481
Jens Geyer06045cf2013-03-27 20:26:25 +0200482 {$IFDEF StressTest}
483 StressTest( client);
484 {$ENDIF StressTest}
485
Jens Geyer17c3ad92017-09-05 20:31:27 +0200486 {$IFDEF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000487 // in-depth exception test
488 // (1) do we get an exception at all?
489 // (2) do we get the right exception?
490 // (3) does the exception contain the expected data?
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200491 StartTestGroup( 'testException', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000492 // case 1: exception type declared in IDL at the function call
493 try
494 client.testException('Xception');
495 Expect( FALSE, 'testException(''Xception''): must trow an exception');
496 except
497 on e:TXception do begin
498 Expect( e.ErrorCode = 1001, 'error code');
499 Expect( e.Message_ = 'Xception', 'error message');
500 Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ );
501 end;
502 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
503 on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
504 end;
505
506 // case 2: exception type NOT declared in IDL at the function call
507 // this will close the connection
508 try
509 client.testException('TException');
510 Expect( FALSE, 'testException(''TException''): must trow an exception');
511 except
512 on e:TTransportException do begin
513 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Roger Meier3bef8c22012-10-06 06:58:00 +0000514 end;
Jens Geyer6bbbf192014-09-07 01:45:56 +0200515 on e:TApplicationException do begin
516 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Jens Geyer6bbbf192014-09-07 01:45:56 +0200517 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000518 on e:TException do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
519 on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
520 end;
521
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100522
Jens Geyer2ad6c302015-02-26 19:38:53 +0100523 if FTransport.IsOpen then FTransport.Close;
524 FTransport.Open; // re-open connection, server has already closed
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100525
Jens Geyer2ad6c302015-02-26 19:38:53 +0100526
Roger Meier3bef8c22012-10-06 06:58:00 +0000527 // case 3: no exception
528 try
529 client.testException('something');
530 Expect( TRUE, 'testException(''something''): must not trow an exception');
531 except
532 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
533 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
534 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200535 {$ENDIF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000536
537
538 // simple things
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200539 StartTestGroup( 'simple Thrift calls', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000540 client.testVoid();
541 Expect( TRUE, 'testVoid()'); // success := no exception
542
Jens Geyer39ba6b72015-09-22 00:00:49 +0200543 s := BoolToString( client.testBool(TRUE));
544 Expect( s = BoolToString(TRUE), 'testBool(TRUE) = '+s);
545 s := BoolToString( client.testBool(FALSE));
546 Expect( s = BoolToString(FALSE), 'testBool(FALSE) = '+s);
547
Roger Meier3bef8c22012-10-06 06:58:00 +0000548 s := client.testString('Test');
549 Expect( s = 'Test', 'testString(''Test'') = "'+s+'"');
550
Jens Geyercf892d42017-09-09 10:08:22 +0200551 s := client.testString(''); // empty string
552 Expect( s = '', 'testString('''') = "'+s+'"');
553
Jens Geyer06045cf2013-03-27 20:26:25 +0200554 s := client.testString(HUGE_TEST_STRING);
555 Expect( length(s) = length(HUGE_TEST_STRING),
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100556 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
Jens Geyer06045cf2013-03-27 20:26:25 +0200557 +'=> length(result) = '+IntToStr(Length(s)));
558
Roger Meier3bef8c22012-10-06 06:58:00 +0000559 i8 := client.testByte(1);
560 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
561
562 i32 := client.testI32(-1);
563 Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32));
564
565 Console.WriteLine('testI64(-34359738368)');
566 i64 := client.testI64(-34359738368);
567 Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
568
Jens Geyerd4df9172017-10-25 22:30:23 +0200569 // random binary small
Jens Geyer85827152018-01-12 21:20:59 +0100570 for testsize := Low(TTestSize) to High(TTestSize) do begin
571 binOut := PrepareBinaryData( TRUE, testsize);
572 Console.WriteLine('testBinary('+BytesToHex(binOut)+')');
573 try
574 binIn := client.testBinary(binOut);
575 Expect( Length(binOut) = Length(binIn), 'testBinary(): length '+IntToStr(Length(binOut))+' = '+IntToStr(Length(binIn)));
576 i32 := Min( Length(binOut), Length(binIn));
577 Expect( CompareMem( binOut, binIn, i32), 'testBinary('+BytesToHex(binOut)+') = '+BytesToHex(binIn));
578 except
579 on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message);
580 on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message);
581 end;
Jens Geyercf892d42017-09-09 10:08:22 +0200582 end;
583
Roger Meier3bef8c22012-10-06 06:58:00 +0000584 Console.WriteLine('testDouble(5.325098235)');
585 dub := client.testDouble(5.325098235);
586 Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
587
588 // structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200589 StartTestGroup( 'testStruct', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000590 Console.WriteLine('testStruct({''Zero'', 1, -3, -5})');
591 o := TXtructImpl.Create;
592 o.String_thing := 'Zero';
593 o.Byte_thing := 1;
594 o.I32_thing := -3;
595 o.I64_thing := -5;
596 i := client.testStruct(o);
597 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
598 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
599 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
600 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
601 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
602 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
603 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
604 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
605
606 // nested structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200607 StartTestGroup( 'testNest', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000608 Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})');
609 o2 := TXtruct2Impl.Create;
610 o2.Byte_thing := 1;
611 o2.Struct_thing := o;
612 o2.I32_thing := 5;
613 i2 := client.testNest(o2);
614 i := i2.Struct_thing;
615 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
616 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
617 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
618 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
619 Expect( i2.Byte_thing = 1, 'i2.Byte_thing = '+IntToStr(i2.Byte_thing));
620 Expect( i2.I32_thing = 5, 'i2.I32_thing = '+IntToStr(i2.I32_thing));
621 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
622 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
623 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
624 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
625 Expect( i2.__isset_Byte_thing, 'i2.__isset_Byte_thing');
626 Expect( i2.__isset_I32_thing, 'i2.__isset_I32_thing');
627
628 // map<type1,type2>: A map of strictly unique keys to values.
629 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200630 StartTestGroup( 'testMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000631 mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
632 for j := 0 to 4 do
633 begin
634 mapout.AddOrSetValue( j, j - 10);
635 end;
636 Console.Write('testMap({');
637 first := True;
638 for key in mapout.Keys do
639 begin
640 if first
641 then first := False
642 else Console.Write( ', ' );
643 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
644 end;
645 Console.WriteLine('})');
646
647 mapin := client.testMap( mapout );
648 Expect( mapin.Count = mapout.Count, 'testMap: mapin.Count = mapout.Count');
649 for j := 0 to 4 do
650 begin
651 Expect( mapout.ContainsKey(j), 'testMap: mapout.ContainsKey('+IntToStr(j)+') = '+BoolToString(mapout.ContainsKey(j)));
652 end;
653 for key in mapin.Keys do
654 begin
655 Expect( mapin[key] = mapout[key], 'testMap: '+IntToStr(key) + ' => ' + IntToStr( mapin[key]));
656 Expect( mapin[key] = key - 10, 'testMap: mapin['+IntToStr(key)+'] = '+IntToStr( mapin[key]));
657 end;
658
659
660 // map<type1,type2>: A map of strictly unique keys to values.
661 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200662 StartTestGroup( 'testStringMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000663 strmapout := TThriftDictionaryImpl<string,string>.Create;
664 for j := 0 to 4 do
665 begin
666 strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10));
667 end;
668 Console.Write('testStringMap({');
669 first := True;
670 for strkey in strmapout.Keys do
671 begin
672 if first
673 then first := False
674 else Console.Write( ', ' );
675 Console.Write( strkey + ' => ' + strmapout[strkey]);
676 end;
677 Console.WriteLine('})');
678
679 strmapin := client.testStringMap( strmapout );
680 Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count');
681 for j := 0 to 4 do
682 begin
683 Expect( strmapout.ContainsKey(IntToStr(j)),
684 'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = '
685 + BoolToString(strmapout.ContainsKey(IntToStr(j))));
686 end;
687 for strkey in strmapin.Keys do
688 begin
689 Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]);
690 Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]);
691 end;
692
693
694 // set<type>: An unordered set of unique elements.
695 // Translates to an STL set, Java HashSet, set in Python, etc.
696 // Note: PHP does not support sets, so it is treated similar to a List
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200697 StartTestGroup( 'testSet', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000698 setout := THashSetImpl<Integer>.Create;
699 for j := -2 to 2 do
700 begin
701 setout.Add( j );
702 end;
703 Console.Write('testSet({');
704 first := True;
705 for j in setout do
706 begin
707 if first
708 then first := False
709 else Console.Write(', ');
710 Console.Write(IntToStr( j));
711 end;
712 Console.WriteLine('})');
713
714 setin := client.testSet(setout);
715 Expect( setin.Count = setout.Count, 'testSet: setin.Count = setout.Count');
716 Expect( setin.Count = 5, 'testSet: setin.Count = '+IntToStr(setin.Count));
717 for j := -2 to 2 do // unordered, we can't rely on the order => test for known elements only
718 begin
719 Expect( setin.Contains(j), 'testSet: setin.Contains('+IntToStr(j)+') => '+BoolToString(setin.Contains(j)));
720 end;
721
722 // list<type>: An ordered list of elements.
723 // Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200724 StartTestGroup( 'testList', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000725 listout := TThriftListImpl<Integer>.Create;
726 listout.Add( +1);
727 listout.Add( -2);
728 listout.Add( +3);
729 listout.Add( -4);
730 listout.Add( 0);
731 Console.Write('testList({');
732 first := True;
733 for j in listout do
734 begin
735 if first
736 then first := False
737 else Console.Write(', ');
738 Console.Write(IntToStr( j));
739 end;
740 Console.WriteLine('})');
741
742 listin := client.testList(listout);
743 Expect( listin.Count = listout.Count, 'testList: listin.Count = listout.Count');
744 Expect( listin.Count = 5, 'testList: listin.Count = '+IntToStr(listin.Count));
745 Expect( listin[0] = +1, 'listin[0] = '+IntToStr( listin[0]));
746 Expect( listin[1] = -2, 'listin[1] = '+IntToStr( listin[1]));
747 Expect( listin[2] = +3, 'listin[2] = '+IntToStr( listin[2]));
748 Expect( listin[3] = -4, 'listin[3] = '+IntToStr( listin[3]));
749 Expect( listin[4] = 0, 'listin[4] = '+IntToStr( listin[4]));
750
751 // enums
752 ret := client.testEnum(TNumberz.ONE);
753 Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret)));
754
755 ret := client.testEnum(TNumberz.TWO);
756 Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret)));
757
758 ret := client.testEnum(TNumberz.THREE);
759 Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret)));
760
761 ret := client.testEnum(TNumberz.FIVE);
762 Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret)));
763
764 ret := client.testEnum(TNumberz.EIGHT);
765 Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret)));
766
767
768 // typedef
769 uid := client.testTypedef(309858235082523);
770 Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid));
771
772
773 // maps of maps
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200774 StartTestGroup( 'testMapMap(1)', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000775 mm := client.testMapMap(1);
776 Console.Write(' = {');
777 for key in mm.Keys do
778 begin
779 Console.Write( IntToStr( key) + ' => {');
780 m2 := mm[key];
781 for k2 in m2.Keys do
782 begin
783 Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', ');
784 end;
785 Console.Write('}, ');
786 end;
787 Console.WriteLine('}');
788
789 // verify result data
790 Expect( mm.Count = 2, 'mm.Count = '+IntToStr(mm.Count));
791 pos := mm[4];
792 neg := mm[-4];
793 for j := 1 to 4 do
794 begin
795 Expect( pos[j] = j, 'pos[j] = '+IntToStr(pos[j]));
796 Expect( neg[-j] = -j, 'neg[-j] = '+IntToStr(neg[-j]));
797 end;
798
799
800
801 // insanity
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200802 StartTestGroup( 'testInsanity', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000803 insane := TInsanityImpl.Create;
804 insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
805 insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000);
806 truck := TXtructImpl.Create;
807 truck.String_thing := 'Truck';
Jens Geyer540e3462016-12-28 14:25:41 +0100808 truck.Byte_thing := -8; // byte is signed
809 truck.I32_thing := 32;
810 truck.I64_thing := 64;
Roger Meier3bef8c22012-10-06 06:58:00 +0000811 insane.Xtructs := TThriftListImpl<IXtruct>.Create;
812 insane.Xtructs.Add( truck );
813 whoa := client.testInsanity( insane );
814 Console.Write(' = {');
815 for key64 in whoa.Keys do
816 begin
817 val := whoa[key64];
818 Console.Write( IntToStr( key64) + ' => {');
819 for k2_2 in val.Keys do
820 begin
821 v2 := val[k2_2];
822 Console.Write( IntToStr( Integer( k2_2)) + ' => {');
823 userMap := v2.UserMap;
824 Console.Write('{');
825 if userMap <> nil then
826 begin
827 for k3 in userMap.Keys do
828 begin
829 Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', ');
830 end;
831 end else
832 begin
833 Console.Write('null');
834 end;
835 Console.Write('}, ');
836 xtructs := v2.Xtructs;
837 Console.Write('{');
838
839 if xtructs <> nil then
840 begin
841 for x in xtructs do
842 begin
843 Console.Write('{"' + x.String_thing + '", ' +
844 IntToStr( x.Byte_thing) + ', ' +
845 IntToStr( x.I32_thing) + ', ' +
846 IntToStr( x.I32_thing) + '}, ');
847 end;
848 end else
849 begin
850 Console.Write('null');
851 end;
852 Console.Write('}');
853 Console.Write('}, ');
854 end;
855 Console.Write('}, ');
856 end;
857 Console.WriteLine('}');
858
Jens Geyer540e3462016-12-28 14:25:41 +0100859 (**
860 * So you think you've got this all worked, out eh?
861 *
862 * Creates a the returned map with these values and prints it out:
863 * { 1 => { 2 => argument,
864 * 3 => argument,
865 * },
866 * 2 => { 6 => <empty Insanity struct>, },
867 * }
868 * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
869 *)
870
Roger Meier3bef8c22012-10-06 06:58:00 +0000871 // verify result data
872 Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count));
873 //
874 first_map := whoa[1];
875 second_map := whoa[2];
876 Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count));
877 Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count));
878 //
879 looney := second_map[TNumberz.SIX];
880 Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney)));
881 Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
882 Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
883 //
884 for ret in [TNumberz.TWO, TNumberz.THREE] do begin
885 crazy := first_map[ret];
886 Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
887
888 Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap));
889 Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs));
890
Jens Geyer540e3462016-12-28 14:25:41 +0100891 Expect( crazy.UserMap.Count = insane.UserMap.Count, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count));
892 for pair in insane.UserMap do begin
893 Expect( crazy.UserMap[pair.Key] = pair.Value, 'crazy.UserMap['+IntToStr(Ord(pair.key))+'] = '+IntToStr(crazy.UserMap[pair.Key]));
894 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000895
Jens Geyer540e3462016-12-28 14:25:41 +0100896 Expect( crazy.Xtructs.Count = insane.Xtructs.Count, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count));
897 for arg0 := 0 to insane.Xtructs.Count-1 do begin
898 hello := insane.Xtructs[arg0];
899 goodbye := crazy.Xtructs[arg0];
900 Expect( goodbye.String_thing = hello.String_thing, 'goodbye.String_thing = '+goodbye.String_thing);
901 Expect( goodbye.Byte_thing = hello.Byte_thing, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing));
902 Expect( goodbye.I32_thing = hello.I32_thing, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing));
903 Expect( goodbye.I64_thing = hello.I64_thing, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing));
904 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000905 end;
906
907
908 // multi args
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200909 StartTestGroup( 'testMulti', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000910 arg0 := 1;
911 arg1 := 2;
912 arg2 := High(Int64);
913 arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
914 arg3.AddOrSetValue( 1, 'one');
915 arg4 := TNumberz.FIVE;
916 arg5 := 5000000;
917 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
918 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
919 arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
920 IntToStr( arg5) + ')');
921
922 i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5);
923 Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"');
924 Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing));
925 Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing));
926 Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing));
927 Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
928 Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
929 Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
930 Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
931
932 // multi exception
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200933 StartTestGroup( 'testMultiException(1)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000934 try
935 i := client.testMultiException( 'need more pizza', 'run out of beer');
936 Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"');
937 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
Jens Geyer6bbbf192014-09-07 01:45:56 +0200938 { this is not necessarily true, these fields are default-serialized
Jens Geyerd5436f52014-10-03 19:50:38 +0200939 Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
Roger Meier3bef8c22012-10-06 06:58:00 +0000940 Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
941 Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
Jens Geyerd5436f52014-10-03 19:50:38 +0200942 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000943 except
944 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
945 end;
946
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200947 StartTestGroup( 'testMultiException(Xception)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000948 try
949 i := client.testMultiException( 'Xception', 'second test');
950 Expect( FALSE, 'testMultiException(''Xception''): must trow an exception');
951 except
952 on x:TXception do begin
953 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
954 Expect( x.__isset_Message_, 'x.__isset_Message_ = '+BoolToString(x.__isset_Message_));
955 Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
956 Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"');
957 end;
958 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
959 end;
960
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200961 StartTestGroup( 'testMultiException(Xception2)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000962 try
963 i := client.testMultiException( 'Xception2', 'third test');
964 Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception');
965 except
966 on x:TXception2 do begin
967 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
968 Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing));
969 Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
970 Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"');
971 Expect( x.Struct_thing.__isset_String_thing, 'x.Struct_thing.__isset_String_thing = '+BoolToString(x.Struct_thing.__isset_String_thing));
Jens Geyer6bbbf192014-09-07 01:45:56 +0200972 { this is not necessarily true, these fields are default-serialized
Roger Meier3bef8c22012-10-06 06:58:00 +0000973 Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing));
974 Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing));
975 Expect( not x.Struct_thing.__isset_I64_thing, 'x.Struct_thing.__isset_I64_thing = '+BoolToString(x.Struct_thing.__isset_I64_thing));
Jens Geyerd5436f52014-10-03 19:50:38 +0200976 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000977 end;
978 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
979 end;
980
981
982 // oneway functions
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200983 StartTestGroup( 'Test Oneway(1)', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000984 client.testOneway(1);
985 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
986
987 // call time
Jens Geyer06045cf2013-03-27 20:26:25 +0200988 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000989 StartTestGroup( 'Test Calltime()');
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200990 StartTick := GetTickCount;
Roger Meier3bef8c22012-10-06 06:58:00 +0000991 for k := 0 to 1000 - 1 do
992 begin
993 client.testVoid();
994 end;
995 Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
Jens Geyer06045cf2013-03-27 20:26:25 +0200996 {$ENDIF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000997
998 // no more tests here
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200999 StartTestGroup( '', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001000end;
1001
1002
Jens Geyer14f5d502017-12-09 13:47:09 +01001003{$IFDEF SupportsAsync}
1004procedure TClientThread.ClientAsyncTest;
1005var
1006 client : TThriftTest.IAsync;
1007 s : string;
1008 i8 : ShortInt;
1009begin
1010 StartTestGroup( 'Async Tests', test_Unknown);
1011 client := TThriftTest.TClient.Create( FProtocol);
1012 FTransport.Open;
1013
1014 // oneway void functions
1015 client.testOnewayAsync(1).Wait;
1016 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
1017
1018 // normal functions
1019 s := client.testStringAsync(HUGE_TEST_STRING).Value;
1020 Expect( length(s) = length(HUGE_TEST_STRING),
1021 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
1022 +'=> length(result) = '+IntToStr(Length(s)));
1023
1024 i8 := client.testByte(1).Value;
1025 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
1026end;
1027{$ENDIF}
1028
1029
Jens Geyer718f6ee2013-09-06 21:02:34 +02001030{$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +02001031procedure TClientThread.StressTest(const client : TThriftTest.Iface);
1032begin
1033 while TRUE do begin
1034 try
1035 if not FTransport.IsOpen then FTransport.Open; // re-open connection, server has already closed
1036 try
1037 client.testString('Test');
1038 Write('.');
1039 finally
1040 if FTransport.IsOpen then FTransport.Close;
1041 end;
1042 except
1043 on e:Exception do Writeln(#10+e.message);
1044 end;
1045 end;
1046end;
Jens Geyer718f6ee2013-09-06 21:02:34 +02001047{$ENDIF}
Jens Geyer06045cf2013-03-27 20:26:25 +02001048
Jens Geyerfd1b3582014-12-13 23:42:58 +01001049
Jens Geyer85827152018-01-12 21:20:59 +01001050function TClientThread.PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes;
Jens Geyerd4df9172017-10-25 22:30:23 +02001051var i : Integer;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001052begin
Jens Geyer85827152018-01-12 21:20:59 +01001053 case aSize of
1054 Empty : SetLength( result, 0);
1055 Normal : SetLength( result, $100);
1056 ByteArrayTest : SetLength( result, SizeOf(TByteArray) + 128);
1057 PipeWriteLimit : SetLength( result, 65535 + 128);
1058 else
1059 raise EArgumentException.Create('aSize');
1060 end;
1061
Jens Geyerfd1b3582014-12-13 23:42:58 +01001062 ASSERT( Low(result) = 0);
Jens Geyer85827152018-01-12 21:20:59 +01001063 if Length(result) = 0 then Exit;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001064
1065 // linear distribution, unless random is requested
1066 if not aRandomDist then begin
1067 for i := Low(result) to High(result) do begin
Jens Geyer85827152018-01-12 21:20:59 +01001068 result[i] := i mod $100;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001069 end;
1070 Exit;
1071 end;
1072
1073 // random distribution of all 256 values
1074 FillChar( result[0], Length(result) * SizeOf(result[0]), $0);
Jens Geyerd4df9172017-10-25 22:30:23 +02001075 for i := Low(result) to High(result) do begin
1076 result[i] := Byte( Random($100));
Jens Geyerfd1b3582014-12-13 23:42:58 +01001077 end;
1078end;
1079
1080
Jens Geyerf7904452017-07-26 15:02:12 +02001081{$IFDEF Win64}
1082procedure TClientThread.UseInterlockedExchangeAdd64;
1083var a,b : Int64;
1084begin
1085 a := 1;
1086 b := 2;
1087 Thrift.Utils.InterlockedExchangeAdd64( a,b);
1088 Expect( a = 3, 'InterlockedExchangeAdd64');
1089end;
1090{$ENDIF}
1091
1092
Roger Meier3bef8c22012-10-06 06:58:00 +00001093procedure TClientThread.JSONProtocolReadWriteTest;
1094// Tests only then read/write procedures of the JSON protocol
1095// All tests succeed, if we can read what we wrote before
1096// Note that passing this test does not imply, that our JSON is really compatible to what
1097// other clients or servers expect as the real JSON. This is beyond the scope of this test.
1098var prot : IProtocol;
1099 stm : TStringStream;
Jens Geyer17c3ad92017-09-05 20:31:27 +02001100 list : TThriftList;
Jens Geyercf892d42017-09-09 10:08:22 +02001101 binary, binRead, emptyBinary : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +00001102 i,iErr : Integer;
1103const
1104 TEST_SHORT = ShortInt( $FE);
1105 TEST_SMALL = SmallInt( $FEDC);
1106 TEST_LONG = LongInt( $FEDCBA98);
1107 TEST_I64 = Int64( $FEDCBA9876543210);
1108 TEST_DOUBLE = -1.234e-56;
1109 DELTA_DOUBLE = TEST_DOUBLE * 1e-14;
1110 TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars")
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001111 // Test THRIFT-2336 and THRIFT-3404 with U+1D11E (G Clef symbol) and 'Русское Название';
1112 G_CLEF_AND_CYRILLIC_TEXT = #$1d11e' '#$0420#$0443#$0441#$0441#$043a#$043e#$0435' '#$041d#$0430#$0437#$0432#$0430#$043d#$0438#$0435;
1113 G_CLEF_AND_CYRILLIC_JSON = '"\ud834\udd1e \u0420\u0443\u0441\u0441\u043a\u043e\u0435 \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435"';
Jens Geyer21366942013-12-30 22:04:51 +01001114 // test both possible solidus encodings
1115 SOLIDUS_JSON_DATA = '"one/two\/three"';
1116 SOLIDUS_EXCPECTED = 'one/two/three';
Roger Meier3bef8c22012-10-06 06:58:00 +00001117begin
1118 stm := TStringStream.Create;
1119 try
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001120 StartTestGroup( 'JsonProtocolTest', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001121
1122 // prepare binary data
Jens Geyer85827152018-01-12 21:20:59 +01001123 binary := PrepareBinaryData( FALSE, Normal);
Jens Geyercf892d42017-09-09 10:08:22 +02001124 SetLength( emptyBinary, 0); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001125
1126 // output setup
1127 prot := TJSONProtocolImpl.Create(
1128 TStreamTransportImpl.Create(
1129 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
1130
1131 // write
Jens Geyer17c3ad92017-09-05 20:31:27 +02001132 Init( list, TType.String_, 9);
1133 prot.WriteListBegin( list);
Roger Meier3bef8c22012-10-06 06:58:00 +00001134 prot.WriteBool( TRUE);
1135 prot.WriteBool( FALSE);
1136 prot.WriteByte( TEST_SHORT);
1137 prot.WriteI16( TEST_SMALL);
1138 prot.WriteI32( TEST_LONG);
1139 prot.WriteI64( TEST_I64);
1140 prot.WriteDouble( TEST_DOUBLE);
1141 prot.WriteString( TEST_STRING);
1142 prot.WriteBinary( binary);
Jens Geyercf892d42017-09-09 10:08:22 +02001143 prot.WriteString( ''); // empty string
1144 prot.WriteBinary( emptyBinary); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001145 prot.WriteListEnd;
1146
1147 // input setup
1148 Expect( stm.Position = stm.Size, 'Stream position/length after write');
1149 stm.Position := 0;
1150 prot := TJSONProtocolImpl.Create(
1151 TStreamTransportImpl.Create(
1152 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1153
1154 // read and compare
1155 list := prot.ReadListBegin;
1156 Expect( list.ElementType = TType.String_, 'list element type');
1157 Expect( list.Count = 9, 'list element count');
1158 Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE');
1159 Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE');
1160 Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte');
1161 Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16');
1162 Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32');
1163 Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64');
1164 Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble');
1165 Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString');
1166 binRead := prot.ReadBinary;
Jens Geyercf892d42017-09-09 10:08:22 +02001167 Expect( Length(prot.ReadString) = 0, 'WriteString/ReadString (empty string)');
1168 Expect( Length(prot.ReadBinary) = 0, 'empty WriteBinary/ReadBinary (empty data block)');
Roger Meier3bef8c22012-10-06 06:58:00 +00001169 prot.ReadListEnd;
1170
1171 // test binary data
1172 Expect( Length(binary) = Length(binRead), 'Binary data length check');
1173 iErr := -1;
1174 for i := Low(binary) to High(binary) do begin
1175 if binary[i] <> binRead[i] then begin
1176 iErr := i;
1177 Break;
1178 end;
1179 end;
1180 if iErr < 0
1181 then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)')
1182 else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr));
1183
1184 Expect( stm.Position = stm.Size, 'Stream position after read');
1185
Jens Geyer7bb44a32014-02-07 22:24:37 +01001186
Jens Geyer21366942013-12-30 22:04:51 +01001187 // Solidus can be encoded in two ways. Make sure we can read both
1188 stm.Position := 0;
1189 stm.Size := 0;
1190 stm.WriteString(SOLIDUS_JSON_DATA);
1191 stm.Position := 0;
1192 prot := TJSONProtocolImpl.Create(
1193 TStreamTransportImpl.Create(
1194 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1195 Expect( prot.ReadString = SOLIDUS_EXCPECTED, 'Solidus encoding');
1196
1197
Jens Geyer7bb44a32014-02-07 22:24:37 +01001198 // Widechars should work too. Do they?
1199 // After writing, we ensure that we are able to read it back
1200 // We can't assume hex-encoding, since (nearly) any Unicode char is valid JSON
1201 stm.Position := 0;
1202 stm.Size := 0;
1203 prot := TJSONProtocolImpl.Create(
1204 TStreamTransportImpl.Create(
1205 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001206 prot.WriteString( G_CLEF_AND_CYRILLIC_TEXT);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001207 stm.Position := 0;
1208 prot := TJSONProtocolImpl.Create(
1209 TStreamTransportImpl.Create(
1210 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001211 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Writing JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001212
1213 // Widechars should work with hex-encoding too. Do they?
1214 stm.Position := 0;
1215 stm.Size := 0;
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001216 stm.WriteString( G_CLEF_AND_CYRILLIC_JSON);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001217 stm.Position := 0;
1218 prot := TJSONProtocolImpl.Create(
1219 TStreamTransportImpl.Create(
1220 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001221 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Reading JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001222
1223
Roger Meier3bef8c22012-10-06 06:58:00 +00001224 finally
1225 stm.Free;
1226 prot := nil; //-> Release
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001227 StartTestGroup( '', test_Unknown); // no more tests here
Roger Meier3bef8c22012-10-06 06:58:00 +00001228 end;
1229end;
1230
1231
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001232procedure TClientThread.StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +00001233begin
1234 FTestGroup := aGroup;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001235 FCurrentTest := aTest;
1236
1237 Include( FExecuted, aTest);
1238
Roger Meier3bef8c22012-10-06 06:58:00 +00001239 if FTestGroup <> '' then begin
1240 Console.WriteLine('');
1241 Console.WriteLine( aGroup+' tests');
1242 Console.WriteLine( StringOfChar('-',60));
1243 end;
1244end;
1245
1246
1247procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
1248begin
1249 if aTestResult then begin
1250 Inc(FSuccesses);
1251 Console.WriteLine( aTestInfo+': passed');
1252 end
1253 else begin
1254 FErrors.Add( FTestGroup+': '+aTestInfo);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001255 Include( FFailed, FCurrentTest);
Roger Meier3bef8c22012-10-06 06:58:00 +00001256 Console.WriteLine( aTestInfo+': *** FAILED ***');
1257
1258 // We have a failed test!
1259 // -> issue DebugBreak ONLY if a debugger is attached,
1260 // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise
Jens Geyer9f7f11e2016-04-14 21:37:11 +02001261 if IsDebuggerPresent
1262 then {$IFDEF CPUX64} DebugBreak {$ELSE} asm int 3 end {$ENDIF};
Roger Meier3bef8c22012-10-06 06:58:00 +00001263 end;
1264end;
1265
1266
1267procedure TClientThread.ReportResults;
1268var nTotal : Integer;
1269 sLine : string;
1270begin
1271 // prevent us from stupid DIV/0 errors
1272 nTotal := FSuccesses + FErrors.Count;
1273 if nTotal = 0 then begin
1274 Console.WriteLine('No results logged');
1275 Exit;
1276 end;
1277
1278 Console.WriteLine('');
1279 Console.WriteLine( StringOfChar('=',60));
1280 Console.WriteLine( IntToStr(nTotal)+' tests performed');
1281 Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)');
1282 Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)');
1283 Console.WriteLine( StringOfChar('=',60));
1284 if FErrors.Count > 0 then begin
1285 Console.WriteLine('FAILED TESTS:');
1286 for sLine in FErrors do Console.WriteLine('- '+sLine);
1287 Console.WriteLine( StringOfChar('=',60));
1288 InterlockedIncrement( ExitCode); // return <> 0 on errors
1289 end;
1290 Console.WriteLine('');
1291end;
1292
1293
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001294function TClientThread.CalculateExitCode : Byte;
1295var test : TTestGroup;
1296begin
1297 result := EXITCODE_SUCCESS;
1298 for test := Low(TTestGroup) to High(TTestGroup) do begin
1299 if (test in FFailed) or not (test in FExecuted)
1300 then result := result or MAP_FAILURES_TO_EXITCODE_BITS[test];
1301 end;
1302end;
1303
1304
Roger Meier3bef8c22012-10-06 06:58:00 +00001305constructor TClientThread.Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
1306begin
1307 inherited Create( True );
1308 FNumIteration := ANumIteration;
1309 FTransport := ATransport;
1310 FProtocol := AProtocol;
1311 FConsole := TThreadConsole.Create( Self );
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001312 FCurrentTest := test_Unknown;
Roger Meier3bef8c22012-10-06 06:58:00 +00001313
1314 // error list: keep correct order, allow for duplicates
1315 FErrors := TStringList.Create;
1316 FErrors.Sorted := FALSE;
1317 FErrors.Duplicates := dupAccept;
1318end;
1319
1320destructor TClientThread.Destroy;
1321begin
1322 FreeAndNil( FConsole);
1323 FreeAndNil( FErrors);
1324 inherited;
1325end;
1326
1327procedure TClientThread.Execute;
1328var
1329 i : Integer;
1330 proc : TThreadProcedure;
1331begin
1332 // perform all tests
1333 try
Jens Geyerf7904452017-07-26 15:02:12 +02001334 {$IFDEF Win64}
1335 UseInterlockedExchangeAdd64;
Jens Geyer14f5d502017-12-09 13:47:09 +01001336 {$ENDIF}
Jens Geyer7bb44a32014-02-07 22:24:37 +01001337 JSONProtocolReadWriteTest;
Jens Geyerf7904452017-07-26 15:02:12 +02001338
Roger Meier3bef8c22012-10-06 06:58:00 +00001339 for i := 0 to FNumIteration - 1 do
1340 begin
1341 ClientTest;
Jens Geyer14f5d502017-12-09 13:47:09 +01001342 {$IFDEF SupportsAsync}
1343 ClientAsyncTest;
1344 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +00001345 end;
1346 except
1347 on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"');
1348 end;
1349
1350 // report the outcome
1351 ReportResults;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001352 SetReturnValue( CalculateExitCode);
Roger Meier3bef8c22012-10-06 06:58:00 +00001353
1354 // shutdown
1355 proc := procedure
1356 begin
1357 if FTransport <> nil then
1358 begin
1359 FTransport.Close;
1360 FTransport := nil;
1361 end;
1362 end;
1363
1364 Synchronize( proc );
1365end;
1366
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001367
Roger Meier3bef8c22012-10-06 06:58:00 +00001368{ TThreadConsole }
1369
1370constructor TThreadConsole.Create(AThread: TThread);
1371begin
Jens Geyer718f6ee2013-09-06 21:02:34 +02001372 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +00001373 FThread := AThread;
1374end;
1375
1376procedure TThreadConsole.Write(const S: string);
1377var
1378 proc : TThreadProcedure;
1379begin
1380 proc := procedure
1381 begin
1382 Console.Write( S );
1383 end;
1384 TThread.Synchronize( FThread, proc);
1385end;
1386
1387procedure TThreadConsole.WriteLine(const S: string);
1388var
1389 proc : TThreadProcedure;
1390begin
1391 proc := procedure
1392 begin
1393 Console.WriteLine( S );
1394 end;
1395 TThread.Synchronize( FThread, proc);
1396end;
1397
1398initialization
1399begin
1400 TTestClient.FNumIteration := 1;
1401 TTestClient.FNumThread := 1;
1402end;
1403
1404end.