blob: 6f155a2df48cbb45cb85ec83d1b10c7d919ead88 [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,
33 Thrift.Protocol.JSON,
34 Thrift.Protocol,
35 Thrift.Transport.Pipes,
36 Thrift.Transport,
37 Thrift.Stream,
38 Thrift.Test,
39 Thrift.Collections,
40 Thrift.Console;
41
42type
43 TThreadConsole = class
44 private
45 FThread : TThread;
46 public
47 procedure Write( const S : string);
48 procedure WriteLine( const S : string);
49 constructor Create( AThread: TThread);
50 end;
51
52 TClientThread = class( TThread )
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020053 private type
54 TTestGroup = (
55 test_Unknown,
56 test_BaseTypes,
57 test_Structs,
58 test_Containers,
59 test_Exceptions
60 // new values here
61 );
62 TTestGroups = set of TTestGroup;
63
Roger Meier3bef8c22012-10-06 06:58:00 +000064 private
65 FTransport : ITransport;
66 FProtocol : IProtocol;
67 FNumIteration : Integer;
68 FConsole : TThreadConsole;
69
70 // test reporting, will be refactored out into separate class later
71 FTestGroup : string;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020072 FCurrentTest : TTestGroup;
Roger Meier3bef8c22012-10-06 06:58:00 +000073 FSuccesses : Integer;
74 FErrors : TStringList;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020075 FFailed : TTestGroups;
76 FExecuted : TTestGroups;
77 procedure StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +000078 procedure Expect( aTestResult : Boolean; const aTestInfo : string);
79 procedure ReportResults;
Jens Geyerfd1b3582014-12-13 23:42:58 +010080 function CalculateExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +000081
82 procedure ClientTest;
83 procedure JSONProtocolReadWriteTest;
Jens Geyerfd1b3582014-12-13 23:42:58 +010084 function PrepareBinaryData( aRandomDist : Boolean = FALSE) : TBytes;
Jens Geyer718f6ee2013-09-06 21:02:34 +020085 {$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +020086 procedure StressTest(const client : TThriftTest.Iface);
Jens Geyer718f6ee2013-09-06 21:02:34 +020087 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +000088 protected
89 procedure Execute; override;
90 public
91 constructor Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
92 destructor Destroy; override;
93 end;
94
95 TTestClient = class
96 private
97 class var
98 FNumIteration : Integer;
99 FNumThread : Integer;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200100
101 class procedure PrintCmdLineHelp;
102 class procedure InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000103 public
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200104 class function Execute( const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000105 end;
106
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200107
Roger Meier3bef8c22012-10-06 06:58:00 +0000108implementation
109
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200110const
111 EXITCODE_SUCCESS = $00; // no errors bits set
112 //
113 EXITCODE_FAILBIT_BASETYPES = $01;
114 EXITCODE_FAILBIT_STRUCTS = $02;
115 EXITCODE_FAILBIT_CONTAINERS = $04;
116 EXITCODE_FAILBIT_EXCEPTIONS = $08;
117
118 MAP_FAILURES_TO_EXITCODE_BITS : array[TClientThread.TTestGroup] of Byte = (
119 EXITCODE_SUCCESS, // no bits here
120 EXITCODE_FAILBIT_BASETYPES,
121 EXITCODE_FAILBIT_STRUCTS,
122 EXITCODE_FAILBIT_CONTAINERS,
123 EXITCODE_FAILBIT_EXCEPTIONS
124 );
125
126
127
Roger Meier3bef8c22012-10-06 06:58:00 +0000128function BoolToString( b : Boolean) : string;
129// overrides global BoolToString()
130begin
131 if b
132 then result := 'true'
133 else result := 'false';
134end;
135
136// not available in all versions, so make sure we have this one imported
137function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent';
138
139{ TTestClient }
140
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200141class procedure TTestClient.PrintCmdLineHelp;
142const HELPTEXT = ' [options]'#10
143 + #10
144 + 'Allowed options:'#10
145 + ' -h [ --help ] produce help message'#10
146 + ' --host arg (=localhost) Host to connect'#10
147 + ' --port arg (=9090) Port number to connect'#10
148 + ' --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift),'#10
149 + ' instead of host and port'#10
150 + ' --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)'#10
151 + ' --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)'#10
152 + ' --transport arg (=sockets) Transport: buffered, framed, http, evhttp'#10
153 + ' --protocol arg (=binary) Protocol: binary, compact, json'#10
154 + ' --ssl Encrypted Transport using SSL'#10
155 + ' -n [ --testloops ] arg (=1) Number of Tests'#10
156 + ' -t [ --threads ] arg (=1) Number of Test threads'#10
157 ;
158begin
159 Writeln( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + HELPTEXT);
160end;
161
162class procedure TTestClient.InvalidArgs;
163begin
164 Console.WriteLine( 'Invalid args.');
165 Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + ' -h for more information');
166 Abort;
167end;
168
169class function TTestClient.Execute(const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000170var
171 i : Integer;
172 host : string;
173 port : Integer;
Roger Meier3bef8c22012-10-06 06:58:00 +0000174 sPipeName : string;
175 hAnonRead, hAnonWrite : THandle;
176 s : string;
Roger Meier3bef8c22012-10-06 06:58:00 +0000177 threads : array of TThread;
178 dtStart : TDateTime;
179 test : Integer;
180 thread : TThread;
181 trans : ITransport;
182 prot : IProtocol;
183 streamtrans : IStreamTransport;
184 http : IHTTPClient;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200185 protType : TKnownProtocol;
186 endpoint : TEndpointTransport;
187 layered : TLayeredTransports;
188 UseSSL : Boolean; // include where appropriate (TLayeredTransport?)
Jens Geyer0b20cc82013-03-07 20:47:01 +0100189const
190 // pipe timeouts to be used
191 DEBUG_TIMEOUT = 30 * 1000;
Jens Geyer3e8d9272014-09-14 20:10:40 +0200192 RELEASE_TIMEOUT = DEFAULT_THRIFT_TIMEOUT;
Jens Geyer0b20cc82013-03-07 20:47:01 +0100193 TIMEOUT = RELEASE_TIMEOUT;
Roger Meier3bef8c22012-10-06 06:58:00 +0000194begin
Roger Meier3bef8c22012-10-06 06:58:00 +0000195 protType := prot_Binary;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200196 endpoint := trns_Sockets;
197 layered := [];
198 UseSSL := FALSE;
199 host := 'localhost';
200 port := 9090;
201 sPipeName := '';
202 hAnonRead := INVALID_HANDLE_VALUE;
203 hAnonWrite := INVALID_HANDLE_VALUE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000204 try
Roger Meier3bef8c22012-10-06 06:58:00 +0000205 i := 0;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200206 while ( i < Length(args) ) do begin
207 s := args[i];
208 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000209
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200210 if (s = '-h') or (s = '--help') then begin
211 // -h [ --help ] produce help message
212 PrintCmdLineHelp;
213 result := $FF; // all tests failed
214 Exit;
215 end
Jens Geyerb360b652014-09-28 01:55:46 +0200216 else if s = '--host' then begin
217 // --host arg (=localhost) Host to connect
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200218 host := args[i];
219 Inc( i);
220 end
Jens Geyerb360b652014-09-28 01:55:46 +0200221 else if s = '--port' then begin
222 // --port arg (=9090) Port number to connect
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200223 s := args[i];
224 Inc( i);
225 port := StrToIntDef(s,0);
226 if port <= 0 then InvalidArgs;
227 end
Jens Geyerb360b652014-09-28 01:55:46 +0200228 else if s = '--domain-socket' then begin
229 // --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200230 raise Exception.Create('domain-socket not supported');
231 end
Jens Geyerb360b652014-09-28 01:55:46 +0200232 else if s = '--named-pipe' then begin
233 // --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200234 endpoint := trns_NamedPipes;
235 sPipeName := args[i];
236 Inc( i);
237 end
Jens Geyerb360b652014-09-28 01:55:46 +0200238 else if s = '--anon-pipes' then begin
239 // --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200240 endpoint := trns_AnonPipes;
241 hAnonRead := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
242 Inc( i);
243 hAnonWrite := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
244 Inc( i);
245 end
Jens Geyerb360b652014-09-28 01:55:46 +0200246 else if s = '--transport' then begin
247 // --transport arg (=sockets) Transport: buffered, framed, http, evhttp
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200248 s := args[i];
249 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000250
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200251 if s = 'buffered' then Include( layered, trns_Buffered)
252 else if s = 'framed' then Include( layered, trns_Framed)
253 else if s = 'http' then endpoint := trns_Http
254 else if s = 'evhttp' then endpoint := trns_AnonPipes
255 else InvalidArgs;
256 end
Jens Geyerb360b652014-09-28 01:55:46 +0200257 else if s = '--protocol' then begin
258 // --protocol arg (=binary) Protocol: binary, compact, json
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200259 s := args[i];
260 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000261
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200262 if s = 'binary' then protType := prot_Binary
263 else if s = 'compact' then protType := prot_Compact
264 else if s = 'json' then protType := prot_JSON
265 else InvalidArgs;
266 end
Jens Geyerb360b652014-09-28 01:55:46 +0200267 else if s = '--ssl' then begin
268 // --ssl Encrypted Transport using SSL
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200269 UseSSL := TRUE;
270
271 end
272 else if (s = '-n') or (s = '--testloops') then begin
273 // -n [ --testloops ] arg (=1) Number of Tests
274 FNumIteration := StrToIntDef( args[i], 0);
275 Inc( i);
276 if FNumIteration <= 0
277 then InvalidArgs;
278
279 end
280 else if (s = '-t') or (s = '--threads') then begin
281 // -t [ --threads ] arg (=1) Number of Test threads
282 FNumThread := StrToIntDef( args[i], 0);
283 Inc( i);
284 if FNumThread <= 0
285 then InvalidArgs;
286 end
287 else begin
288 InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000289 end;
290 end;
291
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200292
Roger Meier79655fb2012-10-20 20:59:41 +0000293 // In the anonymous pipes mode the client is launched by the test server
294 // -> behave nicely and allow for attaching a debugger to this process
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200295 if (endpoint = trns_AnonPipes) and not IsDebuggerPresent
Roger Meier79655fb2012-10-20 20:59:41 +0000296 then MessageBox( 0, 'Attach Debugger and/or click OK to continue.',
297 'Thrift TestClient (Delphi)',
298 MB_OK or MB_ICONEXCLAMATION);
299
Roger Meier3bef8c22012-10-06 06:58:00 +0000300 SetLength( threads, FNumThread);
301 dtStart := Now;
302
303 for test := 0 to FNumThread - 1 do
304 begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200305 case endpoint of
306 trns_Sockets: begin
Roger Meier3bef8c22012-10-06 06:58:00 +0000307 Console.WriteLine('Using sockets ('+host+' port '+IntToStr(port)+')');
308 streamtrans := TSocketImpl.Create( host, port );
309 end;
310
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200311 trns_Http: begin
312 Console.WriteLine('Using HTTPClient');
313 http := THTTPClientImpl.Create( host);
314 trans := http;
Roger Meier3bef8c22012-10-06 06:58:00 +0000315 end;
316
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200317 trns_EvHttp: begin
318 raise Exception.Create(ENDPOINT_TRANSPORTS[endpoint]+' transport not implemented');
Roger Meier3bef8c22012-10-06 06:58:00 +0000319 end;
320
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200321 trns_NamedPipes: begin
322 Console.WriteLine('Using named pipe ('+sPipeName+')');
323 streamtrans := TNamedPipeTransportClientEndImpl.Create( sPipeName, 0, nil, TIMEOUT);
324 end;
325
326 trns_AnonPipes: begin
327 Console.WriteLine('Using anonymous pipes ('+IntToStr(Integer(hAnonRead))+' and '+IntToStr(Integer(hAnonWrite))+')');
328 streamtrans := TAnonymousPipeTransportImpl.Create( hAnonRead, hAnonWrite, FALSE);
329 end;
330
331 else
332 raise Exception.Create('Unhandled endpoint transport');
333 end;
334 trans := streamtrans;
335 ASSERT( trans <> nil);
336
337 if (trns_Buffered in layered) then begin
338 trans := TBufferedTransportImpl.Create( streamtrans, 32); // small buffer to test read()
339 Console.WriteLine('Using buffered transport');
340 end;
341
342 if (trns_Framed in layered) then begin
343 trans := TFramedTransportImpl.Create( trans );
344 Console.WriteLine('Using framed transport');
345 end;
346
347 if UseSSL then begin
348 raise Exception.Create('SSL not implemented');
Roger Meier3bef8c22012-10-06 06:58:00 +0000349 end;
350
351 // create protocol instance, default to BinaryProtocol
352 case protType of
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200353 prot_Binary : prot := TBinaryProtocolImpl.Create( trans, BINARY_STRICT_READ, BINARY_STRICT_WRITE);
354 prot_JSON : prot := TJSONProtocolImpl.Create( trans);
355 prot_Compact : raise Exception.Create('Compact protocol not implemented');
Roger Meier3bef8c22012-10-06 06:58:00 +0000356 else
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200357 raise Exception.Create('Unhandled protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000358 end;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200359 ASSERT( trans <> nil);
360 Console.WriteLine(THRIFT_PROTOCOLS[protType]+' protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000361
362 thread := TClientThread.Create( trans, prot, FNumIteration);
363 threads[test] := thread;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200364 thread.Start;
Roger Meier3bef8c22012-10-06 06:58:00 +0000365 end;
366
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200367 result := 0;
368 for test := 0 to FNumThread - 1 do begin
369 result := result or threads[test].WaitFor;
Roger Meier3bef8c22012-10-06 06:58:00 +0000370 end;
371
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200372 for test := 0 to FNumThread - 1
373 do threads[test].Free;
Roger Meier3bef8c22012-10-06 06:58:00 +0000374
375 Console.Write('Total time: ' + IntToStr( MilliSecondsBetween(Now, dtStart)));
376
377 except
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200378 on E: EAbort do raise;
379 on E: Exception do begin
380 Console.WriteLine( E.Message + #10 + E.StackTrace);
381 raise;
Roger Meier3bef8c22012-10-06 06:58:00 +0000382 end;
383 end;
384
385 Console.WriteLine('');
386 Console.WriteLine('done!');
387end;
388
389{ TClientThread }
390
391procedure TClientThread.ClientTest;
392var
393 client : TThriftTest.Iface;
394 s : string;
395 i8 : ShortInt;
396 i32 : Integer;
397 i64 : Int64;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100398 binOut,binIn : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000399 dub : Double;
400 o : IXtruct;
401 o2 : IXtruct2;
402 i : IXtruct;
403 i2 : IXtruct2;
404 mapout : IThriftDictionary<Integer,Integer>;
405 mapin : IThriftDictionary<Integer,Integer>;
406 strmapout : IThriftDictionary<string,string>;
407 strmapin : IThriftDictionary<string,string>;
408 j : Integer;
409 first : Boolean;
410 key : Integer;
411 strkey : string;
412 listout : IThriftList<Integer>;
413 listin : IThriftList<Integer>;
414 setout : IHashSet<Integer>;
415 setin : IHashSet<Integer>;
416 ret : TNumberz;
417 uid : Int64;
418 mm : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
419 pos : IThriftDictionary<Integer, Integer>;
420 neg : IThriftDictionary<Integer, Integer>;
421 m2 : IThriftDictionary<Integer, Integer>;
422 k2 : Integer;
423 insane : IInsanity;
424 truck : IXtruct;
425 whoa : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
426 key64 : Int64;
427 val : IThriftDictionary<TNumberz, IInsanity>;
428 k2_2 : TNumberz;
429 k3 : TNumberz;
430 v2 : IInsanity;
431 userMap : IThriftDictionary<TNumberz, Int64>;
432 xtructs : IThriftList<IXtruct>;
433 x : IXtruct;
434 arg0 : ShortInt;
435 arg1 : Integer;
436 arg2 : Int64;
437 arg3 : IThriftDictionary<SmallInt, string>;
438 arg4 : TNumberz;
439 arg5 : Int64;
440 StartTick : Cardinal;
441 k : Integer;
442 proc : TThreadProcedure;
443 hello, goodbye : IXtruct;
444 crazy : IInsanity;
445 looney : IInsanity;
446 first_map : IThriftDictionary<TNumberz, IInsanity>;
447 second_map : IThriftDictionary<TNumberz, IInsanity>;
448
449begin
450 client := TThriftTest.TClient.Create( FProtocol);
451 FTransport.Open;
452
Jens Geyer06045cf2013-03-27 20:26:25 +0200453 {$IFDEF StressTest}
454 StressTest( client);
455 {$ENDIF StressTest}
456
Roger Meier3bef8c22012-10-06 06:58:00 +0000457 // in-depth exception test
458 // (1) do we get an exception at all?
459 // (2) do we get the right exception?
460 // (3) does the exception contain the expected data?
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200461 StartTestGroup( 'testException', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000462 // case 1: exception type declared in IDL at the function call
463 try
464 client.testException('Xception');
465 Expect( FALSE, 'testException(''Xception''): must trow an exception');
466 except
467 on e:TXception do begin
468 Expect( e.ErrorCode = 1001, 'error code');
469 Expect( e.Message_ = 'Xception', 'error message');
470 Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ );
471 end;
472 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
473 on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
474 end;
475
476 // case 2: exception type NOT declared in IDL at the function call
477 // this will close the connection
478 try
479 client.testException('TException');
480 Expect( FALSE, 'testException(''TException''): must trow an exception');
481 except
482 on e:TTransportException do begin
483 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
484 if FTransport.IsOpen then FTransport.Close;
485 FTransport.Open; // re-open connection, server has already closed
486 end;
Jens Geyer6bbbf192014-09-07 01:45:56 +0200487 on e:TApplicationException do begin
488 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
489 if FTransport.IsOpen then FTransport.Close;
490 FTransport.Open; // re-open connection, server has already closed
491 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000492 on e:TException do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
493 on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
494 end;
495
496 // case 3: no exception
497 try
498 client.testException('something');
499 Expect( TRUE, 'testException(''something''): must not trow an exception');
500 except
501 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
502 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
503 end;
504
505
506 // simple things
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200507 StartTestGroup( 'simple Thrift calls', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000508 client.testVoid();
509 Expect( TRUE, 'testVoid()'); // success := no exception
510
511 s := client.testString('Test');
512 Expect( s = 'Test', 'testString(''Test'') = "'+s+'"');
513
Jens Geyer06045cf2013-03-27 20:26:25 +0200514 s := client.testString(HUGE_TEST_STRING);
515 Expect( length(s) = length(HUGE_TEST_STRING),
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100516 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
Jens Geyer06045cf2013-03-27 20:26:25 +0200517 +'=> length(result) = '+IntToStr(Length(s)));
518
Roger Meier3bef8c22012-10-06 06:58:00 +0000519 i8 := client.testByte(1);
520 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
521
522 i32 := client.testI32(-1);
523 Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32));
524
525 Console.WriteLine('testI64(-34359738368)');
526 i64 := client.testI64(-34359738368);
527 Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
528
Jens Geyerfd1b3582014-12-13 23:42:58 +0100529 binOut := PrepareBinaryData( TRUE);
530 Console.WriteLine('testBinary('+BytesToHex(binOut)+')');
531 try
532 binIn := client.testBinary(binOut);
533 Expect( Length(binOut) = Length(binIn), 'testBinary(): length '+IntToStr(Length(binOut))+' = '+IntToStr(Length(binIn)));
534 i32 := Min( Length(binOut), Length(binIn));
535 Expect( CompareMem( binOut, binIn, i32), 'testBinary('+BytesToHex(binOut)+') = '+BytesToHex(binIn));
536 except
537 on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message);
538 on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message);
539 end;
540
Roger Meier3bef8c22012-10-06 06:58:00 +0000541 Console.WriteLine('testDouble(5.325098235)');
542 dub := client.testDouble(5.325098235);
543 Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
544
545 // structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200546 StartTestGroup( 'testStruct', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000547 Console.WriteLine('testStruct({''Zero'', 1, -3, -5})');
548 o := TXtructImpl.Create;
549 o.String_thing := 'Zero';
550 o.Byte_thing := 1;
551 o.I32_thing := -3;
552 o.I64_thing := -5;
553 i := client.testStruct(o);
554 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
555 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
556 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
557 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
558 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
559 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
560 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
561 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
562
563 // nested structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200564 StartTestGroup( 'testNest', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000565 Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})');
566 o2 := TXtruct2Impl.Create;
567 o2.Byte_thing := 1;
568 o2.Struct_thing := o;
569 o2.I32_thing := 5;
570 i2 := client.testNest(o2);
571 i := i2.Struct_thing;
572 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
573 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
574 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
575 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
576 Expect( i2.Byte_thing = 1, 'i2.Byte_thing = '+IntToStr(i2.Byte_thing));
577 Expect( i2.I32_thing = 5, 'i2.I32_thing = '+IntToStr(i2.I32_thing));
578 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
579 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
580 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
581 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
582 Expect( i2.__isset_Byte_thing, 'i2.__isset_Byte_thing');
583 Expect( i2.__isset_I32_thing, 'i2.__isset_I32_thing');
584
585 // map<type1,type2>: A map of strictly unique keys to values.
586 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200587 StartTestGroup( 'testMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000588 mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
589 for j := 0 to 4 do
590 begin
591 mapout.AddOrSetValue( j, j - 10);
592 end;
593 Console.Write('testMap({');
594 first := True;
595 for key in mapout.Keys do
596 begin
597 if first
598 then first := False
599 else Console.Write( ', ' );
600 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
601 end;
602 Console.WriteLine('})');
603
604 mapin := client.testMap( mapout );
605 Expect( mapin.Count = mapout.Count, 'testMap: mapin.Count = mapout.Count');
606 for j := 0 to 4 do
607 begin
608 Expect( mapout.ContainsKey(j), 'testMap: mapout.ContainsKey('+IntToStr(j)+') = '+BoolToString(mapout.ContainsKey(j)));
609 end;
610 for key in mapin.Keys do
611 begin
612 Expect( mapin[key] = mapout[key], 'testMap: '+IntToStr(key) + ' => ' + IntToStr( mapin[key]));
613 Expect( mapin[key] = key - 10, 'testMap: mapin['+IntToStr(key)+'] = '+IntToStr( mapin[key]));
614 end;
615
616
617 // map<type1,type2>: A map of strictly unique keys to values.
618 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200619 StartTestGroup( 'testStringMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000620 strmapout := TThriftDictionaryImpl<string,string>.Create;
621 for j := 0 to 4 do
622 begin
623 strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10));
624 end;
625 Console.Write('testStringMap({');
626 first := True;
627 for strkey in strmapout.Keys do
628 begin
629 if first
630 then first := False
631 else Console.Write( ', ' );
632 Console.Write( strkey + ' => ' + strmapout[strkey]);
633 end;
634 Console.WriteLine('})');
635
636 strmapin := client.testStringMap( strmapout );
637 Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count');
638 for j := 0 to 4 do
639 begin
640 Expect( strmapout.ContainsKey(IntToStr(j)),
641 'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = '
642 + BoolToString(strmapout.ContainsKey(IntToStr(j))));
643 end;
644 for strkey in strmapin.Keys do
645 begin
646 Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]);
647 Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]);
648 end;
649
650
651 // set<type>: An unordered set of unique elements.
652 // Translates to an STL set, Java HashSet, set in Python, etc.
653 // Note: PHP does not support sets, so it is treated similar to a List
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200654 StartTestGroup( 'testSet', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000655 setout := THashSetImpl<Integer>.Create;
656 for j := -2 to 2 do
657 begin
658 setout.Add( j );
659 end;
660 Console.Write('testSet({');
661 first := True;
662 for j in setout do
663 begin
664 if first
665 then first := False
666 else Console.Write(', ');
667 Console.Write(IntToStr( j));
668 end;
669 Console.WriteLine('})');
670
671 setin := client.testSet(setout);
672 Expect( setin.Count = setout.Count, 'testSet: setin.Count = setout.Count');
673 Expect( setin.Count = 5, 'testSet: setin.Count = '+IntToStr(setin.Count));
674 for j := -2 to 2 do // unordered, we can't rely on the order => test for known elements only
675 begin
676 Expect( setin.Contains(j), 'testSet: setin.Contains('+IntToStr(j)+') => '+BoolToString(setin.Contains(j)));
677 end;
678
679 // list<type>: An ordered list of elements.
680 // Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200681 StartTestGroup( 'testList', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000682 listout := TThriftListImpl<Integer>.Create;
683 listout.Add( +1);
684 listout.Add( -2);
685 listout.Add( +3);
686 listout.Add( -4);
687 listout.Add( 0);
688 Console.Write('testList({');
689 first := True;
690 for j in listout do
691 begin
692 if first
693 then first := False
694 else Console.Write(', ');
695 Console.Write(IntToStr( j));
696 end;
697 Console.WriteLine('})');
698
699 listin := client.testList(listout);
700 Expect( listin.Count = listout.Count, 'testList: listin.Count = listout.Count');
701 Expect( listin.Count = 5, 'testList: listin.Count = '+IntToStr(listin.Count));
702 Expect( listin[0] = +1, 'listin[0] = '+IntToStr( listin[0]));
703 Expect( listin[1] = -2, 'listin[1] = '+IntToStr( listin[1]));
704 Expect( listin[2] = +3, 'listin[2] = '+IntToStr( listin[2]));
705 Expect( listin[3] = -4, 'listin[3] = '+IntToStr( listin[3]));
706 Expect( listin[4] = 0, 'listin[4] = '+IntToStr( listin[4]));
707
708 // enums
709 ret := client.testEnum(TNumberz.ONE);
710 Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret)));
711
712 ret := client.testEnum(TNumberz.TWO);
713 Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret)));
714
715 ret := client.testEnum(TNumberz.THREE);
716 Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret)));
717
718 ret := client.testEnum(TNumberz.FIVE);
719 Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret)));
720
721 ret := client.testEnum(TNumberz.EIGHT);
722 Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret)));
723
724
725 // typedef
726 uid := client.testTypedef(309858235082523);
727 Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid));
728
729
730 // maps of maps
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200731 StartTestGroup( 'testMapMap(1)', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000732 mm := client.testMapMap(1);
733 Console.Write(' = {');
734 for key in mm.Keys do
735 begin
736 Console.Write( IntToStr( key) + ' => {');
737 m2 := mm[key];
738 for k2 in m2.Keys do
739 begin
740 Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', ');
741 end;
742 Console.Write('}, ');
743 end;
744 Console.WriteLine('}');
745
746 // verify result data
747 Expect( mm.Count = 2, 'mm.Count = '+IntToStr(mm.Count));
748 pos := mm[4];
749 neg := mm[-4];
750 for j := 1 to 4 do
751 begin
752 Expect( pos[j] = j, 'pos[j] = '+IntToStr(pos[j]));
753 Expect( neg[-j] = -j, 'neg[-j] = '+IntToStr(neg[-j]));
754 end;
755
756
757
758 // insanity
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200759 StartTestGroup( 'testInsanity', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000760 insane := TInsanityImpl.Create;
761 insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
762 insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000);
763 truck := TXtructImpl.Create;
764 truck.String_thing := 'Truck';
765 truck.Byte_thing := 8;
766 truck.I32_thing := 8;
767 truck.I64_thing := 8;
768 insane.Xtructs := TThriftListImpl<IXtruct>.Create;
769 insane.Xtructs.Add( truck );
770 whoa := client.testInsanity( insane );
771 Console.Write(' = {');
772 for key64 in whoa.Keys do
773 begin
774 val := whoa[key64];
775 Console.Write( IntToStr( key64) + ' => {');
776 for k2_2 in val.Keys do
777 begin
778 v2 := val[k2_2];
779 Console.Write( IntToStr( Integer( k2_2)) + ' => {');
780 userMap := v2.UserMap;
781 Console.Write('{');
782 if userMap <> nil then
783 begin
784 for k3 in userMap.Keys do
785 begin
786 Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', ');
787 end;
788 end else
789 begin
790 Console.Write('null');
791 end;
792 Console.Write('}, ');
793 xtructs := v2.Xtructs;
794 Console.Write('{');
795
796 if xtructs <> nil then
797 begin
798 for x in xtructs do
799 begin
800 Console.Write('{"' + x.String_thing + '", ' +
801 IntToStr( x.Byte_thing) + ', ' +
802 IntToStr( x.I32_thing) + ', ' +
803 IntToStr( x.I32_thing) + '}, ');
804 end;
805 end else
806 begin
807 Console.Write('null');
808 end;
809 Console.Write('}');
810 Console.Write('}, ');
811 end;
812 Console.Write('}, ');
813 end;
814 Console.WriteLine('}');
815
816 // verify result data
817 Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count));
818 //
819 first_map := whoa[1];
820 second_map := whoa[2];
821 Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count));
822 Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count));
823 //
824 looney := second_map[TNumberz.SIX];
825 Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney)));
826 Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
827 Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
828 //
829 for ret in [TNumberz.TWO, TNumberz.THREE] do begin
830 crazy := first_map[ret];
831 Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
832
833 Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap));
834 Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs));
835
836 Expect( crazy.UserMap.Count = 2, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count));
837 Expect( crazy.UserMap[TNumberz.FIVE] = 5, 'crazy.UserMap[TNumberz.FIVE] = '+IntToStr(crazy.UserMap[TNumberz.FIVE]));
838 Expect( crazy.UserMap[TNumberz.EIGHT] = 8, 'crazy.UserMap[TNumberz.EIGHT] = '+IntToStr(crazy.UserMap[TNumberz.EIGHT]));
839
840 Expect( crazy.Xtructs.Count = 2, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count));
841 goodbye := crazy.Xtructs[0]; // lists are ordered, so we are allowed to assume this order
Jens Geyerd5436f52014-10-03 19:50:38 +0200842 hello := crazy.Xtructs[1];
Roger Meier3bef8c22012-10-06 06:58:00 +0000843
844 Expect( goodbye.String_thing = 'Goodbye4', 'goodbye.String_thing = "'+goodbye.String_thing+'"');
845 Expect( goodbye.Byte_thing = 4, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing));
846 Expect( goodbye.I32_thing = 4, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing));
847 Expect( goodbye.I64_thing = 4, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing));
848 Expect( goodbye.__isset_String_thing, 'goodbye.__isset_String_thing = '+BoolToString(goodbye.__isset_String_thing));
849 Expect( goodbye.__isset_Byte_thing, 'goodbye.__isset_Byte_thing = '+BoolToString(goodbye.__isset_Byte_thing));
850 Expect( goodbye.__isset_I32_thing, 'goodbye.__isset_I32_thing = '+BoolToString(goodbye.__isset_I32_thing));
851 Expect( goodbye.__isset_I64_thing, 'goodbye.__isset_I64_thing = '+BoolToString(goodbye.__isset_I64_thing));
852
853 Expect( hello.String_thing = 'Hello2', 'hello.String_thing = "'+hello.String_thing+'"');
854 Expect( hello.Byte_thing = 2, 'hello.Byte_thing = '+IntToStr(hello.Byte_thing));
855 Expect( hello.I32_thing = 2, 'hello.I32_thing = '+IntToStr(hello.I32_thing));
856 Expect( hello.I64_thing = 2, 'hello.I64_thing = '+IntToStr(hello.I64_thing));
857 Expect( hello.__isset_String_thing, 'hello.__isset_String_thing = '+BoolToString(hello.__isset_String_thing));
858 Expect( hello.__isset_Byte_thing, 'hello.__isset_Byte_thing = '+BoolToString(hello.__isset_Byte_thing));
859 Expect( hello.__isset_I32_thing, 'hello.__isset_I32_thing = '+BoolToString(hello.__isset_I32_thing));
860 Expect( hello.__isset_I64_thing, 'hello.__isset_I64_thing = '+BoolToString(hello.__isset_I64_thing));
861 end;
862
863
864 // multi args
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200865 StartTestGroup( 'testMulti', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000866 arg0 := 1;
867 arg1 := 2;
868 arg2 := High(Int64);
869 arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
870 arg3.AddOrSetValue( 1, 'one');
871 arg4 := TNumberz.FIVE;
872 arg5 := 5000000;
873 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
874 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
875 arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
876 IntToStr( arg5) + ')');
877
878 i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5);
879 Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"');
880 Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing));
881 Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing));
882 Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing));
883 Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
884 Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
885 Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
886 Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
887
888 // multi exception
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200889 StartTestGroup( 'testMultiException(1)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000890 try
891 i := client.testMultiException( 'need more pizza', 'run out of beer');
892 Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"');
893 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
Jens Geyer6bbbf192014-09-07 01:45:56 +0200894 { this is not necessarily true, these fields are default-serialized
Jens Geyerd5436f52014-10-03 19:50:38 +0200895 Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
Roger Meier3bef8c22012-10-06 06:58:00 +0000896 Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
897 Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
Jens Geyerd5436f52014-10-03 19:50:38 +0200898 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000899 except
900 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
901 end;
902
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200903 StartTestGroup( 'testMultiException(Xception)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000904 try
905 i := client.testMultiException( 'Xception', 'second test');
906 Expect( FALSE, 'testMultiException(''Xception''): must trow an exception');
907 except
908 on x:TXception do begin
909 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
910 Expect( x.__isset_Message_, 'x.__isset_Message_ = '+BoolToString(x.__isset_Message_));
911 Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
912 Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"');
913 end;
914 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
915 end;
916
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200917 StartTestGroup( 'testMultiException(Xception2)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000918 try
919 i := client.testMultiException( 'Xception2', 'third test');
920 Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception');
921 except
922 on x:TXception2 do begin
923 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
924 Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing));
925 Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
926 Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"');
927 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 +0200928 { this is not necessarily true, these fields are default-serialized
Roger Meier3bef8c22012-10-06 06:58:00 +0000929 Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing));
930 Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing));
931 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 +0200932 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000933 end;
934 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
935 end;
936
937
938 // oneway functions
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200939 StartTestGroup( 'Test Oneway(1)', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000940 client.testOneway(1);
941 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
942
943 // call time
Jens Geyer06045cf2013-03-27 20:26:25 +0200944 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000945 StartTestGroup( 'Test Calltime()');
946 StartTick := GetTIckCount;
947 for k := 0 to 1000 - 1 do
948 begin
949 client.testVoid();
950 end;
951 Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
Jens Geyer06045cf2013-03-27 20:26:25 +0200952 {$ENDIF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000953
954 // no more tests here
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200955 StartTestGroup( '', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000956end;
957
958
Jens Geyer718f6ee2013-09-06 21:02:34 +0200959{$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200960procedure TClientThread.StressTest(const client : TThriftTest.Iface);
961begin
962 while TRUE do begin
963 try
964 if not FTransport.IsOpen then FTransport.Open; // re-open connection, server has already closed
965 try
966 client.testString('Test');
967 Write('.');
968 finally
969 if FTransport.IsOpen then FTransport.Close;
970 end;
971 except
972 on e:Exception do Writeln(#10+e.message);
973 end;
974 end;
975end;
Jens Geyer718f6ee2013-09-06 21:02:34 +0200976{$ENDIF}
Jens Geyer06045cf2013-03-27 20:26:25 +0200977
Jens Geyerfd1b3582014-12-13 23:42:58 +0100978
979function TClientThread.PrepareBinaryData( aRandomDist : Boolean = FALSE) : TBytes;
980var i, nextPos : Integer;
981begin
982 SetLength( result, $100);
983 ASSERT( Low(result) = 0);
984
985 // linear distribution, unless random is requested
986 if not aRandomDist then begin
987 for i := Low(result) to High(result) do begin
988 result[i] := i;
989 end;
990 Exit;
991 end;
992
993 // random distribution of all 256 values
994 FillChar( result[0], Length(result) * SizeOf(result[0]), $0);
995 i := 1;
996 while i < Length(result) do begin
997 nextPos := Byte( Random($100));
998 if result[nextPos] = 0 then begin // unused?
999 result[nextPos] := i;
1000 Inc(i);
1001 end;
1002 end;
1003end;
1004
1005
Roger Meier3bef8c22012-10-06 06:58:00 +00001006procedure TClientThread.JSONProtocolReadWriteTest;
1007// Tests only then read/write procedures of the JSON protocol
1008// All tests succeed, if we can read what we wrote before
1009// Note that passing this test does not imply, that our JSON is really compatible to what
1010// other clients or servers expect as the real JSON. This is beyond the scope of this test.
1011var prot : IProtocol;
1012 stm : TStringStream;
1013 list : IList;
1014 binary, binRead : TBytes;
1015 i,iErr : Integer;
1016const
1017 TEST_SHORT = ShortInt( $FE);
1018 TEST_SMALL = SmallInt( $FEDC);
1019 TEST_LONG = LongInt( $FEDCBA98);
1020 TEST_I64 = Int64( $FEDCBA9876543210);
1021 TEST_DOUBLE = -1.234e-56;
1022 DELTA_DOUBLE = TEST_DOUBLE * 1e-14;
1023 TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars")
Jens Geyer7bb44a32014-02-07 22:24:37 +01001024 // Test THRIFT-2336 with 'Русское Название';
1025 RUSSIAN_TEXT = #$0420#$0443#$0441#$0441#$043a#$043e#$0435' '#$041d#$0430#$0437#$0432#$0430#$043d#$0438#$0435;
1026 RUSSIAN_JSON = '"\u0420\u0443\u0441\u0441\u043a\u043e\u0435 \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435"';
Jens Geyer21366942013-12-30 22:04:51 +01001027 // test both possible solidus encodings
1028 SOLIDUS_JSON_DATA = '"one/two\/three"';
1029 SOLIDUS_EXCPECTED = 'one/two/three';
Roger Meier3bef8c22012-10-06 06:58:00 +00001030begin
1031 stm := TStringStream.Create;
1032 try
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001033 StartTestGroup( 'JsonProtocolTest', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001034
1035 // prepare binary data
Jens Geyerfd1b3582014-12-13 23:42:58 +01001036 binary := PrepareBinaryData( FALSE);
Roger Meier3bef8c22012-10-06 06:58:00 +00001037
1038 // output setup
1039 prot := TJSONProtocolImpl.Create(
1040 TStreamTransportImpl.Create(
1041 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
1042
1043 // write
1044 prot.WriteListBegin( TListImpl.Create( TType.String_, 9));
1045 prot.WriteBool( TRUE);
1046 prot.WriteBool( FALSE);
1047 prot.WriteByte( TEST_SHORT);
1048 prot.WriteI16( TEST_SMALL);
1049 prot.WriteI32( TEST_LONG);
1050 prot.WriteI64( TEST_I64);
1051 prot.WriteDouble( TEST_DOUBLE);
1052 prot.WriteString( TEST_STRING);
1053 prot.WriteBinary( binary);
1054 prot.WriteListEnd;
1055
1056 // input setup
1057 Expect( stm.Position = stm.Size, 'Stream position/length after write');
1058 stm.Position := 0;
1059 prot := TJSONProtocolImpl.Create(
1060 TStreamTransportImpl.Create(
1061 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1062
1063 // read and compare
1064 list := prot.ReadListBegin;
1065 Expect( list.ElementType = TType.String_, 'list element type');
1066 Expect( list.Count = 9, 'list element count');
1067 Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE');
1068 Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE');
1069 Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte');
1070 Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16');
1071 Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32');
1072 Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64');
1073 Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble');
1074 Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString');
1075 binRead := prot.ReadBinary;
1076 prot.ReadListEnd;
1077
1078 // test binary data
1079 Expect( Length(binary) = Length(binRead), 'Binary data length check');
1080 iErr := -1;
1081 for i := Low(binary) to High(binary) do begin
1082 if binary[i] <> binRead[i] then begin
1083 iErr := i;
1084 Break;
1085 end;
1086 end;
1087 if iErr < 0
1088 then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)')
1089 else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr));
1090
1091 Expect( stm.Position = stm.Size, 'Stream position after read');
1092
Jens Geyer7bb44a32014-02-07 22:24:37 +01001093
Jens Geyer21366942013-12-30 22:04:51 +01001094 // Solidus can be encoded in two ways. Make sure we can read both
1095 stm.Position := 0;
1096 stm.Size := 0;
1097 stm.WriteString(SOLIDUS_JSON_DATA);
1098 stm.Position := 0;
1099 prot := TJSONProtocolImpl.Create(
1100 TStreamTransportImpl.Create(
1101 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1102 Expect( prot.ReadString = SOLIDUS_EXCPECTED, 'Solidus encoding');
1103
1104
Jens Geyer7bb44a32014-02-07 22:24:37 +01001105 // Widechars should work too. Do they?
1106 // After writing, we ensure that we are able to read it back
1107 // We can't assume hex-encoding, since (nearly) any Unicode char is valid JSON
1108 stm.Position := 0;
1109 stm.Size := 0;
1110 prot := TJSONProtocolImpl.Create(
1111 TStreamTransportImpl.Create(
1112 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
1113 prot.WriteString( RUSSIAN_TEXT);
1114 stm.Position := 0;
1115 prot := TJSONProtocolImpl.Create(
1116 TStreamTransportImpl.Create(
1117 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1118 Expect( prot.ReadString = RUSSIAN_TEXT, 'Writing JSON with chars > 8 bit');
1119
1120 // Widechars should work with hex-encoding too. Do they?
1121 stm.Position := 0;
1122 stm.Size := 0;
1123 stm.WriteString( RUSSIAN_JSON);
1124 stm.Position := 0;
1125 prot := TJSONProtocolImpl.Create(
1126 TStreamTransportImpl.Create(
1127 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1128 Expect( prot.ReadString = RUSSIAN_TEXT, 'Reading JSON with chars > 8 bit');
1129
1130
Roger Meier3bef8c22012-10-06 06:58:00 +00001131 finally
1132 stm.Free;
1133 prot := nil; //-> Release
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001134 StartTestGroup( '', test_Unknown); // no more tests here
Roger Meier3bef8c22012-10-06 06:58:00 +00001135 end;
1136end;
1137
1138
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001139procedure TClientThread.StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +00001140begin
1141 FTestGroup := aGroup;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001142 FCurrentTest := aTest;
1143
1144 Include( FExecuted, aTest);
1145
Roger Meier3bef8c22012-10-06 06:58:00 +00001146 if FTestGroup <> '' then begin
1147 Console.WriteLine('');
1148 Console.WriteLine( aGroup+' tests');
1149 Console.WriteLine( StringOfChar('-',60));
1150 end;
1151end;
1152
1153
1154procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
1155begin
1156 if aTestResult then begin
1157 Inc(FSuccesses);
1158 Console.WriteLine( aTestInfo+': passed');
1159 end
1160 else begin
1161 FErrors.Add( FTestGroup+': '+aTestInfo);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001162 Include( FFailed, FCurrentTest);
Roger Meier3bef8c22012-10-06 06:58:00 +00001163 Console.WriteLine( aTestInfo+': *** FAILED ***');
1164
1165 // We have a failed test!
1166 // -> issue DebugBreak ONLY if a debugger is attached,
1167 // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise
1168 if IsDebuggerPresent then asm int 3 end;
1169 end;
1170end;
1171
1172
1173procedure TClientThread.ReportResults;
1174var nTotal : Integer;
1175 sLine : string;
1176begin
1177 // prevent us from stupid DIV/0 errors
1178 nTotal := FSuccesses + FErrors.Count;
1179 if nTotal = 0 then begin
1180 Console.WriteLine('No results logged');
1181 Exit;
1182 end;
1183
1184 Console.WriteLine('');
1185 Console.WriteLine( StringOfChar('=',60));
1186 Console.WriteLine( IntToStr(nTotal)+' tests performed');
1187 Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)');
1188 Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)');
1189 Console.WriteLine( StringOfChar('=',60));
1190 if FErrors.Count > 0 then begin
1191 Console.WriteLine('FAILED TESTS:');
1192 for sLine in FErrors do Console.WriteLine('- '+sLine);
1193 Console.WriteLine( StringOfChar('=',60));
1194 InterlockedIncrement( ExitCode); // return <> 0 on errors
1195 end;
1196 Console.WriteLine('');
1197end;
1198
1199
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001200function TClientThread.CalculateExitCode : Byte;
1201var test : TTestGroup;
1202begin
1203 result := EXITCODE_SUCCESS;
1204 for test := Low(TTestGroup) to High(TTestGroup) do begin
1205 if (test in FFailed) or not (test in FExecuted)
1206 then result := result or MAP_FAILURES_TO_EXITCODE_BITS[test];
1207 end;
1208end;
1209
1210
Roger Meier3bef8c22012-10-06 06:58:00 +00001211constructor TClientThread.Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
1212begin
1213 inherited Create( True );
1214 FNumIteration := ANumIteration;
1215 FTransport := ATransport;
1216 FProtocol := AProtocol;
1217 FConsole := TThreadConsole.Create( Self );
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001218 FCurrentTest := test_Unknown;
Roger Meier3bef8c22012-10-06 06:58:00 +00001219
1220 // error list: keep correct order, allow for duplicates
1221 FErrors := TStringList.Create;
1222 FErrors.Sorted := FALSE;
1223 FErrors.Duplicates := dupAccept;
1224end;
1225
1226destructor TClientThread.Destroy;
1227begin
1228 FreeAndNil( FConsole);
1229 FreeAndNil( FErrors);
1230 inherited;
1231end;
1232
1233procedure TClientThread.Execute;
1234var
1235 i : Integer;
1236 proc : TThreadProcedure;
1237begin
1238 // perform all tests
1239 try
Jens Geyer7bb44a32014-02-07 22:24:37 +01001240 JSONProtocolReadWriteTest;
Roger Meier3bef8c22012-10-06 06:58:00 +00001241 for i := 0 to FNumIteration - 1 do
1242 begin
1243 ClientTest;
Roger Meier3bef8c22012-10-06 06:58:00 +00001244 end;
1245 except
1246 on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"');
1247 end;
1248
1249 // report the outcome
1250 ReportResults;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001251 SetReturnValue( CalculateExitCode);
Roger Meier3bef8c22012-10-06 06:58:00 +00001252
1253 // shutdown
1254 proc := procedure
1255 begin
1256 if FTransport <> nil then
1257 begin
1258 FTransport.Close;
1259 FTransport := nil;
1260 end;
1261 end;
1262
1263 Synchronize( proc );
1264end;
1265
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001266
Roger Meier3bef8c22012-10-06 06:58:00 +00001267{ TThreadConsole }
1268
1269constructor TThreadConsole.Create(AThread: TThread);
1270begin
Jens Geyer718f6ee2013-09-06 21:02:34 +02001271 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +00001272 FThread := AThread;
1273end;
1274
1275procedure TThreadConsole.Write(const S: string);
1276var
1277 proc : TThreadProcedure;
1278begin
1279 proc := procedure
1280 begin
1281 Console.Write( S );
1282 end;
1283 TThread.Synchronize( FThread, proc);
1284end;
1285
1286procedure TThreadConsole.WriteLine(const S: string);
1287var
1288 proc : TThreadProcedure;
1289begin
1290 proc := procedure
1291 begin
1292 Console.WriteLine( S );
1293 end;
1294 TThread.Synchronize( FThread, proc);
1295end;
1296
1297initialization
1298begin
1299 TTestClient.FNumIteration := 1;
1300 TTestClient.FNumThread := 1;
1301end;
1302
1303end.