blob: 08b3965738e57d583cce02e64182886978f90684 [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
513 s := client.testString('Test');
514 Expect( s = 'Test', 'testString(''Test'') = "'+s+'"');
515
Jens Geyer06045cf2013-03-27 20:26:25 +0200516 s := client.testString(HUGE_TEST_STRING);
517 Expect( length(s) = length(HUGE_TEST_STRING),
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100518 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
Jens Geyer06045cf2013-03-27 20:26:25 +0200519 +'=> length(result) = '+IntToStr(Length(s)));
520
Roger Meier3bef8c22012-10-06 06:58:00 +0000521 i8 := client.testByte(1);
522 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
523
524 i32 := client.testI32(-1);
525 Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32));
526
527 Console.WriteLine('testI64(-34359738368)');
528 i64 := client.testI64(-34359738368);
529 Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
530
Jens Geyerfd1b3582014-12-13 23:42:58 +0100531 binOut := PrepareBinaryData( TRUE);
532 Console.WriteLine('testBinary('+BytesToHex(binOut)+')');
533 try
534 binIn := client.testBinary(binOut);
535 Expect( Length(binOut) = Length(binIn), 'testBinary(): length '+IntToStr(Length(binOut))+' = '+IntToStr(Length(binIn)));
536 i32 := Min( Length(binOut), Length(binIn));
537 Expect( CompareMem( binOut, binIn, i32), 'testBinary('+BytesToHex(binOut)+') = '+BytesToHex(binIn));
538 except
539 on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message);
540 on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message);
541 end;
542
Roger Meier3bef8c22012-10-06 06:58:00 +0000543 Console.WriteLine('testDouble(5.325098235)');
544 dub := client.testDouble(5.325098235);
545 Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
546
547 // structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200548 StartTestGroup( 'testStruct', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000549 Console.WriteLine('testStruct({''Zero'', 1, -3, -5})');
550 o := TXtructImpl.Create;
551 o.String_thing := 'Zero';
552 o.Byte_thing := 1;
553 o.I32_thing := -3;
554 o.I64_thing := -5;
555 i := client.testStruct(o);
556 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
557 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
558 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
559 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
560 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
561 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
562 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
563 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
564
565 // nested structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200566 StartTestGroup( 'testNest', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000567 Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})');
568 o2 := TXtruct2Impl.Create;
569 o2.Byte_thing := 1;
570 o2.Struct_thing := o;
571 o2.I32_thing := 5;
572 i2 := client.testNest(o2);
573 i := i2.Struct_thing;
574 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
575 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
576 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
577 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
578 Expect( i2.Byte_thing = 1, 'i2.Byte_thing = '+IntToStr(i2.Byte_thing));
579 Expect( i2.I32_thing = 5, 'i2.I32_thing = '+IntToStr(i2.I32_thing));
580 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
581 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
582 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
583 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
584 Expect( i2.__isset_Byte_thing, 'i2.__isset_Byte_thing');
585 Expect( i2.__isset_I32_thing, 'i2.__isset_I32_thing');
586
587 // map<type1,type2>: A map of strictly unique keys to values.
588 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200589 StartTestGroup( 'testMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000590 mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
591 for j := 0 to 4 do
592 begin
593 mapout.AddOrSetValue( j, j - 10);
594 end;
595 Console.Write('testMap({');
596 first := True;
597 for key in mapout.Keys do
598 begin
599 if first
600 then first := False
601 else Console.Write( ', ' );
602 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
603 end;
604 Console.WriteLine('})');
605
606 mapin := client.testMap( mapout );
607 Expect( mapin.Count = mapout.Count, 'testMap: mapin.Count = mapout.Count');
608 for j := 0 to 4 do
609 begin
610 Expect( mapout.ContainsKey(j), 'testMap: mapout.ContainsKey('+IntToStr(j)+') = '+BoolToString(mapout.ContainsKey(j)));
611 end;
612 for key in mapin.Keys do
613 begin
614 Expect( mapin[key] = mapout[key], 'testMap: '+IntToStr(key) + ' => ' + IntToStr( mapin[key]));
615 Expect( mapin[key] = key - 10, 'testMap: mapin['+IntToStr(key)+'] = '+IntToStr( mapin[key]));
616 end;
617
618
619 // map<type1,type2>: A map of strictly unique keys to values.
620 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200621 StartTestGroup( 'testStringMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000622 strmapout := TThriftDictionaryImpl<string,string>.Create;
623 for j := 0 to 4 do
624 begin
625 strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10));
626 end;
627 Console.Write('testStringMap({');
628 first := True;
629 for strkey in strmapout.Keys do
630 begin
631 if first
632 then first := False
633 else Console.Write( ', ' );
634 Console.Write( strkey + ' => ' + strmapout[strkey]);
635 end;
636 Console.WriteLine('})');
637
638 strmapin := client.testStringMap( strmapout );
639 Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count');
640 for j := 0 to 4 do
641 begin
642 Expect( strmapout.ContainsKey(IntToStr(j)),
643 'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = '
644 + BoolToString(strmapout.ContainsKey(IntToStr(j))));
645 end;
646 for strkey in strmapin.Keys do
647 begin
648 Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]);
649 Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]);
650 end;
651
652
653 // set<type>: An unordered set of unique elements.
654 // Translates to an STL set, Java HashSet, set in Python, etc.
655 // Note: PHP does not support sets, so it is treated similar to a List
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200656 StartTestGroup( 'testSet', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000657 setout := THashSetImpl<Integer>.Create;
658 for j := -2 to 2 do
659 begin
660 setout.Add( j );
661 end;
662 Console.Write('testSet({');
663 first := True;
664 for j in setout do
665 begin
666 if first
667 then first := False
668 else Console.Write(', ');
669 Console.Write(IntToStr( j));
670 end;
671 Console.WriteLine('})');
672
673 setin := client.testSet(setout);
674 Expect( setin.Count = setout.Count, 'testSet: setin.Count = setout.Count');
675 Expect( setin.Count = 5, 'testSet: setin.Count = '+IntToStr(setin.Count));
676 for j := -2 to 2 do // unordered, we can't rely on the order => test for known elements only
677 begin
678 Expect( setin.Contains(j), 'testSet: setin.Contains('+IntToStr(j)+') => '+BoolToString(setin.Contains(j)));
679 end;
680
681 // list<type>: An ordered list of elements.
682 // Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200683 StartTestGroup( 'testList', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000684 listout := TThriftListImpl<Integer>.Create;
685 listout.Add( +1);
686 listout.Add( -2);
687 listout.Add( +3);
688 listout.Add( -4);
689 listout.Add( 0);
690 Console.Write('testList({');
691 first := True;
692 for j in listout do
693 begin
694 if first
695 then first := False
696 else Console.Write(', ');
697 Console.Write(IntToStr( j));
698 end;
699 Console.WriteLine('})');
700
701 listin := client.testList(listout);
702 Expect( listin.Count = listout.Count, 'testList: listin.Count = listout.Count');
703 Expect( listin.Count = 5, 'testList: listin.Count = '+IntToStr(listin.Count));
704 Expect( listin[0] = +1, 'listin[0] = '+IntToStr( listin[0]));
705 Expect( listin[1] = -2, 'listin[1] = '+IntToStr( listin[1]));
706 Expect( listin[2] = +3, 'listin[2] = '+IntToStr( listin[2]));
707 Expect( listin[3] = -4, 'listin[3] = '+IntToStr( listin[3]));
708 Expect( listin[4] = 0, 'listin[4] = '+IntToStr( listin[4]));
709
710 // enums
711 ret := client.testEnum(TNumberz.ONE);
712 Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret)));
713
714 ret := client.testEnum(TNumberz.TWO);
715 Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret)));
716
717 ret := client.testEnum(TNumberz.THREE);
718 Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret)));
719
720 ret := client.testEnum(TNumberz.FIVE);
721 Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret)));
722
723 ret := client.testEnum(TNumberz.EIGHT);
724 Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret)));
725
726
727 // typedef
728 uid := client.testTypedef(309858235082523);
729 Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid));
730
731
732 // maps of maps
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200733 StartTestGroup( 'testMapMap(1)', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000734 mm := client.testMapMap(1);
735 Console.Write(' = {');
736 for key in mm.Keys do
737 begin
738 Console.Write( IntToStr( key) + ' => {');
739 m2 := mm[key];
740 for k2 in m2.Keys do
741 begin
742 Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', ');
743 end;
744 Console.Write('}, ');
745 end;
746 Console.WriteLine('}');
747
748 // verify result data
749 Expect( mm.Count = 2, 'mm.Count = '+IntToStr(mm.Count));
750 pos := mm[4];
751 neg := mm[-4];
752 for j := 1 to 4 do
753 begin
754 Expect( pos[j] = j, 'pos[j] = '+IntToStr(pos[j]));
755 Expect( neg[-j] = -j, 'neg[-j] = '+IntToStr(neg[-j]));
756 end;
757
758
759
760 // insanity
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200761 StartTestGroup( 'testInsanity', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000762 insane := TInsanityImpl.Create;
763 insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
764 insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000);
765 truck := TXtructImpl.Create;
766 truck.String_thing := 'Truck';
767 truck.Byte_thing := 8;
768 truck.I32_thing := 8;
769 truck.I64_thing := 8;
770 insane.Xtructs := TThriftListImpl<IXtruct>.Create;
771 insane.Xtructs.Add( truck );
772 whoa := client.testInsanity( insane );
773 Console.Write(' = {');
774 for key64 in whoa.Keys do
775 begin
776 val := whoa[key64];
777 Console.Write( IntToStr( key64) + ' => {');
778 for k2_2 in val.Keys do
779 begin
780 v2 := val[k2_2];
781 Console.Write( IntToStr( Integer( k2_2)) + ' => {');
782 userMap := v2.UserMap;
783 Console.Write('{');
784 if userMap <> nil then
785 begin
786 for k3 in userMap.Keys do
787 begin
788 Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', ');
789 end;
790 end else
791 begin
792 Console.Write('null');
793 end;
794 Console.Write('}, ');
795 xtructs := v2.Xtructs;
796 Console.Write('{');
797
798 if xtructs <> nil then
799 begin
800 for x in xtructs do
801 begin
802 Console.Write('{"' + x.String_thing + '", ' +
803 IntToStr( x.Byte_thing) + ', ' +
804 IntToStr( x.I32_thing) + ', ' +
805 IntToStr( x.I32_thing) + '}, ');
806 end;
807 end else
808 begin
809 Console.Write('null');
810 end;
811 Console.Write('}');
812 Console.Write('}, ');
813 end;
814 Console.Write('}, ');
815 end;
816 Console.WriteLine('}');
817
818 // verify result data
819 Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count));
820 //
821 first_map := whoa[1];
822 second_map := whoa[2];
823 Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count));
824 Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count));
825 //
826 looney := second_map[TNumberz.SIX];
827 Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney)));
828 Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
829 Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
830 //
831 for ret in [TNumberz.TWO, TNumberz.THREE] do begin
832 crazy := first_map[ret];
833 Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
834
835 Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap));
836 Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs));
837
838 Expect( crazy.UserMap.Count = 2, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count));
839 Expect( crazy.UserMap[TNumberz.FIVE] = 5, 'crazy.UserMap[TNumberz.FIVE] = '+IntToStr(crazy.UserMap[TNumberz.FIVE]));
840 Expect( crazy.UserMap[TNumberz.EIGHT] = 8, 'crazy.UserMap[TNumberz.EIGHT] = '+IntToStr(crazy.UserMap[TNumberz.EIGHT]));
841
842 Expect( crazy.Xtructs.Count = 2, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count));
843 goodbye := crazy.Xtructs[0]; // lists are ordered, so we are allowed to assume this order
Jens Geyerd5436f52014-10-03 19:50:38 +0200844 hello := crazy.Xtructs[1];
Roger Meier3bef8c22012-10-06 06:58:00 +0000845
846 Expect( goodbye.String_thing = 'Goodbye4', 'goodbye.String_thing = "'+goodbye.String_thing+'"');
847 Expect( goodbye.Byte_thing = 4, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing));
848 Expect( goodbye.I32_thing = 4, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing));
849 Expect( goodbye.I64_thing = 4, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing));
850 Expect( goodbye.__isset_String_thing, 'goodbye.__isset_String_thing = '+BoolToString(goodbye.__isset_String_thing));
851 Expect( goodbye.__isset_Byte_thing, 'goodbye.__isset_Byte_thing = '+BoolToString(goodbye.__isset_Byte_thing));
852 Expect( goodbye.__isset_I32_thing, 'goodbye.__isset_I32_thing = '+BoolToString(goodbye.__isset_I32_thing));
853 Expect( goodbye.__isset_I64_thing, 'goodbye.__isset_I64_thing = '+BoolToString(goodbye.__isset_I64_thing));
854
855 Expect( hello.String_thing = 'Hello2', 'hello.String_thing = "'+hello.String_thing+'"');
856 Expect( hello.Byte_thing = 2, 'hello.Byte_thing = '+IntToStr(hello.Byte_thing));
857 Expect( hello.I32_thing = 2, 'hello.I32_thing = '+IntToStr(hello.I32_thing));
858 Expect( hello.I64_thing = 2, 'hello.I64_thing = '+IntToStr(hello.I64_thing));
859 Expect( hello.__isset_String_thing, 'hello.__isset_String_thing = '+BoolToString(hello.__isset_String_thing));
860 Expect( hello.__isset_Byte_thing, 'hello.__isset_Byte_thing = '+BoolToString(hello.__isset_Byte_thing));
861 Expect( hello.__isset_I32_thing, 'hello.__isset_I32_thing = '+BoolToString(hello.__isset_I32_thing));
862 Expect( hello.__isset_I64_thing, 'hello.__isset_I64_thing = '+BoolToString(hello.__isset_I64_thing));
863 end;
864
865
866 // multi args
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200867 StartTestGroup( 'testMulti', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000868 arg0 := 1;
869 arg1 := 2;
870 arg2 := High(Int64);
871 arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
872 arg3.AddOrSetValue( 1, 'one');
873 arg4 := TNumberz.FIVE;
874 arg5 := 5000000;
875 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
876 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
877 arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
878 IntToStr( arg5) + ')');
879
880 i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5);
881 Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"');
882 Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing));
883 Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing));
884 Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing));
885 Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
886 Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
887 Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
888 Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
889
890 // multi exception
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200891 StartTestGroup( 'testMultiException(1)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000892 try
893 i := client.testMultiException( 'need more pizza', 'run out of beer');
894 Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"');
895 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
Jens Geyer6bbbf192014-09-07 01:45:56 +0200896 { this is not necessarily true, these fields are default-serialized
Jens Geyerd5436f52014-10-03 19:50:38 +0200897 Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
Roger Meier3bef8c22012-10-06 06:58:00 +0000898 Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
899 Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
Jens Geyerd5436f52014-10-03 19:50:38 +0200900 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000901 except
902 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
903 end;
904
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200905 StartTestGroup( 'testMultiException(Xception)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000906 try
907 i := client.testMultiException( 'Xception', 'second test');
908 Expect( FALSE, 'testMultiException(''Xception''): must trow an exception');
909 except
910 on x:TXception do begin
911 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
912 Expect( x.__isset_Message_, 'x.__isset_Message_ = '+BoolToString(x.__isset_Message_));
913 Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
914 Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"');
915 end;
916 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
917 end;
918
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200919 StartTestGroup( 'testMultiException(Xception2)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000920 try
921 i := client.testMultiException( 'Xception2', 'third test');
922 Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception');
923 except
924 on x:TXception2 do begin
925 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
926 Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing));
927 Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
928 Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"');
929 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 +0200930 { this is not necessarily true, these fields are default-serialized
Roger Meier3bef8c22012-10-06 06:58:00 +0000931 Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing));
932 Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing));
933 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 +0200934 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000935 end;
936 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
937 end;
938
939
940 // oneway functions
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200941 StartTestGroup( 'Test Oneway(1)', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000942 client.testOneway(1);
943 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
944
945 // call time
Jens Geyer06045cf2013-03-27 20:26:25 +0200946 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000947 StartTestGroup( 'Test Calltime()');
948 StartTick := GetTIckCount;
949 for k := 0 to 1000 - 1 do
950 begin
951 client.testVoid();
952 end;
953 Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
Jens Geyer06045cf2013-03-27 20:26:25 +0200954 {$ENDIF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000955
956 // no more tests here
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200957 StartTestGroup( '', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000958end;
959
960
Jens Geyer718f6ee2013-09-06 21:02:34 +0200961{$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200962procedure TClientThread.StressTest(const client : TThriftTest.Iface);
963begin
964 while TRUE do begin
965 try
966 if not FTransport.IsOpen then FTransport.Open; // re-open connection, server has already closed
967 try
968 client.testString('Test');
969 Write('.');
970 finally
971 if FTransport.IsOpen then FTransport.Close;
972 end;
973 except
974 on e:Exception do Writeln(#10+e.message);
975 end;
976 end;
977end;
Jens Geyer718f6ee2013-09-06 21:02:34 +0200978{$ENDIF}
Jens Geyer06045cf2013-03-27 20:26:25 +0200979
Jens Geyerfd1b3582014-12-13 23:42:58 +0100980
981function TClientThread.PrepareBinaryData( aRandomDist : Boolean = FALSE) : TBytes;
982var i, nextPos : Integer;
983begin
984 SetLength( result, $100);
985 ASSERT( Low(result) = 0);
986
987 // linear distribution, unless random is requested
988 if not aRandomDist then begin
989 for i := Low(result) to High(result) do begin
990 result[i] := i;
991 end;
992 Exit;
993 end;
994
995 // random distribution of all 256 values
996 FillChar( result[0], Length(result) * SizeOf(result[0]), $0);
997 i := 1;
998 while i < Length(result) do begin
999 nextPos := Byte( Random($100));
1000 if result[nextPos] = 0 then begin // unused?
1001 result[nextPos] := i;
1002 Inc(i);
1003 end;
1004 end;
1005end;
1006
1007
Roger Meier3bef8c22012-10-06 06:58:00 +00001008procedure TClientThread.JSONProtocolReadWriteTest;
1009// Tests only then read/write procedures of the JSON protocol
1010// All tests succeed, if we can read what we wrote before
1011// Note that passing this test does not imply, that our JSON is really compatible to what
1012// other clients or servers expect as the real JSON. This is beyond the scope of this test.
1013var prot : IProtocol;
1014 stm : TStringStream;
1015 list : IList;
1016 binary, binRead : TBytes;
1017 i,iErr : Integer;
1018const
1019 TEST_SHORT = ShortInt( $FE);
1020 TEST_SMALL = SmallInt( $FEDC);
1021 TEST_LONG = LongInt( $FEDCBA98);
1022 TEST_I64 = Int64( $FEDCBA9876543210);
1023 TEST_DOUBLE = -1.234e-56;
1024 DELTA_DOUBLE = TEST_DOUBLE * 1e-14;
1025 TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars")
Jens Geyer7bb44a32014-02-07 22:24:37 +01001026 // Test THRIFT-2336 with 'Русское Название';
1027 RUSSIAN_TEXT = #$0420#$0443#$0441#$0441#$043a#$043e#$0435' '#$041d#$0430#$0437#$0432#$0430#$043d#$0438#$0435;
1028 RUSSIAN_JSON = '"\u0420\u0443\u0441\u0441\u043a\u043e\u0435 \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435"';
Jens Geyer21366942013-12-30 22:04:51 +01001029 // test both possible solidus encodings
1030 SOLIDUS_JSON_DATA = '"one/two\/three"';
1031 SOLIDUS_EXCPECTED = 'one/two/three';
Roger Meier3bef8c22012-10-06 06:58:00 +00001032begin
1033 stm := TStringStream.Create;
1034 try
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001035 StartTestGroup( 'JsonProtocolTest', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001036
1037 // prepare binary data
Jens Geyerfd1b3582014-12-13 23:42:58 +01001038 binary := PrepareBinaryData( FALSE);
Roger Meier3bef8c22012-10-06 06:58:00 +00001039
1040 // output setup
1041 prot := TJSONProtocolImpl.Create(
1042 TStreamTransportImpl.Create(
1043 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
1044
1045 // write
1046 prot.WriteListBegin( TListImpl.Create( TType.String_, 9));
1047 prot.WriteBool( TRUE);
1048 prot.WriteBool( FALSE);
1049 prot.WriteByte( TEST_SHORT);
1050 prot.WriteI16( TEST_SMALL);
1051 prot.WriteI32( TEST_LONG);
1052 prot.WriteI64( TEST_I64);
1053 prot.WriteDouble( TEST_DOUBLE);
1054 prot.WriteString( TEST_STRING);
1055 prot.WriteBinary( binary);
1056 prot.WriteListEnd;
1057
1058 // input setup
1059 Expect( stm.Position = stm.Size, 'Stream position/length after write');
1060 stm.Position := 0;
1061 prot := TJSONProtocolImpl.Create(
1062 TStreamTransportImpl.Create(
1063 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1064
1065 // read and compare
1066 list := prot.ReadListBegin;
1067 Expect( list.ElementType = TType.String_, 'list element type');
1068 Expect( list.Count = 9, 'list element count');
1069 Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE');
1070 Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE');
1071 Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte');
1072 Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16');
1073 Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32');
1074 Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64');
1075 Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble');
1076 Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString');
1077 binRead := prot.ReadBinary;
1078 prot.ReadListEnd;
1079
1080 // test binary data
1081 Expect( Length(binary) = Length(binRead), 'Binary data length check');
1082 iErr := -1;
1083 for i := Low(binary) to High(binary) do begin
1084 if binary[i] <> binRead[i] then begin
1085 iErr := i;
1086 Break;
1087 end;
1088 end;
1089 if iErr < 0
1090 then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)')
1091 else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr));
1092
1093 Expect( stm.Position = stm.Size, 'Stream position after read');
1094
Jens Geyer7bb44a32014-02-07 22:24:37 +01001095
Jens Geyer21366942013-12-30 22:04:51 +01001096 // Solidus can be encoded in two ways. Make sure we can read both
1097 stm.Position := 0;
1098 stm.Size := 0;
1099 stm.WriteString(SOLIDUS_JSON_DATA);
1100 stm.Position := 0;
1101 prot := TJSONProtocolImpl.Create(
1102 TStreamTransportImpl.Create(
1103 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1104 Expect( prot.ReadString = SOLIDUS_EXCPECTED, 'Solidus encoding');
1105
1106
Jens Geyer7bb44a32014-02-07 22:24:37 +01001107 // Widechars should work too. Do they?
1108 // After writing, we ensure that we are able to read it back
1109 // We can't assume hex-encoding, since (nearly) any Unicode char is valid JSON
1110 stm.Position := 0;
1111 stm.Size := 0;
1112 prot := TJSONProtocolImpl.Create(
1113 TStreamTransportImpl.Create(
1114 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
1115 prot.WriteString( RUSSIAN_TEXT);
1116 stm.Position := 0;
1117 prot := TJSONProtocolImpl.Create(
1118 TStreamTransportImpl.Create(
1119 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1120 Expect( prot.ReadString = RUSSIAN_TEXT, 'Writing JSON with chars > 8 bit');
1121
1122 // Widechars should work with hex-encoding too. Do they?
1123 stm.Position := 0;
1124 stm.Size := 0;
1125 stm.WriteString( RUSSIAN_JSON);
1126 stm.Position := 0;
1127 prot := TJSONProtocolImpl.Create(
1128 TStreamTransportImpl.Create(
1129 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1130 Expect( prot.ReadString = RUSSIAN_TEXT, 'Reading JSON with chars > 8 bit');
1131
1132
Roger Meier3bef8c22012-10-06 06:58:00 +00001133 finally
1134 stm.Free;
1135 prot := nil; //-> Release
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001136 StartTestGroup( '', test_Unknown); // no more tests here
Roger Meier3bef8c22012-10-06 06:58:00 +00001137 end;
1138end;
1139
1140
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001141procedure TClientThread.StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +00001142begin
1143 FTestGroup := aGroup;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001144 FCurrentTest := aTest;
1145
1146 Include( FExecuted, aTest);
1147
Roger Meier3bef8c22012-10-06 06:58:00 +00001148 if FTestGroup <> '' then begin
1149 Console.WriteLine('');
1150 Console.WriteLine( aGroup+' tests');
1151 Console.WriteLine( StringOfChar('-',60));
1152 end;
1153end;
1154
1155
1156procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
1157begin
1158 if aTestResult then begin
1159 Inc(FSuccesses);
1160 Console.WriteLine( aTestInfo+': passed');
1161 end
1162 else begin
1163 FErrors.Add( FTestGroup+': '+aTestInfo);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001164 Include( FFailed, FCurrentTest);
Roger Meier3bef8c22012-10-06 06:58:00 +00001165 Console.WriteLine( aTestInfo+': *** FAILED ***');
1166
1167 // We have a failed test!
1168 // -> issue DebugBreak ONLY if a debugger is attached,
1169 // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise
1170 if IsDebuggerPresent then asm int 3 end;
1171 end;
1172end;
1173
1174
1175procedure TClientThread.ReportResults;
1176var nTotal : Integer;
1177 sLine : string;
1178begin
1179 // prevent us from stupid DIV/0 errors
1180 nTotal := FSuccesses + FErrors.Count;
1181 if nTotal = 0 then begin
1182 Console.WriteLine('No results logged');
1183 Exit;
1184 end;
1185
1186 Console.WriteLine('');
1187 Console.WriteLine( StringOfChar('=',60));
1188 Console.WriteLine( IntToStr(nTotal)+' tests performed');
1189 Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)');
1190 Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)');
1191 Console.WriteLine( StringOfChar('=',60));
1192 if FErrors.Count > 0 then begin
1193 Console.WriteLine('FAILED TESTS:');
1194 for sLine in FErrors do Console.WriteLine('- '+sLine);
1195 Console.WriteLine( StringOfChar('=',60));
1196 InterlockedIncrement( ExitCode); // return <> 0 on errors
1197 end;
1198 Console.WriteLine('');
1199end;
1200
1201
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001202function TClientThread.CalculateExitCode : Byte;
1203var test : TTestGroup;
1204begin
1205 result := EXITCODE_SUCCESS;
1206 for test := Low(TTestGroup) to High(TTestGroup) do begin
1207 if (test in FFailed) or not (test in FExecuted)
1208 then result := result or MAP_FAILURES_TO_EXITCODE_BITS[test];
1209 end;
1210end;
1211
1212
Roger Meier3bef8c22012-10-06 06:58:00 +00001213constructor TClientThread.Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
1214begin
1215 inherited Create( True );
1216 FNumIteration := ANumIteration;
1217 FTransport := ATransport;
1218 FProtocol := AProtocol;
1219 FConsole := TThreadConsole.Create( Self );
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001220 FCurrentTest := test_Unknown;
Roger Meier3bef8c22012-10-06 06:58:00 +00001221
1222 // error list: keep correct order, allow for duplicates
1223 FErrors := TStringList.Create;
1224 FErrors.Sorted := FALSE;
1225 FErrors.Duplicates := dupAccept;
1226end;
1227
1228destructor TClientThread.Destroy;
1229begin
1230 FreeAndNil( FConsole);
1231 FreeAndNil( FErrors);
1232 inherited;
1233end;
1234
1235procedure TClientThread.Execute;
1236var
1237 i : Integer;
1238 proc : TThreadProcedure;
1239begin
1240 // perform all tests
1241 try
Jens Geyer7bb44a32014-02-07 22:24:37 +01001242 JSONProtocolReadWriteTest;
Roger Meier3bef8c22012-10-06 06:58:00 +00001243 for i := 0 to FNumIteration - 1 do
1244 begin
1245 ClientTest;
Roger Meier3bef8c22012-10-06 06:58:00 +00001246 end;
1247 except
1248 on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"');
1249 end;
1250
1251 // report the outcome
1252 ReportResults;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001253 SetReturnValue( CalculateExitCode);
Roger Meier3bef8c22012-10-06 06:58:00 +00001254
1255 // shutdown
1256 proc := procedure
1257 begin
1258 if FTransport <> nil then
1259 begin
1260 FTransport.Close;
1261 FTransport := nil;
1262 end;
1263 end;
1264
1265 Synchronize( proc );
1266end;
1267
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001268
Roger Meier3bef8c22012-10-06 06:58:00 +00001269{ TThreadConsole }
1270
1271constructor TThreadConsole.Create(AThread: TThread);
1272begin
Jens Geyer718f6ee2013-09-06 21:02:34 +02001273 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +00001274 FThread := AThread;
1275end;
1276
1277procedure TThreadConsole.Write(const S: string);
1278var
1279 proc : TThreadProcedure;
1280begin
1281 proc := procedure
1282 begin
1283 Console.Write( S );
1284 end;
1285 TThread.Synchronize( FThread, proc);
1286end;
1287
1288procedure TThreadConsole.WriteLine(const S: string);
1289var
1290 proc : TThreadProcedure;
1291begin
1292 proc := procedure
1293 begin
1294 Console.WriteLine( S );
1295 end;
1296 TThread.Synchronize( FThread, proc);
1297end;
1298
1299initialization
1300begin
1301 TTestClient.FNumIteration := 1;
1302 TTestClient.FNumThread := 1;
1303end;
1304
1305end.