blob: 0fa43b0be66e23e503f480a027db7b982b29b0d4 [file] [log] [blame]
Jens Geyer7bb44a32014-02-07 22:24:37 +01001(*
Roger Meier3bef8c22012-10-06 06:58:00 +00002 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *)
19
20unit TestClient;
21
Jens Geyer9f7f11e2016-04-14 21:37:11 +020022{$I ../src/Thrift.Defines.inc}
23
Jens Geyer06045cf2013-03-27 20:26:25 +020024{.$DEFINE StressTest} // activate to stress-test the server with frequent connects/disconnects
Jens Geyer17c3ad92017-09-05 20:31:27 +020025{.$DEFINE PerfTest} // activate the performance test
26{$DEFINE Exceptions} // activate the exceptions test (or disable while debugging)
Jens Geyer06045cf2013-03-27 20:26:25 +020027
Jens Geyer14f5d502017-12-09 13:47:09 +010028{$if CompilerVersion >= 28}
29{$DEFINE SupportsAsync}
30{$ifend}
31
Roger Meier3bef8c22012-10-06 06:58:00 +000032interface
33
34uses
Jens Geyeraf7ecd62018-06-22 22:41:27 +020035 Windows, SysUtils, Classes, Math, ComObj, ActiveX,
Jens Geyer14f5d502017-12-09 13:47:09 +010036 {$IFDEF SupportsAsync} System.Threading, {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +000037 DateUtils,
38 Generics.Collections,
39 TestConstants,
Jens Geyer3d556242018-01-24 19:14:32 +010040 ConsoleHelper,
Roger Meier3bef8c22012-10-06 06:58:00 +000041 Thrift,
Jens Geyerf0e63312015-03-01 18:47:49 +010042 Thrift.Protocol.Compact,
Roger Meier3bef8c22012-10-06 06:58:00 +000043 Thrift.Protocol.JSON,
44 Thrift.Protocol,
45 Thrift.Transport.Pipes,
46 Thrift.Transport,
47 Thrift.Stream,
48 Thrift.Test,
Jens Geyerf7904452017-07-26 15:02:12 +020049 Thrift.Utils,
Jens Geyer3d556242018-01-24 19:14:32 +010050 Thrift.Collections;
Roger Meier3bef8c22012-10-06 06:58:00 +000051
52type
53 TThreadConsole = class
54 private
55 FThread : TThread;
56 public
57 procedure Write( const S : string);
58 procedure WriteLine( const S : string);
59 constructor Create( AThread: TThread);
60 end;
61
Jens Geyeraf7ecd62018-06-22 22:41:27 +020062 TTestSetup = record
63 protType : TKnownProtocol;
64 endpoint : TEndpointTransport;
65 layered : TLayeredTransports;
66 useSSL : Boolean; // include where appropriate (TLayeredTransport?)
67 host : string;
68 port : Integer;
69 sPipeName : string;
70 hAnonRead, hAnonWrite : THandle;
71 end;
72
Roger Meier3bef8c22012-10-06 06:58:00 +000073 TClientThread = class( TThread )
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020074 private type
75 TTestGroup = (
76 test_Unknown,
77 test_BaseTypes,
78 test_Structs,
79 test_Containers,
80 test_Exceptions
81 // new values here
82 );
83 TTestGroups = set of TTestGroup;
84
Jens Geyer85827152018-01-12 21:20:59 +010085 TTestSize = (
86 Empty, // Edge case: the zero-length empty binary
87 Normal, // Fairly small array of usual size (256 bytes)
88 ByteArrayTest, // THRIFT-4454 Large writes/reads may cause range check errors in debug mode
89 PipeWriteLimit // THRIFT-4372 Pipe write operations across a network are limited to 65,535 bytes per write.
90 );
91
Roger Meier3bef8c22012-10-06 06:58:00 +000092 private
Jens Geyeraf7ecd62018-06-22 22:41:27 +020093 FSetup : TTestSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +000094 FTransport : ITransport;
95 FProtocol : IProtocol;
96 FNumIteration : Integer;
97 FConsole : TThreadConsole;
98
99 // test reporting, will be refactored out into separate class later
100 FTestGroup : string;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200101 FCurrentTest : TTestGroup;
Roger Meier3bef8c22012-10-06 06:58:00 +0000102 FSuccesses : Integer;
103 FErrors : TStringList;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200104 FFailed : TTestGroups;
105 FExecuted : TTestGroups;
106 procedure StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +0000107 procedure Expect( aTestResult : Boolean; const aTestInfo : string);
108 procedure ReportResults;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100109 function CalculateExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000110
111 procedure ClientTest;
Jens Geyer14f5d502017-12-09 13:47:09 +0100112 {$IFDEF SupportsAsync}
113 procedure ClientAsyncTest;
114 {$ENDIF}
115
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200116 procedure InitializeProtocolTransportStack;
117 procedure ShutdownProtocolTransportStack;
118
Roger Meier3bef8c22012-10-06 06:58:00 +0000119 procedure JSONProtocolReadWriteTest;
Jens Geyer85827152018-01-12 21:20:59 +0100120 function PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes;
Jens Geyer718f6ee2013-09-06 21:02:34 +0200121 {$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200122 procedure StressTest(const client : TThriftTest.Iface);
Jens Geyer718f6ee2013-09-06 21:02:34 +0200123 {$ENDIF}
Jens Geyerf7904452017-07-26 15:02:12 +0200124 {$IFDEF Win64}
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200125 procedure UseInterlockedExchangeAdd64;
Jens Geyerf7904452017-07-26 15:02:12 +0200126 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000127 protected
128 procedure Execute; override;
129 public
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200130 constructor Create( const aSetup : TTestSetup; const aNumIteration: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +0000131 destructor Destroy; override;
132 end;
133
134 TTestClient = class
135 private
136 class var
137 FNumIteration : Integer;
138 FNumThread : Integer;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200139
140 class procedure PrintCmdLineHelp;
141 class procedure InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000142 public
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200143 class function Execute( const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000144 end;
145
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200146
Roger Meier3bef8c22012-10-06 06:58:00 +0000147implementation
148
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200149const
150 EXITCODE_SUCCESS = $00; // no errors bits set
151 //
152 EXITCODE_FAILBIT_BASETYPES = $01;
153 EXITCODE_FAILBIT_STRUCTS = $02;
154 EXITCODE_FAILBIT_CONTAINERS = $04;
155 EXITCODE_FAILBIT_EXCEPTIONS = $08;
156
157 MAP_FAILURES_TO_EXITCODE_BITS : array[TClientThread.TTestGroup] of Byte = (
158 EXITCODE_SUCCESS, // no bits here
159 EXITCODE_FAILBIT_BASETYPES,
160 EXITCODE_FAILBIT_STRUCTS,
161 EXITCODE_FAILBIT_CONTAINERS,
162 EXITCODE_FAILBIT_EXCEPTIONS
163 );
164
165
166
Roger Meier3bef8c22012-10-06 06:58:00 +0000167function BoolToString( b : Boolean) : string;
168// overrides global BoolToString()
169begin
170 if b
171 then result := 'true'
172 else result := 'false';
173end;
174
175// not available in all versions, so make sure we have this one imported
176function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent';
177
178{ TTestClient }
179
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200180class procedure TTestClient.PrintCmdLineHelp;
181const HELPTEXT = ' [options]'#10
182 + #10
183 + 'Allowed options:'#10
184 + ' -h [ --help ] produce help message'#10
185 + ' --host arg (=localhost) Host to connect'#10
186 + ' --port arg (=9090) Port number to connect'#10
187 + ' --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift),'#10
188 + ' instead of host and port'#10
189 + ' --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)'#10
190 + ' --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)'#10
191 + ' --transport arg (=sockets) Transport: buffered, framed, http, evhttp'#10
192 + ' --protocol arg (=binary) Protocol: binary, compact, json'#10
193 + ' --ssl Encrypted Transport using SSL'#10
194 + ' -n [ --testloops ] arg (=1) Number of Tests'#10
195 + ' -t [ --threads ] arg (=1) Number of Test threads'#10
196 ;
197begin
198 Writeln( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + HELPTEXT);
199end;
200
201class procedure TTestClient.InvalidArgs;
202begin
203 Console.WriteLine( 'Invalid args.');
204 Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + ' -h for more information');
205 Abort;
206end;
207
208class function TTestClient.Execute(const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000209var
210 i : Integer;
Jens Geyer14f5d502017-12-09 13:47:09 +0100211 threadExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000212 s : string;
Roger Meier3bef8c22012-10-06 06:58:00 +0000213 threads : array of TThread;
214 dtStart : TDateTime;
215 test : Integer;
216 thread : TThread;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200217 setup : TTestSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +0000218begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200219 // init record
220 with setup do begin
221 protType := prot_Binary;
222 endpoint := trns_Sockets;
223 layered := [];
224 useSSL := FALSE;
225 host := 'localhost';
226 port := 9090;
227 sPipeName := '';
228 hAnonRead := INVALID_HANDLE_VALUE;
229 hAnonWrite := INVALID_HANDLE_VALUE;
230 end;
231
Roger Meier3bef8c22012-10-06 06:58:00 +0000232 try
Roger Meier3bef8c22012-10-06 06:58:00 +0000233 i := 0;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200234 while ( i < Length(args) ) do begin
235 s := args[i];
236 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000237
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200238 if (s = '-h') or (s = '--help') then begin
239 // -h [ --help ] produce help message
240 PrintCmdLineHelp;
241 result := $FF; // all tests failed
242 Exit;
243 end
Jens Geyerb360b652014-09-28 01:55:46 +0200244 else if s = '--host' then begin
245 // --host arg (=localhost) Host to connect
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200246 setup.host := args[i];
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200247 Inc( i);
248 end
Jens Geyerb360b652014-09-28 01:55:46 +0200249 else if s = '--port' then begin
250 // --port arg (=9090) Port number to connect
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200251 s := args[i];
252 Inc( i);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200253 setup.port := StrToIntDef(s,0);
254 if setup.port <= 0 then InvalidArgs;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200255 end
Jens Geyerb360b652014-09-28 01:55:46 +0200256 else if s = '--domain-socket' then begin
257 // --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200258 raise Exception.Create('domain-socket not supported');
259 end
Jens Geyerb360b652014-09-28 01:55:46 +0200260 else if s = '--named-pipe' then begin
261 // --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200262 setup.endpoint := trns_NamedPipes;
263 setup.sPipeName := args[i];
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200264 Inc( i);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200265 Console.WriteLine('Using named pipe ('+setup.sPipeName+')');
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200266 end
Jens Geyerb360b652014-09-28 01:55:46 +0200267 else if s = '--anon-pipes' then begin
268 // --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200269 setup.endpoint := trns_AnonPipes;
270 setup.hAnonRead := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200271 Inc( i);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200272 setup.hAnonWrite := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200273 Inc( i);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200274 Console.WriteLine('Using anonymous pipes ('+IntToStr(Integer(setup.hAnonRead))+' and '+IntToStr(Integer(setup.hAnonWrite))+')');
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200275 end
Jens Geyerb360b652014-09-28 01:55:46 +0200276 else if s = '--transport' then begin
277 // --transport arg (=sockets) Transport: buffered, framed, http, evhttp
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200278 s := args[i];
279 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000280
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200281 if s = 'buffered' then Include( setup.layered, trns_Buffered)
282 else if s = 'framed' then Include( setup.layered, trns_Framed)
283 else if s = 'http' then setup.endpoint := trns_Http
284 else if s = 'evhttp' then setup.endpoint := trns_EvHttp
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200285 else InvalidArgs;
286 end
Jens Geyerb360b652014-09-28 01:55:46 +0200287 else if s = '--protocol' then begin
288 // --protocol arg (=binary) Protocol: binary, compact, json
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200289 s := args[i];
290 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000291
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200292 if s = 'binary' then setup.protType := prot_Binary
293 else if s = 'compact' then setup.protType := prot_Compact
294 else if s = 'json' then setup.protType := prot_JSON
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200295 else InvalidArgs;
296 end
Jens Geyerb360b652014-09-28 01:55:46 +0200297 else if s = '--ssl' then begin
298 // --ssl Encrypted Transport using SSL
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200299 setup.useSSL := TRUE;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200300
301 end
302 else if (s = '-n') or (s = '--testloops') then begin
303 // -n [ --testloops ] arg (=1) Number of Tests
304 FNumIteration := StrToIntDef( args[i], 0);
305 Inc( i);
306 if FNumIteration <= 0
307 then InvalidArgs;
308
309 end
310 else if (s = '-t') or (s = '--threads') then begin
311 // -t [ --threads ] arg (=1) Number of Test threads
312 FNumThread := StrToIntDef( args[i], 0);
313 Inc( i);
314 if FNumThread <= 0
315 then InvalidArgs;
316 end
317 else begin
318 InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000319 end;
320 end;
321
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200322
Roger Meier79655fb2012-10-20 20:59:41 +0000323 // In the anonymous pipes mode the client is launched by the test server
324 // -> behave nicely and allow for attaching a debugger to this process
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200325 if (setup.endpoint = trns_AnonPipes) and not IsDebuggerPresent
Roger Meier79655fb2012-10-20 20:59:41 +0000326 then MessageBox( 0, 'Attach Debugger and/or click OK to continue.',
327 'Thrift TestClient (Delphi)',
328 MB_OK or MB_ICONEXCLAMATION);
329
Roger Meier3bef8c22012-10-06 06:58:00 +0000330 SetLength( threads, FNumThread);
331 dtStart := Now;
332
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200333 // layered transports are not really meant to be stacked upon each other
334 if (trns_Framed in setup.layered) then begin
335 Console.WriteLine('Using framed transport');
336 end
337 else if (trns_Buffered in setup.layered) then begin
338 Console.WriteLine('Using buffered transport');
339 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000340
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200341 Console.WriteLine(THRIFT_PROTOCOLS[setup.protType]+' protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000342
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200343 for test := 0 to FNumThread - 1 do begin
344 thread := TClientThread.Create( setup, FNumIteration);
Roger Meier3bef8c22012-10-06 06:58:00 +0000345 threads[test] := thread;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200346 thread.Start;
Roger Meier3bef8c22012-10-06 06:58:00 +0000347 end;
348
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200349 result := 0;
350 for test := 0 to FNumThread - 1 do begin
Jens Geyer14f5d502017-12-09 13:47:09 +0100351 threadExitCode := threads[test].WaitFor;
352 result := result or threadExitCode;
Jens Geyer14f5d502017-12-09 13:47:09 +0100353 threads[test].Free;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200354 threads[test] := nil;
Jens Geyer14f5d502017-12-09 13:47:09 +0100355 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000356
357 Console.Write('Total time: ' + IntToStr( MilliSecondsBetween(Now, dtStart)));
358
359 except
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200360 on E: EAbort do raise;
361 on E: Exception do begin
362 Console.WriteLine( E.Message + #10 + E.StackTrace);
363 raise;
Roger Meier3bef8c22012-10-06 06:58:00 +0000364 end;
365 end;
366
367 Console.WriteLine('');
368 Console.WriteLine('done!');
369end;
370
371{ TClientThread }
372
373procedure TClientThread.ClientTest;
374var
375 client : TThriftTest.Iface;
376 s : string;
377 i8 : ShortInt;
378 i32 : Integer;
379 i64 : Int64;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100380 binOut,binIn : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000381 dub : Double;
382 o : IXtruct;
383 o2 : IXtruct2;
384 i : IXtruct;
385 i2 : IXtruct2;
386 mapout : IThriftDictionary<Integer,Integer>;
387 mapin : IThriftDictionary<Integer,Integer>;
388 strmapout : IThriftDictionary<string,string>;
389 strmapin : IThriftDictionary<string,string>;
390 j : Integer;
391 first : Boolean;
392 key : Integer;
393 strkey : string;
394 listout : IThriftList<Integer>;
395 listin : IThriftList<Integer>;
396 setout : IHashSet<Integer>;
397 setin : IHashSet<Integer>;
398 ret : TNumberz;
399 uid : Int64;
400 mm : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
401 pos : IThriftDictionary<Integer, Integer>;
402 neg : IThriftDictionary<Integer, Integer>;
403 m2 : IThriftDictionary<Integer, Integer>;
404 k2 : Integer;
405 insane : IInsanity;
406 truck : IXtruct;
407 whoa : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
408 key64 : Int64;
409 val : IThriftDictionary<TNumberz, IInsanity>;
410 k2_2 : TNumberz;
411 k3 : TNumberz;
412 v2 : IInsanity;
413 userMap : IThriftDictionary<TNumberz, Int64>;
414 xtructs : IThriftList<IXtruct>;
415 x : IXtruct;
416 arg0 : ShortInt;
417 arg1 : Integer;
418 arg2 : Int64;
419 arg3 : IThriftDictionary<SmallInt, string>;
420 arg4 : TNumberz;
421 arg5 : Int64;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200422 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000423 StartTick : Cardinal;
424 k : Integer;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200425 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000426 hello, goodbye : IXtruct;
427 crazy : IInsanity;
428 looney : IInsanity;
429 first_map : IThriftDictionary<TNumberz, IInsanity>;
430 second_map : IThriftDictionary<TNumberz, IInsanity>;
Jens Geyer540e3462016-12-28 14:25:41 +0100431 pair : TPair<TNumberz, TUserId>;
Jens Geyer85827152018-01-12 21:20:59 +0100432 testsize : TTestSize;
Roger Meier3bef8c22012-10-06 06:58:00 +0000433begin
434 client := TThriftTest.TClient.Create( FProtocol);
435 FTransport.Open;
436
Jens Geyer06045cf2013-03-27 20:26:25 +0200437 {$IFDEF StressTest}
438 StressTest( client);
439 {$ENDIF StressTest}
440
Jens Geyer17c3ad92017-09-05 20:31:27 +0200441 {$IFDEF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000442 // in-depth exception test
443 // (1) do we get an exception at all?
444 // (2) do we get the right exception?
445 // (3) does the exception contain the expected data?
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200446 StartTestGroup( 'testException', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000447 // case 1: exception type declared in IDL at the function call
448 try
449 client.testException('Xception');
450 Expect( FALSE, 'testException(''Xception''): must trow an exception');
451 except
452 on e:TXception do begin
453 Expect( e.ErrorCode = 1001, 'error code');
454 Expect( e.Message_ = 'Xception', 'error message');
455 Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ );
456 end;
457 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200458 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000459 end;
460
461 // case 2: exception type NOT declared in IDL at the function call
462 // this will close the connection
463 try
464 client.testException('TException');
465 Expect( FALSE, 'testException(''TException''): must trow an exception');
466 except
467 on e:TTransportException do begin
468 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Roger Meier3bef8c22012-10-06 06:58:00 +0000469 end;
Jens Geyer6bbbf192014-09-07 01:45:56 +0200470 on e:TApplicationException do begin
471 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Jens Geyer6bbbf192014-09-07 01:45:56 +0200472 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200473 on e:TException do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
474 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000475 end;
476
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100477
Jens Geyer2ad6c302015-02-26 19:38:53 +0100478 if FTransport.IsOpen then FTransport.Close;
479 FTransport.Open; // re-open connection, server has already closed
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100480
Jens Geyer2ad6c302015-02-26 19:38:53 +0100481
Roger Meier3bef8c22012-10-06 06:58:00 +0000482 // case 3: no exception
483 try
484 client.testException('something');
485 Expect( TRUE, 'testException(''something''): must not trow an exception');
486 except
487 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200488 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000489 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200490 {$ENDIF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000491
492
493 // simple things
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200494 StartTestGroup( 'simple Thrift calls', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000495 client.testVoid();
496 Expect( TRUE, 'testVoid()'); // success := no exception
497
Jens Geyer39ba6b72015-09-22 00:00:49 +0200498 s := BoolToString( client.testBool(TRUE));
499 Expect( s = BoolToString(TRUE), 'testBool(TRUE) = '+s);
500 s := BoolToString( client.testBool(FALSE));
501 Expect( s = BoolToString(FALSE), 'testBool(FALSE) = '+s);
502
Roger Meier3bef8c22012-10-06 06:58:00 +0000503 s := client.testString('Test');
504 Expect( s = 'Test', 'testString(''Test'') = "'+s+'"');
505
Jens Geyercf892d42017-09-09 10:08:22 +0200506 s := client.testString(''); // empty string
507 Expect( s = '', 'testString('''') = "'+s+'"');
508
Jens Geyer06045cf2013-03-27 20:26:25 +0200509 s := client.testString(HUGE_TEST_STRING);
510 Expect( length(s) = length(HUGE_TEST_STRING),
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100511 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
Jens Geyer06045cf2013-03-27 20:26:25 +0200512 +'=> length(result) = '+IntToStr(Length(s)));
513
Roger Meier3bef8c22012-10-06 06:58:00 +0000514 i8 := client.testByte(1);
515 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
516
517 i32 := client.testI32(-1);
518 Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32));
519
520 Console.WriteLine('testI64(-34359738368)');
521 i64 := client.testI64(-34359738368);
522 Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
523
Jens Geyerd4df9172017-10-25 22:30:23 +0200524 // random binary small
Jens Geyer85827152018-01-12 21:20:59 +0100525 for testsize := Low(TTestSize) to High(TTestSize) do begin
526 binOut := PrepareBinaryData( TRUE, testsize);
527 Console.WriteLine('testBinary('+BytesToHex(binOut)+')');
528 try
529 binIn := client.testBinary(binOut);
530 Expect( Length(binOut) = Length(binIn), 'testBinary(): length '+IntToStr(Length(binOut))+' = '+IntToStr(Length(binIn)));
531 i32 := Min( Length(binOut), Length(binIn));
532 Expect( CompareMem( binOut, binIn, i32), 'testBinary('+BytesToHex(binOut)+') = '+BytesToHex(binIn));
533 except
534 on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message);
535 on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message);
536 end;
Jens Geyercf892d42017-09-09 10:08:22 +0200537 end;
538
Roger Meier3bef8c22012-10-06 06:58:00 +0000539 Console.WriteLine('testDouble(5.325098235)');
540 dub := client.testDouble(5.325098235);
541 Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
542
543 // structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200544 StartTestGroup( 'testStruct', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000545 Console.WriteLine('testStruct({''Zero'', 1, -3, -5})');
546 o := TXtructImpl.Create;
547 o.String_thing := 'Zero';
548 o.Byte_thing := 1;
549 o.I32_thing := -3;
550 o.I64_thing := -5;
551 i := client.testStruct(o);
552 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
553 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
554 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
555 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
556 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
557 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
558 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
559 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
560
561 // nested structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200562 StartTestGroup( 'testNest', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000563 Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})');
564 o2 := TXtruct2Impl.Create;
565 o2.Byte_thing := 1;
566 o2.Struct_thing := o;
567 o2.I32_thing := 5;
568 i2 := client.testNest(o2);
569 i := i2.Struct_thing;
570 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
571 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
572 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
573 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
574 Expect( i2.Byte_thing = 1, 'i2.Byte_thing = '+IntToStr(i2.Byte_thing));
575 Expect( i2.I32_thing = 5, 'i2.I32_thing = '+IntToStr(i2.I32_thing));
576 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
577 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
578 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
579 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
580 Expect( i2.__isset_Byte_thing, 'i2.__isset_Byte_thing');
581 Expect( i2.__isset_I32_thing, 'i2.__isset_I32_thing');
582
583 // map<type1,type2>: A map of strictly unique keys to values.
584 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200585 StartTestGroup( 'testMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000586 mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
587 for j := 0 to 4 do
588 begin
589 mapout.AddOrSetValue( j, j - 10);
590 end;
591 Console.Write('testMap({');
592 first := True;
593 for key in mapout.Keys do
594 begin
595 if first
596 then first := False
597 else Console.Write( ', ' );
598 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
599 end;
600 Console.WriteLine('})');
601
602 mapin := client.testMap( mapout );
603 Expect( mapin.Count = mapout.Count, 'testMap: mapin.Count = mapout.Count');
604 for j := 0 to 4 do
605 begin
606 Expect( mapout.ContainsKey(j), 'testMap: mapout.ContainsKey('+IntToStr(j)+') = '+BoolToString(mapout.ContainsKey(j)));
607 end;
608 for key in mapin.Keys do
609 begin
610 Expect( mapin[key] = mapout[key], 'testMap: '+IntToStr(key) + ' => ' + IntToStr( mapin[key]));
611 Expect( mapin[key] = key - 10, 'testMap: mapin['+IntToStr(key)+'] = '+IntToStr( mapin[key]));
612 end;
613
614
615 // map<type1,type2>: A map of strictly unique keys to values.
616 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200617 StartTestGroup( 'testStringMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000618 strmapout := TThriftDictionaryImpl<string,string>.Create;
619 for j := 0 to 4 do
620 begin
621 strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10));
622 end;
623 Console.Write('testStringMap({');
624 first := True;
625 for strkey in strmapout.Keys do
626 begin
627 if first
628 then first := False
629 else Console.Write( ', ' );
630 Console.Write( strkey + ' => ' + strmapout[strkey]);
631 end;
632 Console.WriteLine('})');
633
634 strmapin := client.testStringMap( strmapout );
635 Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count');
636 for j := 0 to 4 do
637 begin
638 Expect( strmapout.ContainsKey(IntToStr(j)),
639 'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = '
640 + BoolToString(strmapout.ContainsKey(IntToStr(j))));
641 end;
642 for strkey in strmapin.Keys do
643 begin
644 Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]);
645 Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]);
646 end;
647
648
649 // set<type>: An unordered set of unique elements.
650 // Translates to an STL set, Java HashSet, set in Python, etc.
651 // Note: PHP does not support sets, so it is treated similar to a List
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200652 StartTestGroup( 'testSet', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000653 setout := THashSetImpl<Integer>.Create;
654 for j := -2 to 2 do
655 begin
656 setout.Add( j );
657 end;
658 Console.Write('testSet({');
659 first := True;
660 for j in setout do
661 begin
662 if first
663 then first := False
664 else Console.Write(', ');
665 Console.Write(IntToStr( j));
666 end;
667 Console.WriteLine('})');
668
669 setin := client.testSet(setout);
670 Expect( setin.Count = setout.Count, 'testSet: setin.Count = setout.Count');
671 Expect( setin.Count = 5, 'testSet: setin.Count = '+IntToStr(setin.Count));
672 for j := -2 to 2 do // unordered, we can't rely on the order => test for known elements only
673 begin
674 Expect( setin.Contains(j), 'testSet: setin.Contains('+IntToStr(j)+') => '+BoolToString(setin.Contains(j)));
675 end;
676
677 // list<type>: An ordered list of elements.
678 // Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200679 StartTestGroup( 'testList', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000680 listout := TThriftListImpl<Integer>.Create;
681 listout.Add( +1);
682 listout.Add( -2);
683 listout.Add( +3);
684 listout.Add( -4);
685 listout.Add( 0);
686 Console.Write('testList({');
687 first := True;
688 for j in listout do
689 begin
690 if first
691 then first := False
692 else Console.Write(', ');
693 Console.Write(IntToStr( j));
694 end;
695 Console.WriteLine('})');
696
697 listin := client.testList(listout);
698 Expect( listin.Count = listout.Count, 'testList: listin.Count = listout.Count');
699 Expect( listin.Count = 5, 'testList: listin.Count = '+IntToStr(listin.Count));
700 Expect( listin[0] = +1, 'listin[0] = '+IntToStr( listin[0]));
701 Expect( listin[1] = -2, 'listin[1] = '+IntToStr( listin[1]));
702 Expect( listin[2] = +3, 'listin[2] = '+IntToStr( listin[2]));
703 Expect( listin[3] = -4, 'listin[3] = '+IntToStr( listin[3]));
704 Expect( listin[4] = 0, 'listin[4] = '+IntToStr( listin[4]));
705
706 // enums
707 ret := client.testEnum(TNumberz.ONE);
708 Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret)));
709
710 ret := client.testEnum(TNumberz.TWO);
711 Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret)));
712
713 ret := client.testEnum(TNumberz.THREE);
714 Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret)));
715
716 ret := client.testEnum(TNumberz.FIVE);
717 Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret)));
718
719 ret := client.testEnum(TNumberz.EIGHT);
720 Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret)));
721
722
723 // typedef
724 uid := client.testTypedef(309858235082523);
725 Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid));
726
727
728 // maps of maps
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200729 StartTestGroup( 'testMapMap(1)', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000730 mm := client.testMapMap(1);
731 Console.Write(' = {');
732 for key in mm.Keys do
733 begin
734 Console.Write( IntToStr( key) + ' => {');
735 m2 := mm[key];
736 for k2 in m2.Keys do
737 begin
738 Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', ');
739 end;
740 Console.Write('}, ');
741 end;
742 Console.WriteLine('}');
743
744 // verify result data
745 Expect( mm.Count = 2, 'mm.Count = '+IntToStr(mm.Count));
746 pos := mm[4];
747 neg := mm[-4];
748 for j := 1 to 4 do
749 begin
750 Expect( pos[j] = j, 'pos[j] = '+IntToStr(pos[j]));
751 Expect( neg[-j] = -j, 'neg[-j] = '+IntToStr(neg[-j]));
752 end;
753
754
755
756 // insanity
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200757 StartTestGroup( 'testInsanity', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000758 insane := TInsanityImpl.Create;
759 insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
760 insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000);
761 truck := TXtructImpl.Create;
762 truck.String_thing := 'Truck';
Jens Geyer540e3462016-12-28 14:25:41 +0100763 truck.Byte_thing := -8; // byte is signed
764 truck.I32_thing := 32;
765 truck.I64_thing := 64;
Roger Meier3bef8c22012-10-06 06:58:00 +0000766 insane.Xtructs := TThriftListImpl<IXtruct>.Create;
767 insane.Xtructs.Add( truck );
768 whoa := client.testInsanity( insane );
769 Console.Write(' = {');
770 for key64 in whoa.Keys do
771 begin
772 val := whoa[key64];
773 Console.Write( IntToStr( key64) + ' => {');
774 for k2_2 in val.Keys do
775 begin
776 v2 := val[k2_2];
777 Console.Write( IntToStr( Integer( k2_2)) + ' => {');
778 userMap := v2.UserMap;
779 Console.Write('{');
780 if userMap <> nil then
781 begin
782 for k3 in userMap.Keys do
783 begin
784 Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', ');
785 end;
786 end else
787 begin
788 Console.Write('null');
789 end;
790 Console.Write('}, ');
791 xtructs := v2.Xtructs;
792 Console.Write('{');
793
794 if xtructs <> nil then
795 begin
796 for x in xtructs do
797 begin
798 Console.Write('{"' + x.String_thing + '", ' +
799 IntToStr( x.Byte_thing) + ', ' +
800 IntToStr( x.I32_thing) + ', ' +
801 IntToStr( x.I32_thing) + '}, ');
802 end;
803 end else
804 begin
805 Console.Write('null');
806 end;
807 Console.Write('}');
808 Console.Write('}, ');
809 end;
810 Console.Write('}, ');
811 end;
812 Console.WriteLine('}');
813
Jens Geyer540e3462016-12-28 14:25:41 +0100814 (**
815 * So you think you've got this all worked, out eh?
816 *
817 * Creates a the returned map with these values and prints it out:
818 * { 1 => { 2 => argument,
819 * 3 => argument,
820 * },
821 * 2 => { 6 => <empty Insanity struct>, },
822 * }
823 * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
824 *)
825
Roger Meier3bef8c22012-10-06 06:58:00 +0000826 // verify result data
827 Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count));
828 //
829 first_map := whoa[1];
830 second_map := whoa[2];
831 Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count));
832 Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count));
833 //
834 looney := second_map[TNumberz.SIX];
835 Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney)));
836 Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
837 Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
838 //
839 for ret in [TNumberz.TWO, TNumberz.THREE] do begin
840 crazy := first_map[ret];
841 Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
842
843 Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap));
844 Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs));
845
Jens Geyer540e3462016-12-28 14:25:41 +0100846 Expect( crazy.UserMap.Count = insane.UserMap.Count, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count));
847 for pair in insane.UserMap do begin
848 Expect( crazy.UserMap[pair.Key] = pair.Value, 'crazy.UserMap['+IntToStr(Ord(pair.key))+'] = '+IntToStr(crazy.UserMap[pair.Key]));
849 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000850
Jens Geyer540e3462016-12-28 14:25:41 +0100851 Expect( crazy.Xtructs.Count = insane.Xtructs.Count, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count));
852 for arg0 := 0 to insane.Xtructs.Count-1 do begin
853 hello := insane.Xtructs[arg0];
854 goodbye := crazy.Xtructs[arg0];
855 Expect( goodbye.String_thing = hello.String_thing, 'goodbye.String_thing = '+goodbye.String_thing);
856 Expect( goodbye.Byte_thing = hello.Byte_thing, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing));
857 Expect( goodbye.I32_thing = hello.I32_thing, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing));
858 Expect( goodbye.I64_thing = hello.I64_thing, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing));
859 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000860 end;
861
862
863 // multi args
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200864 StartTestGroup( 'testMulti', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000865 arg0 := 1;
866 arg1 := 2;
867 arg2 := High(Int64);
868 arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
869 arg3.AddOrSetValue( 1, 'one');
870 arg4 := TNumberz.FIVE;
871 arg5 := 5000000;
872 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
873 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
874 arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
875 IntToStr( arg5) + ')');
876
877 i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5);
878 Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"');
879 Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing));
880 Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing));
881 Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing));
882 Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
883 Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
884 Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
885 Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
886
887 // multi exception
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200888 StartTestGroup( 'testMultiException(1)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000889 try
890 i := client.testMultiException( 'need more pizza', 'run out of beer');
891 Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"');
892 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
Jens Geyer6bbbf192014-09-07 01:45:56 +0200893 { this is not necessarily true, these fields are default-serialized
Jens Geyerd5436f52014-10-03 19:50:38 +0200894 Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
Roger Meier3bef8c22012-10-06 06:58:00 +0000895 Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
896 Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
Jens Geyerd5436f52014-10-03 19:50:38 +0200897 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000898 except
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200899 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000900 end;
901
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200902 StartTestGroup( 'testMultiException(Xception)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000903 try
904 i := client.testMultiException( 'Xception', 'second test');
905 Expect( FALSE, 'testMultiException(''Xception''): must trow an exception');
906 except
907 on x:TXception do begin
908 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
909 Expect( x.__isset_Message_, 'x.__isset_Message_ = '+BoolToString(x.__isset_Message_));
910 Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
911 Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"');
912 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200913 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000914 end;
915
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200916 StartTestGroup( 'testMultiException(Xception2)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000917 try
918 i := client.testMultiException( 'Xception2', 'third test');
919 Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception');
920 except
921 on x:TXception2 do begin
922 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
923 Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing));
924 Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
925 Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"');
926 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 +0200927 { this is not necessarily true, these fields are default-serialized
Roger Meier3bef8c22012-10-06 06:58:00 +0000928 Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing));
929 Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing));
930 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 +0200931 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000932 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200933 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000934 end;
935
936
937 // oneway functions
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200938 StartTestGroup( 'Test Oneway(1)', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000939 client.testOneway(1);
940 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
941
942 // call time
Jens Geyer06045cf2013-03-27 20:26:25 +0200943 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000944 StartTestGroup( 'Test Calltime()');
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200945 StartTick := GetTickCount;
Roger Meier3bef8c22012-10-06 06:58:00 +0000946 for k := 0 to 1000 - 1 do
947 begin
948 client.testVoid();
949 end;
950 Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
Jens Geyer06045cf2013-03-27 20:26:25 +0200951 {$ENDIF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000952
953 // no more tests here
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200954 StartTestGroup( '', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000955end;
956
957
Jens Geyer14f5d502017-12-09 13:47:09 +0100958{$IFDEF SupportsAsync}
959procedure TClientThread.ClientAsyncTest;
960var
961 client : TThriftTest.IAsync;
962 s : string;
963 i8 : ShortInt;
964begin
965 StartTestGroup( 'Async Tests', test_Unknown);
966 client := TThriftTest.TClient.Create( FProtocol);
967 FTransport.Open;
968
969 // oneway void functions
970 client.testOnewayAsync(1).Wait;
971 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
972
973 // normal functions
974 s := client.testStringAsync(HUGE_TEST_STRING).Value;
975 Expect( length(s) = length(HUGE_TEST_STRING),
976 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
977 +'=> length(result) = '+IntToStr(Length(s)));
978
979 i8 := client.testByte(1).Value;
980 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
981end;
982{$ENDIF}
983
984
Jens Geyer718f6ee2013-09-06 21:02:34 +0200985{$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200986procedure TClientThread.StressTest(const client : TThriftTest.Iface);
987begin
988 while TRUE do begin
989 try
990 if not FTransport.IsOpen then FTransport.Open; // re-open connection, server has already closed
991 try
992 client.testString('Test');
993 Write('.');
994 finally
995 if FTransport.IsOpen then FTransport.Close;
996 end;
997 except
998 on e:Exception do Writeln(#10+e.message);
999 end;
1000 end;
1001end;
Jens Geyer718f6ee2013-09-06 21:02:34 +02001002{$ENDIF}
Jens Geyer06045cf2013-03-27 20:26:25 +02001003
Jens Geyerfd1b3582014-12-13 23:42:58 +01001004
Jens Geyer85827152018-01-12 21:20:59 +01001005function TClientThread.PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes;
Jens Geyerd4df9172017-10-25 22:30:23 +02001006var i : Integer;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001007begin
Jens Geyer85827152018-01-12 21:20:59 +01001008 case aSize of
1009 Empty : SetLength( result, 0);
1010 Normal : SetLength( result, $100);
1011 ByteArrayTest : SetLength( result, SizeOf(TByteArray) + 128);
1012 PipeWriteLimit : SetLength( result, 65535 + 128);
1013 else
1014 raise EArgumentException.Create('aSize');
1015 end;
1016
Jens Geyerfd1b3582014-12-13 23:42:58 +01001017 ASSERT( Low(result) = 0);
Jens Geyer85827152018-01-12 21:20:59 +01001018 if Length(result) = 0 then Exit;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001019
1020 // linear distribution, unless random is requested
1021 if not aRandomDist then begin
1022 for i := Low(result) to High(result) do begin
Jens Geyer85827152018-01-12 21:20:59 +01001023 result[i] := i mod $100;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001024 end;
1025 Exit;
1026 end;
1027
1028 // random distribution of all 256 values
1029 FillChar( result[0], Length(result) * SizeOf(result[0]), $0);
Jens Geyerd4df9172017-10-25 22:30:23 +02001030 for i := Low(result) to High(result) do begin
1031 result[i] := Byte( Random($100));
Jens Geyerfd1b3582014-12-13 23:42:58 +01001032 end;
1033end;
1034
1035
Jens Geyerf7904452017-07-26 15:02:12 +02001036{$IFDEF Win64}
1037procedure TClientThread.UseInterlockedExchangeAdd64;
1038var a,b : Int64;
1039begin
1040 a := 1;
1041 b := 2;
1042 Thrift.Utils.InterlockedExchangeAdd64( a,b);
1043 Expect( a = 3, 'InterlockedExchangeAdd64');
1044end;
1045{$ENDIF}
1046
1047
Roger Meier3bef8c22012-10-06 06:58:00 +00001048procedure TClientThread.JSONProtocolReadWriteTest;
1049// Tests only then read/write procedures of the JSON protocol
1050// All tests succeed, if we can read what we wrote before
1051// Note that passing this test does not imply, that our JSON is really compatible to what
1052// other clients or servers expect as the real JSON. This is beyond the scope of this test.
1053var prot : IProtocol;
1054 stm : TStringStream;
Jens Geyer17c3ad92017-09-05 20:31:27 +02001055 list : TThriftList;
Jens Geyercf892d42017-09-09 10:08:22 +02001056 binary, binRead, emptyBinary : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +00001057 i,iErr : Integer;
1058const
1059 TEST_SHORT = ShortInt( $FE);
1060 TEST_SMALL = SmallInt( $FEDC);
1061 TEST_LONG = LongInt( $FEDCBA98);
1062 TEST_I64 = Int64( $FEDCBA9876543210);
1063 TEST_DOUBLE = -1.234e-56;
1064 DELTA_DOUBLE = TEST_DOUBLE * 1e-14;
1065 TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars")
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001066 // Test THRIFT-2336 and THRIFT-3404 with U+1D11E (G Clef symbol) and 'Русское Название';
1067 G_CLEF_AND_CYRILLIC_TEXT = #$1d11e' '#$0420#$0443#$0441#$0441#$043a#$043e#$0435' '#$041d#$0430#$0437#$0432#$0430#$043d#$0438#$0435;
1068 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 +01001069 // test both possible solidus encodings
1070 SOLIDUS_JSON_DATA = '"one/two\/three"';
1071 SOLIDUS_EXCPECTED = 'one/two/three';
Roger Meier3bef8c22012-10-06 06:58:00 +00001072begin
1073 stm := TStringStream.Create;
1074 try
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001075 StartTestGroup( 'JsonProtocolTest', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001076
1077 // prepare binary data
Jens Geyer85827152018-01-12 21:20:59 +01001078 binary := PrepareBinaryData( FALSE, Normal);
Jens Geyercf892d42017-09-09 10:08:22 +02001079 SetLength( emptyBinary, 0); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001080
1081 // output setup
1082 prot := TJSONProtocolImpl.Create(
1083 TStreamTransportImpl.Create(
1084 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
1085
1086 // write
Jens Geyer17c3ad92017-09-05 20:31:27 +02001087 Init( list, TType.String_, 9);
1088 prot.WriteListBegin( list);
Roger Meier3bef8c22012-10-06 06:58:00 +00001089 prot.WriteBool( TRUE);
1090 prot.WriteBool( FALSE);
1091 prot.WriteByte( TEST_SHORT);
1092 prot.WriteI16( TEST_SMALL);
1093 prot.WriteI32( TEST_LONG);
1094 prot.WriteI64( TEST_I64);
1095 prot.WriteDouble( TEST_DOUBLE);
1096 prot.WriteString( TEST_STRING);
1097 prot.WriteBinary( binary);
Jens Geyercf892d42017-09-09 10:08:22 +02001098 prot.WriteString( ''); // empty string
1099 prot.WriteBinary( emptyBinary); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001100 prot.WriteListEnd;
1101
1102 // input setup
1103 Expect( stm.Position = stm.Size, 'Stream position/length after write');
1104 stm.Position := 0;
1105 prot := TJSONProtocolImpl.Create(
1106 TStreamTransportImpl.Create(
1107 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1108
1109 // read and compare
1110 list := prot.ReadListBegin;
1111 Expect( list.ElementType = TType.String_, 'list element type');
1112 Expect( list.Count = 9, 'list element count');
1113 Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE');
1114 Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE');
1115 Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte');
1116 Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16');
1117 Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32');
1118 Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64');
1119 Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble');
1120 Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString');
1121 binRead := prot.ReadBinary;
Jens Geyercf892d42017-09-09 10:08:22 +02001122 Expect( Length(prot.ReadString) = 0, 'WriteString/ReadString (empty string)');
1123 Expect( Length(prot.ReadBinary) = 0, 'empty WriteBinary/ReadBinary (empty data block)');
Roger Meier3bef8c22012-10-06 06:58:00 +00001124 prot.ReadListEnd;
1125
1126 // test binary data
1127 Expect( Length(binary) = Length(binRead), 'Binary data length check');
1128 iErr := -1;
1129 for i := Low(binary) to High(binary) do begin
1130 if binary[i] <> binRead[i] then begin
1131 iErr := i;
1132 Break;
1133 end;
1134 end;
1135 if iErr < 0
1136 then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)')
1137 else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr));
1138
1139 Expect( stm.Position = stm.Size, 'Stream position after read');
1140
Jens Geyer7bb44a32014-02-07 22:24:37 +01001141
Jens Geyer21366942013-12-30 22:04:51 +01001142 // Solidus can be encoded in two ways. Make sure we can read both
1143 stm.Position := 0;
1144 stm.Size := 0;
1145 stm.WriteString(SOLIDUS_JSON_DATA);
1146 stm.Position := 0;
1147 prot := TJSONProtocolImpl.Create(
1148 TStreamTransportImpl.Create(
1149 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1150 Expect( prot.ReadString = SOLIDUS_EXCPECTED, 'Solidus encoding');
1151
1152
Jens Geyer7bb44a32014-02-07 22:24:37 +01001153 // Widechars should work too. Do they?
1154 // After writing, we ensure that we are able to read it back
1155 // We can't assume hex-encoding, since (nearly) any Unicode char is valid JSON
1156 stm.Position := 0;
1157 stm.Size := 0;
1158 prot := TJSONProtocolImpl.Create(
1159 TStreamTransportImpl.Create(
1160 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001161 prot.WriteString( G_CLEF_AND_CYRILLIC_TEXT);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001162 stm.Position := 0;
1163 prot := TJSONProtocolImpl.Create(
1164 TStreamTransportImpl.Create(
1165 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001166 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Writing JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001167
1168 // Widechars should work with hex-encoding too. Do they?
1169 stm.Position := 0;
1170 stm.Size := 0;
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001171 stm.WriteString( G_CLEF_AND_CYRILLIC_JSON);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001172 stm.Position := 0;
1173 prot := TJSONProtocolImpl.Create(
1174 TStreamTransportImpl.Create(
1175 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001176 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Reading JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001177
1178
Roger Meier3bef8c22012-10-06 06:58:00 +00001179 finally
1180 stm.Free;
1181 prot := nil; //-> Release
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001182 StartTestGroup( '', test_Unknown); // no more tests here
Roger Meier3bef8c22012-10-06 06:58:00 +00001183 end;
1184end;
1185
1186
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001187procedure TClientThread.StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +00001188begin
1189 FTestGroup := aGroup;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001190 FCurrentTest := aTest;
1191
1192 Include( FExecuted, aTest);
1193
Roger Meier3bef8c22012-10-06 06:58:00 +00001194 if FTestGroup <> '' then begin
1195 Console.WriteLine('');
1196 Console.WriteLine( aGroup+' tests');
1197 Console.WriteLine( StringOfChar('-',60));
1198 end;
1199end;
1200
1201
1202procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
1203begin
1204 if aTestResult then begin
1205 Inc(FSuccesses);
1206 Console.WriteLine( aTestInfo+': passed');
1207 end
1208 else begin
1209 FErrors.Add( FTestGroup+': '+aTestInfo);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001210 Include( FFailed, FCurrentTest);
Roger Meier3bef8c22012-10-06 06:58:00 +00001211 Console.WriteLine( aTestInfo+': *** FAILED ***');
1212
1213 // We have a failed test!
1214 // -> issue DebugBreak ONLY if a debugger is attached,
1215 // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise
Jens Geyer9f7f11e2016-04-14 21:37:11 +02001216 if IsDebuggerPresent
1217 then {$IFDEF CPUX64} DebugBreak {$ELSE} asm int 3 end {$ENDIF};
Roger Meier3bef8c22012-10-06 06:58:00 +00001218 end;
1219end;
1220
1221
1222procedure TClientThread.ReportResults;
1223var nTotal : Integer;
1224 sLine : string;
1225begin
1226 // prevent us from stupid DIV/0 errors
1227 nTotal := FSuccesses + FErrors.Count;
1228 if nTotal = 0 then begin
1229 Console.WriteLine('No results logged');
1230 Exit;
1231 end;
1232
1233 Console.WriteLine('');
1234 Console.WriteLine( StringOfChar('=',60));
1235 Console.WriteLine( IntToStr(nTotal)+' tests performed');
1236 Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)');
1237 Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)');
1238 Console.WriteLine( StringOfChar('=',60));
1239 if FErrors.Count > 0 then begin
1240 Console.WriteLine('FAILED TESTS:');
1241 for sLine in FErrors do Console.WriteLine('- '+sLine);
1242 Console.WriteLine( StringOfChar('=',60));
1243 InterlockedIncrement( ExitCode); // return <> 0 on errors
1244 end;
1245 Console.WriteLine('');
1246end;
1247
1248
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001249function TClientThread.CalculateExitCode : Byte;
1250var test : TTestGroup;
1251begin
1252 result := EXITCODE_SUCCESS;
1253 for test := Low(TTestGroup) to High(TTestGroup) do begin
1254 if (test in FFailed) or not (test in FExecuted)
1255 then result := result or MAP_FAILURES_TO_EXITCODE_BITS[test];
1256 end;
1257end;
1258
1259
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001260constructor TClientThread.Create( const aSetup : TTestSetup; const aNumIteration: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +00001261begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001262 FSetup := aSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +00001263 FNumIteration := ANumIteration;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001264
Roger Meier3bef8c22012-10-06 06:58:00 +00001265 FConsole := TThreadConsole.Create( Self );
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001266 FCurrentTest := test_Unknown;
Roger Meier3bef8c22012-10-06 06:58:00 +00001267
1268 // error list: keep correct order, allow for duplicates
1269 FErrors := TStringList.Create;
1270 FErrors.Sorted := FALSE;
1271 FErrors.Duplicates := dupAccept;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001272
1273 inherited Create( TRUE);
Roger Meier3bef8c22012-10-06 06:58:00 +00001274end;
1275
1276destructor TClientThread.Destroy;
1277begin
1278 FreeAndNil( FConsole);
1279 FreeAndNil( FErrors);
1280 inherited;
1281end;
1282
1283procedure TClientThread.Execute;
1284var
1285 i : Integer;
Roger Meier3bef8c22012-10-06 06:58:00 +00001286begin
1287 // perform all tests
1288 try
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001289 {$IFDEF Win64}
Jens Geyerf7904452017-07-26 15:02:12 +02001290 UseInterlockedExchangeAdd64;
Jens Geyer14f5d502017-12-09 13:47:09 +01001291 {$ENDIF}
Jens Geyer7bb44a32014-02-07 22:24:37 +01001292 JSONProtocolReadWriteTest;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001293
1294 // must be run in the context of the thread
1295 InitializeProtocolTransportStack;
1296 try
1297 for i := 0 to FNumIteration - 1 do begin
1298 ClientTest;
1299 {$IFDEF SupportsAsync}
1300 ClientAsyncTest;
1301 {$ENDIF}
1302 end;
1303
1304 // report the outcome
1305 ReportResults;
1306 SetReturnValue( CalculateExitCode);
1307
1308 finally
1309 ShutdownProtocolTransportStack;
Roger Meier3bef8c22012-10-06 06:58:00 +00001310 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001311
Roger Meier3bef8c22012-10-06 06:58:00 +00001312 except
1313 on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"');
1314 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001315end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001316
Roger Meier3bef8c22012-10-06 06:58:00 +00001317
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001318procedure TClientThread.InitializeProtocolTransportStack;
1319var
1320 streamtrans : IStreamTransport;
1321 http : IHTTPClient;
1322 sUrl : string;
1323const
1324 DEBUG_TIMEOUT = 30 * 1000;
1325 RELEASE_TIMEOUT = DEFAULT_THRIFT_TIMEOUT;
1326 PIPE_TIMEOUT = RELEASE_TIMEOUT;
1327 HTTP_TIMEOUTS = 10 * 1000;
1328begin
1329 // needed for HTTP clients as they utilize the MSXML COM components
1330 OleCheck( CoInitialize( nil));
1331
1332 case FSetup.endpoint of
1333 trns_Sockets: begin
1334 Console.WriteLine('Using sockets ('+FSetup.host+' port '+IntToStr(FSetup.port)+')');
1335 streamtrans := TSocketImpl.Create( FSetup.host, FSetup.port );
1336 FTransport := streamtrans;
1337 end;
1338
1339 trns_Http: begin
1340 Console.WriteLine('Using HTTPClient');
1341 if FSetup.useSSL
1342 then sUrl := 'http://'
1343 else sUrl := 'https://';
1344 sUrl := sUrl + FSetup.host;
1345 case FSetup.port of
1346 80 : if FSetup.useSSL then sUrl := sUrl + ':'+ IntToStr(FSetup.port);
1347 443 : if not FSetup.useSSL then sUrl := sUrl + ':'+ IntToStr(FSetup.port);
1348 else
1349 if FSetup.port > 0 then sUrl := sUrl + ':'+ IntToStr(FSetup.port);
1350 end;
1351 http := THTTPClientImpl.Create( sUrl);
1352 http.DnsResolveTimeout := HTTP_TIMEOUTS;
1353 http.ConnectionTimeout := HTTP_TIMEOUTS;
1354 http.SendTimeout := HTTP_TIMEOUTS;
1355 http.ReadTimeout := HTTP_TIMEOUTS;
1356 FTransport := http;
1357 end;
1358
1359 trns_EvHttp: begin
1360 raise Exception.Create(ENDPOINT_TRANSPORTS[FSetup.endpoint]+' transport not implemented');
1361 end;
1362
1363 trns_NamedPipes: begin
1364 streamtrans := TNamedPipeTransportClientEndImpl.Create( FSetup.sPipeName, 0, nil, PIPE_TIMEOUT, PIPE_TIMEOUT);
1365 FTransport := streamtrans;
1366 end;
1367
1368 trns_AnonPipes: begin
1369 streamtrans := TAnonymousPipeTransportImpl.Create( FSetup.hAnonRead, FSetup.hAnonWrite, FALSE);
1370 FTransport := streamtrans;
1371 end;
1372
1373 else
1374 raise Exception.Create('Unhandled endpoint transport');
1375 end;
1376 ASSERT( FTransport <> nil);
1377
1378 // layered transports are not really meant to be stacked upon each other
1379 if (trns_Framed in FSetup.layered) then begin
1380 FTransport := TFramedTransportImpl.Create( FTransport);
1381 end
1382 else if (trns_Buffered in FSetup.layered) and (streamtrans <> nil) then begin
1383 FTransport := TBufferedTransportImpl.Create( streamtrans, 32); // small buffer to test read()
1384 end;
1385
1386 if FSetup.useSSL then begin
1387 raise Exception.Create('SSL/TLS not implemented');
1388 end;
1389
1390 // create protocol instance, default to BinaryProtocol
1391 case FSetup.protType of
1392 prot_Binary : FProtocol := TBinaryProtocolImpl.Create( FTransport, BINARY_STRICT_READ, BINARY_STRICT_WRITE);
1393 prot_JSON : FProtocol := TJSONProtocolImpl.Create( FTransport);
1394 prot_Compact : FProtocol := TCompactProtocolImpl.Create( FTransport);
1395 else
1396 raise Exception.Create('Unhandled protocol');
1397 end;
1398
1399 ASSERT( (FTransport <> nil) and (FProtocol <> nil));
1400end;
1401
1402
1403procedure TClientThread.ShutdownProtocolTransportStack;
1404begin
1405 try
1406 FProtocol := nil;
1407
1408 if FTransport <> nil then begin
Roger Meier3bef8c22012-10-06 06:58:00 +00001409 FTransport.Close;
1410 FTransport := nil;
1411 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001412
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001413 finally
1414 CoUninitialize;
1415 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001416end;
1417
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001418
Roger Meier3bef8c22012-10-06 06:58:00 +00001419{ TThreadConsole }
1420
1421constructor TThreadConsole.Create(AThread: TThread);
1422begin
Jens Geyer718f6ee2013-09-06 21:02:34 +02001423 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +00001424 FThread := AThread;
1425end;
1426
1427procedure TThreadConsole.Write(const S: string);
1428var
1429 proc : TThreadProcedure;
1430begin
1431 proc := procedure
1432 begin
1433 Console.Write( S );
1434 end;
1435 TThread.Synchronize( FThread, proc);
1436end;
1437
1438procedure TThreadConsole.WriteLine(const S: string);
1439var
1440 proc : TThreadProcedure;
1441begin
1442 proc := procedure
1443 begin
1444 Console.WriteLine( S );
1445 end;
1446 TThread.Synchronize( FThread, proc);
1447end;
1448
1449initialization
1450begin
1451 TTestClient.FNumIteration := 1;
1452 TTestClient.FNumThread := 1;
1453end;
1454
1455end.