blob: 55bf92b705f69b962f04ba4a65a88d9fb176e426 [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 Geyeraf7ecd62018-06-22 22:41:27 +020035 Windows, SysUtils, Classes, Math, ComObj, ActiveX,
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,
Jens Geyer02230912019-04-03 01:12:51 +020046 Thrift.Transport.WinHTTP,
47 Thrift.Transport.MsxmlHTTP,
Roger Meier3bef8c22012-10-06 06:58:00 +000048 Thrift.Transport,
49 Thrift.Stream,
50 Thrift.Test,
Jens Geyerf7904452017-07-26 15:02:12 +020051 Thrift.Utils,
Jens Geyer3d556242018-01-24 19:14:32 +010052 Thrift.Collections;
Roger Meier3bef8c22012-10-06 06:58:00 +000053
54type
55 TThreadConsole = class
56 private
57 FThread : TThread;
58 public
59 procedure Write( const S : string);
60 procedure WriteLine( const S : string);
61 constructor Create( AThread: TThread);
62 end;
63
Jens Geyeraf7ecd62018-06-22 22:41:27 +020064 TTestSetup = record
65 protType : TKnownProtocol;
66 endpoint : TEndpointTransport;
67 layered : TLayeredTransports;
68 useSSL : Boolean; // include where appropriate (TLayeredTransport?)
69 host : string;
70 port : Integer;
71 sPipeName : string;
72 hAnonRead, hAnonWrite : THandle;
73 end;
74
Roger Meier3bef8c22012-10-06 06:58:00 +000075 TClientThread = class( TThread )
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020076 private type
77 TTestGroup = (
78 test_Unknown,
79 test_BaseTypes,
80 test_Structs,
81 test_Containers,
82 test_Exceptions
83 // new values here
84 );
85 TTestGroups = set of TTestGroup;
86
Jens Geyer85827152018-01-12 21:20:59 +010087 TTestSize = (
88 Empty, // Edge case: the zero-length empty binary
89 Normal, // Fairly small array of usual size (256 bytes)
90 ByteArrayTest, // THRIFT-4454 Large writes/reads may cause range check errors in debug mode
91 PipeWriteLimit // THRIFT-4372 Pipe write operations across a network are limited to 65,535 bytes per write.
92 );
93
Roger Meier3bef8c22012-10-06 06:58:00 +000094 private
Jens Geyeraf7ecd62018-06-22 22:41:27 +020095 FSetup : TTestSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +000096 FTransport : ITransport;
97 FProtocol : IProtocol;
98 FNumIteration : Integer;
99 FConsole : TThreadConsole;
100
101 // test reporting, will be refactored out into separate class later
102 FTestGroup : string;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200103 FCurrentTest : TTestGroup;
Roger Meier3bef8c22012-10-06 06:58:00 +0000104 FSuccesses : Integer;
105 FErrors : TStringList;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200106 FFailed : TTestGroups;
107 FExecuted : TTestGroups;
108 procedure StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +0000109 procedure Expect( aTestResult : Boolean; const aTestInfo : string);
110 procedure ReportResults;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100111 function CalculateExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000112
113 procedure ClientTest;
Jens Geyer14f5d502017-12-09 13:47:09 +0100114 {$IFDEF SupportsAsync}
115 procedure ClientAsyncTest;
116 {$ENDIF}
117
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200118 procedure InitializeProtocolTransportStack;
119 procedure ShutdownProtocolTransportStack;
Jens Geyer02230912019-04-03 01:12:51 +0200120 function InitializeHttpTransport( const aTimeoutSetting : Integer) : IHTTPClient;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200121
Roger Meier3bef8c22012-10-06 06:58:00 +0000122 procedure JSONProtocolReadWriteTest;
Jens Geyer85827152018-01-12 21:20:59 +0100123 function PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes;
Jens Geyer718f6ee2013-09-06 21:02:34 +0200124 {$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200125 procedure StressTest(const client : TThriftTest.Iface);
Jens Geyer718f6ee2013-09-06 21:02:34 +0200126 {$ENDIF}
Jens Geyerf7904452017-07-26 15:02:12 +0200127 {$IFDEF Win64}
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200128 procedure UseInterlockedExchangeAdd64;
Jens Geyerf7904452017-07-26 15:02:12 +0200129 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000130 protected
131 procedure Execute; override;
132 public
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200133 constructor Create( const aSetup : TTestSetup; const aNumIteration: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +0000134 destructor Destroy; override;
135 end;
136
137 TTestClient = class
138 private
139 class var
140 FNumIteration : Integer;
141 FNumThread : Integer;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200142
143 class procedure PrintCmdLineHelp;
144 class procedure InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000145 public
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200146 class function Execute( const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000147 end;
148
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200149
Roger Meier3bef8c22012-10-06 06:58:00 +0000150implementation
151
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200152const
153 EXITCODE_SUCCESS = $00; // no errors bits set
154 //
155 EXITCODE_FAILBIT_BASETYPES = $01;
156 EXITCODE_FAILBIT_STRUCTS = $02;
157 EXITCODE_FAILBIT_CONTAINERS = $04;
158 EXITCODE_FAILBIT_EXCEPTIONS = $08;
159
160 MAP_FAILURES_TO_EXITCODE_BITS : array[TClientThread.TTestGroup] of Byte = (
161 EXITCODE_SUCCESS, // no bits here
162 EXITCODE_FAILBIT_BASETYPES,
163 EXITCODE_FAILBIT_STRUCTS,
164 EXITCODE_FAILBIT_CONTAINERS,
165 EXITCODE_FAILBIT_EXCEPTIONS
166 );
167
168
169
Roger Meier3bef8c22012-10-06 06:58:00 +0000170function BoolToString( b : Boolean) : string;
171// overrides global BoolToString()
172begin
173 if b
174 then result := 'true'
175 else result := 'false';
176end;
177
178// not available in all versions, so make sure we have this one imported
179function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent';
180
181{ TTestClient }
182
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200183class procedure TTestClient.PrintCmdLineHelp;
184const HELPTEXT = ' [options]'#10
185 + #10
186 + 'Allowed options:'#10
187 + ' -h [ --help ] produce help message'#10
188 + ' --host arg (=localhost) Host to connect'#10
189 + ' --port arg (=9090) Port number to connect'#10
190 + ' --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift),'#10
191 + ' instead of host and port'#10
192 + ' --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)'#10
193 + ' --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)'#10
Jens Geyer02230912019-04-03 01:12:51 +0200194 + ' --transport arg (=sockets) Transport: buffered, framed, http, winhttp'#10
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200195 + ' --protocol arg (=binary) Protocol: binary, compact, json'#10
196 + ' --ssl Encrypted Transport using SSL'#10
197 + ' -n [ --testloops ] arg (=1) Number of Tests'#10
198 + ' -t [ --threads ] arg (=1) Number of Test threads'#10
199 ;
200begin
201 Writeln( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + HELPTEXT);
202end;
203
204class procedure TTestClient.InvalidArgs;
205begin
206 Console.WriteLine( 'Invalid args.');
207 Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + ' -h for more information');
208 Abort;
209end;
210
211class function TTestClient.Execute(const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000212var
213 i : Integer;
Jens Geyer14f5d502017-12-09 13:47:09 +0100214 threadExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000215 s : string;
Roger Meier3bef8c22012-10-06 06:58:00 +0000216 threads : array of TThread;
217 dtStart : TDateTime;
218 test : Integer;
219 thread : TThread;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200220 setup : TTestSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +0000221begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200222 // init record
223 with setup do begin
224 protType := prot_Binary;
225 endpoint := trns_Sockets;
226 layered := [];
227 useSSL := FALSE;
228 host := 'localhost';
229 port := 9090;
230 sPipeName := '';
231 hAnonRead := INVALID_HANDLE_VALUE;
232 hAnonWrite := INVALID_HANDLE_VALUE;
233 end;
234
Roger Meier3bef8c22012-10-06 06:58:00 +0000235 try
Roger Meier3bef8c22012-10-06 06:58:00 +0000236 i := 0;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200237 while ( i < Length(args) ) do begin
238 s := args[i];
239 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000240
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200241 if (s = '-h') or (s = '--help') then begin
242 // -h [ --help ] produce help message
243 PrintCmdLineHelp;
244 result := $FF; // all tests failed
245 Exit;
246 end
Jens Geyerb360b652014-09-28 01:55:46 +0200247 else if s = '--host' then begin
248 // --host arg (=localhost) Host to connect
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200249 setup.host := args[i];
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200250 Inc( i);
251 end
Jens Geyerb360b652014-09-28 01:55:46 +0200252 else if s = '--port' then begin
253 // --port arg (=9090) Port number to connect
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200254 s := args[i];
255 Inc( i);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200256 setup.port := StrToIntDef(s,0);
257 if setup.port <= 0 then InvalidArgs;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200258 end
Jens Geyerb360b652014-09-28 01:55:46 +0200259 else if s = '--domain-socket' then begin
260 // --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200261 raise Exception.Create('domain-socket not supported');
262 end
Jens Geyerb360b652014-09-28 01:55:46 +0200263 else if s = '--named-pipe' then begin
264 // --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200265 setup.endpoint := trns_NamedPipes;
266 setup.sPipeName := args[i];
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200267 Inc( i);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200268 Console.WriteLine('Using named pipe ('+setup.sPipeName+')');
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200269 end
Jens Geyerb360b652014-09-28 01:55:46 +0200270 else if s = '--anon-pipes' then begin
271 // --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200272 setup.endpoint := trns_AnonPipes;
273 setup.hAnonRead := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200274 Inc( i);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200275 setup.hAnonWrite := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200276 Inc( i);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200277 Console.WriteLine('Using anonymous pipes ('+IntToStr(Integer(setup.hAnonRead))+' and '+IntToStr(Integer(setup.hAnonWrite))+')');
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200278 end
Jens Geyerb360b652014-09-28 01:55:46 +0200279 else if s = '--transport' then begin
Jens Geyer02230912019-04-03 01:12:51 +0200280 // --transport arg (=sockets) Transport: buffered, framed, http, winhttp, evhttp
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200281 s := args[i];
282 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000283
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200284 if s = 'buffered' then Include( setup.layered, trns_Buffered)
285 else if s = 'framed' then Include( setup.layered, trns_Framed)
Jens Geyer02230912019-04-03 01:12:51 +0200286 else if s = 'http' then setup.endpoint := trns_MsXmlHttp
287 else if s = 'winhttp' then setup.endpoint := trns_WinHttp
288 else if s = 'evhttp' then setup.endpoint := trns_EvHttp // recognized, but not supported
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200289 else InvalidArgs;
290 end
Jens Geyerb360b652014-09-28 01:55:46 +0200291 else if s = '--protocol' then begin
292 // --protocol arg (=binary) Protocol: binary, compact, json
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200293 s := args[i];
294 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000295
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200296 if s = 'binary' then setup.protType := prot_Binary
297 else if s = 'compact' then setup.protType := prot_Compact
298 else if s = 'json' then setup.protType := prot_JSON
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200299 else InvalidArgs;
300 end
Jens Geyerb360b652014-09-28 01:55:46 +0200301 else if s = '--ssl' then begin
302 // --ssl Encrypted Transport using SSL
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200303 setup.useSSL := TRUE;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200304
305 end
306 else if (s = '-n') or (s = '--testloops') then begin
307 // -n [ --testloops ] arg (=1) Number of Tests
308 FNumIteration := StrToIntDef( args[i], 0);
309 Inc( i);
310 if FNumIteration <= 0
311 then InvalidArgs;
312
313 end
314 else if (s = '-t') or (s = '--threads') then begin
315 // -t [ --threads ] arg (=1) Number of Test threads
316 FNumThread := StrToIntDef( args[i], 0);
317 Inc( i);
318 if FNumThread <= 0
319 then InvalidArgs;
320 end
321 else begin
322 InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000323 end;
324 end;
325
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200326
Roger Meier79655fb2012-10-20 20:59:41 +0000327 // In the anonymous pipes mode the client is launched by the test server
328 // -> behave nicely and allow for attaching a debugger to this process
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200329 if (setup.endpoint = trns_AnonPipes) and not IsDebuggerPresent
Roger Meier79655fb2012-10-20 20:59:41 +0000330 then MessageBox( 0, 'Attach Debugger and/or click OK to continue.',
331 'Thrift TestClient (Delphi)',
332 MB_OK or MB_ICONEXCLAMATION);
333
Roger Meier3bef8c22012-10-06 06:58:00 +0000334 SetLength( threads, FNumThread);
335 dtStart := Now;
336
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200337 // layered transports are not really meant to be stacked upon each other
338 if (trns_Framed in setup.layered) then begin
339 Console.WriteLine('Using framed transport');
340 end
341 else if (trns_Buffered in setup.layered) then begin
342 Console.WriteLine('Using buffered transport');
343 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000344
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200345 Console.WriteLine(THRIFT_PROTOCOLS[setup.protType]+' protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000346
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200347 for test := 0 to FNumThread - 1 do begin
348 thread := TClientThread.Create( setup, FNumIteration);
Roger Meier3bef8c22012-10-06 06:58:00 +0000349 threads[test] := thread;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200350 thread.Start;
Roger Meier3bef8c22012-10-06 06:58:00 +0000351 end;
352
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200353 result := 0;
354 for test := 0 to FNumThread - 1 do begin
Jens Geyer14f5d502017-12-09 13:47:09 +0100355 threadExitCode := threads[test].WaitFor;
356 result := result or threadExitCode;
Jens Geyer14f5d502017-12-09 13:47:09 +0100357 threads[test].Free;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200358 threads[test] := nil;
Jens Geyer14f5d502017-12-09 13:47:09 +0100359 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000360
361 Console.Write('Total time: ' + IntToStr( MilliSecondsBetween(Now, dtStart)));
362
363 except
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200364 on E: EAbort do raise;
365 on E: Exception do begin
366 Console.WriteLine( E.Message + #10 + E.StackTrace);
367 raise;
Roger Meier3bef8c22012-10-06 06:58:00 +0000368 end;
369 end;
370
371 Console.WriteLine('');
372 Console.WriteLine('done!');
373end;
374
375{ TClientThread }
376
377procedure TClientThread.ClientTest;
378var
379 client : TThriftTest.Iface;
380 s : string;
381 i8 : ShortInt;
382 i32 : Integer;
383 i64 : Int64;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100384 binOut,binIn : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000385 dub : Double;
386 o : IXtruct;
387 o2 : IXtruct2;
388 i : IXtruct;
389 i2 : IXtruct2;
390 mapout : IThriftDictionary<Integer,Integer>;
391 mapin : IThriftDictionary<Integer,Integer>;
392 strmapout : IThriftDictionary<string,string>;
393 strmapin : IThriftDictionary<string,string>;
394 j : Integer;
395 first : Boolean;
396 key : Integer;
397 strkey : string;
398 listout : IThriftList<Integer>;
399 listin : IThriftList<Integer>;
400 setout : IHashSet<Integer>;
401 setin : IHashSet<Integer>;
402 ret : TNumberz;
403 uid : Int64;
404 mm : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
405 pos : IThriftDictionary<Integer, Integer>;
406 neg : IThriftDictionary<Integer, Integer>;
407 m2 : IThriftDictionary<Integer, Integer>;
408 k2 : Integer;
409 insane : IInsanity;
410 truck : IXtruct;
411 whoa : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
412 key64 : Int64;
413 val : IThriftDictionary<TNumberz, IInsanity>;
414 k2_2 : TNumberz;
415 k3 : TNumberz;
416 v2 : IInsanity;
417 userMap : IThriftDictionary<TNumberz, Int64>;
418 xtructs : IThriftList<IXtruct>;
419 x : IXtruct;
420 arg0 : ShortInt;
421 arg1 : Integer;
422 arg2 : Int64;
423 arg3 : IThriftDictionary<SmallInt, string>;
424 arg4 : TNumberz;
425 arg5 : Int64;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200426 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000427 StartTick : Cardinal;
428 k : Integer;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200429 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000430 hello, goodbye : IXtruct;
431 crazy : IInsanity;
432 looney : IInsanity;
433 first_map : IThriftDictionary<TNumberz, IInsanity>;
434 second_map : IThriftDictionary<TNumberz, IInsanity>;
Jens Geyer540e3462016-12-28 14:25:41 +0100435 pair : TPair<TNumberz, TUserId>;
Jens Geyer85827152018-01-12 21:20:59 +0100436 testsize : TTestSize;
Roger Meier3bef8c22012-10-06 06:58:00 +0000437begin
438 client := TThriftTest.TClient.Create( FProtocol);
439 FTransport.Open;
440
Jens Geyer06045cf2013-03-27 20:26:25 +0200441 {$IFDEF StressTest}
442 StressTest( client);
443 {$ENDIF StressTest}
444
Jens Geyer17c3ad92017-09-05 20:31:27 +0200445 {$IFDEF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000446 // in-depth exception test
447 // (1) do we get an exception at all?
448 // (2) do we get the right exception?
449 // (3) does the exception contain the expected data?
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200450 StartTestGroup( 'testException', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000451 // case 1: exception type declared in IDL at the function call
452 try
453 client.testException('Xception');
454 Expect( FALSE, 'testException(''Xception''): must trow an exception');
455 except
456 on e:TXception do begin
457 Expect( e.ErrorCode = 1001, 'error code');
458 Expect( e.Message_ = 'Xception', 'error message');
459 Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ );
460 end;
461 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200462 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000463 end;
464
465 // case 2: exception type NOT declared in IDL at the function call
466 // this will close the connection
467 try
468 client.testException('TException');
469 Expect( FALSE, 'testException(''TException''): must trow an exception');
470 except
471 on e:TTransportException do begin
472 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Roger Meier3bef8c22012-10-06 06:58:00 +0000473 end;
Jens Geyer6bbbf192014-09-07 01:45:56 +0200474 on e:TApplicationException do begin
475 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Jens Geyer6bbbf192014-09-07 01:45:56 +0200476 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200477 on e:TException do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
478 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000479 end;
480
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100481
Jens Geyer2ad6c302015-02-26 19:38:53 +0100482 if FTransport.IsOpen then FTransport.Close;
483 FTransport.Open; // re-open connection, server has already closed
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100484
Jens Geyer2ad6c302015-02-26 19:38:53 +0100485
Roger Meier3bef8c22012-10-06 06:58:00 +0000486 // case 3: no exception
487 try
488 client.testException('something');
489 Expect( TRUE, 'testException(''something''): must not trow an exception');
490 except
491 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200492 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000493 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200494 {$ENDIF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000495
496
497 // simple things
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200498 StartTestGroup( 'simple Thrift calls', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000499 client.testVoid();
500 Expect( TRUE, 'testVoid()'); // success := no exception
501
Jens Geyer39ba6b72015-09-22 00:00:49 +0200502 s := BoolToString( client.testBool(TRUE));
503 Expect( s = BoolToString(TRUE), 'testBool(TRUE) = '+s);
504 s := BoolToString( client.testBool(FALSE));
505 Expect( s = BoolToString(FALSE), 'testBool(FALSE) = '+s);
506
Roger Meier3bef8c22012-10-06 06:58:00 +0000507 s := client.testString('Test');
508 Expect( s = 'Test', 'testString(''Test'') = "'+s+'"');
509
Jens Geyercf892d42017-09-09 10:08:22 +0200510 s := client.testString(''); // empty string
511 Expect( s = '', 'testString('''') = "'+s+'"');
512
Jens Geyer06045cf2013-03-27 20:26:25 +0200513 s := client.testString(HUGE_TEST_STRING);
514 Expect( length(s) = length(HUGE_TEST_STRING),
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100515 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
Jens Geyer06045cf2013-03-27 20:26:25 +0200516 +'=> length(result) = '+IntToStr(Length(s)));
517
Roger Meier3bef8c22012-10-06 06:58:00 +0000518 i8 := client.testByte(1);
519 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
520
521 i32 := client.testI32(-1);
522 Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32));
523
524 Console.WriteLine('testI64(-34359738368)');
525 i64 := client.testI64(-34359738368);
526 Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
527
Jens Geyerd4df9172017-10-25 22:30:23 +0200528 // random binary small
Jens Geyer85827152018-01-12 21:20:59 +0100529 for testsize := Low(TTestSize) to High(TTestSize) do begin
530 binOut := PrepareBinaryData( TRUE, testsize);
531 Console.WriteLine('testBinary('+BytesToHex(binOut)+')');
532 try
533 binIn := client.testBinary(binOut);
534 Expect( Length(binOut) = Length(binIn), 'testBinary(): length '+IntToStr(Length(binOut))+' = '+IntToStr(Length(binIn)));
535 i32 := Min( Length(binOut), Length(binIn));
536 Expect( CompareMem( binOut, binIn, i32), 'testBinary('+BytesToHex(binOut)+') = '+BytesToHex(binIn));
537 except
538 on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message);
539 on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message);
540 end;
Jens Geyercf892d42017-09-09 10:08:22 +0200541 end;
542
Roger Meier3bef8c22012-10-06 06:58:00 +0000543 Console.WriteLine('testDouble(5.325098235)');
544 dub := client.testDouble(5.325098235);
545 Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
546
547 // structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200548 StartTestGroup( 'testStruct', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000549 Console.WriteLine('testStruct({''Zero'', 1, -3, -5})');
550 o := TXtructImpl.Create;
551 o.String_thing := 'Zero';
552 o.Byte_thing := 1;
553 o.I32_thing := -3;
554 o.I64_thing := -5;
555 i := client.testStruct(o);
556 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
557 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
558 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
559 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
560 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
561 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
562 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
563 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
564
565 // nested structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200566 StartTestGroup( 'testNest', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000567 Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})');
568 o2 := TXtruct2Impl.Create;
569 o2.Byte_thing := 1;
570 o2.Struct_thing := o;
571 o2.I32_thing := 5;
572 i2 := client.testNest(o2);
573 i := i2.Struct_thing;
574 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
575 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
576 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
577 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
578 Expect( i2.Byte_thing = 1, 'i2.Byte_thing = '+IntToStr(i2.Byte_thing));
579 Expect( i2.I32_thing = 5, 'i2.I32_thing = '+IntToStr(i2.I32_thing));
580 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
581 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
582 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
583 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
584 Expect( i2.__isset_Byte_thing, 'i2.__isset_Byte_thing');
585 Expect( i2.__isset_I32_thing, 'i2.__isset_I32_thing');
586
587 // map<type1,type2>: A map of strictly unique keys to values.
588 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200589 StartTestGroup( 'testMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000590 mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
591 for j := 0 to 4 do
592 begin
593 mapout.AddOrSetValue( j, j - 10);
594 end;
595 Console.Write('testMap({');
596 first := True;
597 for key in mapout.Keys do
598 begin
599 if first
600 then first := False
601 else Console.Write( ', ' );
602 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
603 end;
604 Console.WriteLine('})');
605
606 mapin := client.testMap( mapout );
607 Expect( mapin.Count = mapout.Count, 'testMap: mapin.Count = mapout.Count');
608 for j := 0 to 4 do
609 begin
610 Expect( mapout.ContainsKey(j), 'testMap: mapout.ContainsKey('+IntToStr(j)+') = '+BoolToString(mapout.ContainsKey(j)));
611 end;
612 for key in mapin.Keys do
613 begin
614 Expect( mapin[key] = mapout[key], 'testMap: '+IntToStr(key) + ' => ' + IntToStr( mapin[key]));
615 Expect( mapin[key] = key - 10, 'testMap: mapin['+IntToStr(key)+'] = '+IntToStr( mapin[key]));
616 end;
617
618
619 // map<type1,type2>: A map of strictly unique keys to values.
620 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200621 StartTestGroup( 'testStringMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000622 strmapout := TThriftDictionaryImpl<string,string>.Create;
623 for j := 0 to 4 do
624 begin
625 strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10));
626 end;
627 Console.Write('testStringMap({');
628 first := True;
629 for strkey in strmapout.Keys do
630 begin
631 if first
632 then first := False
633 else Console.Write( ', ' );
634 Console.Write( strkey + ' => ' + strmapout[strkey]);
635 end;
636 Console.WriteLine('})');
637
638 strmapin := client.testStringMap( strmapout );
639 Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count');
640 for j := 0 to 4 do
641 begin
642 Expect( strmapout.ContainsKey(IntToStr(j)),
643 'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = '
644 + BoolToString(strmapout.ContainsKey(IntToStr(j))));
645 end;
646 for strkey in strmapin.Keys do
647 begin
648 Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]);
649 Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]);
650 end;
651
652
653 // set<type>: An unordered set of unique elements.
654 // Translates to an STL set, Java HashSet, set in Python, etc.
655 // Note: PHP does not support sets, so it is treated similar to a List
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200656 StartTestGroup( 'testSet', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000657 setout := THashSetImpl<Integer>.Create;
658 for j := -2 to 2 do
659 begin
660 setout.Add( j );
661 end;
662 Console.Write('testSet({');
663 first := True;
664 for j in setout do
665 begin
666 if first
667 then first := False
668 else Console.Write(', ');
669 Console.Write(IntToStr( j));
670 end;
671 Console.WriteLine('})');
672
673 setin := client.testSet(setout);
674 Expect( setin.Count = setout.Count, 'testSet: setin.Count = setout.Count');
675 Expect( setin.Count = 5, 'testSet: setin.Count = '+IntToStr(setin.Count));
676 for j := -2 to 2 do // unordered, we can't rely on the order => test for known elements only
677 begin
678 Expect( setin.Contains(j), 'testSet: setin.Contains('+IntToStr(j)+') => '+BoolToString(setin.Contains(j)));
679 end;
680
681 // list<type>: An ordered list of elements.
682 // Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200683 StartTestGroup( 'testList', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000684 listout := TThriftListImpl<Integer>.Create;
685 listout.Add( +1);
686 listout.Add( -2);
687 listout.Add( +3);
688 listout.Add( -4);
689 listout.Add( 0);
690 Console.Write('testList({');
691 first := True;
692 for j in listout do
693 begin
694 if first
695 then first := False
696 else Console.Write(', ');
697 Console.Write(IntToStr( j));
698 end;
699 Console.WriteLine('})');
700
701 listin := client.testList(listout);
702 Expect( listin.Count = listout.Count, 'testList: listin.Count = listout.Count');
703 Expect( listin.Count = 5, 'testList: listin.Count = '+IntToStr(listin.Count));
704 Expect( listin[0] = +1, 'listin[0] = '+IntToStr( listin[0]));
705 Expect( listin[1] = -2, 'listin[1] = '+IntToStr( listin[1]));
706 Expect( listin[2] = +3, 'listin[2] = '+IntToStr( listin[2]));
707 Expect( listin[3] = -4, 'listin[3] = '+IntToStr( listin[3]));
708 Expect( listin[4] = 0, 'listin[4] = '+IntToStr( listin[4]));
709
710 // enums
711 ret := client.testEnum(TNumberz.ONE);
712 Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret)));
713
714 ret := client.testEnum(TNumberz.TWO);
715 Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret)));
716
717 ret := client.testEnum(TNumberz.THREE);
718 Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret)));
719
720 ret := client.testEnum(TNumberz.FIVE);
721 Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret)));
722
723 ret := client.testEnum(TNumberz.EIGHT);
724 Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret)));
725
726
727 // typedef
728 uid := client.testTypedef(309858235082523);
729 Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid));
730
731
732 // maps of maps
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200733 StartTestGroup( 'testMapMap(1)', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000734 mm := client.testMapMap(1);
735 Console.Write(' = {');
736 for key in mm.Keys do
737 begin
738 Console.Write( IntToStr( key) + ' => {');
739 m2 := mm[key];
740 for k2 in m2.Keys do
741 begin
742 Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', ');
743 end;
744 Console.Write('}, ');
745 end;
746 Console.WriteLine('}');
747
748 // verify result data
749 Expect( mm.Count = 2, 'mm.Count = '+IntToStr(mm.Count));
750 pos := mm[4];
751 neg := mm[-4];
752 for j := 1 to 4 do
753 begin
754 Expect( pos[j] = j, 'pos[j] = '+IntToStr(pos[j]));
755 Expect( neg[-j] = -j, 'neg[-j] = '+IntToStr(neg[-j]));
756 end;
757
758
759
760 // insanity
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200761 StartTestGroup( 'testInsanity', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000762 insane := TInsanityImpl.Create;
763 insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
764 insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000);
765 truck := TXtructImpl.Create;
766 truck.String_thing := 'Truck';
Jens Geyer540e3462016-12-28 14:25:41 +0100767 truck.Byte_thing := -8; // byte is signed
768 truck.I32_thing := 32;
769 truck.I64_thing := 64;
Roger Meier3bef8c22012-10-06 06:58:00 +0000770 insane.Xtructs := TThriftListImpl<IXtruct>.Create;
771 insane.Xtructs.Add( truck );
772 whoa := client.testInsanity( insane );
773 Console.Write(' = {');
774 for key64 in whoa.Keys do
775 begin
776 val := whoa[key64];
777 Console.Write( IntToStr( key64) + ' => {');
778 for k2_2 in val.Keys do
779 begin
780 v2 := val[k2_2];
781 Console.Write( IntToStr( Integer( k2_2)) + ' => {');
782 userMap := v2.UserMap;
783 Console.Write('{');
784 if userMap <> nil then
785 begin
786 for k3 in userMap.Keys do
787 begin
788 Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', ');
789 end;
790 end else
791 begin
792 Console.Write('null');
793 end;
794 Console.Write('}, ');
795 xtructs := v2.Xtructs;
796 Console.Write('{');
797
798 if xtructs <> nil then
799 begin
800 for x in xtructs do
801 begin
802 Console.Write('{"' + x.String_thing + '", ' +
803 IntToStr( x.Byte_thing) + ', ' +
804 IntToStr( x.I32_thing) + ', ' +
805 IntToStr( x.I32_thing) + '}, ');
806 end;
807 end else
808 begin
809 Console.Write('null');
810 end;
811 Console.Write('}');
812 Console.Write('}, ');
813 end;
814 Console.Write('}, ');
815 end;
816 Console.WriteLine('}');
817
Jens Geyer540e3462016-12-28 14:25:41 +0100818 (**
819 * So you think you've got this all worked, out eh?
820 *
821 * Creates a the returned map with these values and prints it out:
822 * { 1 => { 2 => argument,
823 * 3 => argument,
824 * },
825 * 2 => { 6 => <empty Insanity struct>, },
826 * }
827 * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
828 *)
829
Roger Meier3bef8c22012-10-06 06:58:00 +0000830 // verify result data
831 Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count));
832 //
833 first_map := whoa[1];
834 second_map := whoa[2];
835 Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count));
836 Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count));
837 //
838 looney := second_map[TNumberz.SIX];
839 Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney)));
840 Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
841 Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
842 //
843 for ret in [TNumberz.TWO, TNumberz.THREE] do begin
844 crazy := first_map[ret];
845 Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
846
847 Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap));
848 Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs));
849
Jens Geyer540e3462016-12-28 14:25:41 +0100850 Expect( crazy.UserMap.Count = insane.UserMap.Count, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count));
851 for pair in insane.UserMap do begin
852 Expect( crazy.UserMap[pair.Key] = pair.Value, 'crazy.UserMap['+IntToStr(Ord(pair.key))+'] = '+IntToStr(crazy.UserMap[pair.Key]));
853 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000854
Jens Geyer540e3462016-12-28 14:25:41 +0100855 Expect( crazy.Xtructs.Count = insane.Xtructs.Count, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count));
856 for arg0 := 0 to insane.Xtructs.Count-1 do begin
857 hello := insane.Xtructs[arg0];
858 goodbye := crazy.Xtructs[arg0];
859 Expect( goodbye.String_thing = hello.String_thing, 'goodbye.String_thing = '+goodbye.String_thing);
860 Expect( goodbye.Byte_thing = hello.Byte_thing, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing));
861 Expect( goodbye.I32_thing = hello.I32_thing, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing));
862 Expect( goodbye.I64_thing = hello.I64_thing, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing));
863 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000864 end;
865
866
867 // multi args
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200868 StartTestGroup( 'testMulti', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000869 arg0 := 1;
870 arg1 := 2;
871 arg2 := High(Int64);
872 arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
873 arg3.AddOrSetValue( 1, 'one');
874 arg4 := TNumberz.FIVE;
875 arg5 := 5000000;
876 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
877 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
878 arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
879 IntToStr( arg5) + ')');
880
881 i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5);
882 Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"');
883 Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing));
884 Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing));
885 Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing));
886 Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
887 Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
888 Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
889 Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
890
891 // multi exception
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200892 StartTestGroup( 'testMultiException(1)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000893 try
894 i := client.testMultiException( 'need more pizza', 'run out of beer');
895 Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"');
896 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
Jens Geyer6bbbf192014-09-07 01:45:56 +0200897 { this is not necessarily true, these fields are default-serialized
Jens Geyerd5436f52014-10-03 19:50:38 +0200898 Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
Roger Meier3bef8c22012-10-06 06:58:00 +0000899 Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
900 Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
Jens Geyerd5436f52014-10-03 19:50:38 +0200901 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000902 except
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200903 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000904 end;
905
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200906 StartTestGroup( 'testMultiException(Xception)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000907 try
908 i := client.testMultiException( 'Xception', 'second test');
909 Expect( FALSE, 'testMultiException(''Xception''): must trow an exception');
910 except
911 on x:TXception do begin
912 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
913 Expect( x.__isset_Message_, 'x.__isset_Message_ = '+BoolToString(x.__isset_Message_));
914 Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
915 Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"');
916 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200917 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000918 end;
919
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200920 StartTestGroup( 'testMultiException(Xception2)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000921 try
922 i := client.testMultiException( 'Xception2', 'third test');
923 Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception');
924 except
925 on x:TXception2 do begin
926 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
927 Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing));
928 Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
929 Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"');
930 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 +0200931 { this is not necessarily true, these fields are default-serialized
Roger Meier3bef8c22012-10-06 06:58:00 +0000932 Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing));
933 Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing));
934 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 +0200935 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000936 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200937 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000938 end;
939
940
941 // oneway functions
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200942 StartTestGroup( 'Test Oneway(1)', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000943 client.testOneway(1);
944 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
945
946 // call time
Jens Geyer06045cf2013-03-27 20:26:25 +0200947 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000948 StartTestGroup( 'Test Calltime()');
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200949 StartTick := GetTickCount;
Roger Meier3bef8c22012-10-06 06:58:00 +0000950 for k := 0 to 1000 - 1 do
951 begin
952 client.testVoid();
953 end;
954 Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
Jens Geyer06045cf2013-03-27 20:26:25 +0200955 {$ENDIF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000956
957 // no more tests here
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200958 StartTestGroup( '', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000959end;
960
961
Jens Geyer14f5d502017-12-09 13:47:09 +0100962{$IFDEF SupportsAsync}
963procedure TClientThread.ClientAsyncTest;
964var
965 client : TThriftTest.IAsync;
966 s : string;
967 i8 : ShortInt;
968begin
969 StartTestGroup( 'Async Tests', test_Unknown);
970 client := TThriftTest.TClient.Create( FProtocol);
971 FTransport.Open;
972
973 // oneway void functions
974 client.testOnewayAsync(1).Wait;
975 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
976
977 // normal functions
978 s := client.testStringAsync(HUGE_TEST_STRING).Value;
979 Expect( length(s) = length(HUGE_TEST_STRING),
980 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
981 +'=> length(result) = '+IntToStr(Length(s)));
982
983 i8 := client.testByte(1).Value;
984 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
985end;
986{$ENDIF}
987
988
Jens Geyer718f6ee2013-09-06 21:02:34 +0200989{$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200990procedure TClientThread.StressTest(const client : TThriftTest.Iface);
991begin
992 while TRUE do begin
993 try
994 if not FTransport.IsOpen then FTransport.Open; // re-open connection, server has already closed
995 try
996 client.testString('Test');
997 Write('.');
998 finally
999 if FTransport.IsOpen then FTransport.Close;
1000 end;
1001 except
1002 on e:Exception do Writeln(#10+e.message);
1003 end;
1004 end;
1005end;
Jens Geyer718f6ee2013-09-06 21:02:34 +02001006{$ENDIF}
Jens Geyer06045cf2013-03-27 20:26:25 +02001007
Jens Geyerfd1b3582014-12-13 23:42:58 +01001008
Jens Geyer85827152018-01-12 21:20:59 +01001009function TClientThread.PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes;
Jens Geyerd4df9172017-10-25 22:30:23 +02001010var i : Integer;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001011begin
Jens Geyer85827152018-01-12 21:20:59 +01001012 case aSize of
1013 Empty : SetLength( result, 0);
1014 Normal : SetLength( result, $100);
1015 ByteArrayTest : SetLength( result, SizeOf(TByteArray) + 128);
1016 PipeWriteLimit : SetLength( result, 65535 + 128);
1017 else
1018 raise EArgumentException.Create('aSize');
1019 end;
1020
Jens Geyerfd1b3582014-12-13 23:42:58 +01001021 ASSERT( Low(result) = 0);
Jens Geyer85827152018-01-12 21:20:59 +01001022 if Length(result) = 0 then Exit;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001023
1024 // linear distribution, unless random is requested
1025 if not aRandomDist then begin
1026 for i := Low(result) to High(result) do begin
Jens Geyer85827152018-01-12 21:20:59 +01001027 result[i] := i mod $100;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001028 end;
1029 Exit;
1030 end;
1031
1032 // random distribution of all 256 values
1033 FillChar( result[0], Length(result) * SizeOf(result[0]), $0);
Jens Geyerd4df9172017-10-25 22:30:23 +02001034 for i := Low(result) to High(result) do begin
1035 result[i] := Byte( Random($100));
Jens Geyerfd1b3582014-12-13 23:42:58 +01001036 end;
1037end;
1038
1039
Jens Geyerf7904452017-07-26 15:02:12 +02001040{$IFDEF Win64}
1041procedure TClientThread.UseInterlockedExchangeAdd64;
1042var a,b : Int64;
1043begin
1044 a := 1;
1045 b := 2;
1046 Thrift.Utils.InterlockedExchangeAdd64( a,b);
1047 Expect( a = 3, 'InterlockedExchangeAdd64');
1048end;
1049{$ENDIF}
1050
1051
Roger Meier3bef8c22012-10-06 06:58:00 +00001052procedure TClientThread.JSONProtocolReadWriteTest;
1053// Tests only then read/write procedures of the JSON protocol
1054// All tests succeed, if we can read what we wrote before
1055// Note that passing this test does not imply, that our JSON is really compatible to what
1056// other clients or servers expect as the real JSON. This is beyond the scope of this test.
1057var prot : IProtocol;
1058 stm : TStringStream;
Jens Geyer17c3ad92017-09-05 20:31:27 +02001059 list : TThriftList;
Jens Geyercf892d42017-09-09 10:08:22 +02001060 binary, binRead, emptyBinary : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +00001061 i,iErr : Integer;
1062const
1063 TEST_SHORT = ShortInt( $FE);
1064 TEST_SMALL = SmallInt( $FEDC);
1065 TEST_LONG = LongInt( $FEDCBA98);
1066 TEST_I64 = Int64( $FEDCBA9876543210);
1067 TEST_DOUBLE = -1.234e-56;
1068 DELTA_DOUBLE = TEST_DOUBLE * 1e-14;
1069 TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars")
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001070 // Test THRIFT-2336 and THRIFT-3404 with U+1D11E (G Clef symbol) and 'Русское Название';
1071 G_CLEF_AND_CYRILLIC_TEXT = #$1d11e' '#$0420#$0443#$0441#$0441#$043a#$043e#$0435' '#$041d#$0430#$0437#$0432#$0430#$043d#$0438#$0435;
1072 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 +01001073 // test both possible solidus encodings
1074 SOLIDUS_JSON_DATA = '"one/two\/three"';
1075 SOLIDUS_EXCPECTED = 'one/two/three';
Roger Meier3bef8c22012-10-06 06:58:00 +00001076begin
1077 stm := TStringStream.Create;
1078 try
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001079 StartTestGroup( 'JsonProtocolTest', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001080
1081 // prepare binary data
Jens Geyer85827152018-01-12 21:20:59 +01001082 binary := PrepareBinaryData( FALSE, Normal);
Jens Geyercf892d42017-09-09 10:08:22 +02001083 SetLength( emptyBinary, 0); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001084
1085 // output setup
1086 prot := TJSONProtocolImpl.Create(
1087 TStreamTransportImpl.Create(
1088 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
1089
1090 // write
Jens Geyer17c3ad92017-09-05 20:31:27 +02001091 Init( list, TType.String_, 9);
1092 prot.WriteListBegin( list);
Roger Meier3bef8c22012-10-06 06:58:00 +00001093 prot.WriteBool( TRUE);
1094 prot.WriteBool( FALSE);
1095 prot.WriteByte( TEST_SHORT);
1096 prot.WriteI16( TEST_SMALL);
1097 prot.WriteI32( TEST_LONG);
1098 prot.WriteI64( TEST_I64);
1099 prot.WriteDouble( TEST_DOUBLE);
1100 prot.WriteString( TEST_STRING);
1101 prot.WriteBinary( binary);
Jens Geyercf892d42017-09-09 10:08:22 +02001102 prot.WriteString( ''); // empty string
1103 prot.WriteBinary( emptyBinary); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001104 prot.WriteListEnd;
1105
1106 // input setup
1107 Expect( stm.Position = stm.Size, 'Stream position/length after write');
1108 stm.Position := 0;
1109 prot := TJSONProtocolImpl.Create(
1110 TStreamTransportImpl.Create(
1111 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1112
1113 // read and compare
1114 list := prot.ReadListBegin;
1115 Expect( list.ElementType = TType.String_, 'list element type');
1116 Expect( list.Count = 9, 'list element count');
1117 Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE');
1118 Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE');
1119 Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte');
1120 Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16');
1121 Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32');
1122 Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64');
1123 Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble');
1124 Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString');
1125 binRead := prot.ReadBinary;
Jens Geyercf892d42017-09-09 10:08:22 +02001126 Expect( Length(prot.ReadString) = 0, 'WriteString/ReadString (empty string)');
1127 Expect( Length(prot.ReadBinary) = 0, 'empty WriteBinary/ReadBinary (empty data block)');
Roger Meier3bef8c22012-10-06 06:58:00 +00001128 prot.ReadListEnd;
1129
1130 // test binary data
1131 Expect( Length(binary) = Length(binRead), 'Binary data length check');
1132 iErr := -1;
1133 for i := Low(binary) to High(binary) do begin
1134 if binary[i] <> binRead[i] then begin
1135 iErr := i;
1136 Break;
1137 end;
1138 end;
1139 if iErr < 0
1140 then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)')
1141 else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr));
1142
1143 Expect( stm.Position = stm.Size, 'Stream position after read');
1144
Jens Geyer7bb44a32014-02-07 22:24:37 +01001145
Jens Geyer21366942013-12-30 22:04:51 +01001146 // Solidus can be encoded in two ways. Make sure we can read both
1147 stm.Position := 0;
1148 stm.Size := 0;
1149 stm.WriteString(SOLIDUS_JSON_DATA);
1150 stm.Position := 0;
1151 prot := TJSONProtocolImpl.Create(
1152 TStreamTransportImpl.Create(
1153 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1154 Expect( prot.ReadString = SOLIDUS_EXCPECTED, 'Solidus encoding');
1155
1156
Jens Geyer7bb44a32014-02-07 22:24:37 +01001157 // Widechars should work too. Do they?
1158 // After writing, we ensure that we are able to read it back
1159 // We can't assume hex-encoding, since (nearly) any Unicode char is valid JSON
1160 stm.Position := 0;
1161 stm.Size := 0;
1162 prot := TJSONProtocolImpl.Create(
1163 TStreamTransportImpl.Create(
1164 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001165 prot.WriteString( G_CLEF_AND_CYRILLIC_TEXT);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001166 stm.Position := 0;
1167 prot := TJSONProtocolImpl.Create(
1168 TStreamTransportImpl.Create(
1169 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001170 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Writing JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001171
1172 // Widechars should work with hex-encoding too. Do they?
1173 stm.Position := 0;
1174 stm.Size := 0;
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001175 stm.WriteString( G_CLEF_AND_CYRILLIC_JSON);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001176 stm.Position := 0;
1177 prot := TJSONProtocolImpl.Create(
1178 TStreamTransportImpl.Create(
1179 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001180 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Reading JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001181
1182
Roger Meier3bef8c22012-10-06 06:58:00 +00001183 finally
1184 stm.Free;
1185 prot := nil; //-> Release
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001186 StartTestGroup( '', test_Unknown); // no more tests here
Roger Meier3bef8c22012-10-06 06:58:00 +00001187 end;
1188end;
1189
1190
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001191procedure TClientThread.StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +00001192begin
1193 FTestGroup := aGroup;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001194 FCurrentTest := aTest;
1195
1196 Include( FExecuted, aTest);
1197
Roger Meier3bef8c22012-10-06 06:58:00 +00001198 if FTestGroup <> '' then begin
1199 Console.WriteLine('');
1200 Console.WriteLine( aGroup+' tests');
1201 Console.WriteLine( StringOfChar('-',60));
1202 end;
1203end;
1204
1205
1206procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
1207begin
1208 if aTestResult then begin
1209 Inc(FSuccesses);
1210 Console.WriteLine( aTestInfo+': passed');
1211 end
1212 else begin
1213 FErrors.Add( FTestGroup+': '+aTestInfo);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001214 Include( FFailed, FCurrentTest);
Roger Meier3bef8c22012-10-06 06:58:00 +00001215 Console.WriteLine( aTestInfo+': *** FAILED ***');
1216
1217 // We have a failed test!
1218 // -> issue DebugBreak ONLY if a debugger is attached,
1219 // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise
Jens Geyer9f7f11e2016-04-14 21:37:11 +02001220 if IsDebuggerPresent
1221 then {$IFDEF CPUX64} DebugBreak {$ELSE} asm int 3 end {$ENDIF};
Roger Meier3bef8c22012-10-06 06:58:00 +00001222 end;
1223end;
1224
1225
1226procedure TClientThread.ReportResults;
1227var nTotal : Integer;
1228 sLine : string;
1229begin
1230 // prevent us from stupid DIV/0 errors
1231 nTotal := FSuccesses + FErrors.Count;
1232 if nTotal = 0 then begin
1233 Console.WriteLine('No results logged');
1234 Exit;
1235 end;
1236
1237 Console.WriteLine('');
1238 Console.WriteLine( StringOfChar('=',60));
1239 Console.WriteLine( IntToStr(nTotal)+' tests performed');
1240 Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)');
1241 Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)');
1242 Console.WriteLine( StringOfChar('=',60));
1243 if FErrors.Count > 0 then begin
1244 Console.WriteLine('FAILED TESTS:');
1245 for sLine in FErrors do Console.WriteLine('- '+sLine);
1246 Console.WriteLine( StringOfChar('=',60));
1247 InterlockedIncrement( ExitCode); // return <> 0 on errors
1248 end;
1249 Console.WriteLine('');
1250end;
1251
1252
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001253function TClientThread.CalculateExitCode : Byte;
1254var test : TTestGroup;
1255begin
1256 result := EXITCODE_SUCCESS;
1257 for test := Low(TTestGroup) to High(TTestGroup) do begin
1258 if (test in FFailed) or not (test in FExecuted)
1259 then result := result or MAP_FAILURES_TO_EXITCODE_BITS[test];
1260 end;
1261end;
1262
1263
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001264constructor TClientThread.Create( const aSetup : TTestSetup; const aNumIteration: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +00001265begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001266 FSetup := aSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +00001267 FNumIteration := ANumIteration;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001268
Roger Meier3bef8c22012-10-06 06:58:00 +00001269 FConsole := TThreadConsole.Create( Self );
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001270 FCurrentTest := test_Unknown;
Roger Meier3bef8c22012-10-06 06:58:00 +00001271
1272 // error list: keep correct order, allow for duplicates
1273 FErrors := TStringList.Create;
1274 FErrors.Sorted := FALSE;
1275 FErrors.Duplicates := dupAccept;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001276
1277 inherited Create( TRUE);
Roger Meier3bef8c22012-10-06 06:58:00 +00001278end;
1279
1280destructor TClientThread.Destroy;
1281begin
1282 FreeAndNil( FConsole);
1283 FreeAndNil( FErrors);
1284 inherited;
1285end;
1286
1287procedure TClientThread.Execute;
1288var
1289 i : Integer;
Roger Meier3bef8c22012-10-06 06:58:00 +00001290begin
1291 // perform all tests
1292 try
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001293 {$IFDEF Win64}
Jens Geyerf7904452017-07-26 15:02:12 +02001294 UseInterlockedExchangeAdd64;
Jens Geyer14f5d502017-12-09 13:47:09 +01001295 {$ENDIF}
Jens Geyer7bb44a32014-02-07 22:24:37 +01001296 JSONProtocolReadWriteTest;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001297
1298 // must be run in the context of the thread
1299 InitializeProtocolTransportStack;
1300 try
1301 for i := 0 to FNumIteration - 1 do begin
1302 ClientTest;
1303 {$IFDEF SupportsAsync}
1304 ClientAsyncTest;
1305 {$ENDIF}
1306 end;
1307
1308 // report the outcome
1309 ReportResults;
1310 SetReturnValue( CalculateExitCode);
1311
1312 finally
1313 ShutdownProtocolTransportStack;
Roger Meier3bef8c22012-10-06 06:58:00 +00001314 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001315
Roger Meier3bef8c22012-10-06 06:58:00 +00001316 except
1317 on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"');
1318 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001319end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001320
Roger Meier3bef8c22012-10-06 06:58:00 +00001321
Jens Geyer02230912019-04-03 01:12:51 +02001322function TClientThread.InitializeHttpTransport( const aTimeoutSetting : Integer) : IHTTPClient;
1323var sUrl : string;
1324begin
1325 ASSERT( FSetup.endpoint in [trns_MsxmlHttp, trns_WinHttp]);
1326
1327 if FSetup.useSSL
1328 then sUrl := 'https://'
1329 else sUrl := 'http://';
1330
1331 sUrl := sUrl + FSetup.host;
1332
1333 case FSetup.port of
1334 80 : if FSetup.useSSL then sUrl := sUrl + ':'+ IntToStr(FSetup.port);
1335 443 : if not FSetup.useSSL then sUrl := sUrl + ':'+ IntToStr(FSetup.port);
1336 else
1337 if FSetup.port > 0 then sUrl := sUrl + ':'+ IntToStr(FSetup.port);
1338 end;
1339
1340 Console.WriteLine('Target URL: '+sUrl);
1341 case FSetup.endpoint of
1342 trns_MsxmlHttp : result := TMsxmlHTTPClientImpl.Create( sUrl);
1343 trns_WinHttp : result := TWinHTTPClientImpl.Create( sUrl);
1344 else
1345 raise Exception.Create(ENDPOINT_TRANSPORTS[FSetup.endpoint]+' unhandled case');
1346 end;
1347
1348 result.DnsResolveTimeout := aTimeoutSetting;
1349 result.ConnectionTimeout := aTimeoutSetting;
1350 result.SendTimeout := aTimeoutSetting;
1351 result.ReadTimeout := aTimeoutSetting;
1352end;
1353
1354
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001355procedure TClientThread.InitializeProtocolTransportStack;
Jens Geyer02230912019-04-03 01:12:51 +02001356var streamtrans : IStreamTransport;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001357const
1358 DEBUG_TIMEOUT = 30 * 1000;
1359 RELEASE_TIMEOUT = DEFAULT_THRIFT_TIMEOUT;
1360 PIPE_TIMEOUT = RELEASE_TIMEOUT;
1361 HTTP_TIMEOUTS = 10 * 1000;
1362begin
1363 // needed for HTTP clients as they utilize the MSXML COM components
1364 OleCheck( CoInitialize( nil));
1365
1366 case FSetup.endpoint of
1367 trns_Sockets: begin
1368 Console.WriteLine('Using sockets ('+FSetup.host+' port '+IntToStr(FSetup.port)+')');
1369 streamtrans := TSocketImpl.Create( FSetup.host, FSetup.port );
1370 FTransport := streamtrans;
1371 end;
1372
Jens Geyer02230912019-04-03 01:12:51 +02001373 trns_MsxmlHttp,
1374 trns_WinHttp: begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001375 Console.WriteLine('Using HTTPClient');
Jens Geyer02230912019-04-03 01:12:51 +02001376 FTransport := InitializeHttpTransport( HTTP_TIMEOUTS);
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001377 end;
1378
1379 trns_EvHttp: begin
1380 raise Exception.Create(ENDPOINT_TRANSPORTS[FSetup.endpoint]+' transport not implemented');
1381 end;
1382
1383 trns_NamedPipes: begin
1384 streamtrans := TNamedPipeTransportClientEndImpl.Create( FSetup.sPipeName, 0, nil, PIPE_TIMEOUT, PIPE_TIMEOUT);
1385 FTransport := streamtrans;
1386 end;
1387
1388 trns_AnonPipes: begin
1389 streamtrans := TAnonymousPipeTransportImpl.Create( FSetup.hAnonRead, FSetup.hAnonWrite, FALSE);
1390 FTransport := streamtrans;
1391 end;
1392
1393 else
1394 raise Exception.Create('Unhandled endpoint transport');
1395 end;
1396 ASSERT( FTransport <> nil);
1397
1398 // layered transports are not really meant to be stacked upon each other
1399 if (trns_Framed in FSetup.layered) then begin
1400 FTransport := TFramedTransportImpl.Create( FTransport);
1401 end
1402 else if (trns_Buffered in FSetup.layered) and (streamtrans <> nil) then begin
1403 FTransport := TBufferedTransportImpl.Create( streamtrans, 32); // small buffer to test read()
1404 end;
1405
1406 if FSetup.useSSL then begin
1407 raise Exception.Create('SSL/TLS not implemented');
1408 end;
1409
1410 // create protocol instance, default to BinaryProtocol
1411 case FSetup.protType of
1412 prot_Binary : FProtocol := TBinaryProtocolImpl.Create( FTransport, BINARY_STRICT_READ, BINARY_STRICT_WRITE);
1413 prot_JSON : FProtocol := TJSONProtocolImpl.Create( FTransport);
1414 prot_Compact : FProtocol := TCompactProtocolImpl.Create( FTransport);
1415 else
1416 raise Exception.Create('Unhandled protocol');
1417 end;
1418
1419 ASSERT( (FTransport <> nil) and (FProtocol <> nil));
1420end;
1421
1422
1423procedure TClientThread.ShutdownProtocolTransportStack;
1424begin
1425 try
1426 FProtocol := nil;
1427
1428 if FTransport <> nil then begin
Roger Meier3bef8c22012-10-06 06:58:00 +00001429 FTransport.Close;
1430 FTransport := nil;
1431 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001432
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001433 finally
1434 CoUninitialize;
1435 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001436end;
1437
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001438
Roger Meier3bef8c22012-10-06 06:58:00 +00001439{ TThreadConsole }
1440
1441constructor TThreadConsole.Create(AThread: TThread);
1442begin
Jens Geyer718f6ee2013-09-06 21:02:34 +02001443 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +00001444 FThread := AThread;
1445end;
1446
1447procedure TThreadConsole.Write(const S: string);
1448var
1449 proc : TThreadProcedure;
1450begin
1451 proc := procedure
1452 begin
1453 Console.Write( S );
1454 end;
1455 TThread.Synchronize( FThread, proc);
1456end;
1457
1458procedure TThreadConsole.WriteLine(const S: string);
1459var
1460 proc : TThreadProcedure;
1461begin
1462 proc := procedure
1463 begin
1464 Console.WriteLine( S );
1465 end;
1466 TThread.Synchronize( FThread, proc);
1467end;
1468
1469initialization
1470begin
1471 TTestClient.FNumIteration := 1;
1472 TTestClient.FNumThread := 1;
1473end;
1474
1475end.