blob: 143611d05d421bd6bf29925a6f21417584ebe107 [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,
40 Thrift,
Jens Geyerf0e63312015-03-01 18:47:49 +010041 Thrift.Protocol.Compact,
Roger Meier3bef8c22012-10-06 06:58:00 +000042 Thrift.Protocol.JSON,
43 Thrift.Protocol,
44 Thrift.Transport.Pipes,
45 Thrift.Transport,
46 Thrift.Stream,
47 Thrift.Test,
Jens Geyerf7904452017-07-26 15:02:12 +020048 Thrift.Utils,
Roger Meier3bef8c22012-10-06 06:58:00 +000049 Thrift.Collections,
50 Thrift.Console;
51
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
Roger Meier3bef8c22012-10-06 06:58:00 +000074 private
75 FTransport : ITransport;
76 FProtocol : IProtocol;
77 FNumIteration : Integer;
78 FConsole : TThreadConsole;
79
80 // test reporting, will be refactored out into separate class later
81 FTestGroup : string;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020082 FCurrentTest : TTestGroup;
Roger Meier3bef8c22012-10-06 06:58:00 +000083 FSuccesses : Integer;
84 FErrors : TStringList;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020085 FFailed : TTestGroups;
86 FExecuted : TTestGroups;
87 procedure StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +000088 procedure Expect( aTestResult : Boolean; const aTestInfo : string);
89 procedure ReportResults;
Jens Geyerfd1b3582014-12-13 23:42:58 +010090 function CalculateExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +000091
92 procedure ClientTest;
Jens Geyer14f5d502017-12-09 13:47:09 +010093 {$IFDEF SupportsAsync}
94 procedure ClientAsyncTest;
95 {$ENDIF}
96
Roger Meier3bef8c22012-10-06 06:58:00 +000097 procedure JSONProtocolReadWriteTest;
Jens Geyerd4df9172017-10-25 22:30:23 +020098 function PrepareBinaryData( aRandomDist, aHuge : Boolean) : TBytes;
Jens Geyer718f6ee2013-09-06 21:02:34 +020099 {$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200100 procedure StressTest(const client : TThriftTest.Iface);
Jens Geyer718f6ee2013-09-06 21:02:34 +0200101 {$ENDIF}
Jens Geyerf7904452017-07-26 15:02:12 +0200102 {$IFDEF Win64}
103 procedure UseInterlockedExchangeAdd64;
104 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000105 protected
106 procedure Execute; override;
107 public
108 constructor Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
109 destructor Destroy; override;
110 end;
111
112 TTestClient = class
113 private
114 class var
115 FNumIteration : Integer;
116 FNumThread : Integer;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200117
118 class procedure PrintCmdLineHelp;
119 class procedure InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000120 public
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200121 class function Execute( const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000122 end;
123
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200124
Roger Meier3bef8c22012-10-06 06:58:00 +0000125implementation
126
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200127const
128 EXITCODE_SUCCESS = $00; // no errors bits set
129 //
130 EXITCODE_FAILBIT_BASETYPES = $01;
131 EXITCODE_FAILBIT_STRUCTS = $02;
132 EXITCODE_FAILBIT_CONTAINERS = $04;
133 EXITCODE_FAILBIT_EXCEPTIONS = $08;
134
135 MAP_FAILURES_TO_EXITCODE_BITS : array[TClientThread.TTestGroup] of Byte = (
136 EXITCODE_SUCCESS, // no bits here
137 EXITCODE_FAILBIT_BASETYPES,
138 EXITCODE_FAILBIT_STRUCTS,
139 EXITCODE_FAILBIT_CONTAINERS,
140 EXITCODE_FAILBIT_EXCEPTIONS
141 );
142
143
144
Roger Meier3bef8c22012-10-06 06:58:00 +0000145function BoolToString( b : Boolean) : string;
146// overrides global BoolToString()
147begin
148 if b
149 then result := 'true'
150 else result := 'false';
151end;
152
153// not available in all versions, so make sure we have this one imported
154function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent';
155
156{ TTestClient }
157
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200158class procedure TTestClient.PrintCmdLineHelp;
159const HELPTEXT = ' [options]'#10
160 + #10
161 + 'Allowed options:'#10
162 + ' -h [ --help ] produce help message'#10
163 + ' --host arg (=localhost) Host to connect'#10
164 + ' --port arg (=9090) Port number to connect'#10
165 + ' --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift),'#10
166 + ' instead of host and port'#10
167 + ' --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)'#10
168 + ' --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)'#10
169 + ' --transport arg (=sockets) Transport: buffered, framed, http, evhttp'#10
170 + ' --protocol arg (=binary) Protocol: binary, compact, json'#10
171 + ' --ssl Encrypted Transport using SSL'#10
172 + ' -n [ --testloops ] arg (=1) Number of Tests'#10
173 + ' -t [ --threads ] arg (=1) Number of Test threads'#10
174 ;
175begin
176 Writeln( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + HELPTEXT);
177end;
178
179class procedure TTestClient.InvalidArgs;
180begin
181 Console.WriteLine( 'Invalid args.');
182 Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + ' -h for more information');
183 Abort;
184end;
185
186class function TTestClient.Execute(const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000187var
188 i : Integer;
Jens Geyer14f5d502017-12-09 13:47:09 +0100189 threadExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000190 host : string;
191 port : Integer;
Roger Meier3bef8c22012-10-06 06:58:00 +0000192 sPipeName : string;
193 hAnonRead, hAnonWrite : THandle;
194 s : string;
Roger Meier3bef8c22012-10-06 06:58:00 +0000195 threads : array of TThread;
196 dtStart : TDateTime;
197 test : Integer;
198 thread : TThread;
199 trans : ITransport;
200 prot : IProtocol;
201 streamtrans : IStreamTransport;
202 http : IHTTPClient;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200203 protType : TKnownProtocol;
204 endpoint : TEndpointTransport;
205 layered : TLayeredTransports;
206 UseSSL : Boolean; // include where appropriate (TLayeredTransport?)
Jens Geyer0b20cc82013-03-07 20:47:01 +0100207const
208 // pipe timeouts to be used
209 DEBUG_TIMEOUT = 30 * 1000;
Jens Geyer3e8d9272014-09-14 20:10:40 +0200210 RELEASE_TIMEOUT = DEFAULT_THRIFT_TIMEOUT;
Jens Geyer0b20cc82013-03-07 20:47:01 +0100211 TIMEOUT = RELEASE_TIMEOUT;
Roger Meier3bef8c22012-10-06 06:58:00 +0000212begin
Roger Meier3bef8c22012-10-06 06:58:00 +0000213 protType := prot_Binary;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200214 endpoint := trns_Sockets;
215 layered := [];
216 UseSSL := FALSE;
217 host := 'localhost';
218 port := 9090;
219 sPipeName := '';
220 hAnonRead := INVALID_HANDLE_VALUE;
221 hAnonWrite := INVALID_HANDLE_VALUE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000222 try
Roger Meier3bef8c22012-10-06 06:58:00 +0000223 i := 0;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200224 while ( i < Length(args) ) do begin
225 s := args[i];
226 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000227
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200228 if (s = '-h') or (s = '--help') then begin
229 // -h [ --help ] produce help message
230 PrintCmdLineHelp;
231 result := $FF; // all tests failed
232 Exit;
233 end
Jens Geyerb360b652014-09-28 01:55:46 +0200234 else if s = '--host' then begin
235 // --host arg (=localhost) Host to connect
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200236 host := args[i];
237 Inc( i);
238 end
Jens Geyerb360b652014-09-28 01:55:46 +0200239 else if s = '--port' then begin
240 // --port arg (=9090) Port number to connect
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200241 s := args[i];
242 Inc( i);
243 port := StrToIntDef(s,0);
244 if port <= 0 then InvalidArgs;
245 end
Jens Geyerb360b652014-09-28 01:55:46 +0200246 else if s = '--domain-socket' then begin
247 // --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200248 raise Exception.Create('domain-socket not supported');
249 end
Jens Geyerb360b652014-09-28 01:55:46 +0200250 else if s = '--named-pipe' then begin
251 // --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200252 endpoint := trns_NamedPipes;
253 sPipeName := args[i];
254 Inc( i);
255 end
Jens Geyerb360b652014-09-28 01:55:46 +0200256 else if s = '--anon-pipes' then begin
257 // --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200258 endpoint := trns_AnonPipes;
259 hAnonRead := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
260 Inc( i);
261 hAnonWrite := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
262 Inc( i);
263 end
Jens Geyerb360b652014-09-28 01:55:46 +0200264 else if s = '--transport' then begin
265 // --transport arg (=sockets) Transport: buffered, framed, http, evhttp
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200266 s := args[i];
267 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000268
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200269 if s = 'buffered' then Include( layered, trns_Buffered)
270 else if s = 'framed' then Include( layered, trns_Framed)
271 else if s = 'http' then endpoint := trns_Http
Jens Geyera62efa42017-09-07 22:24:33 +0200272 else if s = 'evhttp' then endpoint := trns_EvHttp
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200273 else InvalidArgs;
274 end
Jens Geyerb360b652014-09-28 01:55:46 +0200275 else if s = '--protocol' then begin
276 // --protocol arg (=binary) Protocol: binary, compact, json
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200277 s := args[i];
278 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000279
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200280 if s = 'binary' then protType := prot_Binary
281 else if s = 'compact' then protType := prot_Compact
282 else if s = 'json' then protType := prot_JSON
283 else InvalidArgs;
284 end
Jens Geyerb360b652014-09-28 01:55:46 +0200285 else if s = '--ssl' then begin
286 // --ssl Encrypted Transport using SSL
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200287 UseSSL := TRUE;
288
289 end
290 else if (s = '-n') or (s = '--testloops') then begin
291 // -n [ --testloops ] arg (=1) Number of Tests
292 FNumIteration := StrToIntDef( args[i], 0);
293 Inc( i);
294 if FNumIteration <= 0
295 then InvalidArgs;
296
297 end
298 else if (s = '-t') or (s = '--threads') then begin
299 // -t [ --threads ] arg (=1) Number of Test threads
300 FNumThread := StrToIntDef( args[i], 0);
301 Inc( i);
302 if FNumThread <= 0
303 then InvalidArgs;
304 end
305 else begin
306 InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000307 end;
308 end;
309
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200310
Roger Meier79655fb2012-10-20 20:59:41 +0000311 // In the anonymous pipes mode the client is launched by the test server
312 // -> behave nicely and allow for attaching a debugger to this process
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200313 if (endpoint = trns_AnonPipes) and not IsDebuggerPresent
Roger Meier79655fb2012-10-20 20:59:41 +0000314 then MessageBox( 0, 'Attach Debugger and/or click OK to continue.',
315 'Thrift TestClient (Delphi)',
316 MB_OK or MB_ICONEXCLAMATION);
317
Roger Meier3bef8c22012-10-06 06:58:00 +0000318 SetLength( threads, FNumThread);
319 dtStart := Now;
320
321 for test := 0 to FNumThread - 1 do
322 begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200323 case endpoint of
324 trns_Sockets: begin
Roger Meier3bef8c22012-10-06 06:58:00 +0000325 Console.WriteLine('Using sockets ('+host+' port '+IntToStr(port)+')');
326 streamtrans := TSocketImpl.Create( host, port );
327 end;
328
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200329 trns_Http: begin
330 Console.WriteLine('Using HTTPClient');
331 http := THTTPClientImpl.Create( host);
332 trans := http;
Roger Meier3bef8c22012-10-06 06:58:00 +0000333 end;
334
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200335 trns_EvHttp: begin
336 raise Exception.Create(ENDPOINT_TRANSPORTS[endpoint]+' transport not implemented');
Roger Meier3bef8c22012-10-06 06:58:00 +0000337 end;
338
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200339 trns_NamedPipes: begin
340 Console.WriteLine('Using named pipe ('+sPipeName+')');
Jens Geyerae985dd2016-04-20 21:48:35 +0200341 streamtrans := TNamedPipeTransportClientEndImpl.Create( sPipeName, 0, nil, TIMEOUT, TIMEOUT);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200342 end;
343
344 trns_AnonPipes: begin
345 Console.WriteLine('Using anonymous pipes ('+IntToStr(Integer(hAnonRead))+' and '+IntToStr(Integer(hAnonWrite))+')');
346 streamtrans := TAnonymousPipeTransportImpl.Create( hAnonRead, hAnonWrite, FALSE);
347 end;
348
349 else
350 raise Exception.Create('Unhandled endpoint transport');
351 end;
352 trans := streamtrans;
353 ASSERT( trans <> nil);
354
355 if (trns_Buffered in layered) then begin
356 trans := TBufferedTransportImpl.Create( streamtrans, 32); // small buffer to test read()
357 Console.WriteLine('Using buffered transport');
358 end;
359
360 if (trns_Framed in layered) then begin
361 trans := TFramedTransportImpl.Create( trans );
362 Console.WriteLine('Using framed transport');
363 end;
364
365 if UseSSL then begin
366 raise Exception.Create('SSL not implemented');
Roger Meier3bef8c22012-10-06 06:58:00 +0000367 end;
368
369 // create protocol instance, default to BinaryProtocol
370 case protType of
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200371 prot_Binary : prot := TBinaryProtocolImpl.Create( trans, BINARY_STRICT_READ, BINARY_STRICT_WRITE);
372 prot_JSON : prot := TJSONProtocolImpl.Create( trans);
Jens Geyerf0e63312015-03-01 18:47:49 +0100373 prot_Compact : prot := TCompactProtocolImpl.Create( trans);
Roger Meier3bef8c22012-10-06 06:58:00 +0000374 else
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200375 raise Exception.Create('Unhandled protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000376 end;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200377 ASSERT( trans <> nil);
378 Console.WriteLine(THRIFT_PROTOCOLS[protType]+' protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000379
380 thread := TClientThread.Create( trans, prot, FNumIteration);
381 threads[test] := thread;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200382 thread.Start;
Roger Meier3bef8c22012-10-06 06:58:00 +0000383 end;
384
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200385 result := 0;
386 for test := 0 to FNumThread - 1 do begin
Jens Geyer14f5d502017-12-09 13:47:09 +0100387 threadExitCode := threads[test].WaitFor;
388 result := result or threadExitCode;
Roger Meier3bef8c22012-10-06 06:58:00 +0000389 end;
390
Jens Geyer14f5d502017-12-09 13:47:09 +0100391 for test := 0 to FNumThread - 1 do begin
392 threads[test].Free;
393 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000394
395 Console.Write('Total time: ' + IntToStr( MilliSecondsBetween(Now, dtStart)));
396
397 except
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200398 on E: EAbort do raise;
399 on E: Exception do begin
400 Console.WriteLine( E.Message + #10 + E.StackTrace);
401 raise;
Roger Meier3bef8c22012-10-06 06:58:00 +0000402 end;
403 end;
404
405 Console.WriteLine('');
406 Console.WriteLine('done!');
407end;
408
409{ TClientThread }
410
411procedure TClientThread.ClientTest;
412var
413 client : TThriftTest.Iface;
414 s : string;
415 i8 : ShortInt;
416 i32 : Integer;
417 i64 : Int64;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100418 binOut,binIn : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000419 dub : Double;
420 o : IXtruct;
421 o2 : IXtruct2;
422 i : IXtruct;
423 i2 : IXtruct2;
424 mapout : IThriftDictionary<Integer,Integer>;
425 mapin : IThriftDictionary<Integer,Integer>;
426 strmapout : IThriftDictionary<string,string>;
427 strmapin : IThriftDictionary<string,string>;
428 j : Integer;
429 first : Boolean;
430 key : Integer;
431 strkey : string;
432 listout : IThriftList<Integer>;
433 listin : IThriftList<Integer>;
434 setout : IHashSet<Integer>;
435 setin : IHashSet<Integer>;
436 ret : TNumberz;
437 uid : Int64;
438 mm : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
439 pos : IThriftDictionary<Integer, Integer>;
440 neg : IThriftDictionary<Integer, Integer>;
441 m2 : IThriftDictionary<Integer, Integer>;
442 k2 : Integer;
443 insane : IInsanity;
444 truck : IXtruct;
445 whoa : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
446 key64 : Int64;
447 val : IThriftDictionary<TNumberz, IInsanity>;
448 k2_2 : TNumberz;
449 k3 : TNumberz;
450 v2 : IInsanity;
451 userMap : IThriftDictionary<TNumberz, Int64>;
452 xtructs : IThriftList<IXtruct>;
453 x : IXtruct;
454 arg0 : ShortInt;
455 arg1 : Integer;
456 arg2 : Int64;
457 arg3 : IThriftDictionary<SmallInt, string>;
458 arg4 : TNumberz;
459 arg5 : Int64;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200460 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000461 StartTick : Cardinal;
462 k : Integer;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200463 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000464 hello, goodbye : IXtruct;
465 crazy : IInsanity;
466 looney : IInsanity;
467 first_map : IThriftDictionary<TNumberz, IInsanity>;
468 second_map : IThriftDictionary<TNumberz, IInsanity>;
Jens Geyer540e3462016-12-28 14:25:41 +0100469 pair : TPair<TNumberz, TUserId>;
Roger Meier3bef8c22012-10-06 06:58:00 +0000470begin
471 client := TThriftTest.TClient.Create( FProtocol);
472 FTransport.Open;
473
Jens Geyer06045cf2013-03-27 20:26:25 +0200474 {$IFDEF StressTest}
475 StressTest( client);
476 {$ENDIF StressTest}
477
Jens Geyer17c3ad92017-09-05 20:31:27 +0200478 {$IFDEF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000479 // in-depth exception test
480 // (1) do we get an exception at all?
481 // (2) do we get the right exception?
482 // (3) does the exception contain the expected data?
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200483 StartTestGroup( 'testException', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000484 // case 1: exception type declared in IDL at the function call
485 try
486 client.testException('Xception');
487 Expect( FALSE, 'testException(''Xception''): must trow an exception');
488 except
489 on e:TXception do begin
490 Expect( e.ErrorCode = 1001, 'error code');
491 Expect( e.Message_ = 'Xception', 'error message');
492 Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ );
493 end;
494 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
495 on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
496 end;
497
498 // case 2: exception type NOT declared in IDL at the function call
499 // this will close the connection
500 try
501 client.testException('TException');
502 Expect( FALSE, 'testException(''TException''): must trow an exception');
503 except
504 on e:TTransportException do begin
505 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Roger Meier3bef8c22012-10-06 06:58:00 +0000506 end;
Jens Geyer6bbbf192014-09-07 01:45:56 +0200507 on e:TApplicationException do begin
508 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Jens Geyer6bbbf192014-09-07 01:45:56 +0200509 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000510 on e:TException do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
511 on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
512 end;
513
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100514
Jens Geyer2ad6c302015-02-26 19:38:53 +0100515 if FTransport.IsOpen then FTransport.Close;
516 FTransport.Open; // re-open connection, server has already closed
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100517
Jens Geyer2ad6c302015-02-26 19:38:53 +0100518
Roger Meier3bef8c22012-10-06 06:58:00 +0000519 // case 3: no exception
520 try
521 client.testException('something');
522 Expect( TRUE, 'testException(''something''): must not trow an exception');
523 except
524 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
525 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
526 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200527 {$ENDIF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000528
529
530 // simple things
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200531 StartTestGroup( 'simple Thrift calls', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000532 client.testVoid();
533 Expect( TRUE, 'testVoid()'); // success := no exception
534
Jens Geyer39ba6b72015-09-22 00:00:49 +0200535 s := BoolToString( client.testBool(TRUE));
536 Expect( s = BoolToString(TRUE), 'testBool(TRUE) = '+s);
537 s := BoolToString( client.testBool(FALSE));
538 Expect( s = BoolToString(FALSE), 'testBool(FALSE) = '+s);
539
Roger Meier3bef8c22012-10-06 06:58:00 +0000540 s := client.testString('Test');
541 Expect( s = 'Test', 'testString(''Test'') = "'+s+'"');
542
Jens Geyercf892d42017-09-09 10:08:22 +0200543 s := client.testString(''); // empty string
544 Expect( s = '', 'testString('''') = "'+s+'"');
545
Jens Geyer06045cf2013-03-27 20:26:25 +0200546 s := client.testString(HUGE_TEST_STRING);
547 Expect( length(s) = length(HUGE_TEST_STRING),
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100548 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
Jens Geyer06045cf2013-03-27 20:26:25 +0200549 +'=> length(result) = '+IntToStr(Length(s)));
550
Roger Meier3bef8c22012-10-06 06:58:00 +0000551 i8 := client.testByte(1);
552 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
553
554 i32 := client.testI32(-1);
555 Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32));
556
557 Console.WriteLine('testI64(-34359738368)');
558 i64 := client.testI64(-34359738368);
559 Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
560
Jens Geyerd4df9172017-10-25 22:30:23 +0200561 // random binary small
562 binOut := PrepareBinaryData( TRUE, FALSE);
563 Console.WriteLine('testBinary('+BytesToHex(binOut)+')');
564 try
565 binIn := client.testBinary(binOut);
566 Expect( Length(binOut) = Length(binIn), 'testBinary(): length '+IntToStr(Length(binOut))+' = '+IntToStr(Length(binIn)));
567 i32 := Min( Length(binOut), Length(binIn));
568 Expect( CompareMem( binOut, binIn, i32), 'testBinary('+BytesToHex(binOut)+') = '+BytesToHex(binIn));
569 except
570 on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message);
571 on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message);
572 end;
573
574 // random binary huge
575 binOut := PrepareBinaryData( TRUE, TRUE);
Jens Geyerfd1b3582014-12-13 23:42:58 +0100576 Console.WriteLine('testBinary('+BytesToHex(binOut)+')');
577 try
578 binIn := client.testBinary(binOut);
579 Expect( Length(binOut) = Length(binIn), 'testBinary(): length '+IntToStr(Length(binOut))+' = '+IntToStr(Length(binIn)));
580 i32 := Min( Length(binOut), Length(binIn));
581 Expect( CompareMem( binOut, binIn, i32), 'testBinary('+BytesToHex(binOut)+') = '+BytesToHex(binIn));
582 except
583 on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message);
584 on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message);
585 end;
586
Jens Geyercf892d42017-09-09 10:08:22 +0200587 // empty binary
588 SetLength( binOut, 0);
589 Console.WriteLine('testBinary('+BytesToHex(binOut)+')');
590 try
591 binIn := client.testBinary(binOut);
592 Expect( Length(binOut) = Length(binIn), 'testBinary(): length '+IntToStr(Length(binOut))+' = '+IntToStr(Length(binIn)));
593 i32 := Min( Length(binOut), Length(binIn));
594 Expect( CompareMem( binOut, binIn, i32), 'testBinary('+BytesToHex(binOut)+') = '+BytesToHex(binIn));
595 except
596 on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message);
597 on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message);
598 end;
599
Roger Meier3bef8c22012-10-06 06:58:00 +0000600 Console.WriteLine('testDouble(5.325098235)');
601 dub := client.testDouble(5.325098235);
602 Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
603
604 // structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200605 StartTestGroup( 'testStruct', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000606 Console.WriteLine('testStruct({''Zero'', 1, -3, -5})');
607 o := TXtructImpl.Create;
608 o.String_thing := 'Zero';
609 o.Byte_thing := 1;
610 o.I32_thing := -3;
611 o.I64_thing := -5;
612 i := client.testStruct(o);
613 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
614 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
615 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
616 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
617 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
618 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
619 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
620 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
621
622 // nested structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200623 StartTestGroup( 'testNest', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000624 Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})');
625 o2 := TXtruct2Impl.Create;
626 o2.Byte_thing := 1;
627 o2.Struct_thing := o;
628 o2.I32_thing := 5;
629 i2 := client.testNest(o2);
630 i := i2.Struct_thing;
631 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
632 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
633 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
634 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
635 Expect( i2.Byte_thing = 1, 'i2.Byte_thing = '+IntToStr(i2.Byte_thing));
636 Expect( i2.I32_thing = 5, 'i2.I32_thing = '+IntToStr(i2.I32_thing));
637 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
638 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
639 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
640 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
641 Expect( i2.__isset_Byte_thing, 'i2.__isset_Byte_thing');
642 Expect( i2.__isset_I32_thing, 'i2.__isset_I32_thing');
643
644 // map<type1,type2>: A map of strictly unique keys to values.
645 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200646 StartTestGroup( 'testMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000647 mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
648 for j := 0 to 4 do
649 begin
650 mapout.AddOrSetValue( j, j - 10);
651 end;
652 Console.Write('testMap({');
653 first := True;
654 for key in mapout.Keys do
655 begin
656 if first
657 then first := False
658 else Console.Write( ', ' );
659 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
660 end;
661 Console.WriteLine('})');
662
663 mapin := client.testMap( mapout );
664 Expect( mapin.Count = mapout.Count, 'testMap: mapin.Count = mapout.Count');
665 for j := 0 to 4 do
666 begin
667 Expect( mapout.ContainsKey(j), 'testMap: mapout.ContainsKey('+IntToStr(j)+') = '+BoolToString(mapout.ContainsKey(j)));
668 end;
669 for key in mapin.Keys do
670 begin
671 Expect( mapin[key] = mapout[key], 'testMap: '+IntToStr(key) + ' => ' + IntToStr( mapin[key]));
672 Expect( mapin[key] = key - 10, 'testMap: mapin['+IntToStr(key)+'] = '+IntToStr( mapin[key]));
673 end;
674
675
676 // map<type1,type2>: A map of strictly unique keys to values.
677 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200678 StartTestGroup( 'testStringMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000679 strmapout := TThriftDictionaryImpl<string,string>.Create;
680 for j := 0 to 4 do
681 begin
682 strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10));
683 end;
684 Console.Write('testStringMap({');
685 first := True;
686 for strkey in strmapout.Keys do
687 begin
688 if first
689 then first := False
690 else Console.Write( ', ' );
691 Console.Write( strkey + ' => ' + strmapout[strkey]);
692 end;
693 Console.WriteLine('})');
694
695 strmapin := client.testStringMap( strmapout );
696 Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count');
697 for j := 0 to 4 do
698 begin
699 Expect( strmapout.ContainsKey(IntToStr(j)),
700 'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = '
701 + BoolToString(strmapout.ContainsKey(IntToStr(j))));
702 end;
703 for strkey in strmapin.Keys do
704 begin
705 Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]);
706 Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]);
707 end;
708
709
710 // set<type>: An unordered set of unique elements.
711 // Translates to an STL set, Java HashSet, set in Python, etc.
712 // Note: PHP does not support sets, so it is treated similar to a List
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200713 StartTestGroup( 'testSet', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000714 setout := THashSetImpl<Integer>.Create;
715 for j := -2 to 2 do
716 begin
717 setout.Add( j );
718 end;
719 Console.Write('testSet({');
720 first := True;
721 for j in setout do
722 begin
723 if first
724 then first := False
725 else Console.Write(', ');
726 Console.Write(IntToStr( j));
727 end;
728 Console.WriteLine('})');
729
730 setin := client.testSet(setout);
731 Expect( setin.Count = setout.Count, 'testSet: setin.Count = setout.Count');
732 Expect( setin.Count = 5, 'testSet: setin.Count = '+IntToStr(setin.Count));
733 for j := -2 to 2 do // unordered, we can't rely on the order => test for known elements only
734 begin
735 Expect( setin.Contains(j), 'testSet: setin.Contains('+IntToStr(j)+') => '+BoolToString(setin.Contains(j)));
736 end;
737
738 // list<type>: An ordered list of elements.
739 // Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200740 StartTestGroup( 'testList', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000741 listout := TThriftListImpl<Integer>.Create;
742 listout.Add( +1);
743 listout.Add( -2);
744 listout.Add( +3);
745 listout.Add( -4);
746 listout.Add( 0);
747 Console.Write('testList({');
748 first := True;
749 for j in listout do
750 begin
751 if first
752 then first := False
753 else Console.Write(', ');
754 Console.Write(IntToStr( j));
755 end;
756 Console.WriteLine('})');
757
758 listin := client.testList(listout);
759 Expect( listin.Count = listout.Count, 'testList: listin.Count = listout.Count');
760 Expect( listin.Count = 5, 'testList: listin.Count = '+IntToStr(listin.Count));
761 Expect( listin[0] = +1, 'listin[0] = '+IntToStr( listin[0]));
762 Expect( listin[1] = -2, 'listin[1] = '+IntToStr( listin[1]));
763 Expect( listin[2] = +3, 'listin[2] = '+IntToStr( listin[2]));
764 Expect( listin[3] = -4, 'listin[3] = '+IntToStr( listin[3]));
765 Expect( listin[4] = 0, 'listin[4] = '+IntToStr( listin[4]));
766
767 // enums
768 ret := client.testEnum(TNumberz.ONE);
769 Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret)));
770
771 ret := client.testEnum(TNumberz.TWO);
772 Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret)));
773
774 ret := client.testEnum(TNumberz.THREE);
775 Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret)));
776
777 ret := client.testEnum(TNumberz.FIVE);
778 Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret)));
779
780 ret := client.testEnum(TNumberz.EIGHT);
781 Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret)));
782
783
784 // typedef
785 uid := client.testTypedef(309858235082523);
786 Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid));
787
788
789 // maps of maps
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200790 StartTestGroup( 'testMapMap(1)', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000791 mm := client.testMapMap(1);
792 Console.Write(' = {');
793 for key in mm.Keys do
794 begin
795 Console.Write( IntToStr( key) + ' => {');
796 m2 := mm[key];
797 for k2 in m2.Keys do
798 begin
799 Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', ');
800 end;
801 Console.Write('}, ');
802 end;
803 Console.WriteLine('}');
804
805 // verify result data
806 Expect( mm.Count = 2, 'mm.Count = '+IntToStr(mm.Count));
807 pos := mm[4];
808 neg := mm[-4];
809 for j := 1 to 4 do
810 begin
811 Expect( pos[j] = j, 'pos[j] = '+IntToStr(pos[j]));
812 Expect( neg[-j] = -j, 'neg[-j] = '+IntToStr(neg[-j]));
813 end;
814
815
816
817 // insanity
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200818 StartTestGroup( 'testInsanity', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000819 insane := TInsanityImpl.Create;
820 insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
821 insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000);
822 truck := TXtructImpl.Create;
823 truck.String_thing := 'Truck';
Jens Geyer540e3462016-12-28 14:25:41 +0100824 truck.Byte_thing := -8; // byte is signed
825 truck.I32_thing := 32;
826 truck.I64_thing := 64;
Roger Meier3bef8c22012-10-06 06:58:00 +0000827 insane.Xtructs := TThriftListImpl<IXtruct>.Create;
828 insane.Xtructs.Add( truck );
829 whoa := client.testInsanity( insane );
830 Console.Write(' = {');
831 for key64 in whoa.Keys do
832 begin
833 val := whoa[key64];
834 Console.Write( IntToStr( key64) + ' => {');
835 for k2_2 in val.Keys do
836 begin
837 v2 := val[k2_2];
838 Console.Write( IntToStr( Integer( k2_2)) + ' => {');
839 userMap := v2.UserMap;
840 Console.Write('{');
841 if userMap <> nil then
842 begin
843 for k3 in userMap.Keys do
844 begin
845 Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', ');
846 end;
847 end else
848 begin
849 Console.Write('null');
850 end;
851 Console.Write('}, ');
852 xtructs := v2.Xtructs;
853 Console.Write('{');
854
855 if xtructs <> nil then
856 begin
857 for x in xtructs do
858 begin
859 Console.Write('{"' + x.String_thing + '", ' +
860 IntToStr( x.Byte_thing) + ', ' +
861 IntToStr( x.I32_thing) + ', ' +
862 IntToStr( x.I32_thing) + '}, ');
863 end;
864 end else
865 begin
866 Console.Write('null');
867 end;
868 Console.Write('}');
869 Console.Write('}, ');
870 end;
871 Console.Write('}, ');
872 end;
873 Console.WriteLine('}');
874
Jens Geyer540e3462016-12-28 14:25:41 +0100875 (**
876 * So you think you've got this all worked, out eh?
877 *
878 * Creates a the returned map with these values and prints it out:
879 * { 1 => { 2 => argument,
880 * 3 => argument,
881 * },
882 * 2 => { 6 => <empty Insanity struct>, },
883 * }
884 * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
885 *)
886
Roger Meier3bef8c22012-10-06 06:58:00 +0000887 // verify result data
888 Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count));
889 //
890 first_map := whoa[1];
891 second_map := whoa[2];
892 Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count));
893 Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count));
894 //
895 looney := second_map[TNumberz.SIX];
896 Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney)));
897 Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
898 Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
899 //
900 for ret in [TNumberz.TWO, TNumberz.THREE] do begin
901 crazy := first_map[ret];
902 Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
903
904 Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap));
905 Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs));
906
Jens Geyer540e3462016-12-28 14:25:41 +0100907 Expect( crazy.UserMap.Count = insane.UserMap.Count, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count));
908 for pair in insane.UserMap do begin
909 Expect( crazy.UserMap[pair.Key] = pair.Value, 'crazy.UserMap['+IntToStr(Ord(pair.key))+'] = '+IntToStr(crazy.UserMap[pair.Key]));
910 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000911
Jens Geyer540e3462016-12-28 14:25:41 +0100912 Expect( crazy.Xtructs.Count = insane.Xtructs.Count, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count));
913 for arg0 := 0 to insane.Xtructs.Count-1 do begin
914 hello := insane.Xtructs[arg0];
915 goodbye := crazy.Xtructs[arg0];
916 Expect( goodbye.String_thing = hello.String_thing, 'goodbye.String_thing = '+goodbye.String_thing);
917 Expect( goodbye.Byte_thing = hello.Byte_thing, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing));
918 Expect( goodbye.I32_thing = hello.I32_thing, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing));
919 Expect( goodbye.I64_thing = hello.I64_thing, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing));
920 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000921 end;
922
923
924 // multi args
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200925 StartTestGroup( 'testMulti', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000926 arg0 := 1;
927 arg1 := 2;
928 arg2 := High(Int64);
929 arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
930 arg3.AddOrSetValue( 1, 'one');
931 arg4 := TNumberz.FIVE;
932 arg5 := 5000000;
933 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
934 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
935 arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
936 IntToStr( arg5) + ')');
937
938 i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5);
939 Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"');
940 Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing));
941 Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing));
942 Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing));
943 Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
944 Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
945 Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
946 Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
947
948 // multi exception
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200949 StartTestGroup( 'testMultiException(1)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000950 try
951 i := client.testMultiException( 'need more pizza', 'run out of beer');
952 Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"');
953 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
Jens Geyer6bbbf192014-09-07 01:45:56 +0200954 { this is not necessarily true, these fields are default-serialized
Jens Geyerd5436f52014-10-03 19:50:38 +0200955 Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
Roger Meier3bef8c22012-10-06 06:58:00 +0000956 Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
957 Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
Jens Geyerd5436f52014-10-03 19:50:38 +0200958 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000959 except
960 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
961 end;
962
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200963 StartTestGroup( 'testMultiException(Xception)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000964 try
965 i := client.testMultiException( 'Xception', 'second test');
966 Expect( FALSE, 'testMultiException(''Xception''): must trow an exception');
967 except
968 on x:TXception do begin
969 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
970 Expect( x.__isset_Message_, 'x.__isset_Message_ = '+BoolToString(x.__isset_Message_));
971 Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
972 Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"');
973 end;
974 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
975 end;
976
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200977 StartTestGroup( 'testMultiException(Xception2)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000978 try
979 i := client.testMultiException( 'Xception2', 'third test');
980 Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception');
981 except
982 on x:TXception2 do begin
983 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
984 Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing));
985 Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
986 Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"');
987 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 +0200988 { this is not necessarily true, these fields are default-serialized
Roger Meier3bef8c22012-10-06 06:58:00 +0000989 Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing));
990 Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing));
991 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 +0200992 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000993 end;
994 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
995 end;
996
997
998 // oneway functions
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200999 StartTestGroup( 'Test Oneway(1)', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001000 client.testOneway(1);
1001 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
1002
1003 // call time
Jens Geyer06045cf2013-03-27 20:26:25 +02001004 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +00001005 StartTestGroup( 'Test Calltime()');
Jens Geyer9f7f11e2016-04-14 21:37:11 +02001006 StartTick := GetTickCount;
Roger Meier3bef8c22012-10-06 06:58:00 +00001007 for k := 0 to 1000 - 1 do
1008 begin
1009 client.testVoid();
1010 end;
1011 Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
Jens Geyer06045cf2013-03-27 20:26:25 +02001012 {$ENDIF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +00001013
1014 // no more tests here
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001015 StartTestGroup( '', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001016end;
1017
1018
Jens Geyer14f5d502017-12-09 13:47:09 +01001019{$IFDEF SupportsAsync}
1020procedure TClientThread.ClientAsyncTest;
1021var
1022 client : TThriftTest.IAsync;
1023 s : string;
1024 i8 : ShortInt;
1025begin
1026 StartTestGroup( 'Async Tests', test_Unknown);
1027 client := TThriftTest.TClient.Create( FProtocol);
1028 FTransport.Open;
1029
1030 // oneway void functions
1031 client.testOnewayAsync(1).Wait;
1032 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
1033
1034 // normal functions
1035 s := client.testStringAsync(HUGE_TEST_STRING).Value;
1036 Expect( length(s) = length(HUGE_TEST_STRING),
1037 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
1038 +'=> length(result) = '+IntToStr(Length(s)));
1039
1040 i8 := client.testByte(1).Value;
1041 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
1042end;
1043{$ENDIF}
1044
1045
Jens Geyer718f6ee2013-09-06 21:02:34 +02001046{$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +02001047procedure TClientThread.StressTest(const client : TThriftTest.Iface);
1048begin
1049 while TRUE do begin
1050 try
1051 if not FTransport.IsOpen then FTransport.Open; // re-open connection, server has already closed
1052 try
1053 client.testString('Test');
1054 Write('.');
1055 finally
1056 if FTransport.IsOpen then FTransport.Close;
1057 end;
1058 except
1059 on e:Exception do Writeln(#10+e.message);
1060 end;
1061 end;
1062end;
Jens Geyer718f6ee2013-09-06 21:02:34 +02001063{$ENDIF}
Jens Geyer06045cf2013-03-27 20:26:25 +02001064
Jens Geyerfd1b3582014-12-13 23:42:58 +01001065
Jens Geyerd4df9172017-10-25 22:30:23 +02001066function TClientThread.PrepareBinaryData( aRandomDist, aHuge : Boolean) : TBytes;
1067var i : Integer;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001068begin
Jens Geyerd4df9172017-10-25 22:30:23 +02001069 if aHuge
1070 then SetLength( result, $12345) // tests for THRIFT-4372
1071 else SetLength( result, $100);
Jens Geyerfd1b3582014-12-13 23:42:58 +01001072 ASSERT( Low(result) = 0);
1073
1074 // linear distribution, unless random is requested
1075 if not aRandomDist then begin
1076 for i := Low(result) to High(result) do begin
1077 result[i] := i;
1078 end;
1079 Exit;
1080 end;
1081
1082 // random distribution of all 256 values
1083 FillChar( result[0], Length(result) * SizeOf(result[0]), $0);
Jens Geyerd4df9172017-10-25 22:30:23 +02001084 for i := Low(result) to High(result) do begin
1085 result[i] := Byte( Random($100));
Jens Geyerfd1b3582014-12-13 23:42:58 +01001086 end;
1087end;
1088
1089
Jens Geyerf7904452017-07-26 15:02:12 +02001090{$IFDEF Win64}
1091procedure TClientThread.UseInterlockedExchangeAdd64;
1092var a,b : Int64;
1093begin
1094 a := 1;
1095 b := 2;
1096 Thrift.Utils.InterlockedExchangeAdd64( a,b);
1097 Expect( a = 3, 'InterlockedExchangeAdd64');
1098end;
1099{$ENDIF}
1100
1101
Roger Meier3bef8c22012-10-06 06:58:00 +00001102procedure TClientThread.JSONProtocolReadWriteTest;
1103// Tests only then read/write procedures of the JSON protocol
1104// All tests succeed, if we can read what we wrote before
1105// Note that passing this test does not imply, that our JSON is really compatible to what
1106// other clients or servers expect as the real JSON. This is beyond the scope of this test.
1107var prot : IProtocol;
1108 stm : TStringStream;
Jens Geyer17c3ad92017-09-05 20:31:27 +02001109 list : TThriftList;
Jens Geyercf892d42017-09-09 10:08:22 +02001110 binary, binRead, emptyBinary : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +00001111 i,iErr : Integer;
1112const
1113 TEST_SHORT = ShortInt( $FE);
1114 TEST_SMALL = SmallInt( $FEDC);
1115 TEST_LONG = LongInt( $FEDCBA98);
1116 TEST_I64 = Int64( $FEDCBA9876543210);
1117 TEST_DOUBLE = -1.234e-56;
1118 DELTA_DOUBLE = TEST_DOUBLE * 1e-14;
1119 TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars")
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001120 // Test THRIFT-2336 and THRIFT-3404 with U+1D11E (G Clef symbol) and 'Русское Название';
1121 G_CLEF_AND_CYRILLIC_TEXT = #$1d11e' '#$0420#$0443#$0441#$0441#$043a#$043e#$0435' '#$041d#$0430#$0437#$0432#$0430#$043d#$0438#$0435;
1122 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 +01001123 // test both possible solidus encodings
1124 SOLIDUS_JSON_DATA = '"one/two\/three"';
1125 SOLIDUS_EXCPECTED = 'one/two/three';
Roger Meier3bef8c22012-10-06 06:58:00 +00001126begin
1127 stm := TStringStream.Create;
1128 try
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001129 StartTestGroup( 'JsonProtocolTest', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001130
1131 // prepare binary data
Jens Geyerd4df9172017-10-25 22:30:23 +02001132 binary := PrepareBinaryData( FALSE, FALSE);
Jens Geyercf892d42017-09-09 10:08:22 +02001133 SetLength( emptyBinary, 0); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001134
1135 // output setup
1136 prot := TJSONProtocolImpl.Create(
1137 TStreamTransportImpl.Create(
1138 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
1139
1140 // write
Jens Geyer17c3ad92017-09-05 20:31:27 +02001141 Init( list, TType.String_, 9);
1142 prot.WriteListBegin( list);
Roger Meier3bef8c22012-10-06 06:58:00 +00001143 prot.WriteBool( TRUE);
1144 prot.WriteBool( FALSE);
1145 prot.WriteByte( TEST_SHORT);
1146 prot.WriteI16( TEST_SMALL);
1147 prot.WriteI32( TEST_LONG);
1148 prot.WriteI64( TEST_I64);
1149 prot.WriteDouble( TEST_DOUBLE);
1150 prot.WriteString( TEST_STRING);
1151 prot.WriteBinary( binary);
Jens Geyercf892d42017-09-09 10:08:22 +02001152 prot.WriteString( ''); // empty string
1153 prot.WriteBinary( emptyBinary); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001154 prot.WriteListEnd;
1155
1156 // input setup
1157 Expect( stm.Position = stm.Size, 'Stream position/length after write');
1158 stm.Position := 0;
1159 prot := TJSONProtocolImpl.Create(
1160 TStreamTransportImpl.Create(
1161 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1162
1163 // read and compare
1164 list := prot.ReadListBegin;
1165 Expect( list.ElementType = TType.String_, 'list element type');
1166 Expect( list.Count = 9, 'list element count');
1167 Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE');
1168 Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE');
1169 Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte');
1170 Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16');
1171 Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32');
1172 Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64');
1173 Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble');
1174 Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString');
1175 binRead := prot.ReadBinary;
Jens Geyercf892d42017-09-09 10:08:22 +02001176 Expect( Length(prot.ReadString) = 0, 'WriteString/ReadString (empty string)');
1177 Expect( Length(prot.ReadBinary) = 0, 'empty WriteBinary/ReadBinary (empty data block)');
Roger Meier3bef8c22012-10-06 06:58:00 +00001178 prot.ReadListEnd;
1179
1180 // test binary data
1181 Expect( Length(binary) = Length(binRead), 'Binary data length check');
1182 iErr := -1;
1183 for i := Low(binary) to High(binary) do begin
1184 if binary[i] <> binRead[i] then begin
1185 iErr := i;
1186 Break;
1187 end;
1188 end;
1189 if iErr < 0
1190 then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)')
1191 else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr));
1192
1193 Expect( stm.Position = stm.Size, 'Stream position after read');
1194
Jens Geyer7bb44a32014-02-07 22:24:37 +01001195
Jens Geyer21366942013-12-30 22:04:51 +01001196 // Solidus can be encoded in two ways. Make sure we can read both
1197 stm.Position := 0;
1198 stm.Size := 0;
1199 stm.WriteString(SOLIDUS_JSON_DATA);
1200 stm.Position := 0;
1201 prot := TJSONProtocolImpl.Create(
1202 TStreamTransportImpl.Create(
1203 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1204 Expect( prot.ReadString = SOLIDUS_EXCPECTED, 'Solidus encoding');
1205
1206
Jens Geyer7bb44a32014-02-07 22:24:37 +01001207 // Widechars should work too. Do they?
1208 // After writing, we ensure that we are able to read it back
1209 // We can't assume hex-encoding, since (nearly) any Unicode char is valid JSON
1210 stm.Position := 0;
1211 stm.Size := 0;
1212 prot := TJSONProtocolImpl.Create(
1213 TStreamTransportImpl.Create(
1214 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001215 prot.WriteString( G_CLEF_AND_CYRILLIC_TEXT);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001216 stm.Position := 0;
1217 prot := TJSONProtocolImpl.Create(
1218 TStreamTransportImpl.Create(
1219 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001220 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Writing JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001221
1222 // Widechars should work with hex-encoding too. Do they?
1223 stm.Position := 0;
1224 stm.Size := 0;
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001225 stm.WriteString( G_CLEF_AND_CYRILLIC_JSON);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001226 stm.Position := 0;
1227 prot := TJSONProtocolImpl.Create(
1228 TStreamTransportImpl.Create(
1229 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001230 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Reading JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001231
1232
Roger Meier3bef8c22012-10-06 06:58:00 +00001233 finally
1234 stm.Free;
1235 prot := nil; //-> Release
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001236 StartTestGroup( '', test_Unknown); // no more tests here
Roger Meier3bef8c22012-10-06 06:58:00 +00001237 end;
1238end;
1239
1240
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001241procedure TClientThread.StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +00001242begin
1243 FTestGroup := aGroup;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001244 FCurrentTest := aTest;
1245
1246 Include( FExecuted, aTest);
1247
Roger Meier3bef8c22012-10-06 06:58:00 +00001248 if FTestGroup <> '' then begin
1249 Console.WriteLine('');
1250 Console.WriteLine( aGroup+' tests');
1251 Console.WriteLine( StringOfChar('-',60));
1252 end;
1253end;
1254
1255
1256procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
1257begin
1258 if aTestResult then begin
1259 Inc(FSuccesses);
1260 Console.WriteLine( aTestInfo+': passed');
1261 end
1262 else begin
1263 FErrors.Add( FTestGroup+': '+aTestInfo);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001264 Include( FFailed, FCurrentTest);
Roger Meier3bef8c22012-10-06 06:58:00 +00001265 Console.WriteLine( aTestInfo+': *** FAILED ***');
1266
1267 // We have a failed test!
1268 // -> issue DebugBreak ONLY if a debugger is attached,
1269 // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise
Jens Geyer9f7f11e2016-04-14 21:37:11 +02001270 if IsDebuggerPresent
1271 then {$IFDEF CPUX64} DebugBreak {$ELSE} asm int 3 end {$ENDIF};
Roger Meier3bef8c22012-10-06 06:58:00 +00001272 end;
1273end;
1274
1275
1276procedure TClientThread.ReportResults;
1277var nTotal : Integer;
1278 sLine : string;
1279begin
1280 // prevent us from stupid DIV/0 errors
1281 nTotal := FSuccesses + FErrors.Count;
1282 if nTotal = 0 then begin
1283 Console.WriteLine('No results logged');
1284 Exit;
1285 end;
1286
1287 Console.WriteLine('');
1288 Console.WriteLine( StringOfChar('=',60));
1289 Console.WriteLine( IntToStr(nTotal)+' tests performed');
1290 Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)');
1291 Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)');
1292 Console.WriteLine( StringOfChar('=',60));
1293 if FErrors.Count > 0 then begin
1294 Console.WriteLine('FAILED TESTS:');
1295 for sLine in FErrors do Console.WriteLine('- '+sLine);
1296 Console.WriteLine( StringOfChar('=',60));
1297 InterlockedIncrement( ExitCode); // return <> 0 on errors
1298 end;
1299 Console.WriteLine('');
1300end;
1301
1302
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001303function TClientThread.CalculateExitCode : Byte;
1304var test : TTestGroup;
1305begin
1306 result := EXITCODE_SUCCESS;
1307 for test := Low(TTestGroup) to High(TTestGroup) do begin
1308 if (test in FFailed) or not (test in FExecuted)
1309 then result := result or MAP_FAILURES_TO_EXITCODE_BITS[test];
1310 end;
1311end;
1312
1313
Roger Meier3bef8c22012-10-06 06:58:00 +00001314constructor TClientThread.Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
1315begin
1316 inherited Create( True );
1317 FNumIteration := ANumIteration;
1318 FTransport := ATransport;
1319 FProtocol := AProtocol;
1320 FConsole := TThreadConsole.Create( Self );
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001321 FCurrentTest := test_Unknown;
Roger Meier3bef8c22012-10-06 06:58:00 +00001322
1323 // error list: keep correct order, allow for duplicates
1324 FErrors := TStringList.Create;
1325 FErrors.Sorted := FALSE;
1326 FErrors.Duplicates := dupAccept;
1327end;
1328
1329destructor TClientThread.Destroy;
1330begin
1331 FreeAndNil( FConsole);
1332 FreeAndNil( FErrors);
1333 inherited;
1334end;
1335
1336procedure TClientThread.Execute;
1337var
1338 i : Integer;
1339 proc : TThreadProcedure;
1340begin
1341 // perform all tests
1342 try
Jens Geyerf7904452017-07-26 15:02:12 +02001343 {$IFDEF Win64}
1344 UseInterlockedExchangeAdd64;
Jens Geyer14f5d502017-12-09 13:47:09 +01001345 {$ENDIF}
Jens Geyer7bb44a32014-02-07 22:24:37 +01001346 JSONProtocolReadWriteTest;
Jens Geyerf7904452017-07-26 15:02:12 +02001347
Roger Meier3bef8c22012-10-06 06:58:00 +00001348 for i := 0 to FNumIteration - 1 do
1349 begin
1350 ClientTest;
Jens Geyer14f5d502017-12-09 13:47:09 +01001351 {$IFDEF SupportsAsync}
1352 ClientAsyncTest;
1353 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +00001354 end;
1355 except
1356 on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"');
1357 end;
1358
1359 // report the outcome
1360 ReportResults;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001361 SetReturnValue( CalculateExitCode);
Roger Meier3bef8c22012-10-06 06:58:00 +00001362
1363 // shutdown
1364 proc := procedure
1365 begin
1366 if FTransport <> nil then
1367 begin
1368 FTransport.Close;
1369 FTransport := nil;
1370 end;
1371 end;
1372
1373 Synchronize( proc );
1374end;
1375
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001376
Roger Meier3bef8c22012-10-06 06:58:00 +00001377{ TThreadConsole }
1378
1379constructor TThreadConsole.Create(AThread: TThread);
1380begin
Jens Geyer718f6ee2013-09-06 21:02:34 +02001381 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +00001382 FThread := AThread;
1383end;
1384
1385procedure TThreadConsole.Write(const S: string);
1386var
1387 proc : TThreadProcedure;
1388begin
1389 proc := procedure
1390 begin
1391 Console.Write( S );
1392 end;
1393 TThread.Synchronize( FThread, proc);
1394end;
1395
1396procedure TThreadConsole.WriteLine(const S: string);
1397var
1398 proc : TThreadProcedure;
1399begin
1400 proc := procedure
1401 begin
1402 Console.WriteLine( S );
1403 end;
1404 TThread.Synchronize( FThread, proc);
1405end;
1406
1407initialization
1408begin
1409 TTestClient.FNumIteration := 1;
1410 TTestClient.FNumThread := 1;
1411end;
1412
1413end.