blob: 677d416b59b83bddb8262603b320edb2ebed187b [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 Geyer83ff7532019-06-06 22:46:03 +020053 Thrift.WinHTTP,
Jens Geyerf7904452017-07-26 15:02:12 +020054 Thrift.Utils,
Jens Geyer3d556242018-01-24 19:14:32 +010055 Thrift.Collections;
Roger Meier3bef8c22012-10-06 06:58:00 +000056
57type
58 TThreadConsole = class
59 private
60 FThread : TThread;
61 public
62 procedure Write( const S : string);
63 procedure WriteLine( const S : string);
64 constructor Create( AThread: TThread);
65 end;
66
Jens Geyeraf7ecd62018-06-22 22:41:27 +020067 TTestSetup = record
68 protType : TKnownProtocol;
69 endpoint : TEndpointTransport;
70 layered : TLayeredTransports;
71 useSSL : Boolean; // include where appropriate (TLayeredTransport?)
72 host : string;
73 port : Integer;
74 sPipeName : string;
75 hAnonRead, hAnonWrite : THandle;
76 end;
77
Roger Meier3bef8c22012-10-06 06:58:00 +000078 TClientThread = class( TThread )
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020079 private type
80 TTestGroup = (
81 test_Unknown,
82 test_BaseTypes,
83 test_Structs,
84 test_Containers,
85 test_Exceptions
86 // new values here
87 );
88 TTestGroups = set of TTestGroup;
89
Jens Geyer85827152018-01-12 21:20:59 +010090 TTestSize = (
91 Empty, // Edge case: the zero-length empty binary
92 Normal, // Fairly small array of usual size (256 bytes)
93 ByteArrayTest, // THRIFT-4454 Large writes/reads may cause range check errors in debug mode
94 PipeWriteLimit // THRIFT-4372 Pipe write operations across a network are limited to 65,535 bytes per write.
95 );
96
Roger Meier3bef8c22012-10-06 06:58:00 +000097 private
Jens Geyeraf7ecd62018-06-22 22:41:27 +020098 FSetup : TTestSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +000099 FTransport : ITransport;
100 FProtocol : IProtocol;
101 FNumIteration : Integer;
102 FConsole : TThreadConsole;
103
104 // test reporting, will be refactored out into separate class later
105 FTestGroup : string;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200106 FCurrentTest : TTestGroup;
Roger Meier3bef8c22012-10-06 06:58:00 +0000107 FSuccesses : Integer;
108 FErrors : TStringList;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200109 FFailed : TTestGroups;
110 FExecuted : TTestGroups;
111 procedure StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +0000112 procedure Expect( aTestResult : Boolean; const aTestInfo : string);
113 procedure ReportResults;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100114 function CalculateExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000115
116 procedure ClientTest;
Jens Geyer14f5d502017-12-09 13:47:09 +0100117 {$IFDEF SupportsAsync}
118 procedure ClientAsyncTest;
119 {$ENDIF}
120
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200121 procedure InitializeProtocolTransportStack;
122 procedure ShutdownProtocolTransportStack;
Jens Geyer02230912019-04-03 01:12:51 +0200123 function InitializeHttpTransport( const aTimeoutSetting : Integer) : IHTTPClient;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200124
Roger Meier3bef8c22012-10-06 06:58:00 +0000125 procedure JSONProtocolReadWriteTest;
Jens Geyer85827152018-01-12 21:20:59 +0100126 function PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes;
Jens Geyer718f6ee2013-09-06 21:02:34 +0200127 {$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200128 procedure StressTest(const client : TThriftTest.Iface);
Jens Geyer718f6ee2013-09-06 21:02:34 +0200129 {$ENDIF}
Jens Geyerf7904452017-07-26 15:02:12 +0200130 {$IFDEF Win64}
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200131 procedure UseInterlockedExchangeAdd64;
Jens Geyerf7904452017-07-26 15:02:12 +0200132 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000133 protected
134 procedure Execute; override;
135 public
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200136 constructor Create( const aSetup : TTestSetup; const aNumIteration: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +0000137 destructor Destroy; override;
138 end;
139
140 TTestClient = class
141 private
142 class var
143 FNumIteration : Integer;
144 FNumThread : Integer;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200145
146 class procedure PrintCmdLineHelp;
147 class procedure InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000148 public
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200149 class function Execute( const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000150 end;
151
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200152
Roger Meier3bef8c22012-10-06 06:58:00 +0000153implementation
154
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200155const
156 EXITCODE_SUCCESS = $00; // no errors bits set
157 //
158 EXITCODE_FAILBIT_BASETYPES = $01;
159 EXITCODE_FAILBIT_STRUCTS = $02;
160 EXITCODE_FAILBIT_CONTAINERS = $04;
161 EXITCODE_FAILBIT_EXCEPTIONS = $08;
162
163 MAP_FAILURES_TO_EXITCODE_BITS : array[TClientThread.TTestGroup] of Byte = (
164 EXITCODE_SUCCESS, // no bits here
165 EXITCODE_FAILBIT_BASETYPES,
166 EXITCODE_FAILBIT_STRUCTS,
167 EXITCODE_FAILBIT_CONTAINERS,
168 EXITCODE_FAILBIT_EXCEPTIONS
169 );
170
171
172
Roger Meier3bef8c22012-10-06 06:58:00 +0000173function BoolToString( b : Boolean) : string;
174// overrides global BoolToString()
175begin
176 if b
177 then result := 'true'
178 else result := 'false';
179end;
180
181// not available in all versions, so make sure we have this one imported
182function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent';
183
184{ TTestClient }
185
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200186class procedure TTestClient.PrintCmdLineHelp;
187const HELPTEXT = ' [options]'#10
188 + #10
189 + 'Allowed options:'#10
190 + ' -h [ --help ] produce help message'#10
191 + ' --host arg (=localhost) Host to connect'#10
192 + ' --port arg (=9090) Port number to connect'#10
193 + ' --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift),'#10
194 + ' instead of host and port'#10
195 + ' --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)'#10
196 + ' --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)'#10
Jens Geyer02230912019-04-03 01:12:51 +0200197 + ' --transport arg (=sockets) Transport: buffered, framed, http, winhttp'#10
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200198 + ' --protocol arg (=binary) Protocol: binary, compact, json'#10
199 + ' --ssl Encrypted Transport using SSL'#10
200 + ' -n [ --testloops ] arg (=1) Number of Tests'#10
201 + ' -t [ --threads ] arg (=1) Number of Test threads'#10
202 ;
203begin
204 Writeln( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + HELPTEXT);
205end;
206
207class procedure TTestClient.InvalidArgs;
208begin
209 Console.WriteLine( 'Invalid args.');
210 Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + ' -h for more information');
211 Abort;
212end;
213
214class function TTestClient.Execute(const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000215var
216 i : Integer;
Jens Geyer14f5d502017-12-09 13:47:09 +0100217 threadExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000218 s : string;
Roger Meier3bef8c22012-10-06 06:58:00 +0000219 threads : array of TThread;
220 dtStart : TDateTime;
221 test : Integer;
222 thread : TThread;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200223 setup : TTestSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +0000224begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200225 // init record
226 with setup do begin
227 protType := prot_Binary;
228 endpoint := trns_Sockets;
229 layered := [];
230 useSSL := FALSE;
231 host := 'localhost';
232 port := 9090;
233 sPipeName := '';
234 hAnonRead := INVALID_HANDLE_VALUE;
235 hAnonWrite := INVALID_HANDLE_VALUE;
236 end;
237
Roger Meier3bef8c22012-10-06 06:58:00 +0000238 try
Roger Meier3bef8c22012-10-06 06:58:00 +0000239 i := 0;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200240 while ( i < Length(args) ) do begin
241 s := args[i];
242 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000243
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200244 if (s = '-h') or (s = '--help') then begin
245 // -h [ --help ] produce help message
246 PrintCmdLineHelp;
247 result := $FF; // all tests failed
248 Exit;
249 end
Jens Geyerb360b652014-09-28 01:55:46 +0200250 else if s = '--host' then begin
251 // --host arg (=localhost) Host to connect
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200252 setup.host := args[i];
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200253 Inc( i);
254 end
Jens Geyerb360b652014-09-28 01:55:46 +0200255 else if s = '--port' then begin
256 // --port arg (=9090) Port number to connect
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200257 s := args[i];
258 Inc( i);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200259 setup.port := StrToIntDef(s,0);
260 if setup.port <= 0 then InvalidArgs;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200261 end
Jens Geyerb360b652014-09-28 01:55:46 +0200262 else if s = '--domain-socket' then begin
263 // --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200264 raise Exception.Create('domain-socket not supported');
265 end
Jens Geyerb360b652014-09-28 01:55:46 +0200266 else if s = '--named-pipe' then begin
267 // --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200268 setup.endpoint := trns_NamedPipes;
269 setup.sPipeName := args[i];
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200270 Inc( i);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200271 Console.WriteLine('Using named pipe ('+setup.sPipeName+')');
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200272 end
Jens Geyerb360b652014-09-28 01:55:46 +0200273 else if s = '--anon-pipes' then begin
274 // --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200275 setup.endpoint := trns_AnonPipes;
276 setup.hAnonRead := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200277 Inc( i);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200278 setup.hAnonWrite := 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 Console.WriteLine('Using anonymous pipes ('+IntToStr(Integer(setup.hAnonRead))+' and '+IntToStr(Integer(setup.hAnonWrite))+')');
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200281 end
Jens Geyerb360b652014-09-28 01:55:46 +0200282 else if s = '--transport' then begin
Jens Geyer02230912019-04-03 01:12:51 +0200283 // --transport arg (=sockets) Transport: buffered, framed, http, winhttp, evhttp
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200284 s := args[i];
285 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000286
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200287 if s = 'buffered' then Include( setup.layered, trns_Buffered)
288 else if s = 'framed' then Include( setup.layered, trns_Framed)
Jens Geyer02230912019-04-03 01:12:51 +0200289 else if s = 'http' then setup.endpoint := trns_MsXmlHttp
290 else if s = 'winhttp' then setup.endpoint := trns_WinHttp
291 else if s = 'evhttp' then setup.endpoint := trns_EvHttp // recognized, but not supported
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200292 else InvalidArgs;
293 end
Jens Geyerb360b652014-09-28 01:55:46 +0200294 else if s = '--protocol' then begin
295 // --protocol arg (=binary) Protocol: binary, compact, json
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200296 s := args[i];
297 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000298
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200299 if s = 'binary' then setup.protType := prot_Binary
300 else if s = 'compact' then setup.protType := prot_Compact
301 else if s = 'json' then setup.protType := prot_JSON
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200302 else InvalidArgs;
303 end
Jens Geyerb360b652014-09-28 01:55:46 +0200304 else if s = '--ssl' then begin
305 // --ssl Encrypted Transport using SSL
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200306 setup.useSSL := TRUE;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200307
308 end
309 else if (s = '-n') or (s = '--testloops') then begin
310 // -n [ --testloops ] arg (=1) Number of Tests
311 FNumIteration := StrToIntDef( args[i], 0);
312 Inc( i);
313 if FNumIteration <= 0
314 then InvalidArgs;
315
316 end
317 else if (s = '-t') or (s = '--threads') then begin
318 // -t [ --threads ] arg (=1) Number of Test threads
319 FNumThread := StrToIntDef( args[i], 0);
320 Inc( i);
321 if FNumThread <= 0
322 then InvalidArgs;
323 end
324 else begin
325 InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000326 end;
327 end;
328
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200329
Roger Meier79655fb2012-10-20 20:59:41 +0000330 // In the anonymous pipes mode the client is launched by the test server
331 // -> behave nicely and allow for attaching a debugger to this process
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200332 if (setup.endpoint = trns_AnonPipes) and not IsDebuggerPresent
Roger Meier79655fb2012-10-20 20:59:41 +0000333 then MessageBox( 0, 'Attach Debugger and/or click OK to continue.',
334 'Thrift TestClient (Delphi)',
335 MB_OK or MB_ICONEXCLAMATION);
336
Roger Meier3bef8c22012-10-06 06:58:00 +0000337 SetLength( threads, FNumThread);
338 dtStart := Now;
339
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200340 // layered transports are not really meant to be stacked upon each other
341 if (trns_Framed in setup.layered) then begin
342 Console.WriteLine('Using framed transport');
343 end
344 else if (trns_Buffered in setup.layered) then begin
345 Console.WriteLine('Using buffered transport');
346 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000347
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200348 Console.WriteLine(THRIFT_PROTOCOLS[setup.protType]+' protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000349
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200350 for test := 0 to FNumThread - 1 do begin
351 thread := TClientThread.Create( setup, FNumIteration);
Roger Meier3bef8c22012-10-06 06:58:00 +0000352 threads[test] := thread;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200353 thread.Start;
Roger Meier3bef8c22012-10-06 06:58:00 +0000354 end;
355
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200356 result := 0;
357 for test := 0 to FNumThread - 1 do begin
Jens Geyer14f5d502017-12-09 13:47:09 +0100358 threadExitCode := threads[test].WaitFor;
359 result := result or threadExitCode;
Jens Geyer14f5d502017-12-09 13:47:09 +0100360 threads[test].Free;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200361 threads[test] := nil;
Jens Geyer14f5d502017-12-09 13:47:09 +0100362 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000363
364 Console.Write('Total time: ' + IntToStr( MilliSecondsBetween(Now, dtStart)));
365
366 except
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200367 on E: EAbort do raise;
368 on E: Exception do begin
369 Console.WriteLine( E.Message + #10 + E.StackTrace);
370 raise;
Roger Meier3bef8c22012-10-06 06:58:00 +0000371 end;
372 end;
373
374 Console.WriteLine('');
375 Console.WriteLine('done!');
376end;
377
378{ TClientThread }
379
380procedure TClientThread.ClientTest;
381var
382 client : TThriftTest.Iface;
383 s : string;
384 i8 : ShortInt;
385 i32 : Integer;
386 i64 : Int64;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100387 binOut,binIn : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000388 dub : Double;
389 o : IXtruct;
390 o2 : IXtruct2;
391 i : IXtruct;
392 i2 : IXtruct2;
393 mapout : IThriftDictionary<Integer,Integer>;
394 mapin : IThriftDictionary<Integer,Integer>;
395 strmapout : IThriftDictionary<string,string>;
396 strmapin : IThriftDictionary<string,string>;
397 j : Integer;
398 first : Boolean;
399 key : Integer;
400 strkey : string;
401 listout : IThriftList<Integer>;
402 listin : IThriftList<Integer>;
403 setout : IHashSet<Integer>;
404 setin : IHashSet<Integer>;
405 ret : TNumberz;
406 uid : Int64;
407 mm : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
408 pos : IThriftDictionary<Integer, Integer>;
409 neg : IThriftDictionary<Integer, Integer>;
410 m2 : IThriftDictionary<Integer, Integer>;
411 k2 : Integer;
412 insane : IInsanity;
413 truck : IXtruct;
414 whoa : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
415 key64 : Int64;
416 val : IThriftDictionary<TNumberz, IInsanity>;
417 k2_2 : TNumberz;
418 k3 : TNumberz;
419 v2 : IInsanity;
420 userMap : IThriftDictionary<TNumberz, Int64>;
421 xtructs : IThriftList<IXtruct>;
422 x : IXtruct;
423 arg0 : ShortInt;
424 arg1 : Integer;
425 arg2 : Int64;
426 arg3 : IThriftDictionary<SmallInt, string>;
427 arg4 : TNumberz;
428 arg5 : Int64;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200429 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000430 StartTick : Cardinal;
431 k : Integer;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200432 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000433 hello, goodbye : IXtruct;
434 crazy : IInsanity;
435 looney : IInsanity;
436 first_map : IThriftDictionary<TNumberz, IInsanity>;
437 second_map : IThriftDictionary<TNumberz, IInsanity>;
Jens Geyer540e3462016-12-28 14:25:41 +0100438 pair : TPair<TNumberz, TUserId>;
Jens Geyer85827152018-01-12 21:20:59 +0100439 testsize : TTestSize;
Roger Meier3bef8c22012-10-06 06:58:00 +0000440begin
441 client := TThriftTest.TClient.Create( FProtocol);
442 FTransport.Open;
443
Jens Geyer06045cf2013-03-27 20:26:25 +0200444 {$IFDEF StressTest}
445 StressTest( client);
446 {$ENDIF StressTest}
447
Jens Geyer17c3ad92017-09-05 20:31:27 +0200448 {$IFDEF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000449 // in-depth exception test
450 // (1) do we get an exception at all?
451 // (2) do we get the right exception?
452 // (3) does the exception contain the expected data?
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200453 StartTestGroup( 'testException', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000454 // case 1: exception type declared in IDL at the function call
455 try
456 client.testException('Xception');
457 Expect( FALSE, 'testException(''Xception''): must trow an exception');
458 except
459 on e:TXception do begin
460 Expect( e.ErrorCode = 1001, 'error code');
461 Expect( e.Message_ = 'Xception', 'error message');
462 Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ );
463 end;
464 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200465 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000466 end;
467
468 // case 2: exception type NOT declared in IDL at the function call
469 // this will close the connection
470 try
471 client.testException('TException');
472 Expect( FALSE, 'testException(''TException''): must trow an exception');
473 except
474 on e:TTransportException do begin
475 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Roger Meier3bef8c22012-10-06 06:58:00 +0000476 end;
Jens Geyer6bbbf192014-09-07 01:45:56 +0200477 on e:TApplicationException do begin
478 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Jens Geyer6bbbf192014-09-07 01:45:56 +0200479 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200480 on e:TException do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
481 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000482 end;
483
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100484
Jens Geyer2ad6c302015-02-26 19:38:53 +0100485 if FTransport.IsOpen then FTransport.Close;
486 FTransport.Open; // re-open connection, server has already closed
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100487
Jens Geyer2ad6c302015-02-26 19:38:53 +0100488
Roger Meier3bef8c22012-10-06 06:58:00 +0000489 // case 3: no exception
490 try
491 client.testException('something');
492 Expect( TRUE, 'testException(''something''): must not trow an exception');
493 except
494 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200495 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000496 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200497 {$ENDIF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000498
499
500 // simple things
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200501 StartTestGroup( 'simple Thrift calls', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000502 client.testVoid();
503 Expect( TRUE, 'testVoid()'); // success := no exception
504
Jens Geyer39ba6b72015-09-22 00:00:49 +0200505 s := BoolToString( client.testBool(TRUE));
506 Expect( s = BoolToString(TRUE), 'testBool(TRUE) = '+s);
507 s := BoolToString( client.testBool(FALSE));
508 Expect( s = BoolToString(FALSE), 'testBool(FALSE) = '+s);
509
Roger Meier3bef8c22012-10-06 06:58:00 +0000510 s := client.testString('Test');
511 Expect( s = 'Test', 'testString(''Test'') = "'+s+'"');
512
Jens Geyercf892d42017-09-09 10:08:22 +0200513 s := client.testString(''); // empty string
514 Expect( s = '', 'testString('''') = "'+s+'"');
515
Jens Geyer06045cf2013-03-27 20:26:25 +0200516 s := client.testString(HUGE_TEST_STRING);
517 Expect( length(s) = length(HUGE_TEST_STRING),
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100518 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
Jens Geyer06045cf2013-03-27 20:26:25 +0200519 +'=> length(result) = '+IntToStr(Length(s)));
520
Roger Meier3bef8c22012-10-06 06:58:00 +0000521 i8 := client.testByte(1);
522 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
523
524 i32 := client.testI32(-1);
525 Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32));
526
527 Console.WriteLine('testI64(-34359738368)');
528 i64 := client.testI64(-34359738368);
529 Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
530
Jens Geyerd4df9172017-10-25 22:30:23 +0200531 // random binary small
Jens Geyer85827152018-01-12 21:20:59 +0100532 for testsize := Low(TTestSize) to High(TTestSize) do begin
533 binOut := PrepareBinaryData( TRUE, testsize);
534 Console.WriteLine('testBinary('+BytesToHex(binOut)+')');
535 try
536 binIn := client.testBinary(binOut);
537 Expect( Length(binOut) = Length(binIn), 'testBinary(): length '+IntToStr(Length(binOut))+' = '+IntToStr(Length(binIn)));
538 i32 := Min( Length(binOut), Length(binIn));
539 Expect( CompareMem( binOut, binIn, i32), 'testBinary('+BytesToHex(binOut)+') = '+BytesToHex(binIn));
540 except
541 on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message);
542 on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message);
543 end;
Jens Geyercf892d42017-09-09 10:08:22 +0200544 end;
545
Roger Meier3bef8c22012-10-06 06:58:00 +0000546 Console.WriteLine('testDouble(5.325098235)');
547 dub := client.testDouble(5.325098235);
548 Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
549
550 // structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200551 StartTestGroup( 'testStruct', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000552 Console.WriteLine('testStruct({''Zero'', 1, -3, -5})');
553 o := TXtructImpl.Create;
554 o.String_thing := 'Zero';
555 o.Byte_thing := 1;
556 o.I32_thing := -3;
557 o.I64_thing := -5;
558 i := client.testStruct(o);
559 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
560 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
561 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
562 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
563 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
564 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
565 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
566 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
567
568 // nested structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200569 StartTestGroup( 'testNest', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000570 Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})');
571 o2 := TXtruct2Impl.Create;
572 o2.Byte_thing := 1;
573 o2.Struct_thing := o;
574 o2.I32_thing := 5;
575 i2 := client.testNest(o2);
576 i := i2.Struct_thing;
577 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
578 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
579 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
580 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
581 Expect( i2.Byte_thing = 1, 'i2.Byte_thing = '+IntToStr(i2.Byte_thing));
582 Expect( i2.I32_thing = 5, 'i2.I32_thing = '+IntToStr(i2.I32_thing));
583 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
584 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
585 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
586 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
587 Expect( i2.__isset_Byte_thing, 'i2.__isset_Byte_thing');
588 Expect( i2.__isset_I32_thing, 'i2.__isset_I32_thing');
589
590 // map<type1,type2>: A map of strictly unique keys to values.
591 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200592 StartTestGroup( 'testMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000593 mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
594 for j := 0 to 4 do
595 begin
596 mapout.AddOrSetValue( j, j - 10);
597 end;
598 Console.Write('testMap({');
599 first := True;
600 for key in mapout.Keys do
601 begin
602 if first
603 then first := False
604 else Console.Write( ', ' );
605 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
606 end;
607 Console.WriteLine('})');
608
609 mapin := client.testMap( mapout );
610 Expect( mapin.Count = mapout.Count, 'testMap: mapin.Count = mapout.Count');
611 for j := 0 to 4 do
612 begin
613 Expect( mapout.ContainsKey(j), 'testMap: mapout.ContainsKey('+IntToStr(j)+') = '+BoolToString(mapout.ContainsKey(j)));
614 end;
615 for key in mapin.Keys do
616 begin
617 Expect( mapin[key] = mapout[key], 'testMap: '+IntToStr(key) + ' => ' + IntToStr( mapin[key]));
618 Expect( mapin[key] = key - 10, 'testMap: mapin['+IntToStr(key)+'] = '+IntToStr( mapin[key]));
619 end;
620
621
622 // map<type1,type2>: A map of strictly unique keys to values.
623 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200624 StartTestGroup( 'testStringMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000625 strmapout := TThriftDictionaryImpl<string,string>.Create;
626 for j := 0 to 4 do
627 begin
628 strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10));
629 end;
630 Console.Write('testStringMap({');
631 first := True;
632 for strkey in strmapout.Keys do
633 begin
634 if first
635 then first := False
636 else Console.Write( ', ' );
637 Console.Write( strkey + ' => ' + strmapout[strkey]);
638 end;
639 Console.WriteLine('})');
640
641 strmapin := client.testStringMap( strmapout );
642 Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count');
643 for j := 0 to 4 do
644 begin
645 Expect( strmapout.ContainsKey(IntToStr(j)),
646 'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = '
647 + BoolToString(strmapout.ContainsKey(IntToStr(j))));
648 end;
649 for strkey in strmapin.Keys do
650 begin
651 Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]);
652 Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]);
653 end;
654
655
656 // set<type>: An unordered set of unique elements.
657 // Translates to an STL set, Java HashSet, set in Python, etc.
658 // Note: PHP does not support sets, so it is treated similar to a List
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200659 StartTestGroup( 'testSet', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000660 setout := THashSetImpl<Integer>.Create;
661 for j := -2 to 2 do
662 begin
663 setout.Add( j );
664 end;
665 Console.Write('testSet({');
666 first := True;
667 for j in setout do
668 begin
669 if first
670 then first := False
671 else Console.Write(', ');
672 Console.Write(IntToStr( j));
673 end;
674 Console.WriteLine('})');
675
676 setin := client.testSet(setout);
677 Expect( setin.Count = setout.Count, 'testSet: setin.Count = setout.Count');
678 Expect( setin.Count = 5, 'testSet: setin.Count = '+IntToStr(setin.Count));
679 for j := -2 to 2 do // unordered, we can't rely on the order => test for known elements only
680 begin
681 Expect( setin.Contains(j), 'testSet: setin.Contains('+IntToStr(j)+') => '+BoolToString(setin.Contains(j)));
682 end;
683
684 // list<type>: An ordered list of elements.
685 // Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200686 StartTestGroup( 'testList', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000687 listout := TThriftListImpl<Integer>.Create;
688 listout.Add( +1);
689 listout.Add( -2);
690 listout.Add( +3);
691 listout.Add( -4);
692 listout.Add( 0);
693 Console.Write('testList({');
694 first := True;
695 for j in listout do
696 begin
697 if first
698 then first := False
699 else Console.Write(', ');
700 Console.Write(IntToStr( j));
701 end;
702 Console.WriteLine('})');
703
704 listin := client.testList(listout);
705 Expect( listin.Count = listout.Count, 'testList: listin.Count = listout.Count');
706 Expect( listin.Count = 5, 'testList: listin.Count = '+IntToStr(listin.Count));
707 Expect( listin[0] = +1, 'listin[0] = '+IntToStr( listin[0]));
708 Expect( listin[1] = -2, 'listin[1] = '+IntToStr( listin[1]));
709 Expect( listin[2] = +3, 'listin[2] = '+IntToStr( listin[2]));
710 Expect( listin[3] = -4, 'listin[3] = '+IntToStr( listin[3]));
711 Expect( listin[4] = 0, 'listin[4] = '+IntToStr( listin[4]));
712
713 // enums
714 ret := client.testEnum(TNumberz.ONE);
715 Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret)));
716
717 ret := client.testEnum(TNumberz.TWO);
718 Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret)));
719
720 ret := client.testEnum(TNumberz.THREE);
721 Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret)));
722
723 ret := client.testEnum(TNumberz.FIVE);
724 Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret)));
725
726 ret := client.testEnum(TNumberz.EIGHT);
727 Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret)));
728
729
730 // typedef
731 uid := client.testTypedef(309858235082523);
732 Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid));
733
734
735 // maps of maps
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200736 StartTestGroup( 'testMapMap(1)', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000737 mm := client.testMapMap(1);
738 Console.Write(' = {');
739 for key in mm.Keys do
740 begin
741 Console.Write( IntToStr( key) + ' => {');
742 m2 := mm[key];
743 for k2 in m2.Keys do
744 begin
745 Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', ');
746 end;
747 Console.Write('}, ');
748 end;
749 Console.WriteLine('}');
750
751 // verify result data
752 Expect( mm.Count = 2, 'mm.Count = '+IntToStr(mm.Count));
753 pos := mm[4];
754 neg := mm[-4];
755 for j := 1 to 4 do
756 begin
757 Expect( pos[j] = j, 'pos[j] = '+IntToStr(pos[j]));
758 Expect( neg[-j] = -j, 'neg[-j] = '+IntToStr(neg[-j]));
759 end;
760
761
762
763 // insanity
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200764 StartTestGroup( 'testInsanity', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000765 insane := TInsanityImpl.Create;
766 insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
767 insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000);
768 truck := TXtructImpl.Create;
769 truck.String_thing := 'Truck';
Jens Geyer540e3462016-12-28 14:25:41 +0100770 truck.Byte_thing := -8; // byte is signed
771 truck.I32_thing := 32;
772 truck.I64_thing := 64;
Roger Meier3bef8c22012-10-06 06:58:00 +0000773 insane.Xtructs := TThriftListImpl<IXtruct>.Create;
774 insane.Xtructs.Add( truck );
775 whoa := client.testInsanity( insane );
776 Console.Write(' = {');
777 for key64 in whoa.Keys do
778 begin
779 val := whoa[key64];
780 Console.Write( IntToStr( key64) + ' => {');
781 for k2_2 in val.Keys do
782 begin
783 v2 := val[k2_2];
784 Console.Write( IntToStr( Integer( k2_2)) + ' => {');
785 userMap := v2.UserMap;
786 Console.Write('{');
787 if userMap <> nil then
788 begin
789 for k3 in userMap.Keys do
790 begin
791 Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', ');
792 end;
793 end else
794 begin
795 Console.Write('null');
796 end;
797 Console.Write('}, ');
798 xtructs := v2.Xtructs;
799 Console.Write('{');
800
801 if xtructs <> nil then
802 begin
803 for x in xtructs do
804 begin
805 Console.Write('{"' + x.String_thing + '", ' +
806 IntToStr( x.Byte_thing) + ', ' +
807 IntToStr( x.I32_thing) + ', ' +
808 IntToStr( x.I32_thing) + '}, ');
809 end;
810 end else
811 begin
812 Console.Write('null');
813 end;
814 Console.Write('}');
815 Console.Write('}, ');
816 end;
817 Console.Write('}, ');
818 end;
819 Console.WriteLine('}');
820
Jens Geyer540e3462016-12-28 14:25:41 +0100821 (**
822 * So you think you've got this all worked, out eh?
823 *
824 * Creates a the returned map with these values and prints it out:
825 * { 1 => { 2 => argument,
826 * 3 => argument,
827 * },
828 * 2 => { 6 => <empty Insanity struct>, },
829 * }
830 * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
831 *)
832
Roger Meier3bef8c22012-10-06 06:58:00 +0000833 // verify result data
834 Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count));
835 //
836 first_map := whoa[1];
837 second_map := whoa[2];
838 Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count));
839 Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count));
840 //
841 looney := second_map[TNumberz.SIX];
842 Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney)));
843 Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
844 Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
845 //
846 for ret in [TNumberz.TWO, TNumberz.THREE] do begin
847 crazy := first_map[ret];
848 Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
849
850 Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap));
851 Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs));
852
Jens Geyer540e3462016-12-28 14:25:41 +0100853 Expect( crazy.UserMap.Count = insane.UserMap.Count, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count));
854 for pair in insane.UserMap do begin
855 Expect( crazy.UserMap[pair.Key] = pair.Value, 'crazy.UserMap['+IntToStr(Ord(pair.key))+'] = '+IntToStr(crazy.UserMap[pair.Key]));
856 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000857
Jens Geyer540e3462016-12-28 14:25:41 +0100858 Expect( crazy.Xtructs.Count = insane.Xtructs.Count, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count));
859 for arg0 := 0 to insane.Xtructs.Count-1 do begin
860 hello := insane.Xtructs[arg0];
861 goodbye := crazy.Xtructs[arg0];
862 Expect( goodbye.String_thing = hello.String_thing, 'goodbye.String_thing = '+goodbye.String_thing);
863 Expect( goodbye.Byte_thing = hello.Byte_thing, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing));
864 Expect( goodbye.I32_thing = hello.I32_thing, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing));
865 Expect( goodbye.I64_thing = hello.I64_thing, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing));
866 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000867 end;
868
869
870 // multi args
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200871 StartTestGroup( 'testMulti', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000872 arg0 := 1;
873 arg1 := 2;
874 arg2 := High(Int64);
875 arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
876 arg3.AddOrSetValue( 1, 'one');
877 arg4 := TNumberz.FIVE;
878 arg5 := 5000000;
879 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
880 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
881 arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
882 IntToStr( arg5) + ')');
883
884 i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5);
885 Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"');
886 Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing));
887 Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing));
888 Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing));
889 Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
890 Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
891 Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
892 Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
893
894 // multi exception
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200895 StartTestGroup( 'testMultiException(1)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000896 try
897 i := client.testMultiException( 'need more pizza', 'run out of beer');
898 Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"');
899 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
Jens Geyer6bbbf192014-09-07 01:45:56 +0200900 { this is not necessarily true, these fields are default-serialized
Jens Geyerd5436f52014-10-03 19:50:38 +0200901 Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
Roger Meier3bef8c22012-10-06 06:58:00 +0000902 Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
903 Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
Jens Geyerd5436f52014-10-03 19:50:38 +0200904 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000905 except
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200906 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000907 end;
908
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200909 StartTestGroup( 'testMultiException(Xception)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000910 try
911 i := client.testMultiException( 'Xception', 'second test');
912 Expect( FALSE, 'testMultiException(''Xception''): must trow an exception');
913 except
914 on x:TXception do begin
915 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
916 Expect( x.__isset_Message_, 'x.__isset_Message_ = '+BoolToString(x.__isset_Message_));
917 Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
918 Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"');
919 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200920 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000921 end;
922
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200923 StartTestGroup( 'testMultiException(Xception2)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000924 try
925 i := client.testMultiException( 'Xception2', 'third test');
926 Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception');
927 except
928 on x:TXception2 do begin
929 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
930 Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing));
931 Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
932 Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"');
933 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 +0200934 { this is not necessarily true, these fields are default-serialized
Roger Meier3bef8c22012-10-06 06:58:00 +0000935 Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing));
936 Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing));
937 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 +0200938 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000939 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200940 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000941 end;
942
943
944 // oneway functions
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200945 StartTestGroup( 'Test Oneway(1)', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000946 client.testOneway(1);
947 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
948
949 // call time
Jens Geyer06045cf2013-03-27 20:26:25 +0200950 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000951 StartTestGroup( 'Test Calltime()');
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200952 StartTick := GetTickCount;
Roger Meier3bef8c22012-10-06 06:58:00 +0000953 for k := 0 to 1000 - 1 do
954 begin
955 client.testVoid();
956 end;
957 Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
Jens Geyer06045cf2013-03-27 20:26:25 +0200958 {$ENDIF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000959
960 // no more tests here
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200961 StartTestGroup( '', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000962end;
963
964
Jens Geyer14f5d502017-12-09 13:47:09 +0100965{$IFDEF SupportsAsync}
966procedure TClientThread.ClientAsyncTest;
967var
968 client : TThriftTest.IAsync;
969 s : string;
970 i8 : ShortInt;
971begin
972 StartTestGroup( 'Async Tests', test_Unknown);
973 client := TThriftTest.TClient.Create( FProtocol);
974 FTransport.Open;
975
976 // oneway void functions
977 client.testOnewayAsync(1).Wait;
978 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
979
980 // normal functions
981 s := client.testStringAsync(HUGE_TEST_STRING).Value;
982 Expect( length(s) = length(HUGE_TEST_STRING),
983 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
984 +'=> length(result) = '+IntToStr(Length(s)));
985
986 i8 := client.testByte(1).Value;
987 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
988end;
989{$ENDIF}
990
991
Jens Geyer718f6ee2013-09-06 21:02:34 +0200992{$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200993procedure TClientThread.StressTest(const client : TThriftTest.Iface);
994begin
995 while TRUE do begin
996 try
997 if not FTransport.IsOpen then FTransport.Open; // re-open connection, server has already closed
998 try
999 client.testString('Test');
1000 Write('.');
1001 finally
1002 if FTransport.IsOpen then FTransport.Close;
1003 end;
1004 except
1005 on e:Exception do Writeln(#10+e.message);
1006 end;
1007 end;
1008end;
Jens Geyer718f6ee2013-09-06 21:02:34 +02001009{$ENDIF}
Jens Geyer06045cf2013-03-27 20:26:25 +02001010
Jens Geyerfd1b3582014-12-13 23:42:58 +01001011
Jens Geyer85827152018-01-12 21:20:59 +01001012function TClientThread.PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes;
Jens Geyerd4df9172017-10-25 22:30:23 +02001013var i : Integer;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001014begin
Jens Geyer85827152018-01-12 21:20:59 +01001015 case aSize of
1016 Empty : SetLength( result, 0);
1017 Normal : SetLength( result, $100);
1018 ByteArrayTest : SetLength( result, SizeOf(TByteArray) + 128);
1019 PipeWriteLimit : SetLength( result, 65535 + 128);
1020 else
1021 raise EArgumentException.Create('aSize');
1022 end;
1023
Jens Geyerfd1b3582014-12-13 23:42:58 +01001024 ASSERT( Low(result) = 0);
Jens Geyer85827152018-01-12 21:20:59 +01001025 if Length(result) = 0 then Exit;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001026
1027 // linear distribution, unless random is requested
1028 if not aRandomDist then begin
1029 for i := Low(result) to High(result) do begin
Jens Geyer85827152018-01-12 21:20:59 +01001030 result[i] := i mod $100;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001031 end;
1032 Exit;
1033 end;
1034
1035 // random distribution of all 256 values
1036 FillChar( result[0], Length(result) * SizeOf(result[0]), $0);
Jens Geyerd4df9172017-10-25 22:30:23 +02001037 for i := Low(result) to High(result) do begin
1038 result[i] := Byte( Random($100));
Jens Geyerfd1b3582014-12-13 23:42:58 +01001039 end;
1040end;
1041
1042
Jens Geyerf7904452017-07-26 15:02:12 +02001043{$IFDEF Win64}
1044procedure TClientThread.UseInterlockedExchangeAdd64;
1045var a,b : Int64;
1046begin
1047 a := 1;
1048 b := 2;
1049 Thrift.Utils.InterlockedExchangeAdd64( a,b);
1050 Expect( a = 3, 'InterlockedExchangeAdd64');
1051end;
1052{$ENDIF}
1053
1054
Roger Meier3bef8c22012-10-06 06:58:00 +00001055procedure TClientThread.JSONProtocolReadWriteTest;
1056// Tests only then read/write procedures of the JSON protocol
1057// All tests succeed, if we can read what we wrote before
1058// Note that passing this test does not imply, that our JSON is really compatible to what
1059// other clients or servers expect as the real JSON. This is beyond the scope of this test.
1060var prot : IProtocol;
1061 stm : TStringStream;
Jens Geyer17c3ad92017-09-05 20:31:27 +02001062 list : TThriftList;
Jens Geyercf892d42017-09-09 10:08:22 +02001063 binary, binRead, emptyBinary : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +00001064 i,iErr : Integer;
1065const
1066 TEST_SHORT = ShortInt( $FE);
1067 TEST_SMALL = SmallInt( $FEDC);
1068 TEST_LONG = LongInt( $FEDCBA98);
1069 TEST_I64 = Int64( $FEDCBA9876543210);
1070 TEST_DOUBLE = -1.234e-56;
1071 DELTA_DOUBLE = TEST_DOUBLE * 1e-14;
1072 TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars")
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001073 // Test THRIFT-2336 and THRIFT-3404 with U+1D11E (G Clef symbol) and 'Русское Название';
1074 G_CLEF_AND_CYRILLIC_TEXT = #$1d11e' '#$0420#$0443#$0441#$0441#$043a#$043e#$0435' '#$041d#$0430#$0437#$0432#$0430#$043d#$0438#$0435;
1075 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 +01001076 // test both possible solidus encodings
1077 SOLIDUS_JSON_DATA = '"one/two\/three"';
1078 SOLIDUS_EXCPECTED = 'one/two/three';
Roger Meier3bef8c22012-10-06 06:58:00 +00001079begin
1080 stm := TStringStream.Create;
1081 try
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001082 StartTestGroup( 'JsonProtocolTest', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001083
1084 // prepare binary data
Jens Geyer85827152018-01-12 21:20:59 +01001085 binary := PrepareBinaryData( FALSE, Normal);
Jens Geyercf892d42017-09-09 10:08:22 +02001086 SetLength( emptyBinary, 0); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001087
1088 // output setup
1089 prot := TJSONProtocolImpl.Create(
1090 TStreamTransportImpl.Create(
1091 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
1092
1093 // write
Jens Geyer17c3ad92017-09-05 20:31:27 +02001094 Init( list, TType.String_, 9);
1095 prot.WriteListBegin( list);
Roger Meier3bef8c22012-10-06 06:58:00 +00001096 prot.WriteBool( TRUE);
1097 prot.WriteBool( FALSE);
1098 prot.WriteByte( TEST_SHORT);
1099 prot.WriteI16( TEST_SMALL);
1100 prot.WriteI32( TEST_LONG);
1101 prot.WriteI64( TEST_I64);
1102 prot.WriteDouble( TEST_DOUBLE);
1103 prot.WriteString( TEST_STRING);
1104 prot.WriteBinary( binary);
Jens Geyercf892d42017-09-09 10:08:22 +02001105 prot.WriteString( ''); // empty string
1106 prot.WriteBinary( emptyBinary); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001107 prot.WriteListEnd;
1108
1109 // input setup
1110 Expect( stm.Position = stm.Size, 'Stream position/length after write');
1111 stm.Position := 0;
1112 prot := TJSONProtocolImpl.Create(
1113 TStreamTransportImpl.Create(
1114 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1115
1116 // read and compare
1117 list := prot.ReadListBegin;
1118 Expect( list.ElementType = TType.String_, 'list element type');
1119 Expect( list.Count = 9, 'list element count');
1120 Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE');
1121 Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE');
1122 Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte');
1123 Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16');
1124 Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32');
1125 Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64');
1126 Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble');
1127 Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString');
1128 binRead := prot.ReadBinary;
Jens Geyercf892d42017-09-09 10:08:22 +02001129 Expect( Length(prot.ReadString) = 0, 'WriteString/ReadString (empty string)');
1130 Expect( Length(prot.ReadBinary) = 0, 'empty WriteBinary/ReadBinary (empty data block)');
Roger Meier3bef8c22012-10-06 06:58:00 +00001131 prot.ReadListEnd;
1132
1133 // test binary data
1134 Expect( Length(binary) = Length(binRead), 'Binary data length check');
1135 iErr := -1;
1136 for i := Low(binary) to High(binary) do begin
1137 if binary[i] <> binRead[i] then begin
1138 iErr := i;
1139 Break;
1140 end;
1141 end;
1142 if iErr < 0
1143 then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)')
1144 else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr));
1145
1146 Expect( stm.Position = stm.Size, 'Stream position after read');
1147
Jens Geyer7bb44a32014-02-07 22:24:37 +01001148
Jens Geyer21366942013-12-30 22:04:51 +01001149 // Solidus can be encoded in two ways. Make sure we can read both
1150 stm.Position := 0;
1151 stm.Size := 0;
1152 stm.WriteString(SOLIDUS_JSON_DATA);
1153 stm.Position := 0;
1154 prot := TJSONProtocolImpl.Create(
1155 TStreamTransportImpl.Create(
1156 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1157 Expect( prot.ReadString = SOLIDUS_EXCPECTED, 'Solidus encoding');
1158
1159
Jens Geyer7bb44a32014-02-07 22:24:37 +01001160 // Widechars should work too. Do they?
1161 // After writing, we ensure that we are able to read it back
1162 // We can't assume hex-encoding, since (nearly) any Unicode char is valid JSON
1163 stm.Position := 0;
1164 stm.Size := 0;
1165 prot := TJSONProtocolImpl.Create(
1166 TStreamTransportImpl.Create(
1167 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001168 prot.WriteString( G_CLEF_AND_CYRILLIC_TEXT);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001169 stm.Position := 0;
1170 prot := TJSONProtocolImpl.Create(
1171 TStreamTransportImpl.Create(
1172 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001173 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Writing JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001174
1175 // Widechars should work with hex-encoding too. Do they?
1176 stm.Position := 0;
1177 stm.Size := 0;
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001178 stm.WriteString( G_CLEF_AND_CYRILLIC_JSON);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001179 stm.Position := 0;
1180 prot := TJSONProtocolImpl.Create(
1181 TStreamTransportImpl.Create(
1182 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001183 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Reading JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001184
1185
Roger Meier3bef8c22012-10-06 06:58:00 +00001186 finally
1187 stm.Free;
1188 prot := nil; //-> Release
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001189 StartTestGroup( '', test_Unknown); // no more tests here
Roger Meier3bef8c22012-10-06 06:58:00 +00001190 end;
1191end;
1192
1193
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001194procedure TClientThread.StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +00001195begin
1196 FTestGroup := aGroup;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001197 FCurrentTest := aTest;
1198
1199 Include( FExecuted, aTest);
1200
Roger Meier3bef8c22012-10-06 06:58:00 +00001201 if FTestGroup <> '' then begin
1202 Console.WriteLine('');
1203 Console.WriteLine( aGroup+' tests');
1204 Console.WriteLine( StringOfChar('-',60));
1205 end;
1206end;
1207
1208
1209procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
1210begin
1211 if aTestResult then begin
1212 Inc(FSuccesses);
1213 Console.WriteLine( aTestInfo+': passed');
1214 end
1215 else begin
1216 FErrors.Add( FTestGroup+': '+aTestInfo);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001217 Include( FFailed, FCurrentTest);
Roger Meier3bef8c22012-10-06 06:58:00 +00001218 Console.WriteLine( aTestInfo+': *** FAILED ***');
1219
1220 // We have a failed test!
1221 // -> issue DebugBreak ONLY if a debugger is attached,
1222 // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise
Jens Geyer9f7f11e2016-04-14 21:37:11 +02001223 if IsDebuggerPresent
1224 then {$IFDEF CPUX64} DebugBreak {$ELSE} asm int 3 end {$ENDIF};
Roger Meier3bef8c22012-10-06 06:58:00 +00001225 end;
1226end;
1227
1228
1229procedure TClientThread.ReportResults;
1230var nTotal : Integer;
1231 sLine : string;
1232begin
1233 // prevent us from stupid DIV/0 errors
1234 nTotal := FSuccesses + FErrors.Count;
1235 if nTotal = 0 then begin
1236 Console.WriteLine('No results logged');
1237 Exit;
1238 end;
1239
1240 Console.WriteLine('');
1241 Console.WriteLine( StringOfChar('=',60));
1242 Console.WriteLine( IntToStr(nTotal)+' tests performed');
1243 Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)');
1244 Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)');
1245 Console.WriteLine( StringOfChar('=',60));
1246 if FErrors.Count > 0 then begin
1247 Console.WriteLine('FAILED TESTS:');
1248 for sLine in FErrors do Console.WriteLine('- '+sLine);
1249 Console.WriteLine( StringOfChar('=',60));
1250 InterlockedIncrement( ExitCode); // return <> 0 on errors
1251 end;
1252 Console.WriteLine('');
1253end;
1254
1255
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001256function TClientThread.CalculateExitCode : Byte;
1257var test : TTestGroup;
1258begin
1259 result := EXITCODE_SUCCESS;
1260 for test := Low(TTestGroup) to High(TTestGroup) do begin
1261 if (test in FFailed) or not (test in FExecuted)
1262 then result := result or MAP_FAILURES_TO_EXITCODE_BITS[test];
1263 end;
1264end;
1265
1266
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001267constructor TClientThread.Create( const aSetup : TTestSetup; const aNumIteration: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +00001268begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001269 FSetup := aSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +00001270 FNumIteration := ANumIteration;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001271
Roger Meier3bef8c22012-10-06 06:58:00 +00001272 FConsole := TThreadConsole.Create( Self );
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001273 FCurrentTest := test_Unknown;
Roger Meier3bef8c22012-10-06 06:58:00 +00001274
1275 // error list: keep correct order, allow for duplicates
1276 FErrors := TStringList.Create;
1277 FErrors.Sorted := FALSE;
1278 FErrors.Duplicates := dupAccept;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001279
1280 inherited Create( TRUE);
Roger Meier3bef8c22012-10-06 06:58:00 +00001281end;
1282
1283destructor TClientThread.Destroy;
1284begin
1285 FreeAndNil( FConsole);
1286 FreeAndNil( FErrors);
1287 inherited;
1288end;
1289
1290procedure TClientThread.Execute;
1291var
1292 i : Integer;
Roger Meier3bef8c22012-10-06 06:58:00 +00001293begin
1294 // perform all tests
1295 try
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001296 {$IFDEF Win64}
Jens Geyerf7904452017-07-26 15:02:12 +02001297 UseInterlockedExchangeAdd64;
Jens Geyer14f5d502017-12-09 13:47:09 +01001298 {$ENDIF}
Jens Geyer7bb44a32014-02-07 22:24:37 +01001299 JSONProtocolReadWriteTest;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001300
1301 // must be run in the context of the thread
1302 InitializeProtocolTransportStack;
1303 try
1304 for i := 0 to FNumIteration - 1 do begin
1305 ClientTest;
1306 {$IFDEF SupportsAsync}
1307 ClientAsyncTest;
1308 {$ENDIF}
1309 end;
1310
1311 // report the outcome
1312 ReportResults;
1313 SetReturnValue( CalculateExitCode);
1314
1315 finally
1316 ShutdownProtocolTransportStack;
Roger Meier3bef8c22012-10-06 06:58:00 +00001317 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001318
Roger Meier3bef8c22012-10-06 06:58:00 +00001319 except
1320 on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"');
1321 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001322end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001323
Roger Meier3bef8c22012-10-06 06:58:00 +00001324
Jens Geyer02230912019-04-03 01:12:51 +02001325function TClientThread.InitializeHttpTransport( const aTimeoutSetting : Integer) : IHTTPClient;
Jens Geyer83ff7532019-06-06 22:46:03 +02001326var sUrl : string;
1327 comps : URL_COMPONENTS;
1328 dwChars : DWORD;
Jens Geyer02230912019-04-03 01:12:51 +02001329begin
1330 ASSERT( FSetup.endpoint in [trns_MsxmlHttp, trns_WinHttp]);
1331
1332 if FSetup.useSSL
1333 then sUrl := 'https://'
1334 else sUrl := 'http://';
1335
1336 sUrl := sUrl + FSetup.host;
1337
Jens Geyer83ff7532019-06-06 22:46:03 +02001338 // add the port number if necessary and at the right place
1339 FillChar( comps, SizeOf(comps), 0);
1340 comps.dwStructSize := SizeOf(comps);
1341 comps.dwSchemeLength := MAXINT;
1342 comps.dwHostNameLength := MAXINT;
1343 comps.dwUserNameLength := MAXINT;
1344 comps.dwPasswordLength := MAXINT;
1345 comps.dwUrlPathLength := MAXINT;
1346 comps.dwExtraInfoLength := MAXINT;
1347 Win32Check( WinHttpCrackUrl( PChar(sUrl), Length(sUrl), 0, comps));
Jens Geyer02230912019-04-03 01:12:51 +02001348 case FSetup.port of
Jens Geyer83ff7532019-06-06 22:46:03 +02001349 80 : if FSetup.useSSL then comps.nPort := FSetup.port;
1350 443 : if not FSetup.useSSL then comps.nPort := FSetup.port;
Jens Geyer02230912019-04-03 01:12:51 +02001351 else
Jens Geyer83ff7532019-06-06 22:46:03 +02001352 if FSetup.port > 0 then comps.nPort := FSetup.port;
Jens Geyer02230912019-04-03 01:12:51 +02001353 end;
Jens Geyer83ff7532019-06-06 22:46:03 +02001354 dwChars := Length(sUrl) + 64;
1355 SetLength( sUrl, dwChars);
1356 Win32Check( WinHttpCreateUrl( comps, 0, @sUrl[1], dwChars));
1357 SetLength( sUrl, dwChars);
1358
Jens Geyer02230912019-04-03 01:12:51 +02001359
1360 Console.WriteLine('Target URL: '+sUrl);
1361 case FSetup.endpoint of
1362 trns_MsxmlHttp : result := TMsxmlHTTPClientImpl.Create( sUrl);
1363 trns_WinHttp : result := TWinHTTPClientImpl.Create( sUrl);
1364 else
1365 raise Exception.Create(ENDPOINT_TRANSPORTS[FSetup.endpoint]+' unhandled case');
1366 end;
1367
1368 result.DnsResolveTimeout := aTimeoutSetting;
1369 result.ConnectionTimeout := aTimeoutSetting;
1370 result.SendTimeout := aTimeoutSetting;
1371 result.ReadTimeout := aTimeoutSetting;
1372end;
1373
1374
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001375procedure TClientThread.InitializeProtocolTransportStack;
Jens Geyer02230912019-04-03 01:12:51 +02001376var streamtrans : IStreamTransport;
Jens Geyer47f63172019-06-06 22:42:58 +02001377 canSSL : Boolean;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001378const
1379 DEBUG_TIMEOUT = 30 * 1000;
1380 RELEASE_TIMEOUT = DEFAULT_THRIFT_TIMEOUT;
1381 PIPE_TIMEOUT = RELEASE_TIMEOUT;
1382 HTTP_TIMEOUTS = 10 * 1000;
1383begin
1384 // needed for HTTP clients as they utilize the MSXML COM components
1385 OleCheck( CoInitialize( nil));
1386
Jens Geyer47f63172019-06-06 22:42:58 +02001387 canSSL := FALSE;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001388 case FSetup.endpoint of
1389 trns_Sockets: begin
1390 Console.WriteLine('Using sockets ('+FSetup.host+' port '+IntToStr(FSetup.port)+')');
1391 streamtrans := TSocketImpl.Create( FSetup.host, FSetup.port );
1392 FTransport := streamtrans;
1393 end;
1394
Jens Geyer02230912019-04-03 01:12:51 +02001395 trns_MsxmlHttp,
1396 trns_WinHttp: begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001397 Console.WriteLine('Using HTTPClient');
Jens Geyer02230912019-04-03 01:12:51 +02001398 FTransport := InitializeHttpTransport( HTTP_TIMEOUTS);
Jens Geyer47f63172019-06-06 22:42:58 +02001399 canSSL := TRUE;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001400 end;
1401
1402 trns_EvHttp: begin
1403 raise Exception.Create(ENDPOINT_TRANSPORTS[FSetup.endpoint]+' transport not implemented');
1404 end;
1405
1406 trns_NamedPipes: begin
1407 streamtrans := TNamedPipeTransportClientEndImpl.Create( FSetup.sPipeName, 0, nil, PIPE_TIMEOUT, PIPE_TIMEOUT);
1408 FTransport := streamtrans;
1409 end;
1410
1411 trns_AnonPipes: begin
1412 streamtrans := TAnonymousPipeTransportImpl.Create( FSetup.hAnonRead, FSetup.hAnonWrite, FALSE);
1413 FTransport := streamtrans;
1414 end;
1415
1416 else
1417 raise Exception.Create('Unhandled endpoint transport');
1418 end;
1419 ASSERT( FTransport <> nil);
1420
1421 // layered transports are not really meant to be stacked upon each other
1422 if (trns_Framed in FSetup.layered) then begin
1423 FTransport := TFramedTransportImpl.Create( FTransport);
1424 end
1425 else if (trns_Buffered in FSetup.layered) and (streamtrans <> nil) then begin
1426 FTransport := TBufferedTransportImpl.Create( streamtrans, 32); // small buffer to test read()
1427 end;
1428
Jens Geyer47f63172019-06-06 22:42:58 +02001429 if FSetup.useSSL and not canSSL then begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001430 raise Exception.Create('SSL/TLS not implemented');
1431 end;
1432
1433 // create protocol instance, default to BinaryProtocol
1434 case FSetup.protType of
1435 prot_Binary : FProtocol := TBinaryProtocolImpl.Create( FTransport, BINARY_STRICT_READ, BINARY_STRICT_WRITE);
1436 prot_JSON : FProtocol := TJSONProtocolImpl.Create( FTransport);
1437 prot_Compact : FProtocol := TCompactProtocolImpl.Create( FTransport);
1438 else
1439 raise Exception.Create('Unhandled protocol');
1440 end;
1441
1442 ASSERT( (FTransport <> nil) and (FProtocol <> nil));
1443end;
1444
1445
1446procedure TClientThread.ShutdownProtocolTransportStack;
1447begin
1448 try
1449 FProtocol := nil;
1450
1451 if FTransport <> nil then begin
Roger Meier3bef8c22012-10-06 06:58:00 +00001452 FTransport.Close;
1453 FTransport := nil;
1454 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001455
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001456 finally
1457 CoUninitialize;
1458 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001459end;
1460
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001461
Roger Meier3bef8c22012-10-06 06:58:00 +00001462{ TThreadConsole }
1463
1464constructor TThreadConsole.Create(AThread: TThread);
1465begin
Jens Geyer718f6ee2013-09-06 21:02:34 +02001466 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +00001467 FThread := AThread;
1468end;
1469
1470procedure TThreadConsole.Write(const S: string);
1471var
1472 proc : TThreadProcedure;
1473begin
1474 proc := procedure
1475 begin
1476 Console.Write( S );
1477 end;
1478 TThread.Synchronize( FThread, proc);
1479end;
1480
1481procedure TThreadConsole.WriteLine(const S: string);
1482var
1483 proc : TThreadProcedure;
1484begin
1485 proc := procedure
1486 begin
1487 Console.WriteLine( S );
1488 end;
1489 TThread.Synchronize( FThread, proc);
1490end;
1491
1492initialization
1493begin
1494 TTestClient.FNumIteration := 1;
1495 TTestClient.FNumThread := 1;
1496end;
1497
1498end.