blob: 3be32181f8a452af9189690aa36d7fcc4bb01f8e [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
25{.$DEFINE PerfTest} // activate to activate the performance test
26
Roger Meier3bef8c22012-10-06 06:58:00 +000027interface
28
29uses
Jens Geyerfd1b3582014-12-13 23:42:58 +010030 Windows, SysUtils, Classes, Math,
Roger Meier3bef8c22012-10-06 06:58:00 +000031 DateUtils,
32 Generics.Collections,
33 TestConstants,
34 Thrift,
Jens Geyerf0e63312015-03-01 18:47:49 +010035 Thrift.Protocol.Compact,
Roger Meier3bef8c22012-10-06 06:58:00 +000036 Thrift.Protocol.JSON,
37 Thrift.Protocol,
38 Thrift.Transport.Pipes,
39 Thrift.Transport,
40 Thrift.Stream,
41 Thrift.Test,
42 Thrift.Collections,
43 Thrift.Console;
44
45type
46 TThreadConsole = class
47 private
48 FThread : TThread;
49 public
50 procedure Write( const S : string);
51 procedure WriteLine( const S : string);
52 constructor Create( AThread: TThread);
53 end;
54
55 TClientThread = class( TThread )
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020056 private type
57 TTestGroup = (
58 test_Unknown,
59 test_BaseTypes,
60 test_Structs,
61 test_Containers,
62 test_Exceptions
63 // new values here
64 );
65 TTestGroups = set of TTestGroup;
66
Roger Meier3bef8c22012-10-06 06:58:00 +000067 private
68 FTransport : ITransport;
69 FProtocol : IProtocol;
70 FNumIteration : Integer;
71 FConsole : TThreadConsole;
72
73 // test reporting, will be refactored out into separate class later
74 FTestGroup : string;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020075 FCurrentTest : TTestGroup;
Roger Meier3bef8c22012-10-06 06:58:00 +000076 FSuccesses : Integer;
77 FErrors : TStringList;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020078 FFailed : TTestGroups;
79 FExecuted : TTestGroups;
80 procedure StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +000081 procedure Expect( aTestResult : Boolean; const aTestInfo : string);
82 procedure ReportResults;
Jens Geyerfd1b3582014-12-13 23:42:58 +010083 function CalculateExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +000084
85 procedure ClientTest;
86 procedure JSONProtocolReadWriteTest;
Jens Geyerfd1b3582014-12-13 23:42:58 +010087 function PrepareBinaryData( aRandomDist : Boolean = FALSE) : TBytes;
Jens Geyer718f6ee2013-09-06 21:02:34 +020088 {$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +020089 procedure StressTest(const client : TThriftTest.Iface);
Jens Geyer718f6ee2013-09-06 21:02:34 +020090 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +000091 protected
92 procedure Execute; override;
93 public
94 constructor Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
95 destructor Destroy; override;
96 end;
97
98 TTestClient = class
99 private
100 class var
101 FNumIteration : Integer;
102 FNumThread : Integer;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200103
104 class procedure PrintCmdLineHelp;
105 class procedure InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000106 public
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200107 class function Execute( const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000108 end;
109
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200110
Roger Meier3bef8c22012-10-06 06:58:00 +0000111implementation
112
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200113const
114 EXITCODE_SUCCESS = $00; // no errors bits set
115 //
116 EXITCODE_FAILBIT_BASETYPES = $01;
117 EXITCODE_FAILBIT_STRUCTS = $02;
118 EXITCODE_FAILBIT_CONTAINERS = $04;
119 EXITCODE_FAILBIT_EXCEPTIONS = $08;
120
121 MAP_FAILURES_TO_EXITCODE_BITS : array[TClientThread.TTestGroup] of Byte = (
122 EXITCODE_SUCCESS, // no bits here
123 EXITCODE_FAILBIT_BASETYPES,
124 EXITCODE_FAILBIT_STRUCTS,
125 EXITCODE_FAILBIT_CONTAINERS,
126 EXITCODE_FAILBIT_EXCEPTIONS
127 );
128
129
130
Roger Meier3bef8c22012-10-06 06:58:00 +0000131function BoolToString( b : Boolean) : string;
132// overrides global BoolToString()
133begin
134 if b
135 then result := 'true'
136 else result := 'false';
137end;
138
139// not available in all versions, so make sure we have this one imported
140function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent';
141
142{ TTestClient }
143
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200144class procedure TTestClient.PrintCmdLineHelp;
145const HELPTEXT = ' [options]'#10
146 + #10
147 + 'Allowed options:'#10
148 + ' -h [ --help ] produce help message'#10
149 + ' --host arg (=localhost) Host to connect'#10
150 + ' --port arg (=9090) Port number to connect'#10
151 + ' --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift),'#10
152 + ' instead of host and port'#10
153 + ' --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)'#10
154 + ' --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)'#10
155 + ' --transport arg (=sockets) Transport: buffered, framed, http, evhttp'#10
156 + ' --protocol arg (=binary) Protocol: binary, compact, json'#10
157 + ' --ssl Encrypted Transport using SSL'#10
158 + ' -n [ --testloops ] arg (=1) Number of Tests'#10
159 + ' -t [ --threads ] arg (=1) Number of Test threads'#10
160 ;
161begin
162 Writeln( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + HELPTEXT);
163end;
164
165class procedure TTestClient.InvalidArgs;
166begin
167 Console.WriteLine( 'Invalid args.');
168 Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + ' -h for more information');
169 Abort;
170end;
171
172class function TTestClient.Execute(const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000173var
174 i : Integer;
175 host : string;
176 port : Integer;
Roger Meier3bef8c22012-10-06 06:58:00 +0000177 sPipeName : string;
178 hAnonRead, hAnonWrite : THandle;
179 s : string;
Roger Meier3bef8c22012-10-06 06:58:00 +0000180 threads : array of TThread;
181 dtStart : TDateTime;
182 test : Integer;
183 thread : TThread;
184 trans : ITransport;
185 prot : IProtocol;
186 streamtrans : IStreamTransport;
187 http : IHTTPClient;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200188 protType : TKnownProtocol;
189 endpoint : TEndpointTransport;
190 layered : TLayeredTransports;
191 UseSSL : Boolean; // include where appropriate (TLayeredTransport?)
Jens Geyer0b20cc82013-03-07 20:47:01 +0100192const
193 // pipe timeouts to be used
194 DEBUG_TIMEOUT = 30 * 1000;
Jens Geyer3e8d9272014-09-14 20:10:40 +0200195 RELEASE_TIMEOUT = DEFAULT_THRIFT_TIMEOUT;
Jens Geyer0b20cc82013-03-07 20:47:01 +0100196 TIMEOUT = RELEASE_TIMEOUT;
Roger Meier3bef8c22012-10-06 06:58:00 +0000197begin
Roger Meier3bef8c22012-10-06 06:58:00 +0000198 protType := prot_Binary;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200199 endpoint := trns_Sockets;
200 layered := [];
201 UseSSL := FALSE;
202 host := 'localhost';
203 port := 9090;
204 sPipeName := '';
205 hAnonRead := INVALID_HANDLE_VALUE;
206 hAnonWrite := INVALID_HANDLE_VALUE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000207 try
Roger Meier3bef8c22012-10-06 06:58:00 +0000208 i := 0;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200209 while ( i < Length(args) ) do begin
210 s := args[i];
211 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000212
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200213 if (s = '-h') or (s = '--help') then begin
214 // -h [ --help ] produce help message
215 PrintCmdLineHelp;
216 result := $FF; // all tests failed
217 Exit;
218 end
Jens Geyerb360b652014-09-28 01:55:46 +0200219 else if s = '--host' then begin
220 // --host arg (=localhost) Host to connect
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200221 host := args[i];
222 Inc( i);
223 end
Jens Geyerb360b652014-09-28 01:55:46 +0200224 else if s = '--port' then begin
225 // --port arg (=9090) Port number to connect
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200226 s := args[i];
227 Inc( i);
228 port := StrToIntDef(s,0);
229 if port <= 0 then InvalidArgs;
230 end
Jens Geyerb360b652014-09-28 01:55:46 +0200231 else if s = '--domain-socket' then begin
232 // --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200233 raise Exception.Create('domain-socket not supported');
234 end
Jens Geyerb360b652014-09-28 01:55:46 +0200235 else if s = '--named-pipe' then begin
236 // --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200237 endpoint := trns_NamedPipes;
238 sPipeName := args[i];
239 Inc( i);
240 end
Jens Geyerb360b652014-09-28 01:55:46 +0200241 else if s = '--anon-pipes' then begin
242 // --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200243 endpoint := trns_AnonPipes;
244 hAnonRead := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
245 Inc( i);
246 hAnonWrite := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
247 Inc( i);
248 end
Jens Geyerb360b652014-09-28 01:55:46 +0200249 else if s = '--transport' then begin
250 // --transport arg (=sockets) Transport: buffered, framed, http, evhttp
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200251 s := args[i];
252 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000253
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200254 if s = 'buffered' then Include( layered, trns_Buffered)
255 else if s = 'framed' then Include( layered, trns_Framed)
256 else if s = 'http' then endpoint := trns_Http
257 else if s = 'evhttp' then endpoint := trns_AnonPipes
258 else InvalidArgs;
259 end
Jens Geyerb360b652014-09-28 01:55:46 +0200260 else if s = '--protocol' then begin
261 // --protocol arg (=binary) Protocol: binary, compact, json
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200262 s := args[i];
263 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000264
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200265 if s = 'binary' then protType := prot_Binary
266 else if s = 'compact' then protType := prot_Compact
267 else if s = 'json' then protType := prot_JSON
268 else InvalidArgs;
269 end
Jens Geyerb360b652014-09-28 01:55:46 +0200270 else if s = '--ssl' then begin
271 // --ssl Encrypted Transport using SSL
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200272 UseSSL := TRUE;
273
274 end
275 else if (s = '-n') or (s = '--testloops') then begin
276 // -n [ --testloops ] arg (=1) Number of Tests
277 FNumIteration := StrToIntDef( args[i], 0);
278 Inc( i);
279 if FNumIteration <= 0
280 then InvalidArgs;
281
282 end
283 else if (s = '-t') or (s = '--threads') then begin
284 // -t [ --threads ] arg (=1) Number of Test threads
285 FNumThread := StrToIntDef( args[i], 0);
286 Inc( i);
287 if FNumThread <= 0
288 then InvalidArgs;
289 end
290 else begin
291 InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000292 end;
293 end;
294
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200295
Roger Meier79655fb2012-10-20 20:59:41 +0000296 // In the anonymous pipes mode the client is launched by the test server
297 // -> behave nicely and allow for attaching a debugger to this process
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200298 if (endpoint = trns_AnonPipes) and not IsDebuggerPresent
Roger Meier79655fb2012-10-20 20:59:41 +0000299 then MessageBox( 0, 'Attach Debugger and/or click OK to continue.',
300 'Thrift TestClient (Delphi)',
301 MB_OK or MB_ICONEXCLAMATION);
302
Roger Meier3bef8c22012-10-06 06:58:00 +0000303 SetLength( threads, FNumThread);
304 dtStart := Now;
305
306 for test := 0 to FNumThread - 1 do
307 begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200308 case endpoint of
309 trns_Sockets: begin
Roger Meier3bef8c22012-10-06 06:58:00 +0000310 Console.WriteLine('Using sockets ('+host+' port '+IntToStr(port)+')');
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200311 {$IFDEF OLD_SOCKETS}
Roger Meier3bef8c22012-10-06 06:58:00 +0000312 streamtrans := TSocketImpl.Create( host, port );
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200313 {$ELSE}
314 raise Exception.Create(ENDPOINT_TRANSPORTS[endpoint]+' transport not implemented');
315 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000316 end;
317
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200318 trns_Http: begin
319 Console.WriteLine('Using HTTPClient');
320 http := THTTPClientImpl.Create( host);
321 trans := http;
Roger Meier3bef8c22012-10-06 06:58:00 +0000322 end;
323
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200324 trns_EvHttp: begin
325 raise Exception.Create(ENDPOINT_TRANSPORTS[endpoint]+' transport not implemented');
Roger Meier3bef8c22012-10-06 06:58:00 +0000326 end;
327
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200328 trns_NamedPipes: begin
329 Console.WriteLine('Using named pipe ('+sPipeName+')');
330 streamtrans := TNamedPipeTransportClientEndImpl.Create( sPipeName, 0, nil, TIMEOUT);
331 end;
332
333 trns_AnonPipes: begin
334 Console.WriteLine('Using anonymous pipes ('+IntToStr(Integer(hAnonRead))+' and '+IntToStr(Integer(hAnonWrite))+')');
335 streamtrans := TAnonymousPipeTransportImpl.Create( hAnonRead, hAnonWrite, FALSE);
336 end;
337
338 else
339 raise Exception.Create('Unhandled endpoint transport');
340 end;
341 trans := streamtrans;
342 ASSERT( trans <> nil);
343
344 if (trns_Buffered in layered) then begin
345 trans := TBufferedTransportImpl.Create( streamtrans, 32); // small buffer to test read()
346 Console.WriteLine('Using buffered transport');
347 end;
348
349 if (trns_Framed in layered) then begin
350 trans := TFramedTransportImpl.Create( trans );
351 Console.WriteLine('Using framed transport');
352 end;
353
354 if UseSSL then begin
355 raise Exception.Create('SSL not implemented');
Roger Meier3bef8c22012-10-06 06:58:00 +0000356 end;
357
358 // create protocol instance, default to BinaryProtocol
359 case protType of
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200360 prot_Binary : prot := TBinaryProtocolImpl.Create( trans, BINARY_STRICT_READ, BINARY_STRICT_WRITE);
361 prot_JSON : prot := TJSONProtocolImpl.Create( trans);
Jens Geyerf0e63312015-03-01 18:47:49 +0100362 prot_Compact : prot := TCompactProtocolImpl.Create( trans);
Roger Meier3bef8c22012-10-06 06:58:00 +0000363 else
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200364 raise Exception.Create('Unhandled protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000365 end;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200366 ASSERT( trans <> nil);
367 Console.WriteLine(THRIFT_PROTOCOLS[protType]+' protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000368
369 thread := TClientThread.Create( trans, prot, FNumIteration);
370 threads[test] := thread;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200371 thread.Start;
Roger Meier3bef8c22012-10-06 06:58:00 +0000372 end;
373
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200374 result := 0;
375 for test := 0 to FNumThread - 1 do begin
376 result := result or threads[test].WaitFor;
Roger Meier3bef8c22012-10-06 06:58:00 +0000377 end;
378
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200379 for test := 0 to FNumThread - 1
380 do threads[test].Free;
Roger Meier3bef8c22012-10-06 06:58:00 +0000381
382 Console.Write('Total time: ' + IntToStr( MilliSecondsBetween(Now, dtStart)));
383
384 except
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200385 on E: EAbort do raise;
386 on E: Exception do begin
387 Console.WriteLine( E.Message + #10 + E.StackTrace);
388 raise;
Roger Meier3bef8c22012-10-06 06:58:00 +0000389 end;
390 end;
391
392 Console.WriteLine('');
393 Console.WriteLine('done!');
394end;
395
396{ TClientThread }
397
398procedure TClientThread.ClientTest;
399var
400 client : TThriftTest.Iface;
401 s : string;
402 i8 : ShortInt;
403 i32 : Integer;
404 i64 : Int64;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100405 binOut,binIn : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000406 dub : Double;
407 o : IXtruct;
408 o2 : IXtruct2;
409 i : IXtruct;
410 i2 : IXtruct2;
411 mapout : IThriftDictionary<Integer,Integer>;
412 mapin : IThriftDictionary<Integer,Integer>;
413 strmapout : IThriftDictionary<string,string>;
414 strmapin : IThriftDictionary<string,string>;
415 j : Integer;
416 first : Boolean;
417 key : Integer;
418 strkey : string;
419 listout : IThriftList<Integer>;
420 listin : IThriftList<Integer>;
421 setout : IHashSet<Integer>;
422 setin : IHashSet<Integer>;
423 ret : TNumberz;
424 uid : Int64;
425 mm : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
426 pos : IThriftDictionary<Integer, Integer>;
427 neg : IThriftDictionary<Integer, Integer>;
428 m2 : IThriftDictionary<Integer, Integer>;
429 k2 : Integer;
430 insane : IInsanity;
431 truck : IXtruct;
432 whoa : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
433 key64 : Int64;
434 val : IThriftDictionary<TNumberz, IInsanity>;
435 k2_2 : TNumberz;
436 k3 : TNumberz;
437 v2 : IInsanity;
438 userMap : IThriftDictionary<TNumberz, Int64>;
439 xtructs : IThriftList<IXtruct>;
440 x : IXtruct;
441 arg0 : ShortInt;
442 arg1 : Integer;
443 arg2 : Int64;
444 arg3 : IThriftDictionary<SmallInt, string>;
445 arg4 : TNumberz;
446 arg5 : Int64;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200447 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000448 StartTick : Cardinal;
449 k : Integer;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200450 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000451 hello, goodbye : IXtruct;
452 crazy : IInsanity;
453 looney : IInsanity;
454 first_map : IThriftDictionary<TNumberz, IInsanity>;
455 second_map : IThriftDictionary<TNumberz, IInsanity>;
456
457begin
458 client := TThriftTest.TClient.Create( FProtocol);
459 FTransport.Open;
460
Jens Geyer06045cf2013-03-27 20:26:25 +0200461 {$IFDEF StressTest}
462 StressTest( client);
463 {$ENDIF StressTest}
464
Roger Meier3bef8c22012-10-06 06:58:00 +0000465 // in-depth exception test
466 // (1) do we get an exception at all?
467 // (2) do we get the right exception?
468 // (3) does the exception contain the expected data?
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200469 StartTestGroup( 'testException', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000470 // case 1: exception type declared in IDL at the function call
471 try
472 client.testException('Xception');
473 Expect( FALSE, 'testException(''Xception''): must trow an exception');
474 except
475 on e:TXception do begin
476 Expect( e.ErrorCode = 1001, 'error code');
477 Expect( e.Message_ = 'Xception', 'error message');
478 Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ );
479 end;
480 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
481 on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
482 end;
483
484 // case 2: exception type NOT declared in IDL at the function call
485 // this will close the connection
486 try
487 client.testException('TException');
488 Expect( FALSE, 'testException(''TException''): must trow an exception');
489 except
490 on e:TTransportException do begin
491 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Roger Meier3bef8c22012-10-06 06:58:00 +0000492 end;
Jens Geyer6bbbf192014-09-07 01:45:56 +0200493 on e:TApplicationException do begin
494 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Jens Geyer6bbbf192014-09-07 01:45:56 +0200495 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000496 on e:TException do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
497 on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
498 end;
499
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100500
Jens Geyer2ad6c302015-02-26 19:38:53 +0100501 if FTransport.IsOpen then FTransport.Close;
502 FTransport.Open; // re-open connection, server has already closed
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100503
Jens Geyer2ad6c302015-02-26 19:38:53 +0100504
Roger Meier3bef8c22012-10-06 06:58:00 +0000505 // case 3: no exception
506 try
507 client.testException('something');
508 Expect( TRUE, 'testException(''something''): must not trow an exception');
509 except
510 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
511 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
512 end;
513
514
515 // simple things
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200516 StartTestGroup( 'simple Thrift calls', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000517 client.testVoid();
518 Expect( TRUE, 'testVoid()'); // success := no exception
519
Jens Geyer39ba6b72015-09-22 00:00:49 +0200520 s := BoolToString( client.testBool(TRUE));
521 Expect( s = BoolToString(TRUE), 'testBool(TRUE) = '+s);
522 s := BoolToString( client.testBool(FALSE));
523 Expect( s = BoolToString(FALSE), 'testBool(FALSE) = '+s);
524
Roger Meier3bef8c22012-10-06 06:58:00 +0000525 s := client.testString('Test');
526 Expect( s = 'Test', 'testString(''Test'') = "'+s+'"');
527
Jens Geyer06045cf2013-03-27 20:26:25 +0200528 s := client.testString(HUGE_TEST_STRING);
529 Expect( length(s) = length(HUGE_TEST_STRING),
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100530 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
Jens Geyer06045cf2013-03-27 20:26:25 +0200531 +'=> length(result) = '+IntToStr(Length(s)));
532
Roger Meier3bef8c22012-10-06 06:58:00 +0000533 i8 := client.testByte(1);
534 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
535
536 i32 := client.testI32(-1);
537 Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32));
538
539 Console.WriteLine('testI64(-34359738368)');
540 i64 := client.testI64(-34359738368);
541 Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
542
Jens Geyerfd1b3582014-12-13 23:42:58 +0100543 binOut := PrepareBinaryData( TRUE);
544 Console.WriteLine('testBinary('+BytesToHex(binOut)+')');
545 try
546 binIn := client.testBinary(binOut);
547 Expect( Length(binOut) = Length(binIn), 'testBinary(): length '+IntToStr(Length(binOut))+' = '+IntToStr(Length(binIn)));
548 i32 := Min( Length(binOut), Length(binIn));
549 Expect( CompareMem( binOut, binIn, i32), 'testBinary('+BytesToHex(binOut)+') = '+BytesToHex(binIn));
550 except
551 on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message);
552 on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message);
553 end;
554
Roger Meier3bef8c22012-10-06 06:58:00 +0000555 Console.WriteLine('testDouble(5.325098235)');
556 dub := client.testDouble(5.325098235);
557 Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
558
559 // structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200560 StartTestGroup( 'testStruct', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000561 Console.WriteLine('testStruct({''Zero'', 1, -3, -5})');
562 o := TXtructImpl.Create;
563 o.String_thing := 'Zero';
564 o.Byte_thing := 1;
565 o.I32_thing := -3;
566 o.I64_thing := -5;
567 i := client.testStruct(o);
568 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
569 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
570 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
571 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
572 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
573 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
574 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
575 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
576
577 // nested structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200578 StartTestGroup( 'testNest', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000579 Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})');
580 o2 := TXtruct2Impl.Create;
581 o2.Byte_thing := 1;
582 o2.Struct_thing := o;
583 o2.I32_thing := 5;
584 i2 := client.testNest(o2);
585 i := i2.Struct_thing;
586 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
587 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
588 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
589 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
590 Expect( i2.Byte_thing = 1, 'i2.Byte_thing = '+IntToStr(i2.Byte_thing));
591 Expect( i2.I32_thing = 5, 'i2.I32_thing = '+IntToStr(i2.I32_thing));
592 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
593 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
594 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
595 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
596 Expect( i2.__isset_Byte_thing, 'i2.__isset_Byte_thing');
597 Expect( i2.__isset_I32_thing, 'i2.__isset_I32_thing');
598
599 // map<type1,type2>: A map of strictly unique keys to values.
600 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200601 StartTestGroup( 'testMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000602 mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
603 for j := 0 to 4 do
604 begin
605 mapout.AddOrSetValue( j, j - 10);
606 end;
607 Console.Write('testMap({');
608 first := True;
609 for key in mapout.Keys do
610 begin
611 if first
612 then first := False
613 else Console.Write( ', ' );
614 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
615 end;
616 Console.WriteLine('})');
617
618 mapin := client.testMap( mapout );
619 Expect( mapin.Count = mapout.Count, 'testMap: mapin.Count = mapout.Count');
620 for j := 0 to 4 do
621 begin
622 Expect( mapout.ContainsKey(j), 'testMap: mapout.ContainsKey('+IntToStr(j)+') = '+BoolToString(mapout.ContainsKey(j)));
623 end;
624 for key in mapin.Keys do
625 begin
626 Expect( mapin[key] = mapout[key], 'testMap: '+IntToStr(key) + ' => ' + IntToStr( mapin[key]));
627 Expect( mapin[key] = key - 10, 'testMap: mapin['+IntToStr(key)+'] = '+IntToStr( mapin[key]));
628 end;
629
630
631 // map<type1,type2>: A map of strictly unique keys to values.
632 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200633 StartTestGroup( 'testStringMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000634 strmapout := TThriftDictionaryImpl<string,string>.Create;
635 for j := 0 to 4 do
636 begin
637 strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10));
638 end;
639 Console.Write('testStringMap({');
640 first := True;
641 for strkey in strmapout.Keys do
642 begin
643 if first
644 then first := False
645 else Console.Write( ', ' );
646 Console.Write( strkey + ' => ' + strmapout[strkey]);
647 end;
648 Console.WriteLine('})');
649
650 strmapin := client.testStringMap( strmapout );
651 Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count');
652 for j := 0 to 4 do
653 begin
654 Expect( strmapout.ContainsKey(IntToStr(j)),
655 'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = '
656 + BoolToString(strmapout.ContainsKey(IntToStr(j))));
657 end;
658 for strkey in strmapin.Keys do
659 begin
660 Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]);
661 Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]);
662 end;
663
664
665 // set<type>: An unordered set of unique elements.
666 // Translates to an STL set, Java HashSet, set in Python, etc.
667 // Note: PHP does not support sets, so it is treated similar to a List
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200668 StartTestGroup( 'testSet', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000669 setout := THashSetImpl<Integer>.Create;
670 for j := -2 to 2 do
671 begin
672 setout.Add( j );
673 end;
674 Console.Write('testSet({');
675 first := True;
676 for j in setout do
677 begin
678 if first
679 then first := False
680 else Console.Write(', ');
681 Console.Write(IntToStr( j));
682 end;
683 Console.WriteLine('})');
684
685 setin := client.testSet(setout);
686 Expect( setin.Count = setout.Count, 'testSet: setin.Count = setout.Count');
687 Expect( setin.Count = 5, 'testSet: setin.Count = '+IntToStr(setin.Count));
688 for j := -2 to 2 do // unordered, we can't rely on the order => test for known elements only
689 begin
690 Expect( setin.Contains(j), 'testSet: setin.Contains('+IntToStr(j)+') => '+BoolToString(setin.Contains(j)));
691 end;
692
693 // list<type>: An ordered list of elements.
694 // Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200695 StartTestGroup( 'testList', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000696 listout := TThriftListImpl<Integer>.Create;
697 listout.Add( +1);
698 listout.Add( -2);
699 listout.Add( +3);
700 listout.Add( -4);
701 listout.Add( 0);
702 Console.Write('testList({');
703 first := True;
704 for j in listout do
705 begin
706 if first
707 then first := False
708 else Console.Write(', ');
709 Console.Write(IntToStr( j));
710 end;
711 Console.WriteLine('})');
712
713 listin := client.testList(listout);
714 Expect( listin.Count = listout.Count, 'testList: listin.Count = listout.Count');
715 Expect( listin.Count = 5, 'testList: listin.Count = '+IntToStr(listin.Count));
716 Expect( listin[0] = +1, 'listin[0] = '+IntToStr( listin[0]));
717 Expect( listin[1] = -2, 'listin[1] = '+IntToStr( listin[1]));
718 Expect( listin[2] = +3, 'listin[2] = '+IntToStr( listin[2]));
719 Expect( listin[3] = -4, 'listin[3] = '+IntToStr( listin[3]));
720 Expect( listin[4] = 0, 'listin[4] = '+IntToStr( listin[4]));
721
722 // enums
723 ret := client.testEnum(TNumberz.ONE);
724 Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret)));
725
726 ret := client.testEnum(TNumberz.TWO);
727 Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret)));
728
729 ret := client.testEnum(TNumberz.THREE);
730 Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret)));
731
732 ret := client.testEnum(TNumberz.FIVE);
733 Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret)));
734
735 ret := client.testEnum(TNumberz.EIGHT);
736 Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret)));
737
738
739 // typedef
740 uid := client.testTypedef(309858235082523);
741 Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid));
742
743
744 // maps of maps
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200745 StartTestGroup( 'testMapMap(1)', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000746 mm := client.testMapMap(1);
747 Console.Write(' = {');
748 for key in mm.Keys do
749 begin
750 Console.Write( IntToStr( key) + ' => {');
751 m2 := mm[key];
752 for k2 in m2.Keys do
753 begin
754 Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', ');
755 end;
756 Console.Write('}, ');
757 end;
758 Console.WriteLine('}');
759
760 // verify result data
761 Expect( mm.Count = 2, 'mm.Count = '+IntToStr(mm.Count));
762 pos := mm[4];
763 neg := mm[-4];
764 for j := 1 to 4 do
765 begin
766 Expect( pos[j] = j, 'pos[j] = '+IntToStr(pos[j]));
767 Expect( neg[-j] = -j, 'neg[-j] = '+IntToStr(neg[-j]));
768 end;
769
770
771
772 // insanity
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200773 StartTestGroup( 'testInsanity', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000774 insane := TInsanityImpl.Create;
775 insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
776 insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000);
777 truck := TXtructImpl.Create;
778 truck.String_thing := 'Truck';
779 truck.Byte_thing := 8;
780 truck.I32_thing := 8;
781 truck.I64_thing := 8;
782 insane.Xtructs := TThriftListImpl<IXtruct>.Create;
783 insane.Xtructs.Add( truck );
784 whoa := client.testInsanity( insane );
785 Console.Write(' = {');
786 for key64 in whoa.Keys do
787 begin
788 val := whoa[key64];
789 Console.Write( IntToStr( key64) + ' => {');
790 for k2_2 in val.Keys do
791 begin
792 v2 := val[k2_2];
793 Console.Write( IntToStr( Integer( k2_2)) + ' => {');
794 userMap := v2.UserMap;
795 Console.Write('{');
796 if userMap <> nil then
797 begin
798 for k3 in userMap.Keys do
799 begin
800 Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', ');
801 end;
802 end else
803 begin
804 Console.Write('null');
805 end;
806 Console.Write('}, ');
807 xtructs := v2.Xtructs;
808 Console.Write('{');
809
810 if xtructs <> nil then
811 begin
812 for x in xtructs do
813 begin
814 Console.Write('{"' + x.String_thing + '", ' +
815 IntToStr( x.Byte_thing) + ', ' +
816 IntToStr( x.I32_thing) + ', ' +
817 IntToStr( x.I32_thing) + '}, ');
818 end;
819 end else
820 begin
821 Console.Write('null');
822 end;
823 Console.Write('}');
824 Console.Write('}, ');
825 end;
826 Console.Write('}, ');
827 end;
828 Console.WriteLine('}');
829
830 // verify result data
831 Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count));
832 //
833 first_map := whoa[1];
834 second_map := whoa[2];
835 Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count));
836 Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count));
837 //
838 looney := second_map[TNumberz.SIX];
839 Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney)));
840 Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
841 Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
842 //
843 for ret in [TNumberz.TWO, TNumberz.THREE] do begin
844 crazy := first_map[ret];
845 Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
846
847 Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap));
848 Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs));
849
850 Expect( crazy.UserMap.Count = 2, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count));
851 Expect( crazy.UserMap[TNumberz.FIVE] = 5, 'crazy.UserMap[TNumberz.FIVE] = '+IntToStr(crazy.UserMap[TNumberz.FIVE]));
852 Expect( crazy.UserMap[TNumberz.EIGHT] = 8, 'crazy.UserMap[TNumberz.EIGHT] = '+IntToStr(crazy.UserMap[TNumberz.EIGHT]));
853
854 Expect( crazy.Xtructs.Count = 2, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count));
855 goodbye := crazy.Xtructs[0]; // lists are ordered, so we are allowed to assume this order
Jens Geyerd5436f52014-10-03 19:50:38 +0200856 hello := crazy.Xtructs[1];
Roger Meier3bef8c22012-10-06 06:58:00 +0000857
858 Expect( goodbye.String_thing = 'Goodbye4', 'goodbye.String_thing = "'+goodbye.String_thing+'"');
859 Expect( goodbye.Byte_thing = 4, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing));
860 Expect( goodbye.I32_thing = 4, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing));
861 Expect( goodbye.I64_thing = 4, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing));
862 Expect( goodbye.__isset_String_thing, 'goodbye.__isset_String_thing = '+BoolToString(goodbye.__isset_String_thing));
863 Expect( goodbye.__isset_Byte_thing, 'goodbye.__isset_Byte_thing = '+BoolToString(goodbye.__isset_Byte_thing));
864 Expect( goodbye.__isset_I32_thing, 'goodbye.__isset_I32_thing = '+BoolToString(goodbye.__isset_I32_thing));
865 Expect( goodbye.__isset_I64_thing, 'goodbye.__isset_I64_thing = '+BoolToString(goodbye.__isset_I64_thing));
866
867 Expect( hello.String_thing = 'Hello2', 'hello.String_thing = "'+hello.String_thing+'"');
868 Expect( hello.Byte_thing = 2, 'hello.Byte_thing = '+IntToStr(hello.Byte_thing));
869 Expect( hello.I32_thing = 2, 'hello.I32_thing = '+IntToStr(hello.I32_thing));
870 Expect( hello.I64_thing = 2, 'hello.I64_thing = '+IntToStr(hello.I64_thing));
871 Expect( hello.__isset_String_thing, 'hello.__isset_String_thing = '+BoolToString(hello.__isset_String_thing));
872 Expect( hello.__isset_Byte_thing, 'hello.__isset_Byte_thing = '+BoolToString(hello.__isset_Byte_thing));
873 Expect( hello.__isset_I32_thing, 'hello.__isset_I32_thing = '+BoolToString(hello.__isset_I32_thing));
874 Expect( hello.__isset_I64_thing, 'hello.__isset_I64_thing = '+BoolToString(hello.__isset_I64_thing));
875 end;
876
877
878 // multi args
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200879 StartTestGroup( 'testMulti', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000880 arg0 := 1;
881 arg1 := 2;
882 arg2 := High(Int64);
883 arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
884 arg3.AddOrSetValue( 1, 'one');
885 arg4 := TNumberz.FIVE;
886 arg5 := 5000000;
887 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
888 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
889 arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
890 IntToStr( arg5) + ')');
891
892 i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5);
893 Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"');
894 Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing));
895 Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing));
896 Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing));
897 Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
898 Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
899 Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
900 Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
901
902 // multi exception
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200903 StartTestGroup( 'testMultiException(1)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000904 try
905 i := client.testMultiException( 'need more pizza', 'run out of beer');
906 Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"');
907 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
Jens Geyer6bbbf192014-09-07 01:45:56 +0200908 { this is not necessarily true, these fields are default-serialized
Jens Geyerd5436f52014-10-03 19:50:38 +0200909 Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
Roger Meier3bef8c22012-10-06 06:58:00 +0000910 Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
911 Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
Jens Geyerd5436f52014-10-03 19:50:38 +0200912 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000913 except
914 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
915 end;
916
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200917 StartTestGroup( 'testMultiException(Xception)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000918 try
919 i := client.testMultiException( 'Xception', 'second test');
920 Expect( FALSE, 'testMultiException(''Xception''): must trow an exception');
921 except
922 on x:TXception do begin
923 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
924 Expect( x.__isset_Message_, 'x.__isset_Message_ = '+BoolToString(x.__isset_Message_));
925 Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
926 Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"');
927 end;
928 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
929 end;
930
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200931 StartTestGroup( 'testMultiException(Xception2)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000932 try
933 i := client.testMultiException( 'Xception2', 'third test');
934 Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception');
935 except
936 on x:TXception2 do begin
937 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
938 Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing));
939 Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
940 Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"');
941 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 +0200942 { this is not necessarily true, these fields are default-serialized
Roger Meier3bef8c22012-10-06 06:58:00 +0000943 Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing));
944 Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing));
945 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 +0200946 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000947 end;
948 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
949 end;
950
951
952 // oneway functions
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200953 StartTestGroup( 'Test Oneway(1)', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000954 client.testOneway(1);
955 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
956
957 // call time
Jens Geyer06045cf2013-03-27 20:26:25 +0200958 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000959 StartTestGroup( 'Test Calltime()');
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200960 StartTick := GetTickCount;
Roger Meier3bef8c22012-10-06 06:58:00 +0000961 for k := 0 to 1000 - 1 do
962 begin
963 client.testVoid();
964 end;
965 Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
Jens Geyer06045cf2013-03-27 20:26:25 +0200966 {$ENDIF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000967
968 // no more tests here
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200969 StartTestGroup( '', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000970end;
971
972
Jens Geyer718f6ee2013-09-06 21:02:34 +0200973{$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200974procedure TClientThread.StressTest(const client : TThriftTest.Iface);
975begin
976 while TRUE do begin
977 try
978 if not FTransport.IsOpen then FTransport.Open; // re-open connection, server has already closed
979 try
980 client.testString('Test');
981 Write('.');
982 finally
983 if FTransport.IsOpen then FTransport.Close;
984 end;
985 except
986 on e:Exception do Writeln(#10+e.message);
987 end;
988 end;
989end;
Jens Geyer718f6ee2013-09-06 21:02:34 +0200990{$ENDIF}
Jens Geyer06045cf2013-03-27 20:26:25 +0200991
Jens Geyerfd1b3582014-12-13 23:42:58 +0100992
993function TClientThread.PrepareBinaryData( aRandomDist : Boolean = FALSE) : TBytes;
994var i, nextPos : Integer;
995begin
996 SetLength( result, $100);
997 ASSERT( Low(result) = 0);
998
999 // linear distribution, unless random is requested
1000 if not aRandomDist then begin
1001 for i := Low(result) to High(result) do begin
1002 result[i] := i;
1003 end;
1004 Exit;
1005 end;
1006
1007 // random distribution of all 256 values
1008 FillChar( result[0], Length(result) * SizeOf(result[0]), $0);
1009 i := 1;
1010 while i < Length(result) do begin
1011 nextPos := Byte( Random($100));
1012 if result[nextPos] = 0 then begin // unused?
1013 result[nextPos] := i;
1014 Inc(i);
1015 end;
1016 end;
1017end;
1018
1019
Roger Meier3bef8c22012-10-06 06:58:00 +00001020procedure TClientThread.JSONProtocolReadWriteTest;
1021// Tests only then read/write procedures of the JSON protocol
1022// All tests succeed, if we can read what we wrote before
1023// Note that passing this test does not imply, that our JSON is really compatible to what
1024// other clients or servers expect as the real JSON. This is beyond the scope of this test.
1025var prot : IProtocol;
1026 stm : TStringStream;
1027 list : IList;
1028 binary, binRead : TBytes;
1029 i,iErr : Integer;
1030const
1031 TEST_SHORT = ShortInt( $FE);
1032 TEST_SMALL = SmallInt( $FEDC);
1033 TEST_LONG = LongInt( $FEDCBA98);
1034 TEST_I64 = Int64( $FEDCBA9876543210);
1035 TEST_DOUBLE = -1.234e-56;
1036 DELTA_DOUBLE = TEST_DOUBLE * 1e-14;
1037 TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars")
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001038 // Test THRIFT-2336 and THRIFT-3404 with U+1D11E (G Clef symbol) and 'Русское Название';
1039 G_CLEF_AND_CYRILLIC_TEXT = #$1d11e' '#$0420#$0443#$0441#$0441#$043a#$043e#$0435' '#$041d#$0430#$0437#$0432#$0430#$043d#$0438#$0435;
1040 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 +01001041 // test both possible solidus encodings
1042 SOLIDUS_JSON_DATA = '"one/two\/three"';
1043 SOLIDUS_EXCPECTED = 'one/two/three';
Roger Meier3bef8c22012-10-06 06:58:00 +00001044begin
1045 stm := TStringStream.Create;
1046 try
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001047 StartTestGroup( 'JsonProtocolTest', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001048
1049 // prepare binary data
Jens Geyerfd1b3582014-12-13 23:42:58 +01001050 binary := PrepareBinaryData( FALSE);
Roger Meier3bef8c22012-10-06 06:58:00 +00001051
1052 // output setup
1053 prot := TJSONProtocolImpl.Create(
1054 TStreamTransportImpl.Create(
1055 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
1056
1057 // write
1058 prot.WriteListBegin( TListImpl.Create( TType.String_, 9));
1059 prot.WriteBool( TRUE);
1060 prot.WriteBool( FALSE);
1061 prot.WriteByte( TEST_SHORT);
1062 prot.WriteI16( TEST_SMALL);
1063 prot.WriteI32( TEST_LONG);
1064 prot.WriteI64( TEST_I64);
1065 prot.WriteDouble( TEST_DOUBLE);
1066 prot.WriteString( TEST_STRING);
1067 prot.WriteBinary( binary);
1068 prot.WriteListEnd;
1069
1070 // input setup
1071 Expect( stm.Position = stm.Size, 'Stream position/length after write');
1072 stm.Position := 0;
1073 prot := TJSONProtocolImpl.Create(
1074 TStreamTransportImpl.Create(
1075 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1076
1077 // read and compare
1078 list := prot.ReadListBegin;
1079 Expect( list.ElementType = TType.String_, 'list element type');
1080 Expect( list.Count = 9, 'list element count');
1081 Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE');
1082 Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE');
1083 Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte');
1084 Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16');
1085 Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32');
1086 Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64');
1087 Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble');
1088 Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString');
1089 binRead := prot.ReadBinary;
1090 prot.ReadListEnd;
1091
1092 // test binary data
1093 Expect( Length(binary) = Length(binRead), 'Binary data length check');
1094 iErr := -1;
1095 for i := Low(binary) to High(binary) do begin
1096 if binary[i] <> binRead[i] then begin
1097 iErr := i;
1098 Break;
1099 end;
1100 end;
1101 if iErr < 0
1102 then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)')
1103 else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr));
1104
1105 Expect( stm.Position = stm.Size, 'Stream position after read');
1106
Jens Geyer7bb44a32014-02-07 22:24:37 +01001107
Jens Geyer21366942013-12-30 22:04:51 +01001108 // Solidus can be encoded in two ways. Make sure we can read both
1109 stm.Position := 0;
1110 stm.Size := 0;
1111 stm.WriteString(SOLIDUS_JSON_DATA);
1112 stm.Position := 0;
1113 prot := TJSONProtocolImpl.Create(
1114 TStreamTransportImpl.Create(
1115 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1116 Expect( prot.ReadString = SOLIDUS_EXCPECTED, 'Solidus encoding');
1117
1118
Jens Geyer7bb44a32014-02-07 22:24:37 +01001119 // Widechars should work too. Do they?
1120 // After writing, we ensure that we are able to read it back
1121 // We can't assume hex-encoding, since (nearly) any Unicode char is valid JSON
1122 stm.Position := 0;
1123 stm.Size := 0;
1124 prot := TJSONProtocolImpl.Create(
1125 TStreamTransportImpl.Create(
1126 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001127 prot.WriteString( G_CLEF_AND_CYRILLIC_TEXT);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001128 stm.Position := 0;
1129 prot := TJSONProtocolImpl.Create(
1130 TStreamTransportImpl.Create(
1131 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001132 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Writing JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001133
1134 // Widechars should work with hex-encoding too. Do they?
1135 stm.Position := 0;
1136 stm.Size := 0;
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001137 stm.WriteString( G_CLEF_AND_CYRILLIC_JSON);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001138 stm.Position := 0;
1139 prot := TJSONProtocolImpl.Create(
1140 TStreamTransportImpl.Create(
1141 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001142 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Reading JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001143
1144
Roger Meier3bef8c22012-10-06 06:58:00 +00001145 finally
1146 stm.Free;
1147 prot := nil; //-> Release
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001148 StartTestGroup( '', test_Unknown); // no more tests here
Roger Meier3bef8c22012-10-06 06:58:00 +00001149 end;
1150end;
1151
1152
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001153procedure TClientThread.StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +00001154begin
1155 FTestGroup := aGroup;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001156 FCurrentTest := aTest;
1157
1158 Include( FExecuted, aTest);
1159
Roger Meier3bef8c22012-10-06 06:58:00 +00001160 if FTestGroup <> '' then begin
1161 Console.WriteLine('');
1162 Console.WriteLine( aGroup+' tests');
1163 Console.WriteLine( StringOfChar('-',60));
1164 end;
1165end;
1166
1167
1168procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
1169begin
1170 if aTestResult then begin
1171 Inc(FSuccesses);
1172 Console.WriteLine( aTestInfo+': passed');
1173 end
1174 else begin
1175 FErrors.Add( FTestGroup+': '+aTestInfo);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001176 Include( FFailed, FCurrentTest);
Roger Meier3bef8c22012-10-06 06:58:00 +00001177 Console.WriteLine( aTestInfo+': *** FAILED ***');
1178
1179 // We have a failed test!
1180 // -> issue DebugBreak ONLY if a debugger is attached,
1181 // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise
Jens Geyer9f7f11e2016-04-14 21:37:11 +02001182 if IsDebuggerPresent
1183 then {$IFDEF CPUX64} DebugBreak {$ELSE} asm int 3 end {$ENDIF};
Roger Meier3bef8c22012-10-06 06:58:00 +00001184 end;
1185end;
1186
1187
1188procedure TClientThread.ReportResults;
1189var nTotal : Integer;
1190 sLine : string;
1191begin
1192 // prevent us from stupid DIV/0 errors
1193 nTotal := FSuccesses + FErrors.Count;
1194 if nTotal = 0 then begin
1195 Console.WriteLine('No results logged');
1196 Exit;
1197 end;
1198
1199 Console.WriteLine('');
1200 Console.WriteLine( StringOfChar('=',60));
1201 Console.WriteLine( IntToStr(nTotal)+' tests performed');
1202 Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)');
1203 Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)');
1204 Console.WriteLine( StringOfChar('=',60));
1205 if FErrors.Count > 0 then begin
1206 Console.WriteLine('FAILED TESTS:');
1207 for sLine in FErrors do Console.WriteLine('- '+sLine);
1208 Console.WriteLine( StringOfChar('=',60));
1209 InterlockedIncrement( ExitCode); // return <> 0 on errors
1210 end;
1211 Console.WriteLine('');
1212end;
1213
1214
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001215function TClientThread.CalculateExitCode : Byte;
1216var test : TTestGroup;
1217begin
1218 result := EXITCODE_SUCCESS;
1219 for test := Low(TTestGroup) to High(TTestGroup) do begin
1220 if (test in FFailed) or not (test in FExecuted)
1221 then result := result or MAP_FAILURES_TO_EXITCODE_BITS[test];
1222 end;
1223end;
1224
1225
Roger Meier3bef8c22012-10-06 06:58:00 +00001226constructor TClientThread.Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
1227begin
1228 inherited Create( True );
1229 FNumIteration := ANumIteration;
1230 FTransport := ATransport;
1231 FProtocol := AProtocol;
1232 FConsole := TThreadConsole.Create( Self );
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001233 FCurrentTest := test_Unknown;
Roger Meier3bef8c22012-10-06 06:58:00 +00001234
1235 // error list: keep correct order, allow for duplicates
1236 FErrors := TStringList.Create;
1237 FErrors.Sorted := FALSE;
1238 FErrors.Duplicates := dupAccept;
1239end;
1240
1241destructor TClientThread.Destroy;
1242begin
1243 FreeAndNil( FConsole);
1244 FreeAndNil( FErrors);
1245 inherited;
1246end;
1247
1248procedure TClientThread.Execute;
1249var
1250 i : Integer;
1251 proc : TThreadProcedure;
1252begin
1253 // perform all tests
1254 try
Jens Geyer7bb44a32014-02-07 22:24:37 +01001255 JSONProtocolReadWriteTest;
Roger Meier3bef8c22012-10-06 06:58:00 +00001256 for i := 0 to FNumIteration - 1 do
1257 begin
1258 ClientTest;
Roger Meier3bef8c22012-10-06 06:58:00 +00001259 end;
1260 except
1261 on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"');
1262 end;
1263
1264 // report the outcome
1265 ReportResults;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001266 SetReturnValue( CalculateExitCode);
Roger Meier3bef8c22012-10-06 06:58:00 +00001267
1268 // shutdown
1269 proc := procedure
1270 begin
1271 if FTransport <> nil then
1272 begin
1273 FTransport.Close;
1274 FTransport := nil;
1275 end;
1276 end;
1277
1278 Synchronize( proc );
1279end;
1280
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001281
Roger Meier3bef8c22012-10-06 06:58:00 +00001282{ TThreadConsole }
1283
1284constructor TThreadConsole.Create(AThread: TThread);
1285begin
Jens Geyer718f6ee2013-09-06 21:02:34 +02001286 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +00001287 FThread := AThread;
1288end;
1289
1290procedure TThreadConsole.Write(const S: string);
1291var
1292 proc : TThreadProcedure;
1293begin
1294 proc := procedure
1295 begin
1296 Console.Write( S );
1297 end;
1298 TThread.Synchronize( FThread, proc);
1299end;
1300
1301procedure TThreadConsole.WriteLine(const S: string);
1302var
1303 proc : TThreadProcedure;
1304begin
1305 proc := procedure
1306 begin
1307 Console.WriteLine( S );
1308 end;
1309 TThread.Synchronize( FThread, proc);
1310end;
1311
1312initialization
1313begin
1314 TTestClient.FNumIteration := 1;
1315 TTestClient.FNumThread := 1;
1316end;
1317
1318end.