blob: ebda7c6d4cd3c13ff2e09d48ba16a508430e60e6 [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
Jens Geyer47f63172019-06-06 22:42:58 +020032{$WARN SYMBOL_PLATFORM OFF} // Win32Check
33
Roger Meier3bef8c22012-10-06 06:58:00 +000034interface
35
36uses
Jens Geyeraf7ecd62018-06-22 22:41:27 +020037 Windows, SysUtils, Classes, Math, ComObj, ActiveX,
Jens Geyer14f5d502017-12-09 13:47:09 +010038 {$IFDEF SupportsAsync} System.Threading, {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +000039 DateUtils,
40 Generics.Collections,
41 TestConstants,
Jens Geyer3d556242018-01-24 19:14:32 +010042 ConsoleHelper,
Roger Meier3bef8c22012-10-06 06:58:00 +000043 Thrift,
Jens Geyerf0e63312015-03-01 18:47:49 +010044 Thrift.Protocol.Compact,
Roger Meier3bef8c22012-10-06 06:58:00 +000045 Thrift.Protocol.JSON,
46 Thrift.Protocol,
47 Thrift.Transport.Pipes,
Jens Geyer02230912019-04-03 01:12:51 +020048 Thrift.Transport.WinHTTP,
49 Thrift.Transport.MsxmlHTTP,
Roger Meier3bef8c22012-10-06 06:58:00 +000050 Thrift.Transport,
51 Thrift.Stream,
52 Thrift.Test,
Jens Geyerf7904452017-07-26 15:02:12 +020053 Thrift.Utils,
Jens Geyer3d556242018-01-24 19:14:32 +010054 Thrift.Collections;
Roger Meier3bef8c22012-10-06 06:58:00 +000055
56type
57 TThreadConsole = class
58 private
59 FThread : TThread;
60 public
61 procedure Write( const S : string);
62 procedure WriteLine( const S : string);
63 constructor Create( AThread: TThread);
64 end;
65
Jens Geyeraf7ecd62018-06-22 22:41:27 +020066 TTestSetup = record
67 protType : TKnownProtocol;
68 endpoint : TEndpointTransport;
69 layered : TLayeredTransports;
70 useSSL : Boolean; // include where appropriate (TLayeredTransport?)
71 host : string;
72 port : Integer;
73 sPipeName : string;
74 hAnonRead, hAnonWrite : THandle;
75 end;
76
Roger Meier3bef8c22012-10-06 06:58:00 +000077 TClientThread = class( TThread )
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020078 private type
79 TTestGroup = (
80 test_Unknown,
81 test_BaseTypes,
82 test_Structs,
83 test_Containers,
84 test_Exceptions
85 // new values here
86 );
87 TTestGroups = set of TTestGroup;
88
Jens Geyer85827152018-01-12 21:20:59 +010089 TTestSize = (
90 Empty, // Edge case: the zero-length empty binary
91 Normal, // Fairly small array of usual size (256 bytes)
92 ByteArrayTest, // THRIFT-4454 Large writes/reads may cause range check errors in debug mode
93 PipeWriteLimit // THRIFT-4372 Pipe write operations across a network are limited to 65,535 bytes per write.
94 );
95
Roger Meier3bef8c22012-10-06 06:58:00 +000096 private
Jens Geyeraf7ecd62018-06-22 22:41:27 +020097 FSetup : TTestSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +000098 FTransport : ITransport;
99 FProtocol : IProtocol;
100 FNumIteration : Integer;
101 FConsole : TThreadConsole;
102
103 // test reporting, will be refactored out into separate class later
104 FTestGroup : string;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200105 FCurrentTest : TTestGroup;
Roger Meier3bef8c22012-10-06 06:58:00 +0000106 FSuccesses : Integer;
107 FErrors : TStringList;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200108 FFailed : TTestGroups;
109 FExecuted : TTestGroups;
110 procedure StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +0000111 procedure Expect( aTestResult : Boolean; const aTestInfo : string);
112 procedure ReportResults;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100113 function CalculateExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000114
115 procedure ClientTest;
Jens Geyer14f5d502017-12-09 13:47:09 +0100116 {$IFDEF SupportsAsync}
117 procedure ClientAsyncTest;
118 {$ENDIF}
119
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200120 procedure InitializeProtocolTransportStack;
121 procedure ShutdownProtocolTransportStack;
Jens Geyer02230912019-04-03 01:12:51 +0200122 function InitializeHttpTransport( const aTimeoutSetting : Integer) : IHTTPClient;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200123
Roger Meier3bef8c22012-10-06 06:58:00 +0000124 procedure JSONProtocolReadWriteTest;
Jens Geyer85827152018-01-12 21:20:59 +0100125 function PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes;
Jens Geyer718f6ee2013-09-06 21:02:34 +0200126 {$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200127 procedure StressTest(const client : TThriftTest.Iface);
Jens Geyer718f6ee2013-09-06 21:02:34 +0200128 {$ENDIF}
Jens Geyerf7904452017-07-26 15:02:12 +0200129 {$IFDEF Win64}
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200130 procedure UseInterlockedExchangeAdd64;
Jens Geyerf7904452017-07-26 15:02:12 +0200131 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000132 protected
133 procedure Execute; override;
134 public
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200135 constructor Create( const aSetup : TTestSetup; const aNumIteration: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +0000136 destructor Destroy; override;
137 end;
138
139 TTestClient = class
140 private
141 class var
142 FNumIteration : Integer;
143 FNumThread : Integer;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200144
145 class procedure PrintCmdLineHelp;
146 class procedure InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000147 public
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200148 class function Execute( const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000149 end;
150
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200151
Roger Meier3bef8c22012-10-06 06:58:00 +0000152implementation
153
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200154const
155 EXITCODE_SUCCESS = $00; // no errors bits set
156 //
157 EXITCODE_FAILBIT_BASETYPES = $01;
158 EXITCODE_FAILBIT_STRUCTS = $02;
159 EXITCODE_FAILBIT_CONTAINERS = $04;
160 EXITCODE_FAILBIT_EXCEPTIONS = $08;
161
162 MAP_FAILURES_TO_EXITCODE_BITS : array[TClientThread.TTestGroup] of Byte = (
163 EXITCODE_SUCCESS, // no bits here
164 EXITCODE_FAILBIT_BASETYPES,
165 EXITCODE_FAILBIT_STRUCTS,
166 EXITCODE_FAILBIT_CONTAINERS,
167 EXITCODE_FAILBIT_EXCEPTIONS
168 );
169
170
171
Roger Meier3bef8c22012-10-06 06:58:00 +0000172function BoolToString( b : Boolean) : string;
173// overrides global BoolToString()
174begin
175 if b
176 then result := 'true'
177 else result := 'false';
178end;
179
180// not available in all versions, so make sure we have this one imported
181function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent';
182
183{ TTestClient }
184
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200185class procedure TTestClient.PrintCmdLineHelp;
186const HELPTEXT = ' [options]'#10
187 + #10
188 + 'Allowed options:'#10
189 + ' -h [ --help ] produce help message'#10
190 + ' --host arg (=localhost) Host to connect'#10
191 + ' --port arg (=9090) Port number to connect'#10
192 + ' --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift),'#10
193 + ' instead of host and port'#10
194 + ' --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)'#10
195 + ' --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)'#10
Jens Geyer02230912019-04-03 01:12:51 +0200196 + ' --transport arg (=sockets) Transport: buffered, framed, http, winhttp'#10
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200197 + ' --protocol arg (=binary) Protocol: binary, compact, json'#10
198 + ' --ssl Encrypted Transport using SSL'#10
199 + ' -n [ --testloops ] arg (=1) Number of Tests'#10
200 + ' -t [ --threads ] arg (=1) Number of Test threads'#10
201 ;
202begin
203 Writeln( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + HELPTEXT);
204end;
205
206class procedure TTestClient.InvalidArgs;
207begin
208 Console.WriteLine( 'Invalid args.');
209 Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + ' -h for more information');
210 Abort;
211end;
212
213class function TTestClient.Execute(const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000214var
215 i : Integer;
Jens Geyer14f5d502017-12-09 13:47:09 +0100216 threadExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000217 s : string;
Roger Meier3bef8c22012-10-06 06:58:00 +0000218 threads : array of TThread;
219 dtStart : TDateTime;
220 test : Integer;
221 thread : TThread;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200222 setup : TTestSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +0000223begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200224 // init record
225 with setup do begin
226 protType := prot_Binary;
227 endpoint := trns_Sockets;
228 layered := [];
229 useSSL := FALSE;
230 host := 'localhost';
231 port := 9090;
232 sPipeName := '';
233 hAnonRead := INVALID_HANDLE_VALUE;
234 hAnonWrite := INVALID_HANDLE_VALUE;
235 end;
236
Roger Meier3bef8c22012-10-06 06:58:00 +0000237 try
Roger Meier3bef8c22012-10-06 06:58:00 +0000238 i := 0;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200239 while ( i < Length(args) ) do begin
240 s := args[i];
241 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000242
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200243 if (s = '-h') or (s = '--help') then begin
244 // -h [ --help ] produce help message
245 PrintCmdLineHelp;
246 result := $FF; // all tests failed
247 Exit;
248 end
Jens Geyerb360b652014-09-28 01:55:46 +0200249 else if s = '--host' then begin
250 // --host arg (=localhost) Host to connect
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200251 setup.host := args[i];
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200252 Inc( i);
253 end
Jens Geyerb360b652014-09-28 01:55:46 +0200254 else if s = '--port' then begin
255 // --port arg (=9090) Port number to connect
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200256 s := args[i];
257 Inc( i);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200258 setup.port := StrToIntDef(s,0);
259 if setup.port <= 0 then InvalidArgs;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200260 end
Jens Geyerb360b652014-09-28 01:55:46 +0200261 else if s = '--domain-socket' then begin
262 // --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200263 raise Exception.Create('domain-socket not supported');
264 end
Jens Geyerb360b652014-09-28 01:55:46 +0200265 else if s = '--named-pipe' then begin
266 // --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200267 setup.endpoint := trns_NamedPipes;
268 setup.sPipeName := args[i];
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200269 Inc( i);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200270 Console.WriteLine('Using named pipe ('+setup.sPipeName+')');
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200271 end
Jens Geyerb360b652014-09-28 01:55:46 +0200272 else if s = '--anon-pipes' then begin
273 // --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200274 setup.endpoint := trns_AnonPipes;
275 setup.hAnonRead := 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 setup.hAnonWrite := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200278 Inc( i);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200279 Console.WriteLine('Using anonymous pipes ('+IntToStr(Integer(setup.hAnonRead))+' and '+IntToStr(Integer(setup.hAnonWrite))+')');
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200280 end
Jens Geyerb360b652014-09-28 01:55:46 +0200281 else if s = '--transport' then begin
Jens Geyer02230912019-04-03 01:12:51 +0200282 // --transport arg (=sockets) Transport: buffered, framed, http, winhttp, evhttp
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200283 s := args[i];
284 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000285
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200286 if s = 'buffered' then Include( setup.layered, trns_Buffered)
287 else if s = 'framed' then Include( setup.layered, trns_Framed)
Jens Geyer02230912019-04-03 01:12:51 +0200288 else if s = 'http' then setup.endpoint := trns_MsXmlHttp
289 else if s = 'winhttp' then setup.endpoint := trns_WinHttp
290 else if s = 'evhttp' then setup.endpoint := trns_EvHttp // recognized, but not supported
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200291 else InvalidArgs;
292 end
Jens Geyerb360b652014-09-28 01:55:46 +0200293 else if s = '--protocol' then begin
294 // --protocol arg (=binary) Protocol: binary, compact, json
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200295 s := args[i];
296 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000297
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200298 if s = 'binary' then setup.protType := prot_Binary
299 else if s = 'compact' then setup.protType := prot_Compact
300 else if s = 'json' then setup.protType := prot_JSON
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200301 else InvalidArgs;
302 end
Jens Geyerb360b652014-09-28 01:55:46 +0200303 else if s = '--ssl' then begin
304 // --ssl Encrypted Transport using SSL
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200305 setup.useSSL := TRUE;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200306
307 end
308 else if (s = '-n') or (s = '--testloops') then begin
309 // -n [ --testloops ] arg (=1) Number of Tests
310 FNumIteration := StrToIntDef( args[i], 0);
311 Inc( i);
312 if FNumIteration <= 0
313 then InvalidArgs;
314
315 end
316 else if (s = '-t') or (s = '--threads') then begin
317 // -t [ --threads ] arg (=1) Number of Test threads
318 FNumThread := StrToIntDef( args[i], 0);
319 Inc( i);
320 if FNumThread <= 0
321 then InvalidArgs;
322 end
323 else begin
324 InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000325 end;
326 end;
327
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200328
Roger Meier79655fb2012-10-20 20:59:41 +0000329 // In the anonymous pipes mode the client is launched by the test server
330 // -> behave nicely and allow for attaching a debugger to this process
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200331 if (setup.endpoint = trns_AnonPipes) and not IsDebuggerPresent
Roger Meier79655fb2012-10-20 20:59:41 +0000332 then MessageBox( 0, 'Attach Debugger and/or click OK to continue.',
333 'Thrift TestClient (Delphi)',
334 MB_OK or MB_ICONEXCLAMATION);
335
Roger Meier3bef8c22012-10-06 06:58:00 +0000336 SetLength( threads, FNumThread);
337 dtStart := Now;
338
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200339 // layered transports are not really meant to be stacked upon each other
340 if (trns_Framed in setup.layered) then begin
341 Console.WriteLine('Using framed transport');
342 end
343 else if (trns_Buffered in setup.layered) then begin
344 Console.WriteLine('Using buffered transport');
345 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000346
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200347 Console.WriteLine(THRIFT_PROTOCOLS[setup.protType]+' protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000348
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200349 for test := 0 to FNumThread - 1 do begin
350 thread := TClientThread.Create( setup, FNumIteration);
Roger Meier3bef8c22012-10-06 06:58:00 +0000351 threads[test] := thread;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200352 thread.Start;
Roger Meier3bef8c22012-10-06 06:58:00 +0000353 end;
354
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200355 result := 0;
356 for test := 0 to FNumThread - 1 do begin
Jens Geyer14f5d502017-12-09 13:47:09 +0100357 threadExitCode := threads[test].WaitFor;
358 result := result or threadExitCode;
Jens Geyer14f5d502017-12-09 13:47:09 +0100359 threads[test].Free;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200360 threads[test] := nil;
Jens Geyer14f5d502017-12-09 13:47:09 +0100361 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000362
363 Console.Write('Total time: ' + IntToStr( MilliSecondsBetween(Now, dtStart)));
364
365 except
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200366 on E: EAbort do raise;
367 on E: Exception do begin
368 Console.WriteLine( E.Message + #10 + E.StackTrace);
369 raise;
Roger Meier3bef8c22012-10-06 06:58:00 +0000370 end;
371 end;
372
373 Console.WriteLine('');
374 Console.WriteLine('done!');
375end;
376
377{ TClientThread }
378
379procedure TClientThread.ClientTest;
380var
381 client : TThriftTest.Iface;
382 s : string;
383 i8 : ShortInt;
384 i32 : Integer;
385 i64 : Int64;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100386 binOut,binIn : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000387 dub : Double;
388 o : IXtruct;
389 o2 : IXtruct2;
390 i : IXtruct;
391 i2 : IXtruct2;
392 mapout : IThriftDictionary<Integer,Integer>;
393 mapin : IThriftDictionary<Integer,Integer>;
394 strmapout : IThriftDictionary<string,string>;
395 strmapin : IThriftDictionary<string,string>;
396 j : Integer;
397 first : Boolean;
398 key : Integer;
399 strkey : string;
400 listout : IThriftList<Integer>;
401 listin : IThriftList<Integer>;
402 setout : IHashSet<Integer>;
403 setin : IHashSet<Integer>;
404 ret : TNumberz;
405 uid : Int64;
406 mm : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
407 pos : IThriftDictionary<Integer, Integer>;
408 neg : IThriftDictionary<Integer, Integer>;
409 m2 : IThriftDictionary<Integer, Integer>;
410 k2 : Integer;
411 insane : IInsanity;
412 truck : IXtruct;
413 whoa : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
414 key64 : Int64;
415 val : IThriftDictionary<TNumberz, IInsanity>;
416 k2_2 : TNumberz;
417 k3 : TNumberz;
418 v2 : IInsanity;
419 userMap : IThriftDictionary<TNumberz, Int64>;
420 xtructs : IThriftList<IXtruct>;
421 x : IXtruct;
422 arg0 : ShortInt;
423 arg1 : Integer;
424 arg2 : Int64;
425 arg3 : IThriftDictionary<SmallInt, string>;
426 arg4 : TNumberz;
427 arg5 : Int64;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200428 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000429 StartTick : Cardinal;
430 k : Integer;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200431 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000432 hello, goodbye : IXtruct;
433 crazy : IInsanity;
434 looney : IInsanity;
435 first_map : IThriftDictionary<TNumberz, IInsanity>;
436 second_map : IThriftDictionary<TNumberz, IInsanity>;
Jens Geyer540e3462016-12-28 14:25:41 +0100437 pair : TPair<TNumberz, TUserId>;
Jens Geyer85827152018-01-12 21:20:59 +0100438 testsize : TTestSize;
Roger Meier3bef8c22012-10-06 06:58:00 +0000439begin
440 client := TThriftTest.TClient.Create( FProtocol);
441 FTransport.Open;
442
Jens Geyer06045cf2013-03-27 20:26:25 +0200443 {$IFDEF StressTest}
444 StressTest( client);
445 {$ENDIF StressTest}
446
Jens Geyer17c3ad92017-09-05 20:31:27 +0200447 {$IFDEF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000448 // in-depth exception test
449 // (1) do we get an exception at all?
450 // (2) do we get the right exception?
451 // (3) does the exception contain the expected data?
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200452 StartTestGroup( 'testException', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000453 // case 1: exception type declared in IDL at the function call
454 try
455 client.testException('Xception');
456 Expect( FALSE, 'testException(''Xception''): must trow an exception');
457 except
458 on e:TXception do begin
459 Expect( e.ErrorCode = 1001, 'error code');
460 Expect( e.Message_ = 'Xception', 'error message');
461 Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ );
462 end;
463 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200464 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000465 end;
466
467 // case 2: exception type NOT declared in IDL at the function call
468 // this will close the connection
469 try
470 client.testException('TException');
471 Expect( FALSE, 'testException(''TException''): must trow an exception');
472 except
473 on e:TTransportException do begin
474 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Roger Meier3bef8c22012-10-06 06:58:00 +0000475 end;
Jens Geyer6bbbf192014-09-07 01:45:56 +0200476 on e:TApplicationException do begin
477 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Jens Geyer6bbbf192014-09-07 01:45:56 +0200478 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200479 on e:TException do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
480 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000481 end;
482
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100483
Jens Geyer2ad6c302015-02-26 19:38:53 +0100484 if FTransport.IsOpen then FTransport.Close;
485 FTransport.Open; // re-open connection, server has already closed
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100486
Jens Geyer2ad6c302015-02-26 19:38:53 +0100487
Roger Meier3bef8c22012-10-06 06:58:00 +0000488 // case 3: no exception
489 try
490 client.testException('something');
491 Expect( TRUE, 'testException(''something''): must not trow an exception');
492 except
493 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200494 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000495 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200496 {$ENDIF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000497
498
499 // simple things
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200500 StartTestGroup( 'simple Thrift calls', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000501 client.testVoid();
502 Expect( TRUE, 'testVoid()'); // success := no exception
503
Jens Geyer39ba6b72015-09-22 00:00:49 +0200504 s := BoolToString( client.testBool(TRUE));
505 Expect( s = BoolToString(TRUE), 'testBool(TRUE) = '+s);
506 s := BoolToString( client.testBool(FALSE));
507 Expect( s = BoolToString(FALSE), 'testBool(FALSE) = '+s);
508
Roger Meier3bef8c22012-10-06 06:58:00 +0000509 s := client.testString('Test');
510 Expect( s = 'Test', 'testString(''Test'') = "'+s+'"');
511
Jens Geyercf892d42017-09-09 10:08:22 +0200512 s := client.testString(''); // empty string
513 Expect( s = '', 'testString('''') = "'+s+'"');
514
Jens Geyer06045cf2013-03-27 20:26:25 +0200515 s := client.testString(HUGE_TEST_STRING);
516 Expect( length(s) = length(HUGE_TEST_STRING),
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100517 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
Jens Geyer06045cf2013-03-27 20:26:25 +0200518 +'=> length(result) = '+IntToStr(Length(s)));
519
Roger Meier3bef8c22012-10-06 06:58:00 +0000520 i8 := client.testByte(1);
521 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
522
523 i32 := client.testI32(-1);
524 Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32));
525
526 Console.WriteLine('testI64(-34359738368)');
527 i64 := client.testI64(-34359738368);
528 Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
529
Jens Geyerd4df9172017-10-25 22:30:23 +0200530 // random binary small
Jens Geyer85827152018-01-12 21:20:59 +0100531 for testsize := Low(TTestSize) to High(TTestSize) do begin
532 binOut := PrepareBinaryData( TRUE, testsize);
533 Console.WriteLine('testBinary('+BytesToHex(binOut)+')');
534 try
535 binIn := client.testBinary(binOut);
536 Expect( Length(binOut) = Length(binIn), 'testBinary(): length '+IntToStr(Length(binOut))+' = '+IntToStr(Length(binIn)));
537 i32 := Min( Length(binOut), Length(binIn));
538 Expect( CompareMem( binOut, binIn, i32), 'testBinary('+BytesToHex(binOut)+') = '+BytesToHex(binIn));
539 except
540 on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message);
541 on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message);
542 end;
Jens Geyercf892d42017-09-09 10:08:22 +0200543 end;
544
Roger Meier3bef8c22012-10-06 06:58:00 +0000545 Console.WriteLine('testDouble(5.325098235)');
546 dub := client.testDouble(5.325098235);
547 Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
548
549 // structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200550 StartTestGroup( 'testStruct', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000551 Console.WriteLine('testStruct({''Zero'', 1, -3, -5})');
552 o := TXtructImpl.Create;
553 o.String_thing := 'Zero';
554 o.Byte_thing := 1;
555 o.I32_thing := -3;
556 o.I64_thing := -5;
557 i := client.testStruct(o);
558 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
559 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
560 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
561 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
562 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
563 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
564 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
565 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
566
567 // nested structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200568 StartTestGroup( 'testNest', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000569 Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})');
570 o2 := TXtruct2Impl.Create;
571 o2.Byte_thing := 1;
572 o2.Struct_thing := o;
573 o2.I32_thing := 5;
574 i2 := client.testNest(o2);
575 i := i2.Struct_thing;
576 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
577 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
578 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
579 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
580 Expect( i2.Byte_thing = 1, 'i2.Byte_thing = '+IntToStr(i2.Byte_thing));
581 Expect( i2.I32_thing = 5, 'i2.I32_thing = '+IntToStr(i2.I32_thing));
582 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
583 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
584 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
585 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
586 Expect( i2.__isset_Byte_thing, 'i2.__isset_Byte_thing');
587 Expect( i2.__isset_I32_thing, 'i2.__isset_I32_thing');
588
589 // map<type1,type2>: A map of strictly unique keys to values.
590 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200591 StartTestGroup( 'testMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000592 mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
593 for j := 0 to 4 do
594 begin
595 mapout.AddOrSetValue( j, j - 10);
596 end;
597 Console.Write('testMap({');
598 first := True;
599 for key in mapout.Keys do
600 begin
601 if first
602 then first := False
603 else Console.Write( ', ' );
604 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
605 end;
606 Console.WriteLine('})');
607
608 mapin := client.testMap( mapout );
609 Expect( mapin.Count = mapout.Count, 'testMap: mapin.Count = mapout.Count');
610 for j := 0 to 4 do
611 begin
612 Expect( mapout.ContainsKey(j), 'testMap: mapout.ContainsKey('+IntToStr(j)+') = '+BoolToString(mapout.ContainsKey(j)));
613 end;
614 for key in mapin.Keys do
615 begin
616 Expect( mapin[key] = mapout[key], 'testMap: '+IntToStr(key) + ' => ' + IntToStr( mapin[key]));
617 Expect( mapin[key] = key - 10, 'testMap: mapin['+IntToStr(key)+'] = '+IntToStr( mapin[key]));
618 end;
619
620
621 // map<type1,type2>: A map of strictly unique keys to values.
622 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200623 StartTestGroup( 'testStringMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000624 strmapout := TThriftDictionaryImpl<string,string>.Create;
625 for j := 0 to 4 do
626 begin
627 strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10));
628 end;
629 Console.Write('testStringMap({');
630 first := True;
631 for strkey in strmapout.Keys do
632 begin
633 if first
634 then first := False
635 else Console.Write( ', ' );
636 Console.Write( strkey + ' => ' + strmapout[strkey]);
637 end;
638 Console.WriteLine('})');
639
640 strmapin := client.testStringMap( strmapout );
641 Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count');
642 for j := 0 to 4 do
643 begin
644 Expect( strmapout.ContainsKey(IntToStr(j)),
645 'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = '
646 + BoolToString(strmapout.ContainsKey(IntToStr(j))));
647 end;
648 for strkey in strmapin.Keys do
649 begin
650 Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]);
651 Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]);
652 end;
653
654
655 // set<type>: An unordered set of unique elements.
656 // Translates to an STL set, Java HashSet, set in Python, etc.
657 // Note: PHP does not support sets, so it is treated similar to a List
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200658 StartTestGroup( 'testSet', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000659 setout := THashSetImpl<Integer>.Create;
660 for j := -2 to 2 do
661 begin
662 setout.Add( j );
663 end;
664 Console.Write('testSet({');
665 first := True;
666 for j in setout do
667 begin
668 if first
669 then first := False
670 else Console.Write(', ');
671 Console.Write(IntToStr( j));
672 end;
673 Console.WriteLine('})');
674
675 setin := client.testSet(setout);
676 Expect( setin.Count = setout.Count, 'testSet: setin.Count = setout.Count');
677 Expect( setin.Count = 5, 'testSet: setin.Count = '+IntToStr(setin.Count));
678 for j := -2 to 2 do // unordered, we can't rely on the order => test for known elements only
679 begin
680 Expect( setin.Contains(j), 'testSet: setin.Contains('+IntToStr(j)+') => '+BoolToString(setin.Contains(j)));
681 end;
682
683 // list<type>: An ordered list of elements.
684 // Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200685 StartTestGroup( 'testList', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000686 listout := TThriftListImpl<Integer>.Create;
687 listout.Add( +1);
688 listout.Add( -2);
689 listout.Add( +3);
690 listout.Add( -4);
691 listout.Add( 0);
692 Console.Write('testList({');
693 first := True;
694 for j in listout do
695 begin
696 if first
697 then first := False
698 else Console.Write(', ');
699 Console.Write(IntToStr( j));
700 end;
701 Console.WriteLine('})');
702
703 listin := client.testList(listout);
704 Expect( listin.Count = listout.Count, 'testList: listin.Count = listout.Count');
705 Expect( listin.Count = 5, 'testList: listin.Count = '+IntToStr(listin.Count));
706 Expect( listin[0] = +1, 'listin[0] = '+IntToStr( listin[0]));
707 Expect( listin[1] = -2, 'listin[1] = '+IntToStr( listin[1]));
708 Expect( listin[2] = +3, 'listin[2] = '+IntToStr( listin[2]));
709 Expect( listin[3] = -4, 'listin[3] = '+IntToStr( listin[3]));
710 Expect( listin[4] = 0, 'listin[4] = '+IntToStr( listin[4]));
711
712 // enums
713 ret := client.testEnum(TNumberz.ONE);
714 Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret)));
715
716 ret := client.testEnum(TNumberz.TWO);
717 Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret)));
718
719 ret := client.testEnum(TNumberz.THREE);
720 Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret)));
721
722 ret := client.testEnum(TNumberz.FIVE);
723 Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret)));
724
725 ret := client.testEnum(TNumberz.EIGHT);
726 Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret)));
727
728
729 // typedef
730 uid := client.testTypedef(309858235082523);
731 Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid));
732
733
734 // maps of maps
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200735 StartTestGroup( 'testMapMap(1)', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000736 mm := client.testMapMap(1);
737 Console.Write(' = {');
738 for key in mm.Keys do
739 begin
740 Console.Write( IntToStr( key) + ' => {');
741 m2 := mm[key];
742 for k2 in m2.Keys do
743 begin
744 Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', ');
745 end;
746 Console.Write('}, ');
747 end;
748 Console.WriteLine('}');
749
750 // verify result data
751 Expect( mm.Count = 2, 'mm.Count = '+IntToStr(mm.Count));
752 pos := mm[4];
753 neg := mm[-4];
754 for j := 1 to 4 do
755 begin
756 Expect( pos[j] = j, 'pos[j] = '+IntToStr(pos[j]));
757 Expect( neg[-j] = -j, 'neg[-j] = '+IntToStr(neg[-j]));
758 end;
759
760
761
762 // insanity
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200763 StartTestGroup( 'testInsanity', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000764 insane := TInsanityImpl.Create;
765 insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
766 insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000);
767 truck := TXtructImpl.Create;
768 truck.String_thing := 'Truck';
Jens Geyer540e3462016-12-28 14:25:41 +0100769 truck.Byte_thing := -8; // byte is signed
770 truck.I32_thing := 32;
771 truck.I64_thing := 64;
Roger Meier3bef8c22012-10-06 06:58:00 +0000772 insane.Xtructs := TThriftListImpl<IXtruct>.Create;
773 insane.Xtructs.Add( truck );
774 whoa := client.testInsanity( insane );
775 Console.Write(' = {');
776 for key64 in whoa.Keys do
777 begin
778 val := whoa[key64];
779 Console.Write( IntToStr( key64) + ' => {');
780 for k2_2 in val.Keys do
781 begin
782 v2 := val[k2_2];
783 Console.Write( IntToStr( Integer( k2_2)) + ' => {');
784 userMap := v2.UserMap;
785 Console.Write('{');
786 if userMap <> nil then
787 begin
788 for k3 in userMap.Keys do
789 begin
790 Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', ');
791 end;
792 end else
793 begin
794 Console.Write('null');
795 end;
796 Console.Write('}, ');
797 xtructs := v2.Xtructs;
798 Console.Write('{');
799
800 if xtructs <> nil then
801 begin
802 for x in xtructs do
803 begin
804 Console.Write('{"' + x.String_thing + '", ' +
805 IntToStr( x.Byte_thing) + ', ' +
806 IntToStr( x.I32_thing) + ', ' +
807 IntToStr( x.I32_thing) + '}, ');
808 end;
809 end else
810 begin
811 Console.Write('null');
812 end;
813 Console.Write('}');
814 Console.Write('}, ');
815 end;
816 Console.Write('}, ');
817 end;
818 Console.WriteLine('}');
819
Jens Geyer540e3462016-12-28 14:25:41 +0100820 (**
821 * So you think you've got this all worked, out eh?
822 *
823 * Creates a the returned map with these values and prints it out:
824 * { 1 => { 2 => argument,
825 * 3 => argument,
826 * },
827 * 2 => { 6 => <empty Insanity struct>, },
828 * }
829 * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
830 *)
831
Roger Meier3bef8c22012-10-06 06:58:00 +0000832 // verify result data
833 Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count));
834 //
835 first_map := whoa[1];
836 second_map := whoa[2];
837 Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count));
838 Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count));
839 //
840 looney := second_map[TNumberz.SIX];
841 Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney)));
842 Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
843 Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
844 //
845 for ret in [TNumberz.TWO, TNumberz.THREE] do begin
846 crazy := first_map[ret];
847 Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
848
849 Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap));
850 Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs));
851
Jens Geyer540e3462016-12-28 14:25:41 +0100852 Expect( crazy.UserMap.Count = insane.UserMap.Count, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count));
853 for pair in insane.UserMap do begin
854 Expect( crazy.UserMap[pair.Key] = pair.Value, 'crazy.UserMap['+IntToStr(Ord(pair.key))+'] = '+IntToStr(crazy.UserMap[pair.Key]));
855 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000856
Jens Geyer540e3462016-12-28 14:25:41 +0100857 Expect( crazy.Xtructs.Count = insane.Xtructs.Count, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count));
858 for arg0 := 0 to insane.Xtructs.Count-1 do begin
859 hello := insane.Xtructs[arg0];
860 goodbye := crazy.Xtructs[arg0];
861 Expect( goodbye.String_thing = hello.String_thing, 'goodbye.String_thing = '+goodbye.String_thing);
862 Expect( goodbye.Byte_thing = hello.Byte_thing, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing));
863 Expect( goodbye.I32_thing = hello.I32_thing, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing));
864 Expect( goodbye.I64_thing = hello.I64_thing, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing));
865 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000866 end;
867
868
869 // multi args
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200870 StartTestGroup( 'testMulti', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000871 arg0 := 1;
872 arg1 := 2;
873 arg2 := High(Int64);
874 arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
875 arg3.AddOrSetValue( 1, 'one');
876 arg4 := TNumberz.FIVE;
877 arg5 := 5000000;
878 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
879 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
880 arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
881 IntToStr( arg5) + ')');
882
883 i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5);
884 Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"');
885 Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing));
886 Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing));
887 Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing));
888 Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
889 Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
890 Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
891 Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
892
893 // multi exception
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200894 StartTestGroup( 'testMultiException(1)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000895 try
896 i := client.testMultiException( 'need more pizza', 'run out of beer');
897 Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"');
898 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
Jens Geyer6bbbf192014-09-07 01:45:56 +0200899 { this is not necessarily true, these fields are default-serialized
Jens Geyerd5436f52014-10-03 19:50:38 +0200900 Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
Roger Meier3bef8c22012-10-06 06:58:00 +0000901 Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
902 Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
Jens Geyerd5436f52014-10-03 19:50:38 +0200903 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000904 except
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200905 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000906 end;
907
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200908 StartTestGroup( 'testMultiException(Xception)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000909 try
910 i := client.testMultiException( 'Xception', 'second test');
911 Expect( FALSE, 'testMultiException(''Xception''): must trow an exception');
912 except
913 on x:TXception do begin
914 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
915 Expect( x.__isset_Message_, 'x.__isset_Message_ = '+BoolToString(x.__isset_Message_));
916 Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
917 Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"');
918 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200919 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000920 end;
921
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200922 StartTestGroup( 'testMultiException(Xception2)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000923 try
924 i := client.testMultiException( 'Xception2', 'third test');
925 Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception');
926 except
927 on x:TXception2 do begin
928 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
929 Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing));
930 Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
931 Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"');
932 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 +0200933 { this is not necessarily true, these fields are default-serialized
Roger Meier3bef8c22012-10-06 06:58:00 +0000934 Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing));
935 Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing));
936 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 +0200937 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000938 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200939 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000940 end;
941
942
943 // oneway functions
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200944 StartTestGroup( 'Test Oneway(1)', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000945 client.testOneway(1);
946 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
947
948 // call time
Jens Geyer06045cf2013-03-27 20:26:25 +0200949 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000950 StartTestGroup( 'Test Calltime()');
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200951 StartTick := GetTickCount;
Roger Meier3bef8c22012-10-06 06:58:00 +0000952 for k := 0 to 1000 - 1 do
953 begin
954 client.testVoid();
955 end;
956 Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
Jens Geyer06045cf2013-03-27 20:26:25 +0200957 {$ENDIF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000958
959 // no more tests here
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200960 StartTestGroup( '', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000961end;
962
963
Jens Geyer14f5d502017-12-09 13:47:09 +0100964{$IFDEF SupportsAsync}
965procedure TClientThread.ClientAsyncTest;
966var
967 client : TThriftTest.IAsync;
968 s : string;
969 i8 : ShortInt;
970begin
971 StartTestGroup( 'Async Tests', test_Unknown);
972 client := TThriftTest.TClient.Create( FProtocol);
973 FTransport.Open;
974
975 // oneway void functions
976 client.testOnewayAsync(1).Wait;
977 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
978
979 // normal functions
980 s := client.testStringAsync(HUGE_TEST_STRING).Value;
981 Expect( length(s) = length(HUGE_TEST_STRING),
982 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
983 +'=> length(result) = '+IntToStr(Length(s)));
984
985 i8 := client.testByte(1).Value;
986 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
987end;
988{$ENDIF}
989
990
Jens Geyer718f6ee2013-09-06 21:02:34 +0200991{$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200992procedure TClientThread.StressTest(const client : TThriftTest.Iface);
993begin
994 while TRUE do begin
995 try
996 if not FTransport.IsOpen then FTransport.Open; // re-open connection, server has already closed
997 try
998 client.testString('Test');
999 Write('.');
1000 finally
1001 if FTransport.IsOpen then FTransport.Close;
1002 end;
1003 except
1004 on e:Exception do Writeln(#10+e.message);
1005 end;
1006 end;
1007end;
Jens Geyer718f6ee2013-09-06 21:02:34 +02001008{$ENDIF}
Jens Geyer06045cf2013-03-27 20:26:25 +02001009
Jens Geyerfd1b3582014-12-13 23:42:58 +01001010
Jens Geyer85827152018-01-12 21:20:59 +01001011function TClientThread.PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes;
Jens Geyerd4df9172017-10-25 22:30:23 +02001012var i : Integer;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001013begin
Jens Geyer85827152018-01-12 21:20:59 +01001014 case aSize of
1015 Empty : SetLength( result, 0);
1016 Normal : SetLength( result, $100);
1017 ByteArrayTest : SetLength( result, SizeOf(TByteArray) + 128);
1018 PipeWriteLimit : SetLength( result, 65535 + 128);
1019 else
1020 raise EArgumentException.Create('aSize');
1021 end;
1022
Jens Geyerfd1b3582014-12-13 23:42:58 +01001023 ASSERT( Low(result) = 0);
Jens Geyer85827152018-01-12 21:20:59 +01001024 if Length(result) = 0 then Exit;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001025
1026 // linear distribution, unless random is requested
1027 if not aRandomDist then begin
1028 for i := Low(result) to High(result) do begin
Jens Geyer85827152018-01-12 21:20:59 +01001029 result[i] := i mod $100;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001030 end;
1031 Exit;
1032 end;
1033
1034 // random distribution of all 256 values
1035 FillChar( result[0], Length(result) * SizeOf(result[0]), $0);
Jens Geyerd4df9172017-10-25 22:30:23 +02001036 for i := Low(result) to High(result) do begin
1037 result[i] := Byte( Random($100));
Jens Geyerfd1b3582014-12-13 23:42:58 +01001038 end;
1039end;
1040
1041
Jens Geyerf7904452017-07-26 15:02:12 +02001042{$IFDEF Win64}
1043procedure TClientThread.UseInterlockedExchangeAdd64;
1044var a,b : Int64;
1045begin
1046 a := 1;
1047 b := 2;
1048 Thrift.Utils.InterlockedExchangeAdd64( a,b);
1049 Expect( a = 3, 'InterlockedExchangeAdd64');
1050end;
1051{$ENDIF}
1052
1053
Roger Meier3bef8c22012-10-06 06:58:00 +00001054procedure TClientThread.JSONProtocolReadWriteTest;
1055// Tests only then read/write procedures of the JSON protocol
1056// All tests succeed, if we can read what we wrote before
1057// Note that passing this test does not imply, that our JSON is really compatible to what
1058// other clients or servers expect as the real JSON. This is beyond the scope of this test.
1059var prot : IProtocol;
1060 stm : TStringStream;
Jens Geyer17c3ad92017-09-05 20:31:27 +02001061 list : TThriftList;
Jens Geyercf892d42017-09-09 10:08:22 +02001062 binary, binRead, emptyBinary : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +00001063 i,iErr : Integer;
1064const
1065 TEST_SHORT = ShortInt( $FE);
1066 TEST_SMALL = SmallInt( $FEDC);
1067 TEST_LONG = LongInt( $FEDCBA98);
1068 TEST_I64 = Int64( $FEDCBA9876543210);
1069 TEST_DOUBLE = -1.234e-56;
1070 DELTA_DOUBLE = TEST_DOUBLE * 1e-14;
1071 TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars")
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001072 // Test THRIFT-2336 and THRIFT-3404 with U+1D11E (G Clef symbol) and 'Русское Название';
1073 G_CLEF_AND_CYRILLIC_TEXT = #$1d11e' '#$0420#$0443#$0441#$0441#$043a#$043e#$0435' '#$041d#$0430#$0437#$0432#$0430#$043d#$0438#$0435;
1074 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 +01001075 // test both possible solidus encodings
1076 SOLIDUS_JSON_DATA = '"one/two\/three"';
1077 SOLIDUS_EXCPECTED = 'one/two/three';
Roger Meier3bef8c22012-10-06 06:58:00 +00001078begin
1079 stm := TStringStream.Create;
1080 try
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001081 StartTestGroup( 'JsonProtocolTest', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001082
1083 // prepare binary data
Jens Geyer85827152018-01-12 21:20:59 +01001084 binary := PrepareBinaryData( FALSE, Normal);
Jens Geyercf892d42017-09-09 10:08:22 +02001085 SetLength( emptyBinary, 0); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001086
1087 // output setup
1088 prot := TJSONProtocolImpl.Create(
1089 TStreamTransportImpl.Create(
1090 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
1091
1092 // write
Jens Geyer17c3ad92017-09-05 20:31:27 +02001093 Init( list, TType.String_, 9);
1094 prot.WriteListBegin( list);
Roger Meier3bef8c22012-10-06 06:58:00 +00001095 prot.WriteBool( TRUE);
1096 prot.WriteBool( FALSE);
1097 prot.WriteByte( TEST_SHORT);
1098 prot.WriteI16( TEST_SMALL);
1099 prot.WriteI32( TEST_LONG);
1100 prot.WriteI64( TEST_I64);
1101 prot.WriteDouble( TEST_DOUBLE);
1102 prot.WriteString( TEST_STRING);
1103 prot.WriteBinary( binary);
Jens Geyercf892d42017-09-09 10:08:22 +02001104 prot.WriteString( ''); // empty string
1105 prot.WriteBinary( emptyBinary); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001106 prot.WriteListEnd;
1107
1108 // input setup
1109 Expect( stm.Position = stm.Size, 'Stream position/length after write');
1110 stm.Position := 0;
1111 prot := TJSONProtocolImpl.Create(
1112 TStreamTransportImpl.Create(
1113 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1114
1115 // read and compare
1116 list := prot.ReadListBegin;
1117 Expect( list.ElementType = TType.String_, 'list element type');
1118 Expect( list.Count = 9, 'list element count');
1119 Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE');
1120 Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE');
1121 Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte');
1122 Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16');
1123 Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32');
1124 Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64');
1125 Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble');
1126 Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString');
1127 binRead := prot.ReadBinary;
Jens Geyercf892d42017-09-09 10:08:22 +02001128 Expect( Length(prot.ReadString) = 0, 'WriteString/ReadString (empty string)');
1129 Expect( Length(prot.ReadBinary) = 0, 'empty WriteBinary/ReadBinary (empty data block)');
Roger Meier3bef8c22012-10-06 06:58:00 +00001130 prot.ReadListEnd;
1131
1132 // test binary data
1133 Expect( Length(binary) = Length(binRead), 'Binary data length check');
1134 iErr := -1;
1135 for i := Low(binary) to High(binary) do begin
1136 if binary[i] <> binRead[i] then begin
1137 iErr := i;
1138 Break;
1139 end;
1140 end;
1141 if iErr < 0
1142 then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)')
1143 else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr));
1144
1145 Expect( stm.Position = stm.Size, 'Stream position after read');
1146
Jens Geyer7bb44a32014-02-07 22:24:37 +01001147
Jens Geyer21366942013-12-30 22:04:51 +01001148 // Solidus can be encoded in two ways. Make sure we can read both
1149 stm.Position := 0;
1150 stm.Size := 0;
1151 stm.WriteString(SOLIDUS_JSON_DATA);
1152 stm.Position := 0;
1153 prot := TJSONProtocolImpl.Create(
1154 TStreamTransportImpl.Create(
1155 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1156 Expect( prot.ReadString = SOLIDUS_EXCPECTED, 'Solidus encoding');
1157
1158
Jens Geyer7bb44a32014-02-07 22:24:37 +01001159 // Widechars should work too. Do they?
1160 // After writing, we ensure that we are able to read it back
1161 // We can't assume hex-encoding, since (nearly) any Unicode char is valid JSON
1162 stm.Position := 0;
1163 stm.Size := 0;
1164 prot := TJSONProtocolImpl.Create(
1165 TStreamTransportImpl.Create(
1166 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001167 prot.WriteString( G_CLEF_AND_CYRILLIC_TEXT);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001168 stm.Position := 0;
1169 prot := TJSONProtocolImpl.Create(
1170 TStreamTransportImpl.Create(
1171 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001172 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Writing JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001173
1174 // Widechars should work with hex-encoding too. Do they?
1175 stm.Position := 0;
1176 stm.Size := 0;
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001177 stm.WriteString( G_CLEF_AND_CYRILLIC_JSON);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001178 stm.Position := 0;
1179 prot := TJSONProtocolImpl.Create(
1180 TStreamTransportImpl.Create(
1181 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001182 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Reading JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001183
1184
Roger Meier3bef8c22012-10-06 06:58:00 +00001185 finally
1186 stm.Free;
1187 prot := nil; //-> Release
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001188 StartTestGroup( '', test_Unknown); // no more tests here
Roger Meier3bef8c22012-10-06 06:58:00 +00001189 end;
1190end;
1191
1192
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001193procedure TClientThread.StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +00001194begin
1195 FTestGroup := aGroup;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001196 FCurrentTest := aTest;
1197
1198 Include( FExecuted, aTest);
1199
Roger Meier3bef8c22012-10-06 06:58:00 +00001200 if FTestGroup <> '' then begin
1201 Console.WriteLine('');
1202 Console.WriteLine( aGroup+' tests');
1203 Console.WriteLine( StringOfChar('-',60));
1204 end;
1205end;
1206
1207
1208procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
1209begin
1210 if aTestResult then begin
1211 Inc(FSuccesses);
1212 Console.WriteLine( aTestInfo+': passed');
1213 end
1214 else begin
1215 FErrors.Add( FTestGroup+': '+aTestInfo);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001216 Include( FFailed, FCurrentTest);
Roger Meier3bef8c22012-10-06 06:58:00 +00001217 Console.WriteLine( aTestInfo+': *** FAILED ***');
1218
1219 // We have a failed test!
1220 // -> issue DebugBreak ONLY if a debugger is attached,
1221 // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise
Jens Geyer9f7f11e2016-04-14 21:37:11 +02001222 if IsDebuggerPresent
1223 then {$IFDEF CPUX64} DebugBreak {$ELSE} asm int 3 end {$ENDIF};
Roger Meier3bef8c22012-10-06 06:58:00 +00001224 end;
1225end;
1226
1227
1228procedure TClientThread.ReportResults;
1229var nTotal : Integer;
1230 sLine : string;
1231begin
1232 // prevent us from stupid DIV/0 errors
1233 nTotal := FSuccesses + FErrors.Count;
1234 if nTotal = 0 then begin
1235 Console.WriteLine('No results logged');
1236 Exit;
1237 end;
1238
1239 Console.WriteLine('');
1240 Console.WriteLine( StringOfChar('=',60));
1241 Console.WriteLine( IntToStr(nTotal)+' tests performed');
1242 Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)');
1243 Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)');
1244 Console.WriteLine( StringOfChar('=',60));
1245 if FErrors.Count > 0 then begin
1246 Console.WriteLine('FAILED TESTS:');
1247 for sLine in FErrors do Console.WriteLine('- '+sLine);
1248 Console.WriteLine( StringOfChar('=',60));
1249 InterlockedIncrement( ExitCode); // return <> 0 on errors
1250 end;
1251 Console.WriteLine('');
1252end;
1253
1254
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001255function TClientThread.CalculateExitCode : Byte;
1256var test : TTestGroup;
1257begin
1258 result := EXITCODE_SUCCESS;
1259 for test := Low(TTestGroup) to High(TTestGroup) do begin
1260 if (test in FFailed) or not (test in FExecuted)
1261 then result := result or MAP_FAILURES_TO_EXITCODE_BITS[test];
1262 end;
1263end;
1264
1265
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001266constructor TClientThread.Create( const aSetup : TTestSetup; const aNumIteration: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +00001267begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001268 FSetup := aSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +00001269 FNumIteration := ANumIteration;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001270
Roger Meier3bef8c22012-10-06 06:58:00 +00001271 FConsole := TThreadConsole.Create( Self );
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001272 FCurrentTest := test_Unknown;
Roger Meier3bef8c22012-10-06 06:58:00 +00001273
1274 // error list: keep correct order, allow for duplicates
1275 FErrors := TStringList.Create;
1276 FErrors.Sorted := FALSE;
1277 FErrors.Duplicates := dupAccept;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001278
1279 inherited Create( TRUE);
Roger Meier3bef8c22012-10-06 06:58:00 +00001280end;
1281
1282destructor TClientThread.Destroy;
1283begin
1284 FreeAndNil( FConsole);
1285 FreeAndNil( FErrors);
1286 inherited;
1287end;
1288
1289procedure TClientThread.Execute;
1290var
1291 i : Integer;
Roger Meier3bef8c22012-10-06 06:58:00 +00001292begin
1293 // perform all tests
1294 try
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001295 {$IFDEF Win64}
Jens Geyerf7904452017-07-26 15:02:12 +02001296 UseInterlockedExchangeAdd64;
Jens Geyer14f5d502017-12-09 13:47:09 +01001297 {$ENDIF}
Jens Geyer7bb44a32014-02-07 22:24:37 +01001298 JSONProtocolReadWriteTest;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001299
1300 // must be run in the context of the thread
1301 InitializeProtocolTransportStack;
1302 try
1303 for i := 0 to FNumIteration - 1 do begin
1304 ClientTest;
1305 {$IFDEF SupportsAsync}
1306 ClientAsyncTest;
1307 {$ENDIF}
1308 end;
1309
1310 // report the outcome
1311 ReportResults;
1312 SetReturnValue( CalculateExitCode);
1313
1314 finally
1315 ShutdownProtocolTransportStack;
Roger Meier3bef8c22012-10-06 06:58:00 +00001316 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001317
Roger Meier3bef8c22012-10-06 06:58:00 +00001318 except
1319 on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"');
1320 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001321end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001322
Roger Meier3bef8c22012-10-06 06:58:00 +00001323
Jens Geyer02230912019-04-03 01:12:51 +02001324function TClientThread.InitializeHttpTransport( const aTimeoutSetting : Integer) : IHTTPClient;
1325var sUrl : string;
1326begin
1327 ASSERT( FSetup.endpoint in [trns_MsxmlHttp, trns_WinHttp]);
1328
1329 if FSetup.useSSL
1330 then sUrl := 'https://'
1331 else sUrl := 'http://';
1332
1333 sUrl := sUrl + FSetup.host;
1334
1335 case FSetup.port of
1336 80 : if FSetup.useSSL then sUrl := sUrl + ':'+ IntToStr(FSetup.port);
1337 443 : if not FSetup.useSSL then sUrl := sUrl + ':'+ IntToStr(FSetup.port);
1338 else
1339 if FSetup.port > 0 then sUrl := sUrl + ':'+ IntToStr(FSetup.port);
1340 end;
1341
1342 Console.WriteLine('Target URL: '+sUrl);
1343 case FSetup.endpoint of
1344 trns_MsxmlHttp : result := TMsxmlHTTPClientImpl.Create( sUrl);
1345 trns_WinHttp : result := TWinHTTPClientImpl.Create( sUrl);
1346 else
1347 raise Exception.Create(ENDPOINT_TRANSPORTS[FSetup.endpoint]+' unhandled case');
1348 end;
1349
1350 result.DnsResolveTimeout := aTimeoutSetting;
1351 result.ConnectionTimeout := aTimeoutSetting;
1352 result.SendTimeout := aTimeoutSetting;
1353 result.ReadTimeout := aTimeoutSetting;
1354end;
1355
1356
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001357procedure TClientThread.InitializeProtocolTransportStack;
Jens Geyer02230912019-04-03 01:12:51 +02001358var streamtrans : IStreamTransport;
Jens Geyer47f63172019-06-06 22:42:58 +02001359 canSSL : Boolean;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001360const
1361 DEBUG_TIMEOUT = 30 * 1000;
1362 RELEASE_TIMEOUT = DEFAULT_THRIFT_TIMEOUT;
1363 PIPE_TIMEOUT = RELEASE_TIMEOUT;
1364 HTTP_TIMEOUTS = 10 * 1000;
1365begin
1366 // needed for HTTP clients as they utilize the MSXML COM components
1367 OleCheck( CoInitialize( nil));
1368
Jens Geyer47f63172019-06-06 22:42:58 +02001369 canSSL := FALSE;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001370 case FSetup.endpoint of
1371 trns_Sockets: begin
1372 Console.WriteLine('Using sockets ('+FSetup.host+' port '+IntToStr(FSetup.port)+')');
1373 streamtrans := TSocketImpl.Create( FSetup.host, FSetup.port );
1374 FTransport := streamtrans;
1375 end;
1376
Jens Geyer02230912019-04-03 01:12:51 +02001377 trns_MsxmlHttp,
1378 trns_WinHttp: begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001379 Console.WriteLine('Using HTTPClient');
Jens Geyer02230912019-04-03 01:12:51 +02001380 FTransport := InitializeHttpTransport( HTTP_TIMEOUTS);
Jens Geyer47f63172019-06-06 22:42:58 +02001381 canSSL := TRUE;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001382 end;
1383
1384 trns_EvHttp: begin
1385 raise Exception.Create(ENDPOINT_TRANSPORTS[FSetup.endpoint]+' transport not implemented');
1386 end;
1387
1388 trns_NamedPipes: begin
1389 streamtrans := TNamedPipeTransportClientEndImpl.Create( FSetup.sPipeName, 0, nil, PIPE_TIMEOUT, PIPE_TIMEOUT);
1390 FTransport := streamtrans;
1391 end;
1392
1393 trns_AnonPipes: begin
1394 streamtrans := TAnonymousPipeTransportImpl.Create( FSetup.hAnonRead, FSetup.hAnonWrite, FALSE);
1395 FTransport := streamtrans;
1396 end;
1397
1398 else
1399 raise Exception.Create('Unhandled endpoint transport');
1400 end;
1401 ASSERT( FTransport <> nil);
1402
1403 // layered transports are not really meant to be stacked upon each other
1404 if (trns_Framed in FSetup.layered) then begin
1405 FTransport := TFramedTransportImpl.Create( FTransport);
1406 end
1407 else if (trns_Buffered in FSetup.layered) and (streamtrans <> nil) then begin
1408 FTransport := TBufferedTransportImpl.Create( streamtrans, 32); // small buffer to test read()
1409 end;
1410
Jens Geyer47f63172019-06-06 22:42:58 +02001411 if FSetup.useSSL and not canSSL then begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001412 raise Exception.Create('SSL/TLS not implemented');
1413 end;
1414
1415 // create protocol instance, default to BinaryProtocol
1416 case FSetup.protType of
1417 prot_Binary : FProtocol := TBinaryProtocolImpl.Create( FTransport, BINARY_STRICT_READ, BINARY_STRICT_WRITE);
1418 prot_JSON : FProtocol := TJSONProtocolImpl.Create( FTransport);
1419 prot_Compact : FProtocol := TCompactProtocolImpl.Create( FTransport);
1420 else
1421 raise Exception.Create('Unhandled protocol');
1422 end;
1423
1424 ASSERT( (FTransport <> nil) and (FProtocol <> nil));
1425end;
1426
1427
1428procedure TClientThread.ShutdownProtocolTransportStack;
1429begin
1430 try
1431 FProtocol := nil;
1432
1433 if FTransport <> nil then begin
Roger Meier3bef8c22012-10-06 06:58:00 +00001434 FTransport.Close;
1435 FTransport := nil;
1436 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001437
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001438 finally
1439 CoUninitialize;
1440 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001441end;
1442
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001443
Roger Meier3bef8c22012-10-06 06:58:00 +00001444{ TThreadConsole }
1445
1446constructor TThreadConsole.Create(AThread: TThread);
1447begin
Jens Geyer718f6ee2013-09-06 21:02:34 +02001448 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +00001449 FThread := AThread;
1450end;
1451
1452procedure TThreadConsole.Write(const S: string);
1453var
1454 proc : TThreadProcedure;
1455begin
1456 proc := procedure
1457 begin
1458 Console.Write( S );
1459 end;
1460 TThread.Synchronize( FThread, proc);
1461end;
1462
1463procedure TThreadConsole.WriteLine(const S: string);
1464var
1465 proc : TThreadProcedure;
1466begin
1467 proc := procedure
1468 begin
1469 Console.WriteLine( S );
1470 end;
1471 TThread.Synchronize( FThread, proc);
1472end;
1473
1474initialization
1475begin
1476 TTestClient.FNumIteration := 1;
1477 TTestClient.FNumThread := 1;
1478end;
1479
1480end.