blob: 5f375efecc5e6a31756d044dbc7a4ee9d6a15da9 [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 Geyer06045cf2013-03-27 20:26:25 +020022{.$DEFINE StressTest} // activate to stress-test the server with frequent connects/disconnects
23{.$DEFINE PerfTest} // activate to activate the performance test
24
Roger Meier3bef8c22012-10-06 06:58:00 +000025interface
26
27uses
Jens Geyerfd1b3582014-12-13 23:42:58 +010028 Windows, SysUtils, Classes, Math,
Roger Meier3bef8c22012-10-06 06:58:00 +000029 DateUtils,
30 Generics.Collections,
31 TestConstants,
32 Thrift,
Jens Geyerf0e63312015-03-01 18:47:49 +010033 Thrift.Protocol.Compact,
Roger Meier3bef8c22012-10-06 06:58:00 +000034 Thrift.Protocol.JSON,
35 Thrift.Protocol,
36 Thrift.Transport.Pipes,
37 Thrift.Transport,
38 Thrift.Stream,
39 Thrift.Test,
40 Thrift.Collections,
41 Thrift.Console;
42
43type
44 TThreadConsole = class
45 private
46 FThread : TThread;
47 public
48 procedure Write( const S : string);
49 procedure WriteLine( const S : string);
50 constructor Create( AThread: TThread);
51 end;
52
53 TClientThread = class( TThread )
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020054 private type
55 TTestGroup = (
56 test_Unknown,
57 test_BaseTypes,
58 test_Structs,
59 test_Containers,
60 test_Exceptions
61 // new values here
62 );
63 TTestGroups = set of TTestGroup;
64
Roger Meier3bef8c22012-10-06 06:58:00 +000065 private
66 FTransport : ITransport;
67 FProtocol : IProtocol;
68 FNumIteration : Integer;
69 FConsole : TThreadConsole;
70
71 // test reporting, will be refactored out into separate class later
72 FTestGroup : string;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020073 FCurrentTest : TTestGroup;
Roger Meier3bef8c22012-10-06 06:58:00 +000074 FSuccesses : Integer;
75 FErrors : TStringList;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020076 FFailed : TTestGroups;
77 FExecuted : TTestGroups;
78 procedure StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +000079 procedure Expect( aTestResult : Boolean; const aTestInfo : string);
80 procedure ReportResults;
Jens Geyerfd1b3582014-12-13 23:42:58 +010081 function CalculateExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +000082
83 procedure ClientTest;
84 procedure JSONProtocolReadWriteTest;
Jens Geyerfd1b3582014-12-13 23:42:58 +010085 function PrepareBinaryData( aRandomDist : Boolean = FALSE) : TBytes;
Jens Geyer718f6ee2013-09-06 21:02:34 +020086 {$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +020087 procedure StressTest(const client : TThriftTest.Iface);
Jens Geyer718f6ee2013-09-06 21:02:34 +020088 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +000089 protected
90 procedure Execute; override;
91 public
92 constructor Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
93 destructor Destroy; override;
94 end;
95
96 TTestClient = class
97 private
98 class var
99 FNumIteration : Integer;
100 FNumThread : Integer;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200101
102 class procedure PrintCmdLineHelp;
103 class procedure InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000104 public
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200105 class function Execute( const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000106 end;
107
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200108
Roger Meier3bef8c22012-10-06 06:58:00 +0000109implementation
110
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200111const
112 EXITCODE_SUCCESS = $00; // no errors bits set
113 //
114 EXITCODE_FAILBIT_BASETYPES = $01;
115 EXITCODE_FAILBIT_STRUCTS = $02;
116 EXITCODE_FAILBIT_CONTAINERS = $04;
117 EXITCODE_FAILBIT_EXCEPTIONS = $08;
118
119 MAP_FAILURES_TO_EXITCODE_BITS : array[TClientThread.TTestGroup] of Byte = (
120 EXITCODE_SUCCESS, // no bits here
121 EXITCODE_FAILBIT_BASETYPES,
122 EXITCODE_FAILBIT_STRUCTS,
123 EXITCODE_FAILBIT_CONTAINERS,
124 EXITCODE_FAILBIT_EXCEPTIONS
125 );
126
127
128
Roger Meier3bef8c22012-10-06 06:58:00 +0000129function BoolToString( b : Boolean) : string;
130// overrides global BoolToString()
131begin
132 if b
133 then result := 'true'
134 else result := 'false';
135end;
136
137// not available in all versions, so make sure we have this one imported
138function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent';
139
140{ TTestClient }
141
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200142class procedure TTestClient.PrintCmdLineHelp;
143const HELPTEXT = ' [options]'#10
144 + #10
145 + 'Allowed options:'#10
146 + ' -h [ --help ] produce help message'#10
147 + ' --host arg (=localhost) Host to connect'#10
148 + ' --port arg (=9090) Port number to connect'#10
149 + ' --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift),'#10
150 + ' instead of host and port'#10
151 + ' --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)'#10
152 + ' --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)'#10
153 + ' --transport arg (=sockets) Transport: buffered, framed, http, evhttp'#10
154 + ' --protocol arg (=binary) Protocol: binary, compact, json'#10
155 + ' --ssl Encrypted Transport using SSL'#10
156 + ' -n [ --testloops ] arg (=1) Number of Tests'#10
157 + ' -t [ --threads ] arg (=1) Number of Test threads'#10
158 ;
159begin
160 Writeln( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + HELPTEXT);
161end;
162
163class procedure TTestClient.InvalidArgs;
164begin
165 Console.WriteLine( 'Invalid args.');
166 Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + ' -h for more information');
167 Abort;
168end;
169
170class function TTestClient.Execute(const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000171var
172 i : Integer;
173 host : string;
174 port : Integer;
Roger Meier3bef8c22012-10-06 06:58:00 +0000175 sPipeName : string;
176 hAnonRead, hAnonWrite : THandle;
177 s : string;
Roger Meier3bef8c22012-10-06 06:58:00 +0000178 threads : array of TThread;
179 dtStart : TDateTime;
180 test : Integer;
181 thread : TThread;
182 trans : ITransport;
183 prot : IProtocol;
184 streamtrans : IStreamTransport;
185 http : IHTTPClient;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200186 protType : TKnownProtocol;
187 endpoint : TEndpointTransport;
188 layered : TLayeredTransports;
189 UseSSL : Boolean; // include where appropriate (TLayeredTransport?)
Jens Geyer0b20cc82013-03-07 20:47:01 +0100190const
191 // pipe timeouts to be used
192 DEBUG_TIMEOUT = 30 * 1000;
Jens Geyer3e8d9272014-09-14 20:10:40 +0200193 RELEASE_TIMEOUT = DEFAULT_THRIFT_TIMEOUT;
Jens Geyer0b20cc82013-03-07 20:47:01 +0100194 TIMEOUT = RELEASE_TIMEOUT;
Roger Meier3bef8c22012-10-06 06:58:00 +0000195begin
Roger Meier3bef8c22012-10-06 06:58:00 +0000196 protType := prot_Binary;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200197 endpoint := trns_Sockets;
198 layered := [];
199 UseSSL := FALSE;
200 host := 'localhost';
201 port := 9090;
202 sPipeName := '';
203 hAnonRead := INVALID_HANDLE_VALUE;
204 hAnonWrite := INVALID_HANDLE_VALUE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000205 try
Roger Meier3bef8c22012-10-06 06:58:00 +0000206 i := 0;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200207 while ( i < Length(args) ) do begin
208 s := args[i];
209 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000210
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200211 if (s = '-h') or (s = '--help') then begin
212 // -h [ --help ] produce help message
213 PrintCmdLineHelp;
214 result := $FF; // all tests failed
215 Exit;
216 end
Jens Geyerb360b652014-09-28 01:55:46 +0200217 else if s = '--host' then begin
218 // --host arg (=localhost) Host to connect
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200219 host := args[i];
220 Inc( i);
221 end
Jens Geyerb360b652014-09-28 01:55:46 +0200222 else if s = '--port' then begin
223 // --port arg (=9090) Port number to connect
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200224 s := args[i];
225 Inc( i);
226 port := StrToIntDef(s,0);
227 if port <= 0 then InvalidArgs;
228 end
Jens Geyerb360b652014-09-28 01:55:46 +0200229 else if s = '--domain-socket' then begin
230 // --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200231 raise Exception.Create('domain-socket not supported');
232 end
Jens Geyerb360b652014-09-28 01:55:46 +0200233 else if s = '--named-pipe' then begin
234 // --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200235 endpoint := trns_NamedPipes;
236 sPipeName := args[i];
237 Inc( i);
238 end
Jens Geyerb360b652014-09-28 01:55:46 +0200239 else if s = '--anon-pipes' then begin
240 // --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200241 endpoint := trns_AnonPipes;
242 hAnonRead := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
243 Inc( i);
244 hAnonWrite := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
245 Inc( i);
246 end
Jens Geyerb360b652014-09-28 01:55:46 +0200247 else if s = '--transport' then begin
248 // --transport arg (=sockets) Transport: buffered, framed, http, evhttp
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200249 s := args[i];
250 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000251
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200252 if s = 'buffered' then Include( layered, trns_Buffered)
253 else if s = 'framed' then Include( layered, trns_Framed)
254 else if s = 'http' then endpoint := trns_Http
255 else if s = 'evhttp' then endpoint := trns_AnonPipes
256 else InvalidArgs;
257 end
Jens Geyerb360b652014-09-28 01:55:46 +0200258 else if s = '--protocol' then begin
259 // --protocol arg (=binary) Protocol: binary, compact, json
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200260 s := args[i];
261 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000262
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200263 if s = 'binary' then protType := prot_Binary
264 else if s = 'compact' then protType := prot_Compact
265 else if s = 'json' then protType := prot_JSON
266 else InvalidArgs;
267 end
Jens Geyerb360b652014-09-28 01:55:46 +0200268 else if s = '--ssl' then begin
269 // --ssl Encrypted Transport using SSL
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200270 UseSSL := TRUE;
271
272 end
273 else if (s = '-n') or (s = '--testloops') then begin
274 // -n [ --testloops ] arg (=1) Number of Tests
275 FNumIteration := StrToIntDef( args[i], 0);
276 Inc( i);
277 if FNumIteration <= 0
278 then InvalidArgs;
279
280 end
281 else if (s = '-t') or (s = '--threads') then begin
282 // -t [ --threads ] arg (=1) Number of Test threads
283 FNumThread := StrToIntDef( args[i], 0);
284 Inc( i);
285 if FNumThread <= 0
286 then InvalidArgs;
287 end
288 else begin
289 InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000290 end;
291 end;
292
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200293
Roger Meier79655fb2012-10-20 20:59:41 +0000294 // In the anonymous pipes mode the client is launched by the test server
295 // -> behave nicely and allow for attaching a debugger to this process
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200296 if (endpoint = trns_AnonPipes) and not IsDebuggerPresent
Roger Meier79655fb2012-10-20 20:59:41 +0000297 then MessageBox( 0, 'Attach Debugger and/or click OK to continue.',
298 'Thrift TestClient (Delphi)',
299 MB_OK or MB_ICONEXCLAMATION);
300
Roger Meier3bef8c22012-10-06 06:58:00 +0000301 SetLength( threads, FNumThread);
302 dtStart := Now;
303
304 for test := 0 to FNumThread - 1 do
305 begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200306 case endpoint of
307 trns_Sockets: begin
Roger Meier3bef8c22012-10-06 06:58:00 +0000308 Console.WriteLine('Using sockets ('+host+' port '+IntToStr(port)+')');
309 streamtrans := TSocketImpl.Create( host, port );
310 end;
311
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200312 trns_Http: begin
313 Console.WriteLine('Using HTTPClient');
314 http := THTTPClientImpl.Create( host);
315 trans := http;
Roger Meier3bef8c22012-10-06 06:58:00 +0000316 end;
317
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200318 trns_EvHttp: begin
319 raise Exception.Create(ENDPOINT_TRANSPORTS[endpoint]+' transport not implemented');
Roger Meier3bef8c22012-10-06 06:58:00 +0000320 end;
321
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200322 trns_NamedPipes: begin
323 Console.WriteLine('Using named pipe ('+sPipeName+')');
324 streamtrans := TNamedPipeTransportClientEndImpl.Create( sPipeName, 0, nil, TIMEOUT);
325 end;
326
327 trns_AnonPipes: begin
328 Console.WriteLine('Using anonymous pipes ('+IntToStr(Integer(hAnonRead))+' and '+IntToStr(Integer(hAnonWrite))+')');
329 streamtrans := TAnonymousPipeTransportImpl.Create( hAnonRead, hAnonWrite, FALSE);
330 end;
331
332 else
333 raise Exception.Create('Unhandled endpoint transport');
334 end;
335 trans := streamtrans;
336 ASSERT( trans <> nil);
337
338 if (trns_Buffered in layered) then begin
339 trans := TBufferedTransportImpl.Create( streamtrans, 32); // small buffer to test read()
340 Console.WriteLine('Using buffered transport');
341 end;
342
343 if (trns_Framed in layered) then begin
344 trans := TFramedTransportImpl.Create( trans );
345 Console.WriteLine('Using framed transport');
346 end;
347
348 if UseSSL then begin
349 raise Exception.Create('SSL not implemented');
Roger Meier3bef8c22012-10-06 06:58:00 +0000350 end;
351
352 // create protocol instance, default to BinaryProtocol
353 case protType of
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200354 prot_Binary : prot := TBinaryProtocolImpl.Create( trans, BINARY_STRICT_READ, BINARY_STRICT_WRITE);
355 prot_JSON : prot := TJSONProtocolImpl.Create( trans);
Jens Geyerf0e63312015-03-01 18:47:49 +0100356 prot_Compact : prot := TCompactProtocolImpl.Create( trans);
Roger Meier3bef8c22012-10-06 06:58:00 +0000357 else
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200358 raise Exception.Create('Unhandled protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000359 end;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200360 ASSERT( trans <> nil);
361 Console.WriteLine(THRIFT_PROTOCOLS[protType]+' protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000362
363 thread := TClientThread.Create( trans, prot, FNumIteration);
364 threads[test] := thread;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200365 thread.Start;
Roger Meier3bef8c22012-10-06 06:58:00 +0000366 end;
367
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200368 result := 0;
369 for test := 0 to FNumThread - 1 do begin
370 result := result or threads[test].WaitFor;
Roger Meier3bef8c22012-10-06 06:58:00 +0000371 end;
372
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200373 for test := 0 to FNumThread - 1
374 do threads[test].Free;
Roger Meier3bef8c22012-10-06 06:58:00 +0000375
376 Console.Write('Total time: ' + IntToStr( MilliSecondsBetween(Now, dtStart)));
377
378 except
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200379 on E: EAbort do raise;
380 on E: Exception do begin
381 Console.WriteLine( E.Message + #10 + E.StackTrace);
382 raise;
Roger Meier3bef8c22012-10-06 06:58:00 +0000383 end;
384 end;
385
386 Console.WriteLine('');
387 Console.WriteLine('done!');
388end;
389
390{ TClientThread }
391
392procedure TClientThread.ClientTest;
393var
394 client : TThriftTest.Iface;
395 s : string;
396 i8 : ShortInt;
397 i32 : Integer;
398 i64 : Int64;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100399 binOut,binIn : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000400 dub : Double;
401 o : IXtruct;
402 o2 : IXtruct2;
403 i : IXtruct;
404 i2 : IXtruct2;
405 mapout : IThriftDictionary<Integer,Integer>;
406 mapin : IThriftDictionary<Integer,Integer>;
407 strmapout : IThriftDictionary<string,string>;
408 strmapin : IThriftDictionary<string,string>;
409 j : Integer;
410 first : Boolean;
411 key : Integer;
412 strkey : string;
413 listout : IThriftList<Integer>;
414 listin : IThriftList<Integer>;
415 setout : IHashSet<Integer>;
416 setin : IHashSet<Integer>;
417 ret : TNumberz;
418 uid : Int64;
419 mm : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
420 pos : IThriftDictionary<Integer, Integer>;
421 neg : IThriftDictionary<Integer, Integer>;
422 m2 : IThriftDictionary<Integer, Integer>;
423 k2 : Integer;
424 insane : IInsanity;
425 truck : IXtruct;
426 whoa : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
427 key64 : Int64;
428 val : IThriftDictionary<TNumberz, IInsanity>;
429 k2_2 : TNumberz;
430 k3 : TNumberz;
431 v2 : IInsanity;
432 userMap : IThriftDictionary<TNumberz, Int64>;
433 xtructs : IThriftList<IXtruct>;
434 x : IXtruct;
435 arg0 : ShortInt;
436 arg1 : Integer;
437 arg2 : Int64;
438 arg3 : IThriftDictionary<SmallInt, string>;
439 arg4 : TNumberz;
440 arg5 : Int64;
441 StartTick : Cardinal;
442 k : Integer;
443 proc : TThreadProcedure;
444 hello, goodbye : IXtruct;
445 crazy : IInsanity;
446 looney : IInsanity;
447 first_map : IThriftDictionary<TNumberz, IInsanity>;
448 second_map : IThriftDictionary<TNumberz, IInsanity>;
449
450begin
451 client := TThriftTest.TClient.Create( FProtocol);
452 FTransport.Open;
453
Jens Geyer06045cf2013-03-27 20:26:25 +0200454 {$IFDEF StressTest}
455 StressTest( client);
456 {$ENDIF StressTest}
457
Roger Meier3bef8c22012-10-06 06:58:00 +0000458 // in-depth exception test
459 // (1) do we get an exception at all?
460 // (2) do we get the right exception?
461 // (3) does the exception contain the expected data?
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200462 StartTestGroup( 'testException', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000463 // case 1: exception type declared in IDL at the function call
464 try
465 client.testException('Xception');
466 Expect( FALSE, 'testException(''Xception''): must trow an exception');
467 except
468 on e:TXception do begin
469 Expect( e.ErrorCode = 1001, 'error code');
470 Expect( e.Message_ = 'Xception', 'error message');
471 Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ );
472 end;
473 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
474 on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
475 end;
476
477 // case 2: exception type NOT declared in IDL at the function call
478 // this will close the connection
479 try
480 client.testException('TException');
481 Expect( FALSE, 'testException(''TException''): must trow an exception');
482 except
483 on e:TTransportException do begin
484 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Roger Meier3bef8c22012-10-06 06:58:00 +0000485 end;
Jens Geyer6bbbf192014-09-07 01:45:56 +0200486 on e:TApplicationException do begin
487 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Jens Geyer6bbbf192014-09-07 01:45:56 +0200488 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000489 on e:TException do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
490 on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
491 end;
492
Jens Geyer2ad6c302015-02-26 19:38:53 +0100493 {
494 if FTransport.IsOpen then FTransport.Close;
495 FTransport.Open; // re-open connection, server has already closed
496 }
497
Roger Meier3bef8c22012-10-06 06:58:00 +0000498 // case 3: no exception
499 try
500 client.testException('something');
501 Expect( TRUE, 'testException(''something''): must not trow an exception');
502 except
503 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
504 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
505 end;
506
507
508 // simple things
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200509 StartTestGroup( 'simple Thrift calls', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000510 client.testVoid();
511 Expect( TRUE, 'testVoid()'); // success := no exception
512
Jens Geyer39ba6b72015-09-22 00:00:49 +0200513 s := BoolToString( client.testBool(TRUE));
514 Expect( s = BoolToString(TRUE), 'testBool(TRUE) = '+s);
515 s := BoolToString( client.testBool(FALSE));
516 Expect( s = BoolToString(FALSE), 'testBool(FALSE) = '+s);
517
Roger Meier3bef8c22012-10-06 06:58:00 +0000518 s := client.testString('Test');
519 Expect( s = 'Test', 'testString(''Test'') = "'+s+'"');
520
Jens Geyer06045cf2013-03-27 20:26:25 +0200521 s := client.testString(HUGE_TEST_STRING);
522 Expect( length(s) = length(HUGE_TEST_STRING),
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100523 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
Jens Geyer06045cf2013-03-27 20:26:25 +0200524 +'=> length(result) = '+IntToStr(Length(s)));
525
Roger Meier3bef8c22012-10-06 06:58:00 +0000526 i8 := client.testByte(1);
527 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
528
529 i32 := client.testI32(-1);
530 Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32));
531
532 Console.WriteLine('testI64(-34359738368)');
533 i64 := client.testI64(-34359738368);
534 Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
535
Jens Geyerfd1b3582014-12-13 23:42:58 +0100536 binOut := PrepareBinaryData( TRUE);
537 Console.WriteLine('testBinary('+BytesToHex(binOut)+')');
538 try
539 binIn := client.testBinary(binOut);
540 Expect( Length(binOut) = Length(binIn), 'testBinary(): length '+IntToStr(Length(binOut))+' = '+IntToStr(Length(binIn)));
541 i32 := Min( Length(binOut), Length(binIn));
542 Expect( CompareMem( binOut, binIn, i32), 'testBinary('+BytesToHex(binOut)+') = '+BytesToHex(binIn));
543 except
544 on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message);
545 on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message);
546 end;
547
Roger Meier3bef8c22012-10-06 06:58:00 +0000548 Console.WriteLine('testDouble(5.325098235)');
549 dub := client.testDouble(5.325098235);
550 Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
551
552 // structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200553 StartTestGroup( 'testStruct', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000554 Console.WriteLine('testStruct({''Zero'', 1, -3, -5})');
555 o := TXtructImpl.Create;
556 o.String_thing := 'Zero';
557 o.Byte_thing := 1;
558 o.I32_thing := -3;
559 o.I64_thing := -5;
560 i := client.testStruct(o);
561 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
562 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
563 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
564 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
565 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
566 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
567 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
568 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
569
570 // nested structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200571 StartTestGroup( 'testNest', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000572 Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})');
573 o2 := TXtruct2Impl.Create;
574 o2.Byte_thing := 1;
575 o2.Struct_thing := o;
576 o2.I32_thing := 5;
577 i2 := client.testNest(o2);
578 i := i2.Struct_thing;
579 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
580 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
581 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
582 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
583 Expect( i2.Byte_thing = 1, 'i2.Byte_thing = '+IntToStr(i2.Byte_thing));
584 Expect( i2.I32_thing = 5, 'i2.I32_thing = '+IntToStr(i2.I32_thing));
585 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
586 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
587 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
588 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
589 Expect( i2.__isset_Byte_thing, 'i2.__isset_Byte_thing');
590 Expect( i2.__isset_I32_thing, 'i2.__isset_I32_thing');
591
592 // map<type1,type2>: A map of strictly unique keys to values.
593 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200594 StartTestGroup( 'testMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000595 mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
596 for j := 0 to 4 do
597 begin
598 mapout.AddOrSetValue( j, j - 10);
599 end;
600 Console.Write('testMap({');
601 first := True;
602 for key in mapout.Keys do
603 begin
604 if first
605 then first := False
606 else Console.Write( ', ' );
607 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
608 end;
609 Console.WriteLine('})');
610
611 mapin := client.testMap( mapout );
612 Expect( mapin.Count = mapout.Count, 'testMap: mapin.Count = mapout.Count');
613 for j := 0 to 4 do
614 begin
615 Expect( mapout.ContainsKey(j), 'testMap: mapout.ContainsKey('+IntToStr(j)+') = '+BoolToString(mapout.ContainsKey(j)));
616 end;
617 for key in mapin.Keys do
618 begin
619 Expect( mapin[key] = mapout[key], 'testMap: '+IntToStr(key) + ' => ' + IntToStr( mapin[key]));
620 Expect( mapin[key] = key - 10, 'testMap: mapin['+IntToStr(key)+'] = '+IntToStr( mapin[key]));
621 end;
622
623
624 // map<type1,type2>: A map of strictly unique keys to values.
625 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200626 StartTestGroup( 'testStringMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000627 strmapout := TThriftDictionaryImpl<string,string>.Create;
628 for j := 0 to 4 do
629 begin
630 strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10));
631 end;
632 Console.Write('testStringMap({');
633 first := True;
634 for strkey in strmapout.Keys do
635 begin
636 if first
637 then first := False
638 else Console.Write( ', ' );
639 Console.Write( strkey + ' => ' + strmapout[strkey]);
640 end;
641 Console.WriteLine('})');
642
643 strmapin := client.testStringMap( strmapout );
644 Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count');
645 for j := 0 to 4 do
646 begin
647 Expect( strmapout.ContainsKey(IntToStr(j)),
648 'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = '
649 + BoolToString(strmapout.ContainsKey(IntToStr(j))));
650 end;
651 for strkey in strmapin.Keys do
652 begin
653 Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]);
654 Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]);
655 end;
656
657
658 // set<type>: An unordered set of unique elements.
659 // Translates to an STL set, Java HashSet, set in Python, etc.
660 // Note: PHP does not support sets, so it is treated similar to a List
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200661 StartTestGroup( 'testSet', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000662 setout := THashSetImpl<Integer>.Create;
663 for j := -2 to 2 do
664 begin
665 setout.Add( j );
666 end;
667 Console.Write('testSet({');
668 first := True;
669 for j in setout do
670 begin
671 if first
672 then first := False
673 else Console.Write(', ');
674 Console.Write(IntToStr( j));
675 end;
676 Console.WriteLine('})');
677
678 setin := client.testSet(setout);
679 Expect( setin.Count = setout.Count, 'testSet: setin.Count = setout.Count');
680 Expect( setin.Count = 5, 'testSet: setin.Count = '+IntToStr(setin.Count));
681 for j := -2 to 2 do // unordered, we can't rely on the order => test for known elements only
682 begin
683 Expect( setin.Contains(j), 'testSet: setin.Contains('+IntToStr(j)+') => '+BoolToString(setin.Contains(j)));
684 end;
685
686 // list<type>: An ordered list of elements.
687 // Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200688 StartTestGroup( 'testList', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000689 listout := TThriftListImpl<Integer>.Create;
690 listout.Add( +1);
691 listout.Add( -2);
692 listout.Add( +3);
693 listout.Add( -4);
694 listout.Add( 0);
695 Console.Write('testList({');
696 first := True;
697 for j in listout do
698 begin
699 if first
700 then first := False
701 else Console.Write(', ');
702 Console.Write(IntToStr( j));
703 end;
704 Console.WriteLine('})');
705
706 listin := client.testList(listout);
707 Expect( listin.Count = listout.Count, 'testList: listin.Count = listout.Count');
708 Expect( listin.Count = 5, 'testList: listin.Count = '+IntToStr(listin.Count));
709 Expect( listin[0] = +1, 'listin[0] = '+IntToStr( listin[0]));
710 Expect( listin[1] = -2, 'listin[1] = '+IntToStr( listin[1]));
711 Expect( listin[2] = +3, 'listin[2] = '+IntToStr( listin[2]));
712 Expect( listin[3] = -4, 'listin[3] = '+IntToStr( listin[3]));
713 Expect( listin[4] = 0, 'listin[4] = '+IntToStr( listin[4]));
714
715 // enums
716 ret := client.testEnum(TNumberz.ONE);
717 Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret)));
718
719 ret := client.testEnum(TNumberz.TWO);
720 Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret)));
721
722 ret := client.testEnum(TNumberz.THREE);
723 Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret)));
724
725 ret := client.testEnum(TNumberz.FIVE);
726 Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret)));
727
728 ret := client.testEnum(TNumberz.EIGHT);
729 Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret)));
730
731
732 // typedef
733 uid := client.testTypedef(309858235082523);
734 Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid));
735
736
737 // maps of maps
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200738 StartTestGroup( 'testMapMap(1)', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000739 mm := client.testMapMap(1);
740 Console.Write(' = {');
741 for key in mm.Keys do
742 begin
743 Console.Write( IntToStr( key) + ' => {');
744 m2 := mm[key];
745 for k2 in m2.Keys do
746 begin
747 Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', ');
748 end;
749 Console.Write('}, ');
750 end;
751 Console.WriteLine('}');
752
753 // verify result data
754 Expect( mm.Count = 2, 'mm.Count = '+IntToStr(mm.Count));
755 pos := mm[4];
756 neg := mm[-4];
757 for j := 1 to 4 do
758 begin
759 Expect( pos[j] = j, 'pos[j] = '+IntToStr(pos[j]));
760 Expect( neg[-j] = -j, 'neg[-j] = '+IntToStr(neg[-j]));
761 end;
762
763
764
765 // insanity
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200766 StartTestGroup( 'testInsanity', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000767 insane := TInsanityImpl.Create;
768 insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
769 insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000);
770 truck := TXtructImpl.Create;
771 truck.String_thing := 'Truck';
772 truck.Byte_thing := 8;
773 truck.I32_thing := 8;
774 truck.I64_thing := 8;
775 insane.Xtructs := TThriftListImpl<IXtruct>.Create;
776 insane.Xtructs.Add( truck );
777 whoa := client.testInsanity( insane );
778 Console.Write(' = {');
779 for key64 in whoa.Keys do
780 begin
781 val := whoa[key64];
782 Console.Write( IntToStr( key64) + ' => {');
783 for k2_2 in val.Keys do
784 begin
785 v2 := val[k2_2];
786 Console.Write( IntToStr( Integer( k2_2)) + ' => {');
787 userMap := v2.UserMap;
788 Console.Write('{');
789 if userMap <> nil then
790 begin
791 for k3 in userMap.Keys do
792 begin
793 Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', ');
794 end;
795 end else
796 begin
797 Console.Write('null');
798 end;
799 Console.Write('}, ');
800 xtructs := v2.Xtructs;
801 Console.Write('{');
802
803 if xtructs <> nil then
804 begin
805 for x in xtructs do
806 begin
807 Console.Write('{"' + x.String_thing + '", ' +
808 IntToStr( x.Byte_thing) + ', ' +
809 IntToStr( x.I32_thing) + ', ' +
810 IntToStr( x.I32_thing) + '}, ');
811 end;
812 end else
813 begin
814 Console.Write('null');
815 end;
816 Console.Write('}');
817 Console.Write('}, ');
818 end;
819 Console.Write('}, ');
820 end;
821 Console.WriteLine('}');
822
823 // verify result data
824 Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count));
825 //
826 first_map := whoa[1];
827 second_map := whoa[2];
828 Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count));
829 Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count));
830 //
831 looney := second_map[TNumberz.SIX];
832 Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney)));
833 Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
834 Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
835 //
836 for ret in [TNumberz.TWO, TNumberz.THREE] do begin
837 crazy := first_map[ret];
838 Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
839
840 Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap));
841 Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs));
842
843 Expect( crazy.UserMap.Count = 2, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count));
844 Expect( crazy.UserMap[TNumberz.FIVE] = 5, 'crazy.UserMap[TNumberz.FIVE] = '+IntToStr(crazy.UserMap[TNumberz.FIVE]));
845 Expect( crazy.UserMap[TNumberz.EIGHT] = 8, 'crazy.UserMap[TNumberz.EIGHT] = '+IntToStr(crazy.UserMap[TNumberz.EIGHT]));
846
847 Expect( crazy.Xtructs.Count = 2, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count));
848 goodbye := crazy.Xtructs[0]; // lists are ordered, so we are allowed to assume this order
Jens Geyerd5436f52014-10-03 19:50:38 +0200849 hello := crazy.Xtructs[1];
Roger Meier3bef8c22012-10-06 06:58:00 +0000850
851 Expect( goodbye.String_thing = 'Goodbye4', 'goodbye.String_thing = "'+goodbye.String_thing+'"');
852 Expect( goodbye.Byte_thing = 4, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing));
853 Expect( goodbye.I32_thing = 4, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing));
854 Expect( goodbye.I64_thing = 4, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing));
855 Expect( goodbye.__isset_String_thing, 'goodbye.__isset_String_thing = '+BoolToString(goodbye.__isset_String_thing));
856 Expect( goodbye.__isset_Byte_thing, 'goodbye.__isset_Byte_thing = '+BoolToString(goodbye.__isset_Byte_thing));
857 Expect( goodbye.__isset_I32_thing, 'goodbye.__isset_I32_thing = '+BoolToString(goodbye.__isset_I32_thing));
858 Expect( goodbye.__isset_I64_thing, 'goodbye.__isset_I64_thing = '+BoolToString(goodbye.__isset_I64_thing));
859
860 Expect( hello.String_thing = 'Hello2', 'hello.String_thing = "'+hello.String_thing+'"');
861 Expect( hello.Byte_thing = 2, 'hello.Byte_thing = '+IntToStr(hello.Byte_thing));
862 Expect( hello.I32_thing = 2, 'hello.I32_thing = '+IntToStr(hello.I32_thing));
863 Expect( hello.I64_thing = 2, 'hello.I64_thing = '+IntToStr(hello.I64_thing));
864 Expect( hello.__isset_String_thing, 'hello.__isset_String_thing = '+BoolToString(hello.__isset_String_thing));
865 Expect( hello.__isset_Byte_thing, 'hello.__isset_Byte_thing = '+BoolToString(hello.__isset_Byte_thing));
866 Expect( hello.__isset_I32_thing, 'hello.__isset_I32_thing = '+BoolToString(hello.__isset_I32_thing));
867 Expect( hello.__isset_I64_thing, 'hello.__isset_I64_thing = '+BoolToString(hello.__isset_I64_thing));
868 end;
869
870
871 // multi args
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200872 StartTestGroup( 'testMulti', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000873 arg0 := 1;
874 arg1 := 2;
875 arg2 := High(Int64);
876 arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
877 arg3.AddOrSetValue( 1, 'one');
878 arg4 := TNumberz.FIVE;
879 arg5 := 5000000;
880 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
881 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
882 arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
883 IntToStr( arg5) + ')');
884
885 i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5);
886 Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"');
887 Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing));
888 Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing));
889 Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing));
890 Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
891 Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
892 Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
893 Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
894
895 // multi exception
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200896 StartTestGroup( 'testMultiException(1)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000897 try
898 i := client.testMultiException( 'need more pizza', 'run out of beer');
899 Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"');
900 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
Jens Geyer6bbbf192014-09-07 01:45:56 +0200901 { this is not necessarily true, these fields are default-serialized
Jens Geyerd5436f52014-10-03 19:50:38 +0200902 Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
Roger Meier3bef8c22012-10-06 06:58:00 +0000903 Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
904 Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
Jens Geyerd5436f52014-10-03 19:50:38 +0200905 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000906 except
907 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
908 end;
909
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200910 StartTestGroup( 'testMultiException(Xception)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000911 try
912 i := client.testMultiException( 'Xception', 'second test');
913 Expect( FALSE, 'testMultiException(''Xception''): must trow an exception');
914 except
915 on x:TXception do begin
916 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
917 Expect( x.__isset_Message_, 'x.__isset_Message_ = '+BoolToString(x.__isset_Message_));
918 Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
919 Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"');
920 end;
921 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
922 end;
923
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200924 StartTestGroup( 'testMultiException(Xception2)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000925 try
926 i := client.testMultiException( 'Xception2', 'third test');
927 Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception');
928 except
929 on x:TXception2 do begin
930 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
931 Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing));
932 Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
933 Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"');
934 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 +0200935 { this is not necessarily true, these fields are default-serialized
Roger Meier3bef8c22012-10-06 06:58:00 +0000936 Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing));
937 Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing));
938 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 +0200939 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000940 end;
941 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
942 end;
943
944
945 // oneway functions
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200946 StartTestGroup( 'Test Oneway(1)', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000947 client.testOneway(1);
948 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
949
950 // call time
Jens Geyer06045cf2013-03-27 20:26:25 +0200951 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000952 StartTestGroup( 'Test Calltime()');
953 StartTick := GetTIckCount;
954 for k := 0 to 1000 - 1 do
955 begin
956 client.testVoid();
957 end;
958 Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
Jens Geyer06045cf2013-03-27 20:26:25 +0200959 {$ENDIF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000960
961 // no more tests here
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200962 StartTestGroup( '', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000963end;
964
965
Jens Geyer718f6ee2013-09-06 21:02:34 +0200966{$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200967procedure TClientThread.StressTest(const client : TThriftTest.Iface);
968begin
969 while TRUE do begin
970 try
971 if not FTransport.IsOpen then FTransport.Open; // re-open connection, server has already closed
972 try
973 client.testString('Test');
974 Write('.');
975 finally
976 if FTransport.IsOpen then FTransport.Close;
977 end;
978 except
979 on e:Exception do Writeln(#10+e.message);
980 end;
981 end;
982end;
Jens Geyer718f6ee2013-09-06 21:02:34 +0200983{$ENDIF}
Jens Geyer06045cf2013-03-27 20:26:25 +0200984
Jens Geyerfd1b3582014-12-13 23:42:58 +0100985
986function TClientThread.PrepareBinaryData( aRandomDist : Boolean = FALSE) : TBytes;
987var i, nextPos : Integer;
988begin
989 SetLength( result, $100);
990 ASSERT( Low(result) = 0);
991
992 // linear distribution, unless random is requested
993 if not aRandomDist then begin
994 for i := Low(result) to High(result) do begin
995 result[i] := i;
996 end;
997 Exit;
998 end;
999
1000 // random distribution of all 256 values
1001 FillChar( result[0], Length(result) * SizeOf(result[0]), $0);
1002 i := 1;
1003 while i < Length(result) do begin
1004 nextPos := Byte( Random($100));
1005 if result[nextPos] = 0 then begin // unused?
1006 result[nextPos] := i;
1007 Inc(i);
1008 end;
1009 end;
1010end;
1011
1012
Roger Meier3bef8c22012-10-06 06:58:00 +00001013procedure TClientThread.JSONProtocolReadWriteTest;
1014// Tests only then read/write procedures of the JSON protocol
1015// All tests succeed, if we can read what we wrote before
1016// Note that passing this test does not imply, that our JSON is really compatible to what
1017// other clients or servers expect as the real JSON. This is beyond the scope of this test.
1018var prot : IProtocol;
1019 stm : TStringStream;
1020 list : IList;
1021 binary, binRead : TBytes;
1022 i,iErr : Integer;
1023const
1024 TEST_SHORT = ShortInt( $FE);
1025 TEST_SMALL = SmallInt( $FEDC);
1026 TEST_LONG = LongInt( $FEDCBA98);
1027 TEST_I64 = Int64( $FEDCBA9876543210);
1028 TEST_DOUBLE = -1.234e-56;
1029 DELTA_DOUBLE = TEST_DOUBLE * 1e-14;
1030 TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars")
Jens Geyer7bb44a32014-02-07 22:24:37 +01001031 // Test THRIFT-2336 with 'Русское Название';
1032 RUSSIAN_TEXT = #$0420#$0443#$0441#$0441#$043a#$043e#$0435' '#$041d#$0430#$0437#$0432#$0430#$043d#$0438#$0435;
1033 RUSSIAN_JSON = '"\u0420\u0443\u0441\u0441\u043a\u043e\u0435 \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435"';
Jens Geyer21366942013-12-30 22:04:51 +01001034 // test both possible solidus encodings
1035 SOLIDUS_JSON_DATA = '"one/two\/three"';
1036 SOLIDUS_EXCPECTED = 'one/two/three';
Roger Meier3bef8c22012-10-06 06:58:00 +00001037begin
1038 stm := TStringStream.Create;
1039 try
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001040 StartTestGroup( 'JsonProtocolTest', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001041
1042 // prepare binary data
Jens Geyerfd1b3582014-12-13 23:42:58 +01001043 binary := PrepareBinaryData( FALSE);
Roger Meier3bef8c22012-10-06 06:58:00 +00001044
1045 // output setup
1046 prot := TJSONProtocolImpl.Create(
1047 TStreamTransportImpl.Create(
1048 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
1049
1050 // write
1051 prot.WriteListBegin( TListImpl.Create( TType.String_, 9));
1052 prot.WriteBool( TRUE);
1053 prot.WriteBool( FALSE);
1054 prot.WriteByte( TEST_SHORT);
1055 prot.WriteI16( TEST_SMALL);
1056 prot.WriteI32( TEST_LONG);
1057 prot.WriteI64( TEST_I64);
1058 prot.WriteDouble( TEST_DOUBLE);
1059 prot.WriteString( TEST_STRING);
1060 prot.WriteBinary( binary);
1061 prot.WriteListEnd;
1062
1063 // input setup
1064 Expect( stm.Position = stm.Size, 'Stream position/length after write');
1065 stm.Position := 0;
1066 prot := TJSONProtocolImpl.Create(
1067 TStreamTransportImpl.Create(
1068 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1069
1070 // read and compare
1071 list := prot.ReadListBegin;
1072 Expect( list.ElementType = TType.String_, 'list element type');
1073 Expect( list.Count = 9, 'list element count');
1074 Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE');
1075 Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE');
1076 Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte');
1077 Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16');
1078 Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32');
1079 Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64');
1080 Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble');
1081 Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString');
1082 binRead := prot.ReadBinary;
1083 prot.ReadListEnd;
1084
1085 // test binary data
1086 Expect( Length(binary) = Length(binRead), 'Binary data length check');
1087 iErr := -1;
1088 for i := Low(binary) to High(binary) do begin
1089 if binary[i] <> binRead[i] then begin
1090 iErr := i;
1091 Break;
1092 end;
1093 end;
1094 if iErr < 0
1095 then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)')
1096 else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr));
1097
1098 Expect( stm.Position = stm.Size, 'Stream position after read');
1099
Jens Geyer7bb44a32014-02-07 22:24:37 +01001100
Jens Geyer21366942013-12-30 22:04:51 +01001101 // Solidus can be encoded in two ways. Make sure we can read both
1102 stm.Position := 0;
1103 stm.Size := 0;
1104 stm.WriteString(SOLIDUS_JSON_DATA);
1105 stm.Position := 0;
1106 prot := TJSONProtocolImpl.Create(
1107 TStreamTransportImpl.Create(
1108 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1109 Expect( prot.ReadString = SOLIDUS_EXCPECTED, 'Solidus encoding');
1110
1111
Jens Geyer7bb44a32014-02-07 22:24:37 +01001112 // Widechars should work too. Do they?
1113 // After writing, we ensure that we are able to read it back
1114 // We can't assume hex-encoding, since (nearly) any Unicode char is valid JSON
1115 stm.Position := 0;
1116 stm.Size := 0;
1117 prot := TJSONProtocolImpl.Create(
1118 TStreamTransportImpl.Create(
1119 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
1120 prot.WriteString( RUSSIAN_TEXT);
1121 stm.Position := 0;
1122 prot := TJSONProtocolImpl.Create(
1123 TStreamTransportImpl.Create(
1124 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1125 Expect( prot.ReadString = RUSSIAN_TEXT, 'Writing JSON with chars > 8 bit');
1126
1127 // Widechars should work with hex-encoding too. Do they?
1128 stm.Position := 0;
1129 stm.Size := 0;
1130 stm.WriteString( RUSSIAN_JSON);
1131 stm.Position := 0;
1132 prot := TJSONProtocolImpl.Create(
1133 TStreamTransportImpl.Create(
1134 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1135 Expect( prot.ReadString = RUSSIAN_TEXT, 'Reading JSON with chars > 8 bit');
1136
1137
Roger Meier3bef8c22012-10-06 06:58:00 +00001138 finally
1139 stm.Free;
1140 prot := nil; //-> Release
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001141 StartTestGroup( '', test_Unknown); // no more tests here
Roger Meier3bef8c22012-10-06 06:58:00 +00001142 end;
1143end;
1144
1145
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001146procedure TClientThread.StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +00001147begin
1148 FTestGroup := aGroup;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001149 FCurrentTest := aTest;
1150
1151 Include( FExecuted, aTest);
1152
Roger Meier3bef8c22012-10-06 06:58:00 +00001153 if FTestGroup <> '' then begin
1154 Console.WriteLine('');
1155 Console.WriteLine( aGroup+' tests');
1156 Console.WriteLine( StringOfChar('-',60));
1157 end;
1158end;
1159
1160
1161procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
1162begin
1163 if aTestResult then begin
1164 Inc(FSuccesses);
1165 Console.WriteLine( aTestInfo+': passed');
1166 end
1167 else begin
1168 FErrors.Add( FTestGroup+': '+aTestInfo);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001169 Include( FFailed, FCurrentTest);
Roger Meier3bef8c22012-10-06 06:58:00 +00001170 Console.WriteLine( aTestInfo+': *** FAILED ***');
1171
1172 // We have a failed test!
1173 // -> issue DebugBreak ONLY if a debugger is attached,
1174 // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise
1175 if IsDebuggerPresent then asm int 3 end;
1176 end;
1177end;
1178
1179
1180procedure TClientThread.ReportResults;
1181var nTotal : Integer;
1182 sLine : string;
1183begin
1184 // prevent us from stupid DIV/0 errors
1185 nTotal := FSuccesses + FErrors.Count;
1186 if nTotal = 0 then begin
1187 Console.WriteLine('No results logged');
1188 Exit;
1189 end;
1190
1191 Console.WriteLine('');
1192 Console.WriteLine( StringOfChar('=',60));
1193 Console.WriteLine( IntToStr(nTotal)+' tests performed');
1194 Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)');
1195 Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)');
1196 Console.WriteLine( StringOfChar('=',60));
1197 if FErrors.Count > 0 then begin
1198 Console.WriteLine('FAILED TESTS:');
1199 for sLine in FErrors do Console.WriteLine('- '+sLine);
1200 Console.WriteLine( StringOfChar('=',60));
1201 InterlockedIncrement( ExitCode); // return <> 0 on errors
1202 end;
1203 Console.WriteLine('');
1204end;
1205
1206
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001207function TClientThread.CalculateExitCode : Byte;
1208var test : TTestGroup;
1209begin
1210 result := EXITCODE_SUCCESS;
1211 for test := Low(TTestGroup) to High(TTestGroup) do begin
1212 if (test in FFailed) or not (test in FExecuted)
1213 then result := result or MAP_FAILURES_TO_EXITCODE_BITS[test];
1214 end;
1215end;
1216
1217
Roger Meier3bef8c22012-10-06 06:58:00 +00001218constructor TClientThread.Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
1219begin
1220 inherited Create( True );
1221 FNumIteration := ANumIteration;
1222 FTransport := ATransport;
1223 FProtocol := AProtocol;
1224 FConsole := TThreadConsole.Create( Self );
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001225 FCurrentTest := test_Unknown;
Roger Meier3bef8c22012-10-06 06:58:00 +00001226
1227 // error list: keep correct order, allow for duplicates
1228 FErrors := TStringList.Create;
1229 FErrors.Sorted := FALSE;
1230 FErrors.Duplicates := dupAccept;
1231end;
1232
1233destructor TClientThread.Destroy;
1234begin
1235 FreeAndNil( FConsole);
1236 FreeAndNil( FErrors);
1237 inherited;
1238end;
1239
1240procedure TClientThread.Execute;
1241var
1242 i : Integer;
1243 proc : TThreadProcedure;
1244begin
1245 // perform all tests
1246 try
Jens Geyer7bb44a32014-02-07 22:24:37 +01001247 JSONProtocolReadWriteTest;
Roger Meier3bef8c22012-10-06 06:58:00 +00001248 for i := 0 to FNumIteration - 1 do
1249 begin
1250 ClientTest;
Roger Meier3bef8c22012-10-06 06:58:00 +00001251 end;
1252 except
1253 on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"');
1254 end;
1255
1256 // report the outcome
1257 ReportResults;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001258 SetReturnValue( CalculateExitCode);
Roger Meier3bef8c22012-10-06 06:58:00 +00001259
1260 // shutdown
1261 proc := procedure
1262 begin
1263 if FTransport <> nil then
1264 begin
1265 FTransport.Close;
1266 FTransport := nil;
1267 end;
1268 end;
1269
1270 Synchronize( proc );
1271end;
1272
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001273
Roger Meier3bef8c22012-10-06 06:58:00 +00001274{ TThreadConsole }
1275
1276constructor TThreadConsole.Create(AThread: TThread);
1277begin
Jens Geyer718f6ee2013-09-06 21:02:34 +02001278 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +00001279 FThread := AThread;
1280end;
1281
1282procedure TThreadConsole.Write(const S: string);
1283var
1284 proc : TThreadProcedure;
1285begin
1286 proc := procedure
1287 begin
1288 Console.Write( S );
1289 end;
1290 TThread.Synchronize( FThread, proc);
1291end;
1292
1293procedure TThreadConsole.WriteLine(const S: string);
1294var
1295 proc : TThreadProcedure;
1296begin
1297 proc := procedure
1298 begin
1299 Console.WriteLine( S );
1300 end;
1301 TThread.Synchronize( FThread, proc);
1302end;
1303
1304initialization
1305begin
1306 TTestClient.FNumIteration := 1;
1307 TTestClient.FNumThread := 1;
1308end;
1309
1310end.