blob: c2660a26dc256f405bce2ce5ca11f6a3a95da8f7 [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,
Jens Geyerb342bd92019-06-03 20:27:00 +020043 PerfTests,
Roger Meier3bef8c22012-10-06 06:58:00 +000044 Thrift,
Jens Geyerf0e63312015-03-01 18:47:49 +010045 Thrift.Protocol.Compact,
Roger Meier3bef8c22012-10-06 06:58:00 +000046 Thrift.Protocol.JSON,
47 Thrift.Protocol,
48 Thrift.Transport.Pipes,
Jens Geyer02230912019-04-03 01:12:51 +020049 Thrift.Transport.WinHTTP,
50 Thrift.Transport.MsxmlHTTP,
Roger Meier3bef8c22012-10-06 06:58:00 +000051 Thrift.Transport,
52 Thrift.Stream,
53 Thrift.Test,
Jens Geyer83ff7532019-06-06 22:46:03 +020054 Thrift.WinHTTP,
Jens Geyerf7904452017-07-26 15:02:12 +020055 Thrift.Utils,
Jens Geyer3d556242018-01-24 19:14:32 +010056 Thrift.Collections;
Roger Meier3bef8c22012-10-06 06:58:00 +000057
58type
59 TThreadConsole = class
60 private
61 FThread : TThread;
62 public
63 procedure Write( const S : string);
64 procedure WriteLine( const S : string);
65 constructor Create( AThread: TThread);
66 end;
67
Jens Geyeraf7ecd62018-06-22 22:41:27 +020068 TTestSetup = record
69 protType : TKnownProtocol;
70 endpoint : TEndpointTransport;
71 layered : TLayeredTransports;
72 useSSL : Boolean; // include where appropriate (TLayeredTransport?)
73 host : string;
74 port : Integer;
75 sPipeName : string;
76 hAnonRead, hAnonWrite : THandle;
77 end;
78
Roger Meier3bef8c22012-10-06 06:58:00 +000079 TClientThread = class( TThread )
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020080 private type
81 TTestGroup = (
82 test_Unknown,
83 test_BaseTypes,
84 test_Structs,
85 test_Containers,
86 test_Exceptions
87 // new values here
88 );
89 TTestGroups = set of TTestGroup;
90
Jens Geyer85827152018-01-12 21:20:59 +010091 TTestSize = (
92 Empty, // Edge case: the zero-length empty binary
93 Normal, // Fairly small array of usual size (256 bytes)
94 ByteArrayTest, // THRIFT-4454 Large writes/reads may cause range check errors in debug mode
95 PipeWriteLimit // THRIFT-4372 Pipe write operations across a network are limited to 65,535 bytes per write.
96 );
97
Roger Meier3bef8c22012-10-06 06:58:00 +000098 private
Jens Geyeraf7ecd62018-06-22 22:41:27 +020099 FSetup : TTestSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +0000100 FTransport : ITransport;
101 FProtocol : IProtocol;
102 FNumIteration : Integer;
103 FConsole : TThreadConsole;
104
105 // test reporting, will be refactored out into separate class later
106 FTestGroup : string;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200107 FCurrentTest : TTestGroup;
Roger Meier3bef8c22012-10-06 06:58:00 +0000108 FSuccesses : Integer;
109 FErrors : TStringList;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200110 FFailed : TTestGroups;
111 FExecuted : TTestGroups;
112 procedure StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +0000113 procedure Expect( aTestResult : Boolean; const aTestInfo : string);
114 procedure ReportResults;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100115 function CalculateExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000116
117 procedure ClientTest;
Jens Geyer14f5d502017-12-09 13:47:09 +0100118 {$IFDEF SupportsAsync}
119 procedure ClientAsyncTest;
120 {$ENDIF}
121
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200122 procedure InitializeProtocolTransportStack;
123 procedure ShutdownProtocolTransportStack;
Jens Geyer02230912019-04-03 01:12:51 +0200124 function InitializeHttpTransport( const aTimeoutSetting : Integer) : IHTTPClient;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200125
Roger Meier3bef8c22012-10-06 06:58:00 +0000126 procedure JSONProtocolReadWriteTest;
Jens Geyer85827152018-01-12 21:20:59 +0100127 function PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes;
Jens Geyer718f6ee2013-09-06 21:02:34 +0200128 {$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200129 procedure StressTest(const client : TThriftTest.Iface);
Jens Geyer718f6ee2013-09-06 21:02:34 +0200130 {$ENDIF}
Jens Geyerf7904452017-07-26 15:02:12 +0200131 {$IFDEF Win64}
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200132 procedure UseInterlockedExchangeAdd64;
Jens Geyerf7904452017-07-26 15:02:12 +0200133 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000134 protected
135 procedure Execute; override;
136 public
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200137 constructor Create( const aSetup : TTestSetup; const aNumIteration: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +0000138 destructor Destroy; override;
139 end;
140
141 TTestClient = class
142 private
143 class var
144 FNumIteration : Integer;
145 FNumThread : Integer;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200146
147 class procedure PrintCmdLineHelp;
148 class procedure InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000149 public
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200150 class function Execute( const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000151 end;
152
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200153
Roger Meier3bef8c22012-10-06 06:58:00 +0000154implementation
155
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200156const
157 EXITCODE_SUCCESS = $00; // no errors bits set
158 //
159 EXITCODE_FAILBIT_BASETYPES = $01;
160 EXITCODE_FAILBIT_STRUCTS = $02;
161 EXITCODE_FAILBIT_CONTAINERS = $04;
162 EXITCODE_FAILBIT_EXCEPTIONS = $08;
163
164 MAP_FAILURES_TO_EXITCODE_BITS : array[TClientThread.TTestGroup] of Byte = (
165 EXITCODE_SUCCESS, // no bits here
166 EXITCODE_FAILBIT_BASETYPES,
167 EXITCODE_FAILBIT_STRUCTS,
168 EXITCODE_FAILBIT_CONTAINERS,
169 EXITCODE_FAILBIT_EXCEPTIONS
170 );
171
172
173
Roger Meier3bef8c22012-10-06 06:58:00 +0000174function BoolToString( b : Boolean) : string;
175// overrides global BoolToString()
176begin
177 if b
178 then result := 'true'
179 else result := 'false';
180end;
181
182// not available in all versions, so make sure we have this one imported
183function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent';
184
185{ TTestClient }
186
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200187class procedure TTestClient.PrintCmdLineHelp;
188const HELPTEXT = ' [options]'#10
189 + #10
190 + 'Allowed options:'#10
191 + ' -h [ --help ] produce help message'#10
192 + ' --host arg (=localhost) Host to connect'#10
193 + ' --port arg (=9090) Port number to connect'#10
194 + ' --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift),'#10
195 + ' instead of host and port'#10
196 + ' --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)'#10
197 + ' --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)'#10
Jens Geyer02230912019-04-03 01:12:51 +0200198 + ' --transport arg (=sockets) Transport: buffered, framed, http, winhttp'#10
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200199 + ' --protocol arg (=binary) Protocol: binary, compact, json'#10
200 + ' --ssl Encrypted Transport using SSL'#10
201 + ' -n [ --testloops ] arg (=1) Number of Tests'#10
202 + ' -t [ --threads ] arg (=1) Number of Test threads'#10
Jens Geyerb342bd92019-06-03 20:27:00 +0200203 + ' --performance Run the built-in performance test (no other arguments)'#10
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200204 ;
205begin
206 Writeln( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + HELPTEXT);
207end;
208
209class procedure TTestClient.InvalidArgs;
210begin
211 Console.WriteLine( 'Invalid args.');
212 Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + ' -h for more information');
213 Abort;
214end;
215
216class function TTestClient.Execute(const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000217var
218 i : Integer;
Jens Geyer14f5d502017-12-09 13:47:09 +0100219 threadExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000220 s : string;
Roger Meier3bef8c22012-10-06 06:58:00 +0000221 threads : array of TThread;
222 dtStart : TDateTime;
223 test : Integer;
224 thread : TThread;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200225 setup : TTestSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +0000226begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200227 // init record
228 with setup do begin
229 protType := prot_Binary;
230 endpoint := trns_Sockets;
231 layered := [];
232 useSSL := FALSE;
233 host := 'localhost';
234 port := 9090;
235 sPipeName := '';
236 hAnonRead := INVALID_HANDLE_VALUE;
237 hAnonWrite := INVALID_HANDLE_VALUE;
238 end;
239
Roger Meier3bef8c22012-10-06 06:58:00 +0000240 try
Roger Meier3bef8c22012-10-06 06:58:00 +0000241 i := 0;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200242 while ( i < Length(args) ) do begin
243 s := args[i];
244 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000245
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200246 if (s = '-h') or (s = '--help') then begin
247 // -h [ --help ] produce help message
248 PrintCmdLineHelp;
249 result := $FF; // all tests failed
250 Exit;
251 end
Jens Geyerb360b652014-09-28 01:55:46 +0200252 else if s = '--host' then begin
253 // --host arg (=localhost) Host to connect
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200254 setup.host := args[i];
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200255 Inc( i);
256 end
Jens Geyerb360b652014-09-28 01:55:46 +0200257 else if s = '--port' then begin
258 // --port arg (=9090) Port number to connect
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200259 s := args[i];
260 Inc( i);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200261 setup.port := StrToIntDef(s,0);
262 if setup.port <= 0 then InvalidArgs;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200263 end
Jens Geyerb360b652014-09-28 01:55:46 +0200264 else if s = '--domain-socket' then begin
265 // --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200266 raise Exception.Create('domain-socket not supported');
267 end
Jens Geyerb360b652014-09-28 01:55:46 +0200268 else if s = '--named-pipe' then begin
269 // --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200270 setup.endpoint := trns_NamedPipes;
271 setup.sPipeName := args[i];
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200272 Inc( i);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200273 Console.WriteLine('Using named pipe ('+setup.sPipeName+')');
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200274 end
Jens Geyerb360b652014-09-28 01:55:46 +0200275 else if s = '--anon-pipes' then begin
276 // --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200277 setup.endpoint := trns_AnonPipes;
278 setup.hAnonRead := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200279 Inc( i);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200280 setup.hAnonWrite := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200281 Inc( i);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200282 Console.WriteLine('Using anonymous pipes ('+IntToStr(Integer(setup.hAnonRead))+' and '+IntToStr(Integer(setup.hAnonWrite))+')');
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200283 end
Jens Geyerb360b652014-09-28 01:55:46 +0200284 else if s = '--transport' then begin
Jens Geyer02230912019-04-03 01:12:51 +0200285 // --transport arg (=sockets) Transport: buffered, framed, http, winhttp, evhttp
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200286 s := args[i];
287 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000288
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200289 if s = 'buffered' then Include( setup.layered, trns_Buffered)
290 else if s = 'framed' then Include( setup.layered, trns_Framed)
Jens Geyer02230912019-04-03 01:12:51 +0200291 else if s = 'http' then setup.endpoint := trns_MsXmlHttp
292 else if s = 'winhttp' then setup.endpoint := trns_WinHttp
293 else if s = 'evhttp' then setup.endpoint := trns_EvHttp // recognized, but not supported
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200294 else InvalidArgs;
295 end
Jens Geyerb360b652014-09-28 01:55:46 +0200296 else if s = '--protocol' then begin
297 // --protocol arg (=binary) Protocol: binary, compact, json
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200298 s := args[i];
299 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000300
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200301 if s = 'binary' then setup.protType := prot_Binary
302 else if s = 'compact' then setup.protType := prot_Compact
303 else if s = 'json' then setup.protType := prot_JSON
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200304 else InvalidArgs;
305 end
Jens Geyerb360b652014-09-28 01:55:46 +0200306 else if s = '--ssl' then begin
307 // --ssl Encrypted Transport using SSL
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200308 setup.useSSL := TRUE;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200309
310 end
311 else if (s = '-n') or (s = '--testloops') then begin
312 // -n [ --testloops ] arg (=1) Number of Tests
313 FNumIteration := StrToIntDef( args[i], 0);
314 Inc( i);
315 if FNumIteration <= 0
316 then InvalidArgs;
317
318 end
319 else if (s = '-t') or (s = '--threads') then begin
320 // -t [ --threads ] arg (=1) Number of Test threads
321 FNumThread := StrToIntDef( args[i], 0);
322 Inc( i);
323 if FNumThread <= 0
324 then InvalidArgs;
325 end
Jens Geyerb342bd92019-06-03 20:27:00 +0200326 else if (s = '--performance') then begin
327 result := TPerformanceTests.Execute;
328 Exit;
329 end
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200330 else begin
331 InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000332 end;
333 end;
334
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200335
Roger Meier79655fb2012-10-20 20:59:41 +0000336 // In the anonymous pipes mode the client is launched by the test server
337 // -> behave nicely and allow for attaching a debugger to this process
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200338 if (setup.endpoint = trns_AnonPipes) and not IsDebuggerPresent
Roger Meier79655fb2012-10-20 20:59:41 +0000339 then MessageBox( 0, 'Attach Debugger and/or click OK to continue.',
340 'Thrift TestClient (Delphi)',
341 MB_OK or MB_ICONEXCLAMATION);
342
Roger Meier3bef8c22012-10-06 06:58:00 +0000343 SetLength( threads, FNumThread);
344 dtStart := Now;
345
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200346 // layered transports are not really meant to be stacked upon each other
347 if (trns_Framed in setup.layered) then begin
348 Console.WriteLine('Using framed transport');
349 end
350 else if (trns_Buffered in setup.layered) then begin
351 Console.WriteLine('Using buffered transport');
352 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000353
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200354 Console.WriteLine(THRIFT_PROTOCOLS[setup.protType]+' protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000355
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200356 for test := 0 to FNumThread - 1 do begin
357 thread := TClientThread.Create( setup, FNumIteration);
Roger Meier3bef8c22012-10-06 06:58:00 +0000358 threads[test] := thread;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200359 thread.Start;
Roger Meier3bef8c22012-10-06 06:58:00 +0000360 end;
361
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200362 result := 0;
363 for test := 0 to FNumThread - 1 do begin
Jens Geyer14f5d502017-12-09 13:47:09 +0100364 threadExitCode := threads[test].WaitFor;
365 result := result or threadExitCode;
Jens Geyer14f5d502017-12-09 13:47:09 +0100366 threads[test].Free;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200367 threads[test] := nil;
Jens Geyer14f5d502017-12-09 13:47:09 +0100368 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000369
370 Console.Write('Total time: ' + IntToStr( MilliSecondsBetween(Now, dtStart)));
371
372 except
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200373 on E: EAbort do raise;
374 on E: Exception do begin
375 Console.WriteLine( E.Message + #10 + E.StackTrace);
376 raise;
Roger Meier3bef8c22012-10-06 06:58:00 +0000377 end;
378 end;
379
380 Console.WriteLine('');
381 Console.WriteLine('done!');
382end;
383
384{ TClientThread }
385
386procedure TClientThread.ClientTest;
387var
388 client : TThriftTest.Iface;
389 s : string;
390 i8 : ShortInt;
391 i32 : Integer;
392 i64 : Int64;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100393 binOut,binIn : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000394 dub : Double;
395 o : IXtruct;
396 o2 : IXtruct2;
397 i : IXtruct;
398 i2 : IXtruct2;
399 mapout : IThriftDictionary<Integer,Integer>;
400 mapin : IThriftDictionary<Integer,Integer>;
401 strmapout : IThriftDictionary<string,string>;
402 strmapin : IThriftDictionary<string,string>;
403 j : Integer;
404 first : Boolean;
405 key : Integer;
406 strkey : string;
407 listout : IThriftList<Integer>;
408 listin : IThriftList<Integer>;
409 setout : IHashSet<Integer>;
410 setin : IHashSet<Integer>;
411 ret : TNumberz;
412 uid : Int64;
413 mm : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
414 pos : IThriftDictionary<Integer, Integer>;
415 neg : IThriftDictionary<Integer, Integer>;
416 m2 : IThriftDictionary<Integer, Integer>;
417 k2 : Integer;
418 insane : IInsanity;
419 truck : IXtruct;
420 whoa : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
421 key64 : Int64;
422 val : IThriftDictionary<TNumberz, IInsanity>;
423 k2_2 : TNumberz;
424 k3 : TNumberz;
425 v2 : IInsanity;
426 userMap : IThriftDictionary<TNumberz, Int64>;
427 xtructs : IThriftList<IXtruct>;
428 x : IXtruct;
429 arg0 : ShortInt;
430 arg1 : Integer;
431 arg2 : Int64;
432 arg3 : IThriftDictionary<SmallInt, string>;
433 arg4 : TNumberz;
434 arg5 : Int64;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200435 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000436 StartTick : Cardinal;
437 k : Integer;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200438 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000439 hello, goodbye : IXtruct;
440 crazy : IInsanity;
441 looney : IInsanity;
442 first_map : IThriftDictionary<TNumberz, IInsanity>;
443 second_map : IThriftDictionary<TNumberz, IInsanity>;
Jens Geyer540e3462016-12-28 14:25:41 +0100444 pair : TPair<TNumberz, TUserId>;
Jens Geyer85827152018-01-12 21:20:59 +0100445 testsize : TTestSize;
Roger Meier3bef8c22012-10-06 06:58:00 +0000446begin
447 client := TThriftTest.TClient.Create( FProtocol);
448 FTransport.Open;
449
Jens Geyer06045cf2013-03-27 20:26:25 +0200450 {$IFDEF StressTest}
451 StressTest( client);
452 {$ENDIF StressTest}
453
Jens Geyer17c3ad92017-09-05 20:31:27 +0200454 {$IFDEF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000455 // in-depth exception test
456 // (1) do we get an exception at all?
457 // (2) do we get the right exception?
458 // (3) does the exception contain the expected data?
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200459 StartTestGroup( 'testException', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000460 // case 1: exception type declared in IDL at the function call
461 try
462 client.testException('Xception');
463 Expect( FALSE, 'testException(''Xception''): must trow an exception');
464 except
465 on e:TXception do begin
466 Expect( e.ErrorCode = 1001, 'error code');
467 Expect( e.Message_ = 'Xception', 'error message');
468 Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ );
469 end;
470 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200471 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000472 end;
473
474 // case 2: exception type NOT declared in IDL at the function call
475 // this will close the connection
476 try
477 client.testException('TException');
478 Expect( FALSE, 'testException(''TException''): must trow an exception');
479 except
480 on e:TTransportException do begin
481 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Roger Meier3bef8c22012-10-06 06:58:00 +0000482 end;
Jens Geyer6bbbf192014-09-07 01:45:56 +0200483 on e:TApplicationException do begin
484 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Jens Geyer6bbbf192014-09-07 01:45:56 +0200485 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200486 on e:TException do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
487 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000488 end;
489
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100490
Jens Geyer2ad6c302015-02-26 19:38:53 +0100491 if FTransport.IsOpen then FTransport.Close;
492 FTransport.Open; // re-open connection, server has already closed
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100493
Jens Geyer2ad6c302015-02-26 19:38:53 +0100494
Roger Meier3bef8c22012-10-06 06:58:00 +0000495 // case 3: no exception
496 try
497 client.testException('something');
498 Expect( TRUE, 'testException(''something''): must not trow an exception');
499 except
500 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200501 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000502 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200503 {$ENDIF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000504
505
506 // simple things
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200507 StartTestGroup( 'simple Thrift calls', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000508 client.testVoid();
509 Expect( TRUE, 'testVoid()'); // success := no exception
510
Jens Geyer39ba6b72015-09-22 00:00:49 +0200511 s := BoolToString( client.testBool(TRUE));
512 Expect( s = BoolToString(TRUE), 'testBool(TRUE) = '+s);
513 s := BoolToString( client.testBool(FALSE));
514 Expect( s = BoolToString(FALSE), 'testBool(FALSE) = '+s);
515
Roger Meier3bef8c22012-10-06 06:58:00 +0000516 s := client.testString('Test');
517 Expect( s = 'Test', 'testString(''Test'') = "'+s+'"');
518
Jens Geyercf892d42017-09-09 10:08:22 +0200519 s := client.testString(''); // empty string
520 Expect( s = '', 'testString('''') = "'+s+'"');
521
Jens Geyer06045cf2013-03-27 20:26:25 +0200522 s := client.testString(HUGE_TEST_STRING);
523 Expect( length(s) = length(HUGE_TEST_STRING),
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100524 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
Jens Geyer06045cf2013-03-27 20:26:25 +0200525 +'=> length(result) = '+IntToStr(Length(s)));
526
Roger Meier3bef8c22012-10-06 06:58:00 +0000527 i8 := client.testByte(1);
528 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
529
530 i32 := client.testI32(-1);
531 Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32));
532
533 Console.WriteLine('testI64(-34359738368)');
534 i64 := client.testI64(-34359738368);
535 Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
536
Jens Geyerd4df9172017-10-25 22:30:23 +0200537 // random binary small
Jens Geyer85827152018-01-12 21:20:59 +0100538 for testsize := Low(TTestSize) to High(TTestSize) do begin
539 binOut := PrepareBinaryData( TRUE, testsize);
540 Console.WriteLine('testBinary('+BytesToHex(binOut)+')');
541 try
542 binIn := client.testBinary(binOut);
543 Expect( Length(binOut) = Length(binIn), 'testBinary(): length '+IntToStr(Length(binOut))+' = '+IntToStr(Length(binIn)));
544 i32 := Min( Length(binOut), Length(binIn));
545 Expect( CompareMem( binOut, binIn, i32), 'testBinary('+BytesToHex(binOut)+') = '+BytesToHex(binIn));
546 except
547 on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message);
548 on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message);
549 end;
Jens Geyercf892d42017-09-09 10:08:22 +0200550 end;
551
Roger Meier3bef8c22012-10-06 06:58:00 +0000552 Console.WriteLine('testDouble(5.325098235)');
553 dub := client.testDouble(5.325098235);
554 Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
555
556 // structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200557 StartTestGroup( 'testStruct', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000558 Console.WriteLine('testStruct({''Zero'', 1, -3, -5})');
559 o := TXtructImpl.Create;
560 o.String_thing := 'Zero';
561 o.Byte_thing := 1;
562 o.I32_thing := -3;
563 o.I64_thing := -5;
564 i := client.testStruct(o);
565 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
566 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
567 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
568 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
569 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
570 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
571 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
572 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
573
574 // nested structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200575 StartTestGroup( 'testNest', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000576 Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})');
577 o2 := TXtruct2Impl.Create;
578 o2.Byte_thing := 1;
579 o2.Struct_thing := o;
580 o2.I32_thing := 5;
581 i2 := client.testNest(o2);
582 i := i2.Struct_thing;
583 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
584 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
585 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
586 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
587 Expect( i2.Byte_thing = 1, 'i2.Byte_thing = '+IntToStr(i2.Byte_thing));
588 Expect( i2.I32_thing = 5, 'i2.I32_thing = '+IntToStr(i2.I32_thing));
589 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
590 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
591 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
592 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
593 Expect( i2.__isset_Byte_thing, 'i2.__isset_Byte_thing');
594 Expect( i2.__isset_I32_thing, 'i2.__isset_I32_thing');
595
596 // map<type1,type2>: A map of strictly unique keys to values.
597 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200598 StartTestGroup( 'testMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000599 mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
600 for j := 0 to 4 do
601 begin
602 mapout.AddOrSetValue( j, j - 10);
603 end;
604 Console.Write('testMap({');
605 first := True;
606 for key in mapout.Keys do
607 begin
608 if first
609 then first := False
610 else Console.Write( ', ' );
611 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
612 end;
613 Console.WriteLine('})');
614
615 mapin := client.testMap( mapout );
616 Expect( mapin.Count = mapout.Count, 'testMap: mapin.Count = mapout.Count');
617 for j := 0 to 4 do
618 begin
619 Expect( mapout.ContainsKey(j), 'testMap: mapout.ContainsKey('+IntToStr(j)+') = '+BoolToString(mapout.ContainsKey(j)));
620 end;
621 for key in mapin.Keys do
622 begin
623 Expect( mapin[key] = mapout[key], 'testMap: '+IntToStr(key) + ' => ' + IntToStr( mapin[key]));
624 Expect( mapin[key] = key - 10, 'testMap: mapin['+IntToStr(key)+'] = '+IntToStr( mapin[key]));
625 end;
626
627
628 // map<type1,type2>: A map of strictly unique keys to values.
629 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200630 StartTestGroup( 'testStringMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000631 strmapout := TThriftDictionaryImpl<string,string>.Create;
632 for j := 0 to 4 do
633 begin
634 strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10));
635 end;
636 Console.Write('testStringMap({');
637 first := True;
638 for strkey in strmapout.Keys do
639 begin
640 if first
641 then first := False
642 else Console.Write( ', ' );
643 Console.Write( strkey + ' => ' + strmapout[strkey]);
644 end;
645 Console.WriteLine('})');
646
647 strmapin := client.testStringMap( strmapout );
648 Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count');
649 for j := 0 to 4 do
650 begin
651 Expect( strmapout.ContainsKey(IntToStr(j)),
652 'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = '
653 + BoolToString(strmapout.ContainsKey(IntToStr(j))));
654 end;
655 for strkey in strmapin.Keys do
656 begin
657 Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]);
658 Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]);
659 end;
660
661
662 // set<type>: An unordered set of unique elements.
663 // Translates to an STL set, Java HashSet, set in Python, etc.
664 // Note: PHP does not support sets, so it is treated similar to a List
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200665 StartTestGroup( 'testSet', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000666 setout := THashSetImpl<Integer>.Create;
667 for j := -2 to 2 do
668 begin
669 setout.Add( j );
670 end;
671 Console.Write('testSet({');
672 first := True;
673 for j in setout do
674 begin
675 if first
676 then first := False
677 else Console.Write(', ');
678 Console.Write(IntToStr( j));
679 end;
680 Console.WriteLine('})');
681
682 setin := client.testSet(setout);
683 Expect( setin.Count = setout.Count, 'testSet: setin.Count = setout.Count');
684 Expect( setin.Count = 5, 'testSet: setin.Count = '+IntToStr(setin.Count));
685 for j := -2 to 2 do // unordered, we can't rely on the order => test for known elements only
686 begin
687 Expect( setin.Contains(j), 'testSet: setin.Contains('+IntToStr(j)+') => '+BoolToString(setin.Contains(j)));
688 end;
689
690 // list<type>: An ordered list of elements.
691 // Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200692 StartTestGroup( 'testList', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000693 listout := TThriftListImpl<Integer>.Create;
694 listout.Add( +1);
695 listout.Add( -2);
696 listout.Add( +3);
697 listout.Add( -4);
698 listout.Add( 0);
699 Console.Write('testList({');
700 first := True;
701 for j in listout do
702 begin
703 if first
704 then first := False
705 else Console.Write(', ');
706 Console.Write(IntToStr( j));
707 end;
708 Console.WriteLine('})');
709
710 listin := client.testList(listout);
711 Expect( listin.Count = listout.Count, 'testList: listin.Count = listout.Count');
712 Expect( listin.Count = 5, 'testList: listin.Count = '+IntToStr(listin.Count));
713 Expect( listin[0] = +1, 'listin[0] = '+IntToStr( listin[0]));
714 Expect( listin[1] = -2, 'listin[1] = '+IntToStr( listin[1]));
715 Expect( listin[2] = +3, 'listin[2] = '+IntToStr( listin[2]));
716 Expect( listin[3] = -4, 'listin[3] = '+IntToStr( listin[3]));
717 Expect( listin[4] = 0, 'listin[4] = '+IntToStr( listin[4]));
718
719 // enums
720 ret := client.testEnum(TNumberz.ONE);
721 Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret)));
722
723 ret := client.testEnum(TNumberz.TWO);
724 Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret)));
725
726 ret := client.testEnum(TNumberz.THREE);
727 Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret)));
728
729 ret := client.testEnum(TNumberz.FIVE);
730 Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret)));
731
732 ret := client.testEnum(TNumberz.EIGHT);
733 Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret)));
734
735
736 // typedef
737 uid := client.testTypedef(309858235082523);
738 Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid));
739
740
741 // maps of maps
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200742 StartTestGroup( 'testMapMap(1)', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000743 mm := client.testMapMap(1);
744 Console.Write(' = {');
745 for key in mm.Keys do
746 begin
747 Console.Write( IntToStr( key) + ' => {');
748 m2 := mm[key];
749 for k2 in m2.Keys do
750 begin
751 Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', ');
752 end;
753 Console.Write('}, ');
754 end;
755 Console.WriteLine('}');
756
757 // verify result data
758 Expect( mm.Count = 2, 'mm.Count = '+IntToStr(mm.Count));
759 pos := mm[4];
760 neg := mm[-4];
761 for j := 1 to 4 do
762 begin
763 Expect( pos[j] = j, 'pos[j] = '+IntToStr(pos[j]));
764 Expect( neg[-j] = -j, 'neg[-j] = '+IntToStr(neg[-j]));
765 end;
766
767
768
769 // insanity
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200770 StartTestGroup( 'testInsanity', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000771 insane := TInsanityImpl.Create;
772 insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
773 insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000);
774 truck := TXtructImpl.Create;
775 truck.String_thing := 'Truck';
Jens Geyer540e3462016-12-28 14:25:41 +0100776 truck.Byte_thing := -8; // byte is signed
777 truck.I32_thing := 32;
778 truck.I64_thing := 64;
Roger Meier3bef8c22012-10-06 06:58:00 +0000779 insane.Xtructs := TThriftListImpl<IXtruct>.Create;
780 insane.Xtructs.Add( truck );
781 whoa := client.testInsanity( insane );
782 Console.Write(' = {');
783 for key64 in whoa.Keys do
784 begin
785 val := whoa[key64];
786 Console.Write( IntToStr( key64) + ' => {');
787 for k2_2 in val.Keys do
788 begin
789 v2 := val[k2_2];
790 Console.Write( IntToStr( Integer( k2_2)) + ' => {');
791 userMap := v2.UserMap;
792 Console.Write('{');
793 if userMap <> nil then
794 begin
795 for k3 in userMap.Keys do
796 begin
797 Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', ');
798 end;
799 end else
800 begin
801 Console.Write('null');
802 end;
803 Console.Write('}, ');
804 xtructs := v2.Xtructs;
805 Console.Write('{');
806
807 if xtructs <> nil then
808 begin
809 for x in xtructs do
810 begin
811 Console.Write('{"' + x.String_thing + '", ' +
812 IntToStr( x.Byte_thing) + ', ' +
813 IntToStr( x.I32_thing) + ', ' +
814 IntToStr( x.I32_thing) + '}, ');
815 end;
816 end else
817 begin
818 Console.Write('null');
819 end;
820 Console.Write('}');
821 Console.Write('}, ');
822 end;
823 Console.Write('}, ');
824 end;
825 Console.WriteLine('}');
826
Jens Geyer540e3462016-12-28 14:25:41 +0100827 (**
828 * So you think you've got this all worked, out eh?
829 *
830 * Creates a the returned map with these values and prints it out:
831 * { 1 => { 2 => argument,
832 * 3 => argument,
833 * },
834 * 2 => { 6 => <empty Insanity struct>, },
835 * }
836 * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
837 *)
838
Roger Meier3bef8c22012-10-06 06:58:00 +0000839 // verify result data
840 Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count));
841 //
842 first_map := whoa[1];
843 second_map := whoa[2];
844 Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count));
845 Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count));
846 //
847 looney := second_map[TNumberz.SIX];
848 Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney)));
849 Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
850 Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
851 //
852 for ret in [TNumberz.TWO, TNumberz.THREE] do begin
853 crazy := first_map[ret];
854 Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
855
856 Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap));
857 Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs));
858
Jens Geyer540e3462016-12-28 14:25:41 +0100859 Expect( crazy.UserMap.Count = insane.UserMap.Count, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count));
860 for pair in insane.UserMap do begin
861 Expect( crazy.UserMap[pair.Key] = pair.Value, 'crazy.UserMap['+IntToStr(Ord(pair.key))+'] = '+IntToStr(crazy.UserMap[pair.Key]));
862 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000863
Jens Geyer540e3462016-12-28 14:25:41 +0100864 Expect( crazy.Xtructs.Count = insane.Xtructs.Count, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count));
865 for arg0 := 0 to insane.Xtructs.Count-1 do begin
866 hello := insane.Xtructs[arg0];
867 goodbye := crazy.Xtructs[arg0];
868 Expect( goodbye.String_thing = hello.String_thing, 'goodbye.String_thing = '+goodbye.String_thing);
869 Expect( goodbye.Byte_thing = hello.Byte_thing, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing));
870 Expect( goodbye.I32_thing = hello.I32_thing, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing));
871 Expect( goodbye.I64_thing = hello.I64_thing, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing));
872 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000873 end;
874
875
876 // multi args
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200877 StartTestGroup( 'testMulti', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000878 arg0 := 1;
879 arg1 := 2;
880 arg2 := High(Int64);
881 arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
882 arg3.AddOrSetValue( 1, 'one');
883 arg4 := TNumberz.FIVE;
884 arg5 := 5000000;
885 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
886 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
887 arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
888 IntToStr( arg5) + ')');
889
890 i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5);
891 Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"');
892 Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing));
893 Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing));
894 Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing));
895 Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
896 Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
897 Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
898 Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
899
900 // multi exception
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200901 StartTestGroup( 'testMultiException(1)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000902 try
903 i := client.testMultiException( 'need more pizza', 'run out of beer');
904 Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"');
905 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
Jens Geyer6bbbf192014-09-07 01:45:56 +0200906 { this is not necessarily true, these fields are default-serialized
Jens Geyerd5436f52014-10-03 19:50:38 +0200907 Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
Roger Meier3bef8c22012-10-06 06:58:00 +0000908 Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
909 Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
Jens Geyerd5436f52014-10-03 19:50:38 +0200910 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000911 except
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200912 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000913 end;
914
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200915 StartTestGroup( 'testMultiException(Xception)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000916 try
917 i := client.testMultiException( 'Xception', 'second test');
918 Expect( FALSE, 'testMultiException(''Xception''): must trow an exception');
919 except
920 on x:TXception do begin
921 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
922 Expect( x.__isset_Message_, 'x.__isset_Message_ = '+BoolToString(x.__isset_Message_));
923 Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
924 Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"');
925 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200926 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000927 end;
928
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200929 StartTestGroup( 'testMultiException(Xception2)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000930 try
931 i := client.testMultiException( 'Xception2', 'third test');
932 Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception');
933 except
934 on x:TXception2 do begin
935 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
936 Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing));
937 Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
938 Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"');
939 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 +0200940 { this is not necessarily true, these fields are default-serialized
Roger Meier3bef8c22012-10-06 06:58:00 +0000941 Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing));
942 Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing));
943 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 +0200944 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000945 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200946 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000947 end;
948
949
950 // oneway functions
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200951 StartTestGroup( 'Test Oneway(1)', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000952 client.testOneway(1);
953 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
954
955 // call time
Jens Geyer06045cf2013-03-27 20:26:25 +0200956 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000957 StartTestGroup( 'Test Calltime()');
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200958 StartTick := GetTickCount;
Roger Meier3bef8c22012-10-06 06:58:00 +0000959 for k := 0 to 1000 - 1 do
960 begin
961 client.testVoid();
962 end;
963 Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
Jens Geyer06045cf2013-03-27 20:26:25 +0200964 {$ENDIF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000965
966 // no more tests here
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200967 StartTestGroup( '', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000968end;
969
970
Jens Geyer14f5d502017-12-09 13:47:09 +0100971{$IFDEF SupportsAsync}
972procedure TClientThread.ClientAsyncTest;
973var
974 client : TThriftTest.IAsync;
975 s : string;
976 i8 : ShortInt;
977begin
978 StartTestGroup( 'Async Tests', test_Unknown);
979 client := TThriftTest.TClient.Create( FProtocol);
980 FTransport.Open;
981
982 // oneway void functions
983 client.testOnewayAsync(1).Wait;
984 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
985
986 // normal functions
987 s := client.testStringAsync(HUGE_TEST_STRING).Value;
988 Expect( length(s) = length(HUGE_TEST_STRING),
989 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
990 +'=> length(result) = '+IntToStr(Length(s)));
991
992 i8 := client.testByte(1).Value;
993 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
994end;
995{$ENDIF}
996
997
Jens Geyer718f6ee2013-09-06 21:02:34 +0200998{$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200999procedure TClientThread.StressTest(const client : TThriftTest.Iface);
1000begin
1001 while TRUE do begin
1002 try
1003 if not FTransport.IsOpen then FTransport.Open; // re-open connection, server has already closed
1004 try
1005 client.testString('Test');
1006 Write('.');
1007 finally
1008 if FTransport.IsOpen then FTransport.Close;
1009 end;
1010 except
1011 on e:Exception do Writeln(#10+e.message);
1012 end;
1013 end;
1014end;
Jens Geyer718f6ee2013-09-06 21:02:34 +02001015{$ENDIF}
Jens Geyer06045cf2013-03-27 20:26:25 +02001016
Jens Geyerfd1b3582014-12-13 23:42:58 +01001017
Jens Geyer85827152018-01-12 21:20:59 +01001018function TClientThread.PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes;
Jens Geyerd4df9172017-10-25 22:30:23 +02001019var i : Integer;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001020begin
Jens Geyer85827152018-01-12 21:20:59 +01001021 case aSize of
1022 Empty : SetLength( result, 0);
1023 Normal : SetLength( result, $100);
1024 ByteArrayTest : SetLength( result, SizeOf(TByteArray) + 128);
1025 PipeWriteLimit : SetLength( result, 65535 + 128);
1026 else
1027 raise EArgumentException.Create('aSize');
1028 end;
1029
Jens Geyerfd1b3582014-12-13 23:42:58 +01001030 ASSERT( Low(result) = 0);
Jens Geyer85827152018-01-12 21:20:59 +01001031 if Length(result) = 0 then Exit;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001032
1033 // linear distribution, unless random is requested
1034 if not aRandomDist then begin
1035 for i := Low(result) to High(result) do begin
Jens Geyer85827152018-01-12 21:20:59 +01001036 result[i] := i mod $100;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001037 end;
1038 Exit;
1039 end;
1040
1041 // random distribution of all 256 values
1042 FillChar( result[0], Length(result) * SizeOf(result[0]), $0);
Jens Geyerd4df9172017-10-25 22:30:23 +02001043 for i := Low(result) to High(result) do begin
1044 result[i] := Byte( Random($100));
Jens Geyerfd1b3582014-12-13 23:42:58 +01001045 end;
1046end;
1047
1048
Jens Geyerf7904452017-07-26 15:02:12 +02001049{$IFDEF Win64}
1050procedure TClientThread.UseInterlockedExchangeAdd64;
1051var a,b : Int64;
1052begin
1053 a := 1;
1054 b := 2;
1055 Thrift.Utils.InterlockedExchangeAdd64( a,b);
1056 Expect( a = 3, 'InterlockedExchangeAdd64');
1057end;
1058{$ENDIF}
1059
1060
Roger Meier3bef8c22012-10-06 06:58:00 +00001061procedure TClientThread.JSONProtocolReadWriteTest;
1062// Tests only then read/write procedures of the JSON protocol
1063// All tests succeed, if we can read what we wrote before
1064// Note that passing this test does not imply, that our JSON is really compatible to what
1065// other clients or servers expect as the real JSON. This is beyond the scope of this test.
1066var prot : IProtocol;
1067 stm : TStringStream;
Jens Geyer17c3ad92017-09-05 20:31:27 +02001068 list : TThriftList;
Jens Geyercf892d42017-09-09 10:08:22 +02001069 binary, binRead, emptyBinary : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +00001070 i,iErr : Integer;
1071const
1072 TEST_SHORT = ShortInt( $FE);
1073 TEST_SMALL = SmallInt( $FEDC);
1074 TEST_LONG = LongInt( $FEDCBA98);
1075 TEST_I64 = Int64( $FEDCBA9876543210);
1076 TEST_DOUBLE = -1.234e-56;
1077 DELTA_DOUBLE = TEST_DOUBLE * 1e-14;
1078 TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars")
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001079 // Test THRIFT-2336 and THRIFT-3404 with U+1D11E (G Clef symbol) and 'Русское Название';
1080 G_CLEF_AND_CYRILLIC_TEXT = #$1d11e' '#$0420#$0443#$0441#$0441#$043a#$043e#$0435' '#$041d#$0430#$0437#$0432#$0430#$043d#$0438#$0435;
1081 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 +01001082 // test both possible solidus encodings
1083 SOLIDUS_JSON_DATA = '"one/two\/three"';
1084 SOLIDUS_EXCPECTED = 'one/two/three';
Roger Meier3bef8c22012-10-06 06:58:00 +00001085begin
1086 stm := TStringStream.Create;
1087 try
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001088 StartTestGroup( 'JsonProtocolTest', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001089
1090 // prepare binary data
Jens Geyer85827152018-01-12 21:20:59 +01001091 binary := PrepareBinaryData( FALSE, Normal);
Jens Geyercf892d42017-09-09 10:08:22 +02001092 SetLength( emptyBinary, 0); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001093
1094 // output setup
1095 prot := TJSONProtocolImpl.Create(
1096 TStreamTransportImpl.Create(
1097 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
1098
1099 // write
Jens Geyer17c3ad92017-09-05 20:31:27 +02001100 Init( list, TType.String_, 9);
1101 prot.WriteListBegin( list);
Roger Meier3bef8c22012-10-06 06:58:00 +00001102 prot.WriteBool( TRUE);
1103 prot.WriteBool( FALSE);
1104 prot.WriteByte( TEST_SHORT);
1105 prot.WriteI16( TEST_SMALL);
1106 prot.WriteI32( TEST_LONG);
1107 prot.WriteI64( TEST_I64);
1108 prot.WriteDouble( TEST_DOUBLE);
1109 prot.WriteString( TEST_STRING);
1110 prot.WriteBinary( binary);
Jens Geyercf892d42017-09-09 10:08:22 +02001111 prot.WriteString( ''); // empty string
1112 prot.WriteBinary( emptyBinary); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001113 prot.WriteListEnd;
1114
1115 // input setup
1116 Expect( stm.Position = stm.Size, 'Stream position/length after write');
1117 stm.Position := 0;
1118 prot := TJSONProtocolImpl.Create(
1119 TStreamTransportImpl.Create(
1120 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1121
1122 // read and compare
1123 list := prot.ReadListBegin;
1124 Expect( list.ElementType = TType.String_, 'list element type');
1125 Expect( list.Count = 9, 'list element count');
1126 Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE');
1127 Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE');
1128 Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte');
1129 Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16');
1130 Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32');
1131 Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64');
1132 Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble');
1133 Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString');
1134 binRead := prot.ReadBinary;
Jens Geyercf892d42017-09-09 10:08:22 +02001135 Expect( Length(prot.ReadString) = 0, 'WriteString/ReadString (empty string)');
1136 Expect( Length(prot.ReadBinary) = 0, 'empty WriteBinary/ReadBinary (empty data block)');
Roger Meier3bef8c22012-10-06 06:58:00 +00001137 prot.ReadListEnd;
1138
1139 // test binary data
1140 Expect( Length(binary) = Length(binRead), 'Binary data length check');
1141 iErr := -1;
1142 for i := Low(binary) to High(binary) do begin
1143 if binary[i] <> binRead[i] then begin
1144 iErr := i;
1145 Break;
1146 end;
1147 end;
1148 if iErr < 0
1149 then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)')
1150 else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr));
1151
1152 Expect( stm.Position = stm.Size, 'Stream position after read');
1153
Jens Geyer7bb44a32014-02-07 22:24:37 +01001154
Jens Geyer21366942013-12-30 22:04:51 +01001155 // Solidus can be encoded in two ways. Make sure we can read both
1156 stm.Position := 0;
1157 stm.Size := 0;
1158 stm.WriteString(SOLIDUS_JSON_DATA);
1159 stm.Position := 0;
1160 prot := TJSONProtocolImpl.Create(
1161 TStreamTransportImpl.Create(
1162 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1163 Expect( prot.ReadString = SOLIDUS_EXCPECTED, 'Solidus encoding');
1164
1165
Jens Geyer7bb44a32014-02-07 22:24:37 +01001166 // Widechars should work too. Do they?
1167 // After writing, we ensure that we are able to read it back
1168 // We can't assume hex-encoding, since (nearly) any Unicode char is valid JSON
1169 stm.Position := 0;
1170 stm.Size := 0;
1171 prot := TJSONProtocolImpl.Create(
1172 TStreamTransportImpl.Create(
1173 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001174 prot.WriteString( G_CLEF_AND_CYRILLIC_TEXT);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001175 stm.Position := 0;
1176 prot := TJSONProtocolImpl.Create(
1177 TStreamTransportImpl.Create(
1178 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001179 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Writing JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001180
1181 // Widechars should work with hex-encoding too. Do they?
1182 stm.Position := 0;
1183 stm.Size := 0;
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001184 stm.WriteString( G_CLEF_AND_CYRILLIC_JSON);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001185 stm.Position := 0;
1186 prot := TJSONProtocolImpl.Create(
1187 TStreamTransportImpl.Create(
1188 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001189 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Reading JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001190
1191
Roger Meier3bef8c22012-10-06 06:58:00 +00001192 finally
1193 stm.Free;
1194 prot := nil; //-> Release
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001195 StartTestGroup( '', test_Unknown); // no more tests here
Roger Meier3bef8c22012-10-06 06:58:00 +00001196 end;
1197end;
1198
1199
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001200procedure TClientThread.StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +00001201begin
1202 FTestGroup := aGroup;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001203 FCurrentTest := aTest;
1204
1205 Include( FExecuted, aTest);
1206
Roger Meier3bef8c22012-10-06 06:58:00 +00001207 if FTestGroup <> '' then begin
1208 Console.WriteLine('');
1209 Console.WriteLine( aGroup+' tests');
1210 Console.WriteLine( StringOfChar('-',60));
1211 end;
1212end;
1213
1214
1215procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
1216begin
1217 if aTestResult then begin
1218 Inc(FSuccesses);
1219 Console.WriteLine( aTestInfo+': passed');
1220 end
1221 else begin
1222 FErrors.Add( FTestGroup+': '+aTestInfo);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001223 Include( FFailed, FCurrentTest);
Roger Meier3bef8c22012-10-06 06:58:00 +00001224 Console.WriteLine( aTestInfo+': *** FAILED ***');
1225
1226 // We have a failed test!
1227 // -> issue DebugBreak ONLY if a debugger is attached,
1228 // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise
Jens Geyer9f7f11e2016-04-14 21:37:11 +02001229 if IsDebuggerPresent
1230 then {$IFDEF CPUX64} DebugBreak {$ELSE} asm int 3 end {$ENDIF};
Roger Meier3bef8c22012-10-06 06:58:00 +00001231 end;
1232end;
1233
1234
1235procedure TClientThread.ReportResults;
1236var nTotal : Integer;
1237 sLine : string;
1238begin
1239 // prevent us from stupid DIV/0 errors
1240 nTotal := FSuccesses + FErrors.Count;
1241 if nTotal = 0 then begin
1242 Console.WriteLine('No results logged');
1243 Exit;
1244 end;
1245
1246 Console.WriteLine('');
1247 Console.WriteLine( StringOfChar('=',60));
1248 Console.WriteLine( IntToStr(nTotal)+' tests performed');
1249 Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)');
1250 Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)');
1251 Console.WriteLine( StringOfChar('=',60));
1252 if FErrors.Count > 0 then begin
1253 Console.WriteLine('FAILED TESTS:');
1254 for sLine in FErrors do Console.WriteLine('- '+sLine);
1255 Console.WriteLine( StringOfChar('=',60));
1256 InterlockedIncrement( ExitCode); // return <> 0 on errors
1257 end;
1258 Console.WriteLine('');
1259end;
1260
1261
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001262function TClientThread.CalculateExitCode : Byte;
1263var test : TTestGroup;
1264begin
1265 result := EXITCODE_SUCCESS;
1266 for test := Low(TTestGroup) to High(TTestGroup) do begin
1267 if (test in FFailed) or not (test in FExecuted)
1268 then result := result or MAP_FAILURES_TO_EXITCODE_BITS[test];
1269 end;
1270end;
1271
1272
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001273constructor TClientThread.Create( const aSetup : TTestSetup; const aNumIteration: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +00001274begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001275 FSetup := aSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +00001276 FNumIteration := ANumIteration;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001277
Roger Meier3bef8c22012-10-06 06:58:00 +00001278 FConsole := TThreadConsole.Create( Self );
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001279 FCurrentTest := test_Unknown;
Roger Meier3bef8c22012-10-06 06:58:00 +00001280
1281 // error list: keep correct order, allow for duplicates
1282 FErrors := TStringList.Create;
1283 FErrors.Sorted := FALSE;
1284 FErrors.Duplicates := dupAccept;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001285
1286 inherited Create( TRUE);
Roger Meier3bef8c22012-10-06 06:58:00 +00001287end;
1288
1289destructor TClientThread.Destroy;
1290begin
1291 FreeAndNil( FConsole);
1292 FreeAndNil( FErrors);
1293 inherited;
1294end;
1295
1296procedure TClientThread.Execute;
1297var
1298 i : Integer;
Roger Meier3bef8c22012-10-06 06:58:00 +00001299begin
1300 // perform all tests
1301 try
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001302 {$IFDEF Win64}
Jens Geyerf7904452017-07-26 15:02:12 +02001303 UseInterlockedExchangeAdd64;
Jens Geyer14f5d502017-12-09 13:47:09 +01001304 {$ENDIF}
Jens Geyer7bb44a32014-02-07 22:24:37 +01001305 JSONProtocolReadWriteTest;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001306
1307 // must be run in the context of the thread
1308 InitializeProtocolTransportStack;
1309 try
1310 for i := 0 to FNumIteration - 1 do begin
1311 ClientTest;
1312 {$IFDEF SupportsAsync}
1313 ClientAsyncTest;
1314 {$ENDIF}
1315 end;
1316
1317 // report the outcome
1318 ReportResults;
1319 SetReturnValue( CalculateExitCode);
1320
1321 finally
1322 ShutdownProtocolTransportStack;
Roger Meier3bef8c22012-10-06 06:58:00 +00001323 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001324
Roger Meier3bef8c22012-10-06 06:58:00 +00001325 except
1326 on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"');
1327 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001328end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001329
Roger Meier3bef8c22012-10-06 06:58:00 +00001330
Jens Geyer02230912019-04-03 01:12:51 +02001331function TClientThread.InitializeHttpTransport( const aTimeoutSetting : Integer) : IHTTPClient;
Jens Geyer83ff7532019-06-06 22:46:03 +02001332var sUrl : string;
1333 comps : URL_COMPONENTS;
1334 dwChars : DWORD;
Jens Geyer02230912019-04-03 01:12:51 +02001335begin
1336 ASSERT( FSetup.endpoint in [trns_MsxmlHttp, trns_WinHttp]);
1337
1338 if FSetup.useSSL
1339 then sUrl := 'https://'
1340 else sUrl := 'http://';
1341
1342 sUrl := sUrl + FSetup.host;
1343
Jens Geyer83ff7532019-06-06 22:46:03 +02001344 // add the port number if necessary and at the right place
1345 FillChar( comps, SizeOf(comps), 0);
1346 comps.dwStructSize := SizeOf(comps);
1347 comps.dwSchemeLength := MAXINT;
1348 comps.dwHostNameLength := MAXINT;
1349 comps.dwUserNameLength := MAXINT;
1350 comps.dwPasswordLength := MAXINT;
1351 comps.dwUrlPathLength := MAXINT;
1352 comps.dwExtraInfoLength := MAXINT;
1353 Win32Check( WinHttpCrackUrl( PChar(sUrl), Length(sUrl), 0, comps));
Jens Geyer02230912019-04-03 01:12:51 +02001354 case FSetup.port of
Jens Geyer83ff7532019-06-06 22:46:03 +02001355 80 : if FSetup.useSSL then comps.nPort := FSetup.port;
1356 443 : if not FSetup.useSSL then comps.nPort := FSetup.port;
Jens Geyer02230912019-04-03 01:12:51 +02001357 else
Jens Geyer83ff7532019-06-06 22:46:03 +02001358 if FSetup.port > 0 then comps.nPort := FSetup.port;
Jens Geyer02230912019-04-03 01:12:51 +02001359 end;
Jens Geyer83ff7532019-06-06 22:46:03 +02001360 dwChars := Length(sUrl) + 64;
1361 SetLength( sUrl, dwChars);
1362 Win32Check( WinHttpCreateUrl( comps, 0, @sUrl[1], dwChars));
1363 SetLength( sUrl, dwChars);
1364
Jens Geyer02230912019-04-03 01:12:51 +02001365
1366 Console.WriteLine('Target URL: '+sUrl);
1367 case FSetup.endpoint of
1368 trns_MsxmlHttp : result := TMsxmlHTTPClientImpl.Create( sUrl);
1369 trns_WinHttp : result := TWinHTTPClientImpl.Create( sUrl);
1370 else
1371 raise Exception.Create(ENDPOINT_TRANSPORTS[FSetup.endpoint]+' unhandled case');
1372 end;
1373
1374 result.DnsResolveTimeout := aTimeoutSetting;
1375 result.ConnectionTimeout := aTimeoutSetting;
1376 result.SendTimeout := aTimeoutSetting;
1377 result.ReadTimeout := aTimeoutSetting;
1378end;
1379
1380
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001381procedure TClientThread.InitializeProtocolTransportStack;
Jens Geyer02230912019-04-03 01:12:51 +02001382var streamtrans : IStreamTransport;
Jens Geyer47f63172019-06-06 22:42:58 +02001383 canSSL : Boolean;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001384const
1385 DEBUG_TIMEOUT = 30 * 1000;
1386 RELEASE_TIMEOUT = DEFAULT_THRIFT_TIMEOUT;
1387 PIPE_TIMEOUT = RELEASE_TIMEOUT;
1388 HTTP_TIMEOUTS = 10 * 1000;
1389begin
1390 // needed for HTTP clients as they utilize the MSXML COM components
1391 OleCheck( CoInitialize( nil));
1392
Jens Geyer47f63172019-06-06 22:42:58 +02001393 canSSL := FALSE;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001394 case FSetup.endpoint of
1395 trns_Sockets: begin
1396 Console.WriteLine('Using sockets ('+FSetup.host+' port '+IntToStr(FSetup.port)+')');
1397 streamtrans := TSocketImpl.Create( FSetup.host, FSetup.port );
1398 FTransport := streamtrans;
1399 end;
1400
Jens Geyer02230912019-04-03 01:12:51 +02001401 trns_MsxmlHttp,
1402 trns_WinHttp: begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001403 Console.WriteLine('Using HTTPClient');
Jens Geyer02230912019-04-03 01:12:51 +02001404 FTransport := InitializeHttpTransport( HTTP_TIMEOUTS);
Jens Geyer47f63172019-06-06 22:42:58 +02001405 canSSL := TRUE;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001406 end;
1407
1408 trns_EvHttp: begin
1409 raise Exception.Create(ENDPOINT_TRANSPORTS[FSetup.endpoint]+' transport not implemented');
1410 end;
1411
1412 trns_NamedPipes: begin
1413 streamtrans := TNamedPipeTransportClientEndImpl.Create( FSetup.sPipeName, 0, nil, PIPE_TIMEOUT, PIPE_TIMEOUT);
1414 FTransport := streamtrans;
1415 end;
1416
1417 trns_AnonPipes: begin
1418 streamtrans := TAnonymousPipeTransportImpl.Create( FSetup.hAnonRead, FSetup.hAnonWrite, FALSE);
1419 FTransport := streamtrans;
1420 end;
1421
1422 else
1423 raise Exception.Create('Unhandled endpoint transport');
1424 end;
1425 ASSERT( FTransport <> nil);
1426
1427 // layered transports are not really meant to be stacked upon each other
1428 if (trns_Framed in FSetup.layered) then begin
1429 FTransport := TFramedTransportImpl.Create( FTransport);
1430 end
1431 else if (trns_Buffered in FSetup.layered) and (streamtrans <> nil) then begin
1432 FTransport := TBufferedTransportImpl.Create( streamtrans, 32); // small buffer to test read()
1433 end;
1434
Jens Geyer47f63172019-06-06 22:42:58 +02001435 if FSetup.useSSL and not canSSL then begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001436 raise Exception.Create('SSL/TLS not implemented');
1437 end;
1438
1439 // create protocol instance, default to BinaryProtocol
1440 case FSetup.protType of
1441 prot_Binary : FProtocol := TBinaryProtocolImpl.Create( FTransport, BINARY_STRICT_READ, BINARY_STRICT_WRITE);
1442 prot_JSON : FProtocol := TJSONProtocolImpl.Create( FTransport);
1443 prot_Compact : FProtocol := TCompactProtocolImpl.Create( FTransport);
1444 else
1445 raise Exception.Create('Unhandled protocol');
1446 end;
1447
1448 ASSERT( (FTransport <> nil) and (FProtocol <> nil));
1449end;
1450
1451
1452procedure TClientThread.ShutdownProtocolTransportStack;
1453begin
1454 try
1455 FProtocol := nil;
1456
1457 if FTransport <> nil then begin
Roger Meier3bef8c22012-10-06 06:58:00 +00001458 FTransport.Close;
1459 FTransport := nil;
1460 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001461
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001462 finally
1463 CoUninitialize;
1464 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001465end;
1466
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001467
Roger Meier3bef8c22012-10-06 06:58:00 +00001468{ TThreadConsole }
1469
1470constructor TThreadConsole.Create(AThread: TThread);
1471begin
Jens Geyer718f6ee2013-09-06 21:02:34 +02001472 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +00001473 FThread := AThread;
1474end;
1475
1476procedure TThreadConsole.Write(const S: string);
1477var
1478 proc : TThreadProcedure;
1479begin
1480 proc := procedure
1481 begin
1482 Console.Write( S );
1483 end;
1484 TThread.Synchronize( FThread, proc);
1485end;
1486
1487procedure TThreadConsole.WriteLine(const S: string);
1488var
1489 proc : TThreadProcedure;
1490begin
1491 proc := procedure
1492 begin
1493 Console.WriteLine( S );
1494 end;
1495 TThread.Synchronize( FThread, proc);
1496end;
1497
1498initialization
1499begin
1500 TTestClient.FNumIteration := 1;
1501 TTestClient.FNumThread := 1;
1502end;
1503
1504end.