blob: 74130f488b79be7358f26aec14bc1ba2869a5215 [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
Roger Meier3bef8c22012-10-06 06:58:00 +000028interface
29
30uses
Jens Geyerfd1b3582014-12-13 23:42:58 +010031 Windows, SysUtils, Classes, Math,
Roger Meier3bef8c22012-10-06 06:58:00 +000032 DateUtils,
33 Generics.Collections,
34 TestConstants,
35 Thrift,
Jens Geyerf0e63312015-03-01 18:47:49 +010036 Thrift.Protocol.Compact,
Roger Meier3bef8c22012-10-06 06:58:00 +000037 Thrift.Protocol.JSON,
38 Thrift.Protocol,
39 Thrift.Transport.Pipes,
40 Thrift.Transport,
41 Thrift.Stream,
42 Thrift.Test,
Jens Geyerf7904452017-07-26 15:02:12 +020043 Thrift.Utils,
Roger Meier3bef8c22012-10-06 06:58:00 +000044 Thrift.Collections,
45 Thrift.Console;
46
47type
48 TThreadConsole = class
49 private
50 FThread : TThread;
51 public
52 procedure Write( const S : string);
53 procedure WriteLine( const S : string);
54 constructor Create( AThread: TThread);
55 end;
56
57 TClientThread = class( TThread )
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020058 private type
59 TTestGroup = (
60 test_Unknown,
61 test_BaseTypes,
62 test_Structs,
63 test_Containers,
64 test_Exceptions
65 // new values here
66 );
67 TTestGroups = set of TTestGroup;
68
Roger Meier3bef8c22012-10-06 06:58:00 +000069 private
70 FTransport : ITransport;
71 FProtocol : IProtocol;
72 FNumIteration : Integer;
73 FConsole : TThreadConsole;
74
75 // test reporting, will be refactored out into separate class later
76 FTestGroup : string;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020077 FCurrentTest : TTestGroup;
Roger Meier3bef8c22012-10-06 06:58:00 +000078 FSuccesses : Integer;
79 FErrors : TStringList;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020080 FFailed : TTestGroups;
81 FExecuted : TTestGroups;
82 procedure StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +000083 procedure Expect( aTestResult : Boolean; const aTestInfo : string);
84 procedure ReportResults;
Jens Geyerfd1b3582014-12-13 23:42:58 +010085 function CalculateExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +000086
87 procedure ClientTest;
88 procedure JSONProtocolReadWriteTest;
Jens Geyerfd1b3582014-12-13 23:42:58 +010089 function PrepareBinaryData( aRandomDist : Boolean = FALSE) : TBytes;
Jens Geyer718f6ee2013-09-06 21:02:34 +020090 {$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +020091 procedure StressTest(const client : TThriftTest.Iface);
Jens Geyer718f6ee2013-09-06 21:02:34 +020092 {$ENDIF}
Jens Geyerf7904452017-07-26 15:02:12 +020093 {$IFDEF Win64}
94 procedure UseInterlockedExchangeAdd64;
95 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +000096 protected
97 procedure Execute; override;
98 public
99 constructor Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
100 destructor Destroy; override;
101 end;
102
103 TTestClient = class
104 private
105 class var
106 FNumIteration : Integer;
107 FNumThread : Integer;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200108
109 class procedure PrintCmdLineHelp;
110 class procedure InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000111 public
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200112 class function Execute( const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000113 end;
114
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200115
Roger Meier3bef8c22012-10-06 06:58:00 +0000116implementation
117
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200118const
119 EXITCODE_SUCCESS = $00; // no errors bits set
120 //
121 EXITCODE_FAILBIT_BASETYPES = $01;
122 EXITCODE_FAILBIT_STRUCTS = $02;
123 EXITCODE_FAILBIT_CONTAINERS = $04;
124 EXITCODE_FAILBIT_EXCEPTIONS = $08;
125
126 MAP_FAILURES_TO_EXITCODE_BITS : array[TClientThread.TTestGroup] of Byte = (
127 EXITCODE_SUCCESS, // no bits here
128 EXITCODE_FAILBIT_BASETYPES,
129 EXITCODE_FAILBIT_STRUCTS,
130 EXITCODE_FAILBIT_CONTAINERS,
131 EXITCODE_FAILBIT_EXCEPTIONS
132 );
133
134
135
Roger Meier3bef8c22012-10-06 06:58:00 +0000136function BoolToString( b : Boolean) : string;
137// overrides global BoolToString()
138begin
139 if b
140 then result := 'true'
141 else result := 'false';
142end;
143
144// not available in all versions, so make sure we have this one imported
145function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent';
146
147{ TTestClient }
148
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200149class procedure TTestClient.PrintCmdLineHelp;
150const HELPTEXT = ' [options]'#10
151 + #10
152 + 'Allowed options:'#10
153 + ' -h [ --help ] produce help message'#10
154 + ' --host arg (=localhost) Host to connect'#10
155 + ' --port arg (=9090) Port number to connect'#10
156 + ' --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift),'#10
157 + ' instead of host and port'#10
158 + ' --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)'#10
159 + ' --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)'#10
160 + ' --transport arg (=sockets) Transport: buffered, framed, http, evhttp'#10
161 + ' --protocol arg (=binary) Protocol: binary, compact, json'#10
162 + ' --ssl Encrypted Transport using SSL'#10
163 + ' -n [ --testloops ] arg (=1) Number of Tests'#10
164 + ' -t [ --threads ] arg (=1) Number of Test threads'#10
165 ;
166begin
167 Writeln( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + HELPTEXT);
168end;
169
170class procedure TTestClient.InvalidArgs;
171begin
172 Console.WriteLine( 'Invalid args.');
173 Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + ' -h for more information');
174 Abort;
175end;
176
177class function TTestClient.Execute(const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000178var
179 i : Integer;
180 host : string;
181 port : Integer;
Roger Meier3bef8c22012-10-06 06:58:00 +0000182 sPipeName : string;
183 hAnonRead, hAnonWrite : THandle;
184 s : string;
Roger Meier3bef8c22012-10-06 06:58:00 +0000185 threads : array of TThread;
186 dtStart : TDateTime;
187 test : Integer;
188 thread : TThread;
189 trans : ITransport;
190 prot : IProtocol;
191 streamtrans : IStreamTransport;
192 http : IHTTPClient;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200193 protType : TKnownProtocol;
194 endpoint : TEndpointTransport;
195 layered : TLayeredTransports;
196 UseSSL : Boolean; // include where appropriate (TLayeredTransport?)
Jens Geyer0b20cc82013-03-07 20:47:01 +0100197const
198 // pipe timeouts to be used
199 DEBUG_TIMEOUT = 30 * 1000;
Jens Geyer3e8d9272014-09-14 20:10:40 +0200200 RELEASE_TIMEOUT = DEFAULT_THRIFT_TIMEOUT;
Jens Geyer0b20cc82013-03-07 20:47:01 +0100201 TIMEOUT = RELEASE_TIMEOUT;
Roger Meier3bef8c22012-10-06 06:58:00 +0000202begin
Roger Meier3bef8c22012-10-06 06:58:00 +0000203 protType := prot_Binary;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200204 endpoint := trns_Sockets;
205 layered := [];
206 UseSSL := FALSE;
207 host := 'localhost';
208 port := 9090;
209 sPipeName := '';
210 hAnonRead := INVALID_HANDLE_VALUE;
211 hAnonWrite := INVALID_HANDLE_VALUE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000212 try
Roger Meier3bef8c22012-10-06 06:58:00 +0000213 i := 0;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200214 while ( i < Length(args) ) do begin
215 s := args[i];
216 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000217
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200218 if (s = '-h') or (s = '--help') then begin
219 // -h [ --help ] produce help message
220 PrintCmdLineHelp;
221 result := $FF; // all tests failed
222 Exit;
223 end
Jens Geyerb360b652014-09-28 01:55:46 +0200224 else if s = '--host' then begin
225 // --host arg (=localhost) Host to connect
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200226 host := args[i];
227 Inc( i);
228 end
Jens Geyerb360b652014-09-28 01:55:46 +0200229 else if s = '--port' then begin
230 // --port arg (=9090) Port number to connect
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200231 s := args[i];
232 Inc( i);
233 port := StrToIntDef(s,0);
234 if port <= 0 then InvalidArgs;
235 end
Jens Geyerb360b652014-09-28 01:55:46 +0200236 else if s = '--domain-socket' then begin
237 // --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200238 raise Exception.Create('domain-socket not supported');
239 end
Jens Geyerb360b652014-09-28 01:55:46 +0200240 else if s = '--named-pipe' then begin
241 // --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200242 endpoint := trns_NamedPipes;
243 sPipeName := args[i];
244 Inc( i);
245 end
Jens Geyerb360b652014-09-28 01:55:46 +0200246 else if s = '--anon-pipes' then begin
247 // --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200248 endpoint := trns_AnonPipes;
249 hAnonRead := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
250 Inc( i);
251 hAnonWrite := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
252 Inc( i);
253 end
Jens Geyerb360b652014-09-28 01:55:46 +0200254 else if s = '--transport' then begin
255 // --transport arg (=sockets) Transport: buffered, framed, http, evhttp
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200256 s := args[i];
257 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000258
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200259 if s = 'buffered' then Include( layered, trns_Buffered)
260 else if s = 'framed' then Include( layered, trns_Framed)
261 else if s = 'http' then endpoint := trns_Http
Jens Geyera62efa42017-09-07 22:24:33 +0200262 else if s = 'evhttp' then endpoint := trns_EvHttp
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200263 else InvalidArgs;
264 end
Jens Geyerb360b652014-09-28 01:55:46 +0200265 else if s = '--protocol' then begin
266 // --protocol arg (=binary) Protocol: binary, compact, json
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200267 s := args[i];
268 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000269
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200270 if s = 'binary' then protType := prot_Binary
271 else if s = 'compact' then protType := prot_Compact
272 else if s = 'json' then protType := prot_JSON
273 else InvalidArgs;
274 end
Jens Geyerb360b652014-09-28 01:55:46 +0200275 else if s = '--ssl' then begin
276 // --ssl Encrypted Transport using SSL
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200277 UseSSL := TRUE;
278
279 end
280 else if (s = '-n') or (s = '--testloops') then begin
281 // -n [ --testloops ] arg (=1) Number of Tests
282 FNumIteration := StrToIntDef( args[i], 0);
283 Inc( i);
284 if FNumIteration <= 0
285 then InvalidArgs;
286
287 end
288 else if (s = '-t') or (s = '--threads') then begin
289 // -t [ --threads ] arg (=1) Number of Test threads
290 FNumThread := StrToIntDef( args[i], 0);
291 Inc( i);
292 if FNumThread <= 0
293 then InvalidArgs;
294 end
295 else begin
296 InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000297 end;
298 end;
299
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200300
Roger Meier79655fb2012-10-20 20:59:41 +0000301 // In the anonymous pipes mode the client is launched by the test server
302 // -> behave nicely and allow for attaching a debugger to this process
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200303 if (endpoint = trns_AnonPipes) and not IsDebuggerPresent
Roger Meier79655fb2012-10-20 20:59:41 +0000304 then MessageBox( 0, 'Attach Debugger and/or click OK to continue.',
305 'Thrift TestClient (Delphi)',
306 MB_OK or MB_ICONEXCLAMATION);
307
Roger Meier3bef8c22012-10-06 06:58:00 +0000308 SetLength( threads, FNumThread);
309 dtStart := Now;
310
311 for test := 0 to FNumThread - 1 do
312 begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200313 case endpoint of
314 trns_Sockets: begin
Roger Meier3bef8c22012-10-06 06:58:00 +0000315 Console.WriteLine('Using sockets ('+host+' port '+IntToStr(port)+')');
316 streamtrans := TSocketImpl.Create( host, port );
317 end;
318
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200319 trns_Http: begin
320 Console.WriteLine('Using HTTPClient');
321 http := THTTPClientImpl.Create( host);
322 trans := http;
Roger Meier3bef8c22012-10-06 06:58:00 +0000323 end;
324
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200325 trns_EvHttp: begin
326 raise Exception.Create(ENDPOINT_TRANSPORTS[endpoint]+' transport not implemented');
Roger Meier3bef8c22012-10-06 06:58:00 +0000327 end;
328
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200329 trns_NamedPipes: begin
330 Console.WriteLine('Using named pipe ('+sPipeName+')');
Jens Geyerae985dd2016-04-20 21:48:35 +0200331 streamtrans := TNamedPipeTransportClientEndImpl.Create( sPipeName, 0, nil, TIMEOUT, TIMEOUT);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200332 end;
333
334 trns_AnonPipes: begin
335 Console.WriteLine('Using anonymous pipes ('+IntToStr(Integer(hAnonRead))+' and '+IntToStr(Integer(hAnonWrite))+')');
336 streamtrans := TAnonymousPipeTransportImpl.Create( hAnonRead, hAnonWrite, FALSE);
337 end;
338
339 else
340 raise Exception.Create('Unhandled endpoint transport');
341 end;
342 trans := streamtrans;
343 ASSERT( trans <> nil);
344
345 if (trns_Buffered in layered) then begin
346 trans := TBufferedTransportImpl.Create( streamtrans, 32); // small buffer to test read()
347 Console.WriteLine('Using buffered transport');
348 end;
349
350 if (trns_Framed in layered) then begin
351 trans := TFramedTransportImpl.Create( trans );
352 Console.WriteLine('Using framed transport');
353 end;
354
355 if UseSSL then begin
356 raise Exception.Create('SSL not implemented');
Roger Meier3bef8c22012-10-06 06:58:00 +0000357 end;
358
359 // create protocol instance, default to BinaryProtocol
360 case protType of
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200361 prot_Binary : prot := TBinaryProtocolImpl.Create( trans, BINARY_STRICT_READ, BINARY_STRICT_WRITE);
362 prot_JSON : prot := TJSONProtocolImpl.Create( trans);
Jens Geyerf0e63312015-03-01 18:47:49 +0100363 prot_Compact : prot := TCompactProtocolImpl.Create( trans);
Roger Meier3bef8c22012-10-06 06:58:00 +0000364 else
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200365 raise Exception.Create('Unhandled protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000366 end;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200367 ASSERT( trans <> nil);
368 Console.WriteLine(THRIFT_PROTOCOLS[protType]+' protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000369
370 thread := TClientThread.Create( trans, prot, FNumIteration);
371 threads[test] := thread;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200372 thread.Start;
Roger Meier3bef8c22012-10-06 06:58:00 +0000373 end;
374
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200375 result := 0;
376 for test := 0 to FNumThread - 1 do begin
377 result := result or threads[test].WaitFor;
Roger Meier3bef8c22012-10-06 06:58:00 +0000378 end;
379
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200380 for test := 0 to FNumThread - 1
381 do threads[test].Free;
Roger Meier3bef8c22012-10-06 06:58:00 +0000382
383 Console.Write('Total time: ' + IntToStr( MilliSecondsBetween(Now, dtStart)));
384
385 except
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200386 on E: EAbort do raise;
387 on E: Exception do begin
388 Console.WriteLine( E.Message + #10 + E.StackTrace);
389 raise;
Roger Meier3bef8c22012-10-06 06:58:00 +0000390 end;
391 end;
392
393 Console.WriteLine('');
394 Console.WriteLine('done!');
395end;
396
397{ TClientThread }
398
399procedure TClientThread.ClientTest;
400var
401 client : TThriftTest.Iface;
402 s : string;
403 i8 : ShortInt;
404 i32 : Integer;
405 i64 : Int64;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100406 binOut,binIn : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000407 dub : Double;
408 o : IXtruct;
409 o2 : IXtruct2;
410 i : IXtruct;
411 i2 : IXtruct2;
412 mapout : IThriftDictionary<Integer,Integer>;
413 mapin : IThriftDictionary<Integer,Integer>;
414 strmapout : IThriftDictionary<string,string>;
415 strmapin : IThriftDictionary<string,string>;
416 j : Integer;
417 first : Boolean;
418 key : Integer;
419 strkey : string;
420 listout : IThriftList<Integer>;
421 listin : IThriftList<Integer>;
422 setout : IHashSet<Integer>;
423 setin : IHashSet<Integer>;
424 ret : TNumberz;
425 uid : Int64;
426 mm : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
427 pos : IThriftDictionary<Integer, Integer>;
428 neg : IThriftDictionary<Integer, Integer>;
429 m2 : IThriftDictionary<Integer, Integer>;
430 k2 : Integer;
431 insane : IInsanity;
432 truck : IXtruct;
433 whoa : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
434 key64 : Int64;
435 val : IThriftDictionary<TNumberz, IInsanity>;
436 k2_2 : TNumberz;
437 k3 : TNumberz;
438 v2 : IInsanity;
439 userMap : IThriftDictionary<TNumberz, Int64>;
440 xtructs : IThriftList<IXtruct>;
441 x : IXtruct;
442 arg0 : ShortInt;
443 arg1 : Integer;
444 arg2 : Int64;
445 arg3 : IThriftDictionary<SmallInt, string>;
446 arg4 : TNumberz;
447 arg5 : Int64;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200448 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000449 StartTick : Cardinal;
450 k : Integer;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200451 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000452 hello, goodbye : IXtruct;
453 crazy : IInsanity;
454 looney : IInsanity;
455 first_map : IThriftDictionary<TNumberz, IInsanity>;
456 second_map : IThriftDictionary<TNumberz, IInsanity>;
Jens Geyer540e3462016-12-28 14:25:41 +0100457 pair : TPair<TNumberz, TUserId>;
Roger Meier3bef8c22012-10-06 06:58:00 +0000458begin
459 client := TThriftTest.TClient.Create( FProtocol);
460 FTransport.Open;
461
Jens Geyer06045cf2013-03-27 20:26:25 +0200462 {$IFDEF StressTest}
463 StressTest( client);
464 {$ENDIF StressTest}
465
Jens Geyer17c3ad92017-09-05 20:31:27 +0200466 {$IFDEF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000467 // in-depth exception test
468 // (1) do we get an exception at all?
469 // (2) do we get the right exception?
470 // (3) does the exception contain the expected data?
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200471 StartTestGroup( 'testException', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000472 // case 1: exception type declared in IDL at the function call
473 try
474 client.testException('Xception');
475 Expect( FALSE, 'testException(''Xception''): must trow an exception');
476 except
477 on e:TXception do begin
478 Expect( e.ErrorCode = 1001, 'error code');
479 Expect( e.Message_ = 'Xception', 'error message');
480 Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ );
481 end;
482 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
483 on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
484 end;
485
486 // case 2: exception type NOT declared in IDL at the function call
487 // this will close the connection
488 try
489 client.testException('TException');
490 Expect( FALSE, 'testException(''TException''): must trow an exception');
491 except
492 on e:TTransportException do begin
493 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Roger Meier3bef8c22012-10-06 06:58:00 +0000494 end;
Jens Geyer6bbbf192014-09-07 01:45:56 +0200495 on e:TApplicationException do begin
496 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Jens Geyer6bbbf192014-09-07 01:45:56 +0200497 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000498 on e:TException do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
499 on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
500 end;
501
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100502
Jens Geyer2ad6c302015-02-26 19:38:53 +0100503 if FTransport.IsOpen then FTransport.Close;
504 FTransport.Open; // re-open connection, server has already closed
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100505
Jens Geyer2ad6c302015-02-26 19:38:53 +0100506
Roger Meier3bef8c22012-10-06 06:58:00 +0000507 // case 3: no exception
508 try
509 client.testException('something');
510 Expect( TRUE, 'testException(''something''): must not trow an exception');
511 except
512 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
513 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
514 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200515 {$ENDIF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000516
517
518 // simple things
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200519 StartTestGroup( 'simple Thrift calls', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000520 client.testVoid();
521 Expect( TRUE, 'testVoid()'); // success := no exception
522
Jens Geyer39ba6b72015-09-22 00:00:49 +0200523 s := BoolToString( client.testBool(TRUE));
524 Expect( s = BoolToString(TRUE), 'testBool(TRUE) = '+s);
525 s := BoolToString( client.testBool(FALSE));
526 Expect( s = BoolToString(FALSE), 'testBool(FALSE) = '+s);
527
Roger Meier3bef8c22012-10-06 06:58:00 +0000528 s := client.testString('Test');
529 Expect( s = 'Test', 'testString(''Test'') = "'+s+'"');
530
Jens Geyer06045cf2013-03-27 20:26:25 +0200531 s := client.testString(HUGE_TEST_STRING);
532 Expect( length(s) = length(HUGE_TEST_STRING),
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100533 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
Jens Geyer06045cf2013-03-27 20:26:25 +0200534 +'=> length(result) = '+IntToStr(Length(s)));
535
Roger Meier3bef8c22012-10-06 06:58:00 +0000536 i8 := client.testByte(1);
537 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
538
539 i32 := client.testI32(-1);
540 Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32));
541
542 Console.WriteLine('testI64(-34359738368)');
543 i64 := client.testI64(-34359738368);
544 Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
545
Jens Geyerfd1b3582014-12-13 23:42:58 +0100546 binOut := PrepareBinaryData( TRUE);
547 Console.WriteLine('testBinary('+BytesToHex(binOut)+')');
548 try
549 binIn := client.testBinary(binOut);
550 Expect( Length(binOut) = Length(binIn), 'testBinary(): length '+IntToStr(Length(binOut))+' = '+IntToStr(Length(binIn)));
551 i32 := Min( Length(binOut), Length(binIn));
552 Expect( CompareMem( binOut, binIn, i32), 'testBinary('+BytesToHex(binOut)+') = '+BytesToHex(binIn));
553 except
554 on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message);
555 on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message);
556 end;
557
Roger Meier3bef8c22012-10-06 06:58:00 +0000558 Console.WriteLine('testDouble(5.325098235)');
559 dub := client.testDouble(5.325098235);
560 Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
561
562 // structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200563 StartTestGroup( 'testStruct', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000564 Console.WriteLine('testStruct({''Zero'', 1, -3, -5})');
565 o := TXtructImpl.Create;
566 o.String_thing := 'Zero';
567 o.Byte_thing := 1;
568 o.I32_thing := -3;
569 o.I64_thing := -5;
570 i := client.testStruct(o);
571 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
572 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
573 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
574 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
575 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
576 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
577 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
578 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
579
580 // nested structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200581 StartTestGroup( 'testNest', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000582 Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})');
583 o2 := TXtruct2Impl.Create;
584 o2.Byte_thing := 1;
585 o2.Struct_thing := o;
586 o2.I32_thing := 5;
587 i2 := client.testNest(o2);
588 i := i2.Struct_thing;
589 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
590 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
591 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
592 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
593 Expect( i2.Byte_thing = 1, 'i2.Byte_thing = '+IntToStr(i2.Byte_thing));
594 Expect( i2.I32_thing = 5, 'i2.I32_thing = '+IntToStr(i2.I32_thing));
595 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
596 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
597 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
598 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
599 Expect( i2.__isset_Byte_thing, 'i2.__isset_Byte_thing');
600 Expect( i2.__isset_I32_thing, 'i2.__isset_I32_thing');
601
602 // map<type1,type2>: A map of strictly unique keys to values.
603 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200604 StartTestGroup( 'testMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000605 mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
606 for j := 0 to 4 do
607 begin
608 mapout.AddOrSetValue( j, j - 10);
609 end;
610 Console.Write('testMap({');
611 first := True;
612 for key in mapout.Keys do
613 begin
614 if first
615 then first := False
616 else Console.Write( ', ' );
617 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
618 end;
619 Console.WriteLine('})');
620
621 mapin := client.testMap( mapout );
622 Expect( mapin.Count = mapout.Count, 'testMap: mapin.Count = mapout.Count');
623 for j := 0 to 4 do
624 begin
625 Expect( mapout.ContainsKey(j), 'testMap: mapout.ContainsKey('+IntToStr(j)+') = '+BoolToString(mapout.ContainsKey(j)));
626 end;
627 for key in mapin.Keys do
628 begin
629 Expect( mapin[key] = mapout[key], 'testMap: '+IntToStr(key) + ' => ' + IntToStr( mapin[key]));
630 Expect( mapin[key] = key - 10, 'testMap: mapin['+IntToStr(key)+'] = '+IntToStr( mapin[key]));
631 end;
632
633
634 // map<type1,type2>: A map of strictly unique keys to values.
635 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200636 StartTestGroup( 'testStringMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000637 strmapout := TThriftDictionaryImpl<string,string>.Create;
638 for j := 0 to 4 do
639 begin
640 strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10));
641 end;
642 Console.Write('testStringMap({');
643 first := True;
644 for strkey in strmapout.Keys do
645 begin
646 if first
647 then first := False
648 else Console.Write( ', ' );
649 Console.Write( strkey + ' => ' + strmapout[strkey]);
650 end;
651 Console.WriteLine('})');
652
653 strmapin := client.testStringMap( strmapout );
654 Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count');
655 for j := 0 to 4 do
656 begin
657 Expect( strmapout.ContainsKey(IntToStr(j)),
658 'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = '
659 + BoolToString(strmapout.ContainsKey(IntToStr(j))));
660 end;
661 for strkey in strmapin.Keys do
662 begin
663 Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]);
664 Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]);
665 end;
666
667
668 // set<type>: An unordered set of unique elements.
669 // Translates to an STL set, Java HashSet, set in Python, etc.
670 // Note: PHP does not support sets, so it is treated similar to a List
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200671 StartTestGroup( 'testSet', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000672 setout := THashSetImpl<Integer>.Create;
673 for j := -2 to 2 do
674 begin
675 setout.Add( j );
676 end;
677 Console.Write('testSet({');
678 first := True;
679 for j in setout do
680 begin
681 if first
682 then first := False
683 else Console.Write(', ');
684 Console.Write(IntToStr( j));
685 end;
686 Console.WriteLine('})');
687
688 setin := client.testSet(setout);
689 Expect( setin.Count = setout.Count, 'testSet: setin.Count = setout.Count');
690 Expect( setin.Count = 5, 'testSet: setin.Count = '+IntToStr(setin.Count));
691 for j := -2 to 2 do // unordered, we can't rely on the order => test for known elements only
692 begin
693 Expect( setin.Contains(j), 'testSet: setin.Contains('+IntToStr(j)+') => '+BoolToString(setin.Contains(j)));
694 end;
695
696 // list<type>: An ordered list of elements.
697 // Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200698 StartTestGroup( 'testList', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000699 listout := TThriftListImpl<Integer>.Create;
700 listout.Add( +1);
701 listout.Add( -2);
702 listout.Add( +3);
703 listout.Add( -4);
704 listout.Add( 0);
705 Console.Write('testList({');
706 first := True;
707 for j in listout do
708 begin
709 if first
710 then first := False
711 else Console.Write(', ');
712 Console.Write(IntToStr( j));
713 end;
714 Console.WriteLine('})');
715
716 listin := client.testList(listout);
717 Expect( listin.Count = listout.Count, 'testList: listin.Count = listout.Count');
718 Expect( listin.Count = 5, 'testList: listin.Count = '+IntToStr(listin.Count));
719 Expect( listin[0] = +1, 'listin[0] = '+IntToStr( listin[0]));
720 Expect( listin[1] = -2, 'listin[1] = '+IntToStr( listin[1]));
721 Expect( listin[2] = +3, 'listin[2] = '+IntToStr( listin[2]));
722 Expect( listin[3] = -4, 'listin[3] = '+IntToStr( listin[3]));
723 Expect( listin[4] = 0, 'listin[4] = '+IntToStr( listin[4]));
724
725 // enums
726 ret := client.testEnum(TNumberz.ONE);
727 Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret)));
728
729 ret := client.testEnum(TNumberz.TWO);
730 Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret)));
731
732 ret := client.testEnum(TNumberz.THREE);
733 Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret)));
734
735 ret := client.testEnum(TNumberz.FIVE);
736 Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret)));
737
738 ret := client.testEnum(TNumberz.EIGHT);
739 Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret)));
740
741
742 // typedef
743 uid := client.testTypedef(309858235082523);
744 Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid));
745
746
747 // maps of maps
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200748 StartTestGroup( 'testMapMap(1)', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000749 mm := client.testMapMap(1);
750 Console.Write(' = {');
751 for key in mm.Keys do
752 begin
753 Console.Write( IntToStr( key) + ' => {');
754 m2 := mm[key];
755 for k2 in m2.Keys do
756 begin
757 Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', ');
758 end;
759 Console.Write('}, ');
760 end;
761 Console.WriteLine('}');
762
763 // verify result data
764 Expect( mm.Count = 2, 'mm.Count = '+IntToStr(mm.Count));
765 pos := mm[4];
766 neg := mm[-4];
767 for j := 1 to 4 do
768 begin
769 Expect( pos[j] = j, 'pos[j] = '+IntToStr(pos[j]));
770 Expect( neg[-j] = -j, 'neg[-j] = '+IntToStr(neg[-j]));
771 end;
772
773
774
775 // insanity
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200776 StartTestGroup( 'testInsanity', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000777 insane := TInsanityImpl.Create;
778 insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
779 insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000);
780 truck := TXtructImpl.Create;
781 truck.String_thing := 'Truck';
Jens Geyer540e3462016-12-28 14:25:41 +0100782 truck.Byte_thing := -8; // byte is signed
783 truck.I32_thing := 32;
784 truck.I64_thing := 64;
Roger Meier3bef8c22012-10-06 06:58:00 +0000785 insane.Xtructs := TThriftListImpl<IXtruct>.Create;
786 insane.Xtructs.Add( truck );
787 whoa := client.testInsanity( insane );
788 Console.Write(' = {');
789 for key64 in whoa.Keys do
790 begin
791 val := whoa[key64];
792 Console.Write( IntToStr( key64) + ' => {');
793 for k2_2 in val.Keys do
794 begin
795 v2 := val[k2_2];
796 Console.Write( IntToStr( Integer( k2_2)) + ' => {');
797 userMap := v2.UserMap;
798 Console.Write('{');
799 if userMap <> nil then
800 begin
801 for k3 in userMap.Keys do
802 begin
803 Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', ');
804 end;
805 end else
806 begin
807 Console.Write('null');
808 end;
809 Console.Write('}, ');
810 xtructs := v2.Xtructs;
811 Console.Write('{');
812
813 if xtructs <> nil then
814 begin
815 for x in xtructs do
816 begin
817 Console.Write('{"' + x.String_thing + '", ' +
818 IntToStr( x.Byte_thing) + ', ' +
819 IntToStr( x.I32_thing) + ', ' +
820 IntToStr( x.I32_thing) + '}, ');
821 end;
822 end else
823 begin
824 Console.Write('null');
825 end;
826 Console.Write('}');
827 Console.Write('}, ');
828 end;
829 Console.Write('}, ');
830 end;
831 Console.WriteLine('}');
832
Jens Geyer540e3462016-12-28 14:25:41 +0100833 (**
834 * So you think you've got this all worked, out eh?
835 *
836 * Creates a the returned map with these values and prints it out:
837 * { 1 => { 2 => argument,
838 * 3 => argument,
839 * },
840 * 2 => { 6 => <empty Insanity struct>, },
841 * }
842 * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
843 *)
844
Roger Meier3bef8c22012-10-06 06:58:00 +0000845 // verify result data
846 Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count));
847 //
848 first_map := whoa[1];
849 second_map := whoa[2];
850 Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count));
851 Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count));
852 //
853 looney := second_map[TNumberz.SIX];
854 Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney)));
855 Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
856 Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
857 //
858 for ret in [TNumberz.TWO, TNumberz.THREE] do begin
859 crazy := first_map[ret];
860 Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
861
862 Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap));
863 Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs));
864
Jens Geyer540e3462016-12-28 14:25:41 +0100865 Expect( crazy.UserMap.Count = insane.UserMap.Count, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count));
866 for pair in insane.UserMap do begin
867 Expect( crazy.UserMap[pair.Key] = pair.Value, 'crazy.UserMap['+IntToStr(Ord(pair.key))+'] = '+IntToStr(crazy.UserMap[pair.Key]));
868 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000869
Jens Geyer540e3462016-12-28 14:25:41 +0100870 Expect( crazy.Xtructs.Count = insane.Xtructs.Count, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count));
871 for arg0 := 0 to insane.Xtructs.Count-1 do begin
872 hello := insane.Xtructs[arg0];
873 goodbye := crazy.Xtructs[arg0];
874 Expect( goodbye.String_thing = hello.String_thing, 'goodbye.String_thing = '+goodbye.String_thing);
875 Expect( goodbye.Byte_thing = hello.Byte_thing, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing));
876 Expect( goodbye.I32_thing = hello.I32_thing, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing));
877 Expect( goodbye.I64_thing = hello.I64_thing, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing));
878 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000879 end;
880
881
882 // multi args
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200883 StartTestGroup( 'testMulti', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000884 arg0 := 1;
885 arg1 := 2;
886 arg2 := High(Int64);
887 arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
888 arg3.AddOrSetValue( 1, 'one');
889 arg4 := TNumberz.FIVE;
890 arg5 := 5000000;
891 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
892 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
893 arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
894 IntToStr( arg5) + ')');
895
896 i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5);
897 Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"');
898 Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing));
899 Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing));
900 Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing));
901 Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
902 Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
903 Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
904 Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
905
906 // multi exception
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200907 StartTestGroup( 'testMultiException(1)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000908 try
909 i := client.testMultiException( 'need more pizza', 'run out of beer');
910 Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"');
911 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
Jens Geyer6bbbf192014-09-07 01:45:56 +0200912 { this is not necessarily true, these fields are default-serialized
Jens Geyerd5436f52014-10-03 19:50:38 +0200913 Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
Roger Meier3bef8c22012-10-06 06:58:00 +0000914 Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
915 Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
Jens Geyerd5436f52014-10-03 19:50:38 +0200916 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000917 except
918 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
919 end;
920
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200921 StartTestGroup( 'testMultiException(Xception)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000922 try
923 i := client.testMultiException( 'Xception', 'second test');
924 Expect( FALSE, 'testMultiException(''Xception''): must trow an exception');
925 except
926 on x:TXception do begin
927 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
928 Expect( x.__isset_Message_, 'x.__isset_Message_ = '+BoolToString(x.__isset_Message_));
929 Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
930 Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"');
931 end;
932 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
933 end;
934
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200935 StartTestGroup( 'testMultiException(Xception2)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000936 try
937 i := client.testMultiException( 'Xception2', 'third test');
938 Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception');
939 except
940 on x:TXception2 do begin
941 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
942 Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing));
943 Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
944 Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"');
945 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 +0200946 { this is not necessarily true, these fields are default-serialized
Roger Meier3bef8c22012-10-06 06:58:00 +0000947 Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing));
948 Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing));
949 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 +0200950 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000951 end;
952 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
953 end;
954
955
956 // oneway functions
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200957 StartTestGroup( 'Test Oneway(1)', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000958 client.testOneway(1);
959 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
960
961 // call time
Jens Geyer06045cf2013-03-27 20:26:25 +0200962 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000963 StartTestGroup( 'Test Calltime()');
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200964 StartTick := GetTickCount;
Roger Meier3bef8c22012-10-06 06:58:00 +0000965 for k := 0 to 1000 - 1 do
966 begin
967 client.testVoid();
968 end;
969 Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
Jens Geyer06045cf2013-03-27 20:26:25 +0200970 {$ENDIF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000971
972 // no more tests here
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200973 StartTestGroup( '', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000974end;
975
976
Jens Geyer718f6ee2013-09-06 21:02:34 +0200977{$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200978procedure TClientThread.StressTest(const client : TThriftTest.Iface);
979begin
980 while TRUE do begin
981 try
982 if not FTransport.IsOpen then FTransport.Open; // re-open connection, server has already closed
983 try
984 client.testString('Test');
985 Write('.');
986 finally
987 if FTransport.IsOpen then FTransport.Close;
988 end;
989 except
990 on e:Exception do Writeln(#10+e.message);
991 end;
992 end;
993end;
Jens Geyer718f6ee2013-09-06 21:02:34 +0200994{$ENDIF}
Jens Geyer06045cf2013-03-27 20:26:25 +0200995
Jens Geyerfd1b3582014-12-13 23:42:58 +0100996
997function TClientThread.PrepareBinaryData( aRandomDist : Boolean = FALSE) : TBytes;
998var i, nextPos : Integer;
999begin
1000 SetLength( result, $100);
1001 ASSERT( Low(result) = 0);
1002
1003 // linear distribution, unless random is requested
1004 if not aRandomDist then begin
1005 for i := Low(result) to High(result) do begin
1006 result[i] := i;
1007 end;
1008 Exit;
1009 end;
1010
1011 // random distribution of all 256 values
1012 FillChar( result[0], Length(result) * SizeOf(result[0]), $0);
1013 i := 1;
1014 while i < Length(result) do begin
1015 nextPos := Byte( Random($100));
1016 if result[nextPos] = 0 then begin // unused?
1017 result[nextPos] := i;
1018 Inc(i);
1019 end;
1020 end;
1021end;
1022
1023
Jens Geyerf7904452017-07-26 15:02:12 +02001024{$IFDEF Win64}
1025procedure TClientThread.UseInterlockedExchangeAdd64;
1026var a,b : Int64;
1027begin
1028 a := 1;
1029 b := 2;
1030 Thrift.Utils.InterlockedExchangeAdd64( a,b);
1031 Expect( a = 3, 'InterlockedExchangeAdd64');
1032end;
1033{$ENDIF}
1034
1035
Roger Meier3bef8c22012-10-06 06:58:00 +00001036procedure TClientThread.JSONProtocolReadWriteTest;
1037// Tests only then read/write procedures of the JSON protocol
1038// All tests succeed, if we can read what we wrote before
1039// Note that passing this test does not imply, that our JSON is really compatible to what
1040// other clients or servers expect as the real JSON. This is beyond the scope of this test.
1041var prot : IProtocol;
1042 stm : TStringStream;
Jens Geyer17c3ad92017-09-05 20:31:27 +02001043 list : TThriftList;
Roger Meier3bef8c22012-10-06 06:58:00 +00001044 binary, binRead : TBytes;
1045 i,iErr : Integer;
1046const
1047 TEST_SHORT = ShortInt( $FE);
1048 TEST_SMALL = SmallInt( $FEDC);
1049 TEST_LONG = LongInt( $FEDCBA98);
1050 TEST_I64 = Int64( $FEDCBA9876543210);
1051 TEST_DOUBLE = -1.234e-56;
1052 DELTA_DOUBLE = TEST_DOUBLE * 1e-14;
1053 TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars")
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001054 // Test THRIFT-2336 and THRIFT-3404 with U+1D11E (G Clef symbol) and 'Русское Название';
1055 G_CLEF_AND_CYRILLIC_TEXT = #$1d11e' '#$0420#$0443#$0441#$0441#$043a#$043e#$0435' '#$041d#$0430#$0437#$0432#$0430#$043d#$0438#$0435;
1056 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 +01001057 // test both possible solidus encodings
1058 SOLIDUS_JSON_DATA = '"one/two\/three"';
1059 SOLIDUS_EXCPECTED = 'one/two/three';
Roger Meier3bef8c22012-10-06 06:58:00 +00001060begin
1061 stm := TStringStream.Create;
1062 try
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001063 StartTestGroup( 'JsonProtocolTest', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001064
1065 // prepare binary data
Jens Geyerfd1b3582014-12-13 23:42:58 +01001066 binary := PrepareBinaryData( FALSE);
Roger Meier3bef8c22012-10-06 06:58:00 +00001067
1068 // output setup
1069 prot := TJSONProtocolImpl.Create(
1070 TStreamTransportImpl.Create(
1071 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
1072
1073 // write
Jens Geyer17c3ad92017-09-05 20:31:27 +02001074 Init( list, TType.String_, 9);
1075 prot.WriteListBegin( list);
Roger Meier3bef8c22012-10-06 06:58:00 +00001076 prot.WriteBool( TRUE);
1077 prot.WriteBool( FALSE);
1078 prot.WriteByte( TEST_SHORT);
1079 prot.WriteI16( TEST_SMALL);
1080 prot.WriteI32( TEST_LONG);
1081 prot.WriteI64( TEST_I64);
1082 prot.WriteDouble( TEST_DOUBLE);
1083 prot.WriteString( TEST_STRING);
1084 prot.WriteBinary( binary);
1085 prot.WriteListEnd;
1086
1087 // input setup
1088 Expect( stm.Position = stm.Size, 'Stream position/length after write');
1089 stm.Position := 0;
1090 prot := TJSONProtocolImpl.Create(
1091 TStreamTransportImpl.Create(
1092 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1093
1094 // read and compare
1095 list := prot.ReadListBegin;
1096 Expect( list.ElementType = TType.String_, 'list element type');
1097 Expect( list.Count = 9, 'list element count');
1098 Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE');
1099 Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE');
1100 Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte');
1101 Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16');
1102 Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32');
1103 Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64');
1104 Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble');
1105 Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString');
1106 binRead := prot.ReadBinary;
1107 prot.ReadListEnd;
1108
1109 // test binary data
1110 Expect( Length(binary) = Length(binRead), 'Binary data length check');
1111 iErr := -1;
1112 for i := Low(binary) to High(binary) do begin
1113 if binary[i] <> binRead[i] then begin
1114 iErr := i;
1115 Break;
1116 end;
1117 end;
1118 if iErr < 0
1119 then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)')
1120 else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr));
1121
1122 Expect( stm.Position = stm.Size, 'Stream position after read');
1123
Jens Geyer7bb44a32014-02-07 22:24:37 +01001124
Jens Geyer21366942013-12-30 22:04:51 +01001125 // Solidus can be encoded in two ways. Make sure we can read both
1126 stm.Position := 0;
1127 stm.Size := 0;
1128 stm.WriteString(SOLIDUS_JSON_DATA);
1129 stm.Position := 0;
1130 prot := TJSONProtocolImpl.Create(
1131 TStreamTransportImpl.Create(
1132 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1133 Expect( prot.ReadString = SOLIDUS_EXCPECTED, 'Solidus encoding');
1134
1135
Jens Geyer7bb44a32014-02-07 22:24:37 +01001136 // Widechars should work too. Do they?
1137 // After writing, we ensure that we are able to read it back
1138 // We can't assume hex-encoding, since (nearly) any Unicode char is valid JSON
1139 stm.Position := 0;
1140 stm.Size := 0;
1141 prot := TJSONProtocolImpl.Create(
1142 TStreamTransportImpl.Create(
1143 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001144 prot.WriteString( G_CLEF_AND_CYRILLIC_TEXT);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001145 stm.Position := 0;
1146 prot := TJSONProtocolImpl.Create(
1147 TStreamTransportImpl.Create(
1148 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001149 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Writing JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001150
1151 // Widechars should work with hex-encoding too. Do they?
1152 stm.Position := 0;
1153 stm.Size := 0;
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001154 stm.WriteString( G_CLEF_AND_CYRILLIC_JSON);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001155 stm.Position := 0;
1156 prot := TJSONProtocolImpl.Create(
1157 TStreamTransportImpl.Create(
1158 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001159 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Reading JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001160
1161
Roger Meier3bef8c22012-10-06 06:58:00 +00001162 finally
1163 stm.Free;
1164 prot := nil; //-> Release
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001165 StartTestGroup( '', test_Unknown); // no more tests here
Roger Meier3bef8c22012-10-06 06:58:00 +00001166 end;
1167end;
1168
1169
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001170procedure TClientThread.StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +00001171begin
1172 FTestGroup := aGroup;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001173 FCurrentTest := aTest;
1174
1175 Include( FExecuted, aTest);
1176
Roger Meier3bef8c22012-10-06 06:58:00 +00001177 if FTestGroup <> '' then begin
1178 Console.WriteLine('');
1179 Console.WriteLine( aGroup+' tests');
1180 Console.WriteLine( StringOfChar('-',60));
1181 end;
1182end;
1183
1184
1185procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
1186begin
1187 if aTestResult then begin
1188 Inc(FSuccesses);
1189 Console.WriteLine( aTestInfo+': passed');
1190 end
1191 else begin
1192 FErrors.Add( FTestGroup+': '+aTestInfo);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001193 Include( FFailed, FCurrentTest);
Roger Meier3bef8c22012-10-06 06:58:00 +00001194 Console.WriteLine( aTestInfo+': *** FAILED ***');
1195
1196 // We have a failed test!
1197 // -> issue DebugBreak ONLY if a debugger is attached,
1198 // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise
Jens Geyer9f7f11e2016-04-14 21:37:11 +02001199 if IsDebuggerPresent
1200 then {$IFDEF CPUX64} DebugBreak {$ELSE} asm int 3 end {$ENDIF};
Roger Meier3bef8c22012-10-06 06:58:00 +00001201 end;
1202end;
1203
1204
1205procedure TClientThread.ReportResults;
1206var nTotal : Integer;
1207 sLine : string;
1208begin
1209 // prevent us from stupid DIV/0 errors
1210 nTotal := FSuccesses + FErrors.Count;
1211 if nTotal = 0 then begin
1212 Console.WriteLine('No results logged');
1213 Exit;
1214 end;
1215
1216 Console.WriteLine('');
1217 Console.WriteLine( StringOfChar('=',60));
1218 Console.WriteLine( IntToStr(nTotal)+' tests performed');
1219 Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)');
1220 Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)');
1221 Console.WriteLine( StringOfChar('=',60));
1222 if FErrors.Count > 0 then begin
1223 Console.WriteLine('FAILED TESTS:');
1224 for sLine in FErrors do Console.WriteLine('- '+sLine);
1225 Console.WriteLine( StringOfChar('=',60));
1226 InterlockedIncrement( ExitCode); // return <> 0 on errors
1227 end;
1228 Console.WriteLine('');
1229end;
1230
1231
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001232function TClientThread.CalculateExitCode : Byte;
1233var test : TTestGroup;
1234begin
1235 result := EXITCODE_SUCCESS;
1236 for test := Low(TTestGroup) to High(TTestGroup) do begin
1237 if (test in FFailed) or not (test in FExecuted)
1238 then result := result or MAP_FAILURES_TO_EXITCODE_BITS[test];
1239 end;
1240end;
1241
1242
Roger Meier3bef8c22012-10-06 06:58:00 +00001243constructor TClientThread.Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
1244begin
1245 inherited Create( True );
1246 FNumIteration := ANumIteration;
1247 FTransport := ATransport;
1248 FProtocol := AProtocol;
1249 FConsole := TThreadConsole.Create( Self );
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001250 FCurrentTest := test_Unknown;
Roger Meier3bef8c22012-10-06 06:58:00 +00001251
1252 // error list: keep correct order, allow for duplicates
1253 FErrors := TStringList.Create;
1254 FErrors.Sorted := FALSE;
1255 FErrors.Duplicates := dupAccept;
1256end;
1257
1258destructor TClientThread.Destroy;
1259begin
1260 FreeAndNil( FConsole);
1261 FreeAndNil( FErrors);
1262 inherited;
1263end;
1264
1265procedure TClientThread.Execute;
1266var
1267 i : Integer;
1268 proc : TThreadProcedure;
1269begin
1270 // perform all tests
1271 try
Jens Geyerf7904452017-07-26 15:02:12 +02001272 {$IFDEF Win64}
1273 UseInterlockedExchangeAdd64;
1274 {$ENDIF}
Jens Geyer7bb44a32014-02-07 22:24:37 +01001275 JSONProtocolReadWriteTest;
Jens Geyerf7904452017-07-26 15:02:12 +02001276
Roger Meier3bef8c22012-10-06 06:58:00 +00001277 for i := 0 to FNumIteration - 1 do
1278 begin
1279 ClientTest;
Roger Meier3bef8c22012-10-06 06:58:00 +00001280 end;
1281 except
1282 on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"');
1283 end;
1284
1285 // report the outcome
1286 ReportResults;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001287 SetReturnValue( CalculateExitCode);
Roger Meier3bef8c22012-10-06 06:58:00 +00001288
1289 // shutdown
1290 proc := procedure
1291 begin
1292 if FTransport <> nil then
1293 begin
1294 FTransport.Close;
1295 FTransport := nil;
1296 end;
1297 end;
1298
1299 Synchronize( proc );
1300end;
1301
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001302
Roger Meier3bef8c22012-10-06 06:58:00 +00001303{ TThreadConsole }
1304
1305constructor TThreadConsole.Create(AThread: TThread);
1306begin
Jens Geyer718f6ee2013-09-06 21:02:34 +02001307 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +00001308 FThread := AThread;
1309end;
1310
1311procedure TThreadConsole.Write(const S: string);
1312var
1313 proc : TThreadProcedure;
1314begin
1315 proc := procedure
1316 begin
1317 Console.Write( S );
1318 end;
1319 TThread.Synchronize( FThread, proc);
1320end;
1321
1322procedure TThreadConsole.WriteLine(const S: string);
1323var
1324 proc : TThreadProcedure;
1325begin
1326 proc := procedure
1327 begin
1328 Console.WriteLine( S );
1329 end;
1330 TThread.Synchronize( FThread, proc);
1331end;
1332
1333initialization
1334begin
1335 TTestClient.FNumIteration := 1;
1336 TTestClient.FNumThread := 1;
1337end;
1338
1339end.