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