blob: 9f45a91ddfccf456e40e012589205338dfc9add0 [file] [log] [blame]
Jens Geyer7bb44a32014-02-07 22:24:37 +01001(*
Roger Meier3bef8c22012-10-06 06:58:00 +00002 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *)
19
20unit TestClient;
21
Jens Geyer9f7f11e2016-04-14 21:37:11 +020022{$I ../src/Thrift.Defines.inc}
23
Jens Geyer06045cf2013-03-27 20:26:25 +020024{.$DEFINE StressTest} // activate to stress-test the server with frequent connects/disconnects
25{.$DEFINE PerfTest} // activate to activate the performance test
26
Roger Meier3bef8c22012-10-06 06:58:00 +000027interface
28
29uses
Jens Geyerfd1b3582014-12-13 23:42:58 +010030 Windows, SysUtils, Classes, Math,
Roger Meier3bef8c22012-10-06 06:58:00 +000031 DateUtils,
32 Generics.Collections,
33 TestConstants,
34 Thrift,
Jens Geyerf0e63312015-03-01 18:47:49 +010035 Thrift.Protocol.Compact,
Roger Meier3bef8c22012-10-06 06:58:00 +000036 Thrift.Protocol.JSON,
37 Thrift.Protocol,
38 Thrift.Transport.Pipes,
39 Thrift.Transport,
40 Thrift.Stream,
41 Thrift.Test,
42 Thrift.Collections,
43 Thrift.Console;
44
45type
46 TThreadConsole = class
47 private
48 FThread : TThread;
49 public
50 procedure Write( const S : string);
51 procedure WriteLine( const S : string);
52 constructor Create( AThread: TThread);
53 end;
54
55 TClientThread = class( TThread )
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020056 private type
57 TTestGroup = (
58 test_Unknown,
59 test_BaseTypes,
60 test_Structs,
61 test_Containers,
62 test_Exceptions
63 // new values here
64 );
65 TTestGroups = set of TTestGroup;
66
Roger Meier3bef8c22012-10-06 06:58:00 +000067 private
68 FTransport : ITransport;
69 FProtocol : IProtocol;
70 FNumIteration : Integer;
71 FConsole : TThreadConsole;
72
73 // test reporting, will be refactored out into separate class later
74 FTestGroup : string;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020075 FCurrentTest : TTestGroup;
Roger Meier3bef8c22012-10-06 06:58:00 +000076 FSuccesses : Integer;
77 FErrors : TStringList;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020078 FFailed : TTestGroups;
79 FExecuted : TTestGroups;
80 procedure StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +000081 procedure Expect( aTestResult : Boolean; const aTestInfo : string);
82 procedure ReportResults;
Jens Geyerfd1b3582014-12-13 23:42:58 +010083 function CalculateExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +000084
85 procedure ClientTest;
86 procedure JSONProtocolReadWriteTest;
Jens Geyerfd1b3582014-12-13 23:42:58 +010087 function PrepareBinaryData( aRandomDist : Boolean = FALSE) : TBytes;
Jens Geyer718f6ee2013-09-06 21:02:34 +020088 {$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +020089 procedure StressTest(const client : TThriftTest.Iface);
Jens Geyer718f6ee2013-09-06 21:02:34 +020090 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +000091 protected
92 procedure Execute; override;
93 public
94 constructor Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
95 destructor Destroy; override;
96 end;
97
98 TTestClient = class
99 private
100 class var
101 FNumIteration : Integer;
102 FNumThread : Integer;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200103
104 class procedure PrintCmdLineHelp;
105 class procedure InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000106 public
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200107 class function Execute( const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000108 end;
109
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200110
Roger Meier3bef8c22012-10-06 06:58:00 +0000111implementation
112
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200113const
114 EXITCODE_SUCCESS = $00; // no errors bits set
115 //
116 EXITCODE_FAILBIT_BASETYPES = $01;
117 EXITCODE_FAILBIT_STRUCTS = $02;
118 EXITCODE_FAILBIT_CONTAINERS = $04;
119 EXITCODE_FAILBIT_EXCEPTIONS = $08;
120
121 MAP_FAILURES_TO_EXITCODE_BITS : array[TClientThread.TTestGroup] of Byte = (
122 EXITCODE_SUCCESS, // no bits here
123 EXITCODE_FAILBIT_BASETYPES,
124 EXITCODE_FAILBIT_STRUCTS,
125 EXITCODE_FAILBIT_CONTAINERS,
126 EXITCODE_FAILBIT_EXCEPTIONS
127 );
128
129
130
Roger Meier3bef8c22012-10-06 06:58:00 +0000131function BoolToString( b : Boolean) : string;
132// overrides global BoolToString()
133begin
134 if b
135 then result := 'true'
136 else result := 'false';
137end;
138
139// not available in all versions, so make sure we have this one imported
140function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent';
141
142{ TTestClient }
143
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200144class procedure TTestClient.PrintCmdLineHelp;
145const HELPTEXT = ' [options]'#10
146 + #10
147 + 'Allowed options:'#10
148 + ' -h [ --help ] produce help message'#10
149 + ' --host arg (=localhost) Host to connect'#10
150 + ' --port arg (=9090) Port number to connect'#10
151 + ' --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift),'#10
152 + ' instead of host and port'#10
153 + ' --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)'#10
154 + ' --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)'#10
155 + ' --transport arg (=sockets) Transport: buffered, framed, http, evhttp'#10
156 + ' --protocol arg (=binary) Protocol: binary, compact, json'#10
157 + ' --ssl Encrypted Transport using SSL'#10
158 + ' -n [ --testloops ] arg (=1) Number of Tests'#10
159 + ' -t [ --threads ] arg (=1) Number of Test threads'#10
160 ;
161begin
162 Writeln( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + HELPTEXT);
163end;
164
165class procedure TTestClient.InvalidArgs;
166begin
167 Console.WriteLine( 'Invalid args.');
168 Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + ' -h for more information');
169 Abort;
170end;
171
172class function TTestClient.Execute(const args: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000173var
174 i : Integer;
175 host : string;
176 port : Integer;
Roger Meier3bef8c22012-10-06 06:58:00 +0000177 sPipeName : string;
178 hAnonRead, hAnonWrite : THandle;
179 s : string;
Roger Meier3bef8c22012-10-06 06:58:00 +0000180 threads : array of TThread;
181 dtStart : TDateTime;
182 test : Integer;
183 thread : TThread;
184 trans : ITransport;
185 prot : IProtocol;
186 streamtrans : IStreamTransport;
187 http : IHTTPClient;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200188 protType : TKnownProtocol;
189 endpoint : TEndpointTransport;
190 layered : TLayeredTransports;
191 UseSSL : Boolean; // include where appropriate (TLayeredTransport?)
Jens Geyer0b20cc82013-03-07 20:47:01 +0100192const
193 // pipe timeouts to be used
194 DEBUG_TIMEOUT = 30 * 1000;
Jens Geyer3e8d9272014-09-14 20:10:40 +0200195 RELEASE_TIMEOUT = DEFAULT_THRIFT_TIMEOUT;
Jens Geyer0b20cc82013-03-07 20:47:01 +0100196 TIMEOUT = RELEASE_TIMEOUT;
Roger Meier3bef8c22012-10-06 06:58:00 +0000197begin
Roger Meier3bef8c22012-10-06 06:58:00 +0000198 protType := prot_Binary;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200199 endpoint := trns_Sockets;
200 layered := [];
201 UseSSL := FALSE;
202 host := 'localhost';
203 port := 9090;
204 sPipeName := '';
205 hAnonRead := INVALID_HANDLE_VALUE;
206 hAnonWrite := INVALID_HANDLE_VALUE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000207 try
Roger Meier3bef8c22012-10-06 06:58:00 +0000208 i := 0;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200209 while ( i < Length(args) ) do begin
210 s := args[i];
211 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000212
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200213 if (s = '-h') or (s = '--help') then begin
214 // -h [ --help ] produce help message
215 PrintCmdLineHelp;
216 result := $FF; // all tests failed
217 Exit;
218 end
Jens Geyerb360b652014-09-28 01:55:46 +0200219 else if s = '--host' then begin
220 // --host arg (=localhost) Host to connect
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200221 host := args[i];
222 Inc( i);
223 end
Jens Geyerb360b652014-09-28 01:55:46 +0200224 else if s = '--port' then begin
225 // --port arg (=9090) Port number to connect
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200226 s := args[i];
227 Inc( i);
228 port := StrToIntDef(s,0);
229 if port <= 0 then InvalidArgs;
230 end
Jens Geyerb360b652014-09-28 01:55:46 +0200231 else if s = '--domain-socket' then begin
232 // --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200233 raise Exception.Create('domain-socket not supported');
234 end
Jens Geyerb360b652014-09-28 01:55:46 +0200235 else if s = '--named-pipe' then begin
236 // --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200237 endpoint := trns_NamedPipes;
238 sPipeName := args[i];
239 Inc( i);
240 end
Jens Geyerb360b652014-09-28 01:55:46 +0200241 else if s = '--anon-pipes' then begin
242 // --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200243 endpoint := trns_AnonPipes;
244 hAnonRead := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
245 Inc( i);
246 hAnonWrite := THandle( StrToIntDef( args[i], Integer(INVALID_HANDLE_VALUE)));
247 Inc( i);
248 end
Jens Geyerb360b652014-09-28 01:55:46 +0200249 else if s = '--transport' then begin
250 // --transport arg (=sockets) Transport: buffered, framed, http, evhttp
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200251 s := args[i];
252 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000253
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200254 if s = 'buffered' then Include( layered, trns_Buffered)
255 else if s = 'framed' then Include( layered, trns_Framed)
256 else if s = 'http' then endpoint := trns_Http
257 else if s = 'evhttp' then endpoint := trns_AnonPipes
258 else InvalidArgs;
259 end
Jens Geyerb360b652014-09-28 01:55:46 +0200260 else if s = '--protocol' then begin
261 // --protocol arg (=binary) Protocol: binary, compact, json
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200262 s := args[i];
263 Inc( i);
Roger Meier3bef8c22012-10-06 06:58:00 +0000264
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200265 if s = 'binary' then protType := prot_Binary
266 else if s = 'compact' then protType := prot_Compact
267 else if s = 'json' then protType := prot_JSON
268 else InvalidArgs;
269 end
Jens Geyerb360b652014-09-28 01:55:46 +0200270 else if s = '--ssl' then begin
271 // --ssl Encrypted Transport using SSL
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200272 UseSSL := TRUE;
273
274 end
275 else if (s = '-n') or (s = '--testloops') then begin
276 // -n [ --testloops ] arg (=1) Number of Tests
277 FNumIteration := StrToIntDef( args[i], 0);
278 Inc( i);
279 if FNumIteration <= 0
280 then InvalidArgs;
281
282 end
283 else if (s = '-t') or (s = '--threads') then begin
284 // -t [ --threads ] arg (=1) Number of Test threads
285 FNumThread := StrToIntDef( args[i], 0);
286 Inc( i);
287 if FNumThread <= 0
288 then InvalidArgs;
289 end
290 else begin
291 InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000292 end;
293 end;
294
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200295
Roger Meier79655fb2012-10-20 20:59:41 +0000296 // In the anonymous pipes mode the client is launched by the test server
297 // -> behave nicely and allow for attaching a debugger to this process
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200298 if (endpoint = trns_AnonPipes) and not IsDebuggerPresent
Roger Meier79655fb2012-10-20 20:59:41 +0000299 then MessageBox( 0, 'Attach Debugger and/or click OK to continue.',
300 'Thrift TestClient (Delphi)',
301 MB_OK or MB_ICONEXCLAMATION);
302
Roger Meier3bef8c22012-10-06 06:58:00 +0000303 SetLength( threads, FNumThread);
304 dtStart := Now;
305
306 for test := 0 to FNumThread - 1 do
307 begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200308 case endpoint of
309 trns_Sockets: begin
Roger Meier3bef8c22012-10-06 06:58:00 +0000310 Console.WriteLine('Using sockets ('+host+' port '+IntToStr(port)+')');
311 streamtrans := TSocketImpl.Create( host, port );
312 end;
313
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200314 trns_Http: begin
315 Console.WriteLine('Using HTTPClient');
316 http := THTTPClientImpl.Create( host);
317 trans := http;
Roger Meier3bef8c22012-10-06 06:58:00 +0000318 end;
319
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200320 trns_EvHttp: begin
321 raise Exception.Create(ENDPOINT_TRANSPORTS[endpoint]+' transport not implemented');
Roger Meier3bef8c22012-10-06 06:58:00 +0000322 end;
323
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200324 trns_NamedPipes: begin
325 Console.WriteLine('Using named pipe ('+sPipeName+')');
Jens Geyerae985dd2016-04-20 21:48:35 +0200326 streamtrans := TNamedPipeTransportClientEndImpl.Create( sPipeName, 0, nil, TIMEOUT, TIMEOUT);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200327 end;
328
329 trns_AnonPipes: begin
330 Console.WriteLine('Using anonymous pipes ('+IntToStr(Integer(hAnonRead))+' and '+IntToStr(Integer(hAnonWrite))+')');
331 streamtrans := TAnonymousPipeTransportImpl.Create( hAnonRead, hAnonWrite, FALSE);
332 end;
333
334 else
335 raise Exception.Create('Unhandled endpoint transport');
336 end;
337 trans := streamtrans;
338 ASSERT( trans <> nil);
339
340 if (trns_Buffered in layered) then begin
341 trans := TBufferedTransportImpl.Create( streamtrans, 32); // small buffer to test read()
342 Console.WriteLine('Using buffered transport');
343 end;
344
345 if (trns_Framed in layered) then begin
346 trans := TFramedTransportImpl.Create( trans );
347 Console.WriteLine('Using framed transport');
348 end;
349
350 if UseSSL then begin
351 raise Exception.Create('SSL not implemented');
Roger Meier3bef8c22012-10-06 06:58:00 +0000352 end;
353
354 // create protocol instance, default to BinaryProtocol
355 case protType of
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200356 prot_Binary : prot := TBinaryProtocolImpl.Create( trans, BINARY_STRICT_READ, BINARY_STRICT_WRITE);
357 prot_JSON : prot := TJSONProtocolImpl.Create( trans);
Jens Geyerf0e63312015-03-01 18:47:49 +0100358 prot_Compact : prot := TCompactProtocolImpl.Create( trans);
Roger Meier3bef8c22012-10-06 06:58:00 +0000359 else
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200360 raise Exception.Create('Unhandled protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000361 end;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200362 ASSERT( trans <> nil);
363 Console.WriteLine(THRIFT_PROTOCOLS[protType]+' protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000364
365 thread := TClientThread.Create( trans, prot, FNumIteration);
366 threads[test] := thread;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200367 thread.Start;
Roger Meier3bef8c22012-10-06 06:58:00 +0000368 end;
369
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200370 result := 0;
371 for test := 0 to FNumThread - 1 do begin
372 result := result or threads[test].WaitFor;
Roger Meier3bef8c22012-10-06 06:58:00 +0000373 end;
374
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200375 for test := 0 to FNumThread - 1
376 do threads[test].Free;
Roger Meier3bef8c22012-10-06 06:58:00 +0000377
378 Console.Write('Total time: ' + IntToStr( MilliSecondsBetween(Now, dtStart)));
379
380 except
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200381 on E: EAbort do raise;
382 on E: Exception do begin
383 Console.WriteLine( E.Message + #10 + E.StackTrace);
384 raise;
Roger Meier3bef8c22012-10-06 06:58:00 +0000385 end;
386 end;
387
388 Console.WriteLine('');
389 Console.WriteLine('done!');
390end;
391
392{ TClientThread }
393
394procedure TClientThread.ClientTest;
395var
396 client : TThriftTest.Iface;
397 s : string;
398 i8 : ShortInt;
399 i32 : Integer;
400 i64 : Int64;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100401 binOut,binIn : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000402 dub : Double;
403 o : IXtruct;
404 o2 : IXtruct2;
405 i : IXtruct;
406 i2 : IXtruct2;
407 mapout : IThriftDictionary<Integer,Integer>;
408 mapin : IThriftDictionary<Integer,Integer>;
409 strmapout : IThriftDictionary<string,string>;
410 strmapin : IThriftDictionary<string,string>;
411 j : Integer;
412 first : Boolean;
413 key : Integer;
414 strkey : string;
415 listout : IThriftList<Integer>;
416 listin : IThriftList<Integer>;
417 setout : IHashSet<Integer>;
418 setin : IHashSet<Integer>;
419 ret : TNumberz;
420 uid : Int64;
421 mm : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
422 pos : IThriftDictionary<Integer, Integer>;
423 neg : IThriftDictionary<Integer, Integer>;
424 m2 : IThriftDictionary<Integer, Integer>;
425 k2 : Integer;
426 insane : IInsanity;
427 truck : IXtruct;
428 whoa : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
429 key64 : Int64;
430 val : IThriftDictionary<TNumberz, IInsanity>;
431 k2_2 : TNumberz;
432 k3 : TNumberz;
433 v2 : IInsanity;
434 userMap : IThriftDictionary<TNumberz, Int64>;
435 xtructs : IThriftList<IXtruct>;
436 x : IXtruct;
437 arg0 : ShortInt;
438 arg1 : Integer;
439 arg2 : Int64;
440 arg3 : IThriftDictionary<SmallInt, string>;
441 arg4 : TNumberz;
442 arg5 : Int64;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200443 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000444 StartTick : Cardinal;
445 k : Integer;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200446 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000447 hello, goodbye : IXtruct;
448 crazy : IInsanity;
449 looney : IInsanity;
450 first_map : IThriftDictionary<TNumberz, IInsanity>;
451 second_map : IThriftDictionary<TNumberz, IInsanity>;
452
453begin
454 client := TThriftTest.TClient.Create( FProtocol);
455 FTransport.Open;
456
Jens Geyer06045cf2013-03-27 20:26:25 +0200457 {$IFDEF StressTest}
458 StressTest( client);
459 {$ENDIF StressTest}
460
Roger Meier3bef8c22012-10-06 06:58:00 +0000461 // in-depth exception test
462 // (1) do we get an exception at all?
463 // (2) do we get the right exception?
464 // (3) does the exception contain the expected data?
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200465 StartTestGroup( 'testException', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000466 // case 1: exception type declared in IDL at the function call
467 try
468 client.testException('Xception');
469 Expect( FALSE, 'testException(''Xception''): must trow an exception');
470 except
471 on e:TXception do begin
472 Expect( e.ErrorCode = 1001, 'error code');
473 Expect( e.Message_ = 'Xception', 'error message');
474 Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ );
475 end;
476 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
477 on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
478 end;
479
480 // case 2: exception type NOT declared in IDL at the function call
481 // this will close the connection
482 try
483 client.testException('TException');
484 Expect( FALSE, 'testException(''TException''): must trow an exception');
485 except
486 on e:TTransportException do begin
487 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Roger Meier3bef8c22012-10-06 06:58:00 +0000488 end;
Jens Geyer6bbbf192014-09-07 01:45:56 +0200489 on e:TApplicationException do begin
490 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Jens Geyer6bbbf192014-09-07 01:45:56 +0200491 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000492 on e:TException do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
493 on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
494 end;
495
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100496
Jens Geyer2ad6c302015-02-26 19:38:53 +0100497 if FTransport.IsOpen then FTransport.Close;
498 FTransport.Open; // re-open connection, server has already closed
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100499
Jens Geyer2ad6c302015-02-26 19:38:53 +0100500
Roger Meier3bef8c22012-10-06 06:58:00 +0000501 // case 3: no exception
502 try
503 client.testException('something');
504 Expect( TRUE, 'testException(''something''): must not trow an exception');
505 except
506 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
507 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
508 end;
509
510
511 // simple things
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200512 StartTestGroup( 'simple Thrift calls', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000513 client.testVoid();
514 Expect( TRUE, 'testVoid()'); // success := no exception
515
Jens Geyer39ba6b72015-09-22 00:00:49 +0200516 s := BoolToString( client.testBool(TRUE));
517 Expect( s = BoolToString(TRUE), 'testBool(TRUE) = '+s);
518 s := BoolToString( client.testBool(FALSE));
519 Expect( s = BoolToString(FALSE), 'testBool(FALSE) = '+s);
520
Roger Meier3bef8c22012-10-06 06:58:00 +0000521 s := client.testString('Test');
522 Expect( s = 'Test', 'testString(''Test'') = "'+s+'"');
523
Jens Geyer06045cf2013-03-27 20:26:25 +0200524 s := client.testString(HUGE_TEST_STRING);
525 Expect( length(s) = length(HUGE_TEST_STRING),
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100526 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
Jens Geyer06045cf2013-03-27 20:26:25 +0200527 +'=> length(result) = '+IntToStr(Length(s)));
528
Roger Meier3bef8c22012-10-06 06:58:00 +0000529 i8 := client.testByte(1);
530 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
531
532 i32 := client.testI32(-1);
533 Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32));
534
535 Console.WriteLine('testI64(-34359738368)');
536 i64 := client.testI64(-34359738368);
537 Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
538
Jens Geyerfd1b3582014-12-13 23:42:58 +0100539 binOut := PrepareBinaryData( TRUE);
540 Console.WriteLine('testBinary('+BytesToHex(binOut)+')');
541 try
542 binIn := client.testBinary(binOut);
543 Expect( Length(binOut) = Length(binIn), 'testBinary(): length '+IntToStr(Length(binOut))+' = '+IntToStr(Length(binIn)));
544 i32 := Min( Length(binOut), Length(binIn));
545 Expect( CompareMem( binOut, binIn, i32), 'testBinary('+BytesToHex(binOut)+') = '+BytesToHex(binIn));
546 except
547 on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message);
548 on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message);
549 end;
550
Roger Meier3bef8c22012-10-06 06:58:00 +0000551 Console.WriteLine('testDouble(5.325098235)');
552 dub := client.testDouble(5.325098235);
553 Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
554
555 // structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200556 StartTestGroup( 'testStruct', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000557 Console.WriteLine('testStruct({''Zero'', 1, -3, -5})');
558 o := TXtructImpl.Create;
559 o.String_thing := 'Zero';
560 o.Byte_thing := 1;
561 o.I32_thing := -3;
562 o.I64_thing := -5;
563 i := client.testStruct(o);
564 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
565 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
566 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
567 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
568 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
569 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
570 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
571 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
572
573 // nested structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200574 StartTestGroup( 'testNest', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000575 Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})');
576 o2 := TXtruct2Impl.Create;
577 o2.Byte_thing := 1;
578 o2.Struct_thing := o;
579 o2.I32_thing := 5;
580 i2 := client.testNest(o2);
581 i := i2.Struct_thing;
582 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
583 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
584 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
585 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
586 Expect( i2.Byte_thing = 1, 'i2.Byte_thing = '+IntToStr(i2.Byte_thing));
587 Expect( i2.I32_thing = 5, 'i2.I32_thing = '+IntToStr(i2.I32_thing));
588 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
589 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
590 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
591 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
592 Expect( i2.__isset_Byte_thing, 'i2.__isset_Byte_thing');
593 Expect( i2.__isset_I32_thing, 'i2.__isset_I32_thing');
594
595 // map<type1,type2>: A map of strictly unique keys to values.
596 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200597 StartTestGroup( 'testMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000598 mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
599 for j := 0 to 4 do
600 begin
601 mapout.AddOrSetValue( j, j - 10);
602 end;
603 Console.Write('testMap({');
604 first := True;
605 for key in mapout.Keys do
606 begin
607 if first
608 then first := False
609 else Console.Write( ', ' );
610 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
611 end;
612 Console.WriteLine('})');
613
614 mapin := client.testMap( mapout );
615 Expect( mapin.Count = mapout.Count, 'testMap: mapin.Count = mapout.Count');
616 for j := 0 to 4 do
617 begin
618 Expect( mapout.ContainsKey(j), 'testMap: mapout.ContainsKey('+IntToStr(j)+') = '+BoolToString(mapout.ContainsKey(j)));
619 end;
620 for key in mapin.Keys do
621 begin
622 Expect( mapin[key] = mapout[key], 'testMap: '+IntToStr(key) + ' => ' + IntToStr( mapin[key]));
623 Expect( mapin[key] = key - 10, 'testMap: mapin['+IntToStr(key)+'] = '+IntToStr( mapin[key]));
624 end;
625
626
627 // map<type1,type2>: A map of strictly unique keys to values.
628 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200629 StartTestGroup( 'testStringMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000630 strmapout := TThriftDictionaryImpl<string,string>.Create;
631 for j := 0 to 4 do
632 begin
633 strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10));
634 end;
635 Console.Write('testStringMap({');
636 first := True;
637 for strkey in strmapout.Keys do
638 begin
639 if first
640 then first := False
641 else Console.Write( ', ' );
642 Console.Write( strkey + ' => ' + strmapout[strkey]);
643 end;
644 Console.WriteLine('})');
645
646 strmapin := client.testStringMap( strmapout );
647 Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count');
648 for j := 0 to 4 do
649 begin
650 Expect( strmapout.ContainsKey(IntToStr(j)),
651 'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = '
652 + BoolToString(strmapout.ContainsKey(IntToStr(j))));
653 end;
654 for strkey in strmapin.Keys do
655 begin
656 Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]);
657 Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]);
658 end;
659
660
661 // set<type>: An unordered set of unique elements.
662 // Translates to an STL set, Java HashSet, set in Python, etc.
663 // Note: PHP does not support sets, so it is treated similar to a List
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200664 StartTestGroup( 'testSet', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000665 setout := THashSetImpl<Integer>.Create;
666 for j := -2 to 2 do
667 begin
668 setout.Add( j );
669 end;
670 Console.Write('testSet({');
671 first := True;
672 for j in setout do
673 begin
674 if first
675 then first := False
676 else Console.Write(', ');
677 Console.Write(IntToStr( j));
678 end;
679 Console.WriteLine('})');
680
681 setin := client.testSet(setout);
682 Expect( setin.Count = setout.Count, 'testSet: setin.Count = setout.Count');
683 Expect( setin.Count = 5, 'testSet: setin.Count = '+IntToStr(setin.Count));
684 for j := -2 to 2 do // unordered, we can't rely on the order => test for known elements only
685 begin
686 Expect( setin.Contains(j), 'testSet: setin.Contains('+IntToStr(j)+') => '+BoolToString(setin.Contains(j)));
687 end;
688
689 // list<type>: An ordered list of elements.
690 // Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200691 StartTestGroup( 'testList', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000692 listout := TThriftListImpl<Integer>.Create;
693 listout.Add( +1);
694 listout.Add( -2);
695 listout.Add( +3);
696 listout.Add( -4);
697 listout.Add( 0);
698 Console.Write('testList({');
699 first := True;
700 for j in listout do
701 begin
702 if first
703 then first := False
704 else Console.Write(', ');
705 Console.Write(IntToStr( j));
706 end;
707 Console.WriteLine('})');
708
709 listin := client.testList(listout);
710 Expect( listin.Count = listout.Count, 'testList: listin.Count = listout.Count');
711 Expect( listin.Count = 5, 'testList: listin.Count = '+IntToStr(listin.Count));
712 Expect( listin[0] = +1, 'listin[0] = '+IntToStr( listin[0]));
713 Expect( listin[1] = -2, 'listin[1] = '+IntToStr( listin[1]));
714 Expect( listin[2] = +3, 'listin[2] = '+IntToStr( listin[2]));
715 Expect( listin[3] = -4, 'listin[3] = '+IntToStr( listin[3]));
716 Expect( listin[4] = 0, 'listin[4] = '+IntToStr( listin[4]));
717
718 // enums
719 ret := client.testEnum(TNumberz.ONE);
720 Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret)));
721
722 ret := client.testEnum(TNumberz.TWO);
723 Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret)));
724
725 ret := client.testEnum(TNumberz.THREE);
726 Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret)));
727
728 ret := client.testEnum(TNumberz.FIVE);
729 Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret)));
730
731 ret := client.testEnum(TNumberz.EIGHT);
732 Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret)));
733
734
735 // typedef
736 uid := client.testTypedef(309858235082523);
737 Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid));
738
739
740 // maps of maps
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200741 StartTestGroup( 'testMapMap(1)', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000742 mm := client.testMapMap(1);
743 Console.Write(' = {');
744 for key in mm.Keys do
745 begin
746 Console.Write( IntToStr( key) + ' => {');
747 m2 := mm[key];
748 for k2 in m2.Keys do
749 begin
750 Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', ');
751 end;
752 Console.Write('}, ');
753 end;
754 Console.WriteLine('}');
755
756 // verify result data
757 Expect( mm.Count = 2, 'mm.Count = '+IntToStr(mm.Count));
758 pos := mm[4];
759 neg := mm[-4];
760 for j := 1 to 4 do
761 begin
762 Expect( pos[j] = j, 'pos[j] = '+IntToStr(pos[j]));
763 Expect( neg[-j] = -j, 'neg[-j] = '+IntToStr(neg[-j]));
764 end;
765
766
767
768 // insanity
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200769 StartTestGroup( 'testInsanity', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000770 insane := TInsanityImpl.Create;
771 insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
772 insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000);
773 truck := TXtructImpl.Create;
774 truck.String_thing := 'Truck';
775 truck.Byte_thing := 8;
776 truck.I32_thing := 8;
777 truck.I64_thing := 8;
778 insane.Xtructs := TThriftListImpl<IXtruct>.Create;
779 insane.Xtructs.Add( truck );
780 whoa := client.testInsanity( insane );
781 Console.Write(' = {');
782 for key64 in whoa.Keys do
783 begin
784 val := whoa[key64];
785 Console.Write( IntToStr( key64) + ' => {');
786 for k2_2 in val.Keys do
787 begin
788 v2 := val[k2_2];
789 Console.Write( IntToStr( Integer( k2_2)) + ' => {');
790 userMap := v2.UserMap;
791 Console.Write('{');
792 if userMap <> nil then
793 begin
794 for k3 in userMap.Keys do
795 begin
796 Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', ');
797 end;
798 end else
799 begin
800 Console.Write('null');
801 end;
802 Console.Write('}, ');
803 xtructs := v2.Xtructs;
804 Console.Write('{');
805
806 if xtructs <> nil then
807 begin
808 for x in xtructs do
809 begin
810 Console.Write('{"' + x.String_thing + '", ' +
811 IntToStr( x.Byte_thing) + ', ' +
812 IntToStr( x.I32_thing) + ', ' +
813 IntToStr( x.I32_thing) + '}, ');
814 end;
815 end else
816 begin
817 Console.Write('null');
818 end;
819 Console.Write('}');
820 Console.Write('}, ');
821 end;
822 Console.Write('}, ');
823 end;
824 Console.WriteLine('}');
825
826 // verify result data
827 Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count));
828 //
829 first_map := whoa[1];
830 second_map := whoa[2];
831 Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count));
832 Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count));
833 //
834 looney := second_map[TNumberz.SIX];
835 Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney)));
836 Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
837 Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
838 //
839 for ret in [TNumberz.TWO, TNumberz.THREE] do begin
840 crazy := first_map[ret];
841 Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
842
843 Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap));
844 Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs));
845
846 Expect( crazy.UserMap.Count = 2, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count));
847 Expect( crazy.UserMap[TNumberz.FIVE] = 5, 'crazy.UserMap[TNumberz.FIVE] = '+IntToStr(crazy.UserMap[TNumberz.FIVE]));
848 Expect( crazy.UserMap[TNumberz.EIGHT] = 8, 'crazy.UserMap[TNumberz.EIGHT] = '+IntToStr(crazy.UserMap[TNumberz.EIGHT]));
849
850 Expect( crazy.Xtructs.Count = 2, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count));
851 goodbye := crazy.Xtructs[0]; // lists are ordered, so we are allowed to assume this order
Jens Geyerd5436f52014-10-03 19:50:38 +0200852 hello := crazy.Xtructs[1];
Roger Meier3bef8c22012-10-06 06:58:00 +0000853
854 Expect( goodbye.String_thing = 'Goodbye4', 'goodbye.String_thing = "'+goodbye.String_thing+'"');
855 Expect( goodbye.Byte_thing = 4, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing));
856 Expect( goodbye.I32_thing = 4, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing));
857 Expect( goodbye.I64_thing = 4, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing));
858 Expect( goodbye.__isset_String_thing, 'goodbye.__isset_String_thing = '+BoolToString(goodbye.__isset_String_thing));
859 Expect( goodbye.__isset_Byte_thing, 'goodbye.__isset_Byte_thing = '+BoolToString(goodbye.__isset_Byte_thing));
860 Expect( goodbye.__isset_I32_thing, 'goodbye.__isset_I32_thing = '+BoolToString(goodbye.__isset_I32_thing));
861 Expect( goodbye.__isset_I64_thing, 'goodbye.__isset_I64_thing = '+BoolToString(goodbye.__isset_I64_thing));
862
863 Expect( hello.String_thing = 'Hello2', 'hello.String_thing = "'+hello.String_thing+'"');
864 Expect( hello.Byte_thing = 2, 'hello.Byte_thing = '+IntToStr(hello.Byte_thing));
865 Expect( hello.I32_thing = 2, 'hello.I32_thing = '+IntToStr(hello.I32_thing));
866 Expect( hello.I64_thing = 2, 'hello.I64_thing = '+IntToStr(hello.I64_thing));
867 Expect( hello.__isset_String_thing, 'hello.__isset_String_thing = '+BoolToString(hello.__isset_String_thing));
868 Expect( hello.__isset_Byte_thing, 'hello.__isset_Byte_thing = '+BoolToString(hello.__isset_Byte_thing));
869 Expect( hello.__isset_I32_thing, 'hello.__isset_I32_thing = '+BoolToString(hello.__isset_I32_thing));
870 Expect( hello.__isset_I64_thing, 'hello.__isset_I64_thing = '+BoolToString(hello.__isset_I64_thing));
871 end;
872
873
874 // multi args
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200875 StartTestGroup( 'testMulti', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000876 arg0 := 1;
877 arg1 := 2;
878 arg2 := High(Int64);
879 arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
880 arg3.AddOrSetValue( 1, 'one');
881 arg4 := TNumberz.FIVE;
882 arg5 := 5000000;
883 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
884 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
885 arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
886 IntToStr( arg5) + ')');
887
888 i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5);
889 Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"');
890 Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing));
891 Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing));
892 Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing));
893 Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
894 Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
895 Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
896 Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
897
898 // multi exception
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200899 StartTestGroup( 'testMultiException(1)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000900 try
901 i := client.testMultiException( 'need more pizza', 'run out of beer');
902 Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"');
903 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
Jens Geyer6bbbf192014-09-07 01:45:56 +0200904 { this is not necessarily true, these fields are default-serialized
Jens Geyerd5436f52014-10-03 19:50:38 +0200905 Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
Roger Meier3bef8c22012-10-06 06:58:00 +0000906 Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
907 Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
Jens Geyerd5436f52014-10-03 19:50:38 +0200908 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000909 except
910 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
911 end;
912
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200913 StartTestGroup( 'testMultiException(Xception)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000914 try
915 i := client.testMultiException( 'Xception', 'second test');
916 Expect( FALSE, 'testMultiException(''Xception''): must trow an exception');
917 except
918 on x:TXception do begin
919 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
920 Expect( x.__isset_Message_, 'x.__isset_Message_ = '+BoolToString(x.__isset_Message_));
921 Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
922 Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"');
923 end;
924 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
925 end;
926
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200927 StartTestGroup( 'testMultiException(Xception2)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000928 try
929 i := client.testMultiException( 'Xception2', 'third test');
930 Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception');
931 except
932 on x:TXception2 do begin
933 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
934 Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing));
935 Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
936 Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"');
937 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 +0200938 { this is not necessarily true, these fields are default-serialized
Roger Meier3bef8c22012-10-06 06:58:00 +0000939 Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing));
940 Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing));
941 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 +0200942 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000943 end;
944 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
945 end;
946
947
948 // oneway functions
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200949 StartTestGroup( 'Test Oneway(1)', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000950 client.testOneway(1);
951 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
952
953 // call time
Jens Geyer06045cf2013-03-27 20:26:25 +0200954 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000955 StartTestGroup( 'Test Calltime()');
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200956 StartTick := GetTickCount;
Roger Meier3bef8c22012-10-06 06:58:00 +0000957 for k := 0 to 1000 - 1 do
958 begin
959 client.testVoid();
960 end;
961 Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
Jens Geyer06045cf2013-03-27 20:26:25 +0200962 {$ENDIF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000963
964 // no more tests here
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200965 StartTestGroup( '', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000966end;
967
968
Jens Geyer718f6ee2013-09-06 21:02:34 +0200969{$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200970procedure TClientThread.StressTest(const client : TThriftTest.Iface);
971begin
972 while TRUE do begin
973 try
974 if not FTransport.IsOpen then FTransport.Open; // re-open connection, server has already closed
975 try
976 client.testString('Test');
977 Write('.');
978 finally
979 if FTransport.IsOpen then FTransport.Close;
980 end;
981 except
982 on e:Exception do Writeln(#10+e.message);
983 end;
984 end;
985end;
Jens Geyer718f6ee2013-09-06 21:02:34 +0200986{$ENDIF}
Jens Geyer06045cf2013-03-27 20:26:25 +0200987
Jens Geyerfd1b3582014-12-13 23:42:58 +0100988
989function TClientThread.PrepareBinaryData( aRandomDist : Boolean = FALSE) : TBytes;
990var i, nextPos : Integer;
991begin
992 SetLength( result, $100);
993 ASSERT( Low(result) = 0);
994
995 // linear distribution, unless random is requested
996 if not aRandomDist then begin
997 for i := Low(result) to High(result) do begin
998 result[i] := i;
999 end;
1000 Exit;
1001 end;
1002
1003 // random distribution of all 256 values
1004 FillChar( result[0], Length(result) * SizeOf(result[0]), $0);
1005 i := 1;
1006 while i < Length(result) do begin
1007 nextPos := Byte( Random($100));
1008 if result[nextPos] = 0 then begin // unused?
1009 result[nextPos] := i;
1010 Inc(i);
1011 end;
1012 end;
1013end;
1014
1015
Roger Meier3bef8c22012-10-06 06:58:00 +00001016procedure TClientThread.JSONProtocolReadWriteTest;
1017// Tests only then read/write procedures of the JSON protocol
1018// All tests succeed, if we can read what we wrote before
1019// Note that passing this test does not imply, that our JSON is really compatible to what
1020// other clients or servers expect as the real JSON. This is beyond the scope of this test.
1021var prot : IProtocol;
1022 stm : TStringStream;
1023 list : IList;
1024 binary, binRead : TBytes;
1025 i,iErr : Integer;
1026const
1027 TEST_SHORT = ShortInt( $FE);
1028 TEST_SMALL = SmallInt( $FEDC);
1029 TEST_LONG = LongInt( $FEDCBA98);
1030 TEST_I64 = Int64( $FEDCBA9876543210);
1031 TEST_DOUBLE = -1.234e-56;
1032 DELTA_DOUBLE = TEST_DOUBLE * 1e-14;
1033 TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars")
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001034 // Test THRIFT-2336 and THRIFT-3404 with U+1D11E (G Clef symbol) and 'Русское Название';
1035 G_CLEF_AND_CYRILLIC_TEXT = #$1d11e' '#$0420#$0443#$0441#$0441#$043a#$043e#$0435' '#$041d#$0430#$0437#$0432#$0430#$043d#$0438#$0435;
1036 G_CLEF_AND_CYRILLIC_JSON = '"\ud834\udd1e \u0420\u0443\u0441\u0441\u043a\u043e\u0435 \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435"';
Jens Geyer21366942013-12-30 22:04:51 +01001037 // test both possible solidus encodings
1038 SOLIDUS_JSON_DATA = '"one/two\/three"';
1039 SOLIDUS_EXCPECTED = 'one/two/three';
Roger Meier3bef8c22012-10-06 06:58:00 +00001040begin
1041 stm := TStringStream.Create;
1042 try
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001043 StartTestGroup( 'JsonProtocolTest', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001044
1045 // prepare binary data
Jens Geyerfd1b3582014-12-13 23:42:58 +01001046 binary := PrepareBinaryData( FALSE);
Roger Meier3bef8c22012-10-06 06:58:00 +00001047
1048 // output setup
1049 prot := TJSONProtocolImpl.Create(
1050 TStreamTransportImpl.Create(
1051 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
1052
1053 // write
1054 prot.WriteListBegin( TListImpl.Create( TType.String_, 9));
1055 prot.WriteBool( TRUE);
1056 prot.WriteBool( FALSE);
1057 prot.WriteByte( TEST_SHORT);
1058 prot.WriteI16( TEST_SMALL);
1059 prot.WriteI32( TEST_LONG);
1060 prot.WriteI64( TEST_I64);
1061 prot.WriteDouble( TEST_DOUBLE);
1062 prot.WriteString( TEST_STRING);
1063 prot.WriteBinary( binary);
1064 prot.WriteListEnd;
1065
1066 // input setup
1067 Expect( stm.Position = stm.Size, 'Stream position/length after write');
1068 stm.Position := 0;
1069 prot := TJSONProtocolImpl.Create(
1070 TStreamTransportImpl.Create(
1071 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1072
1073 // read and compare
1074 list := prot.ReadListBegin;
1075 Expect( list.ElementType = TType.String_, 'list element type');
1076 Expect( list.Count = 9, 'list element count');
1077 Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE');
1078 Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE');
1079 Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte');
1080 Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16');
1081 Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32');
1082 Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64');
1083 Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble');
1084 Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString');
1085 binRead := prot.ReadBinary;
1086 prot.ReadListEnd;
1087
1088 // test binary data
1089 Expect( Length(binary) = Length(binRead), 'Binary data length check');
1090 iErr := -1;
1091 for i := Low(binary) to High(binary) do begin
1092 if binary[i] <> binRead[i] then begin
1093 iErr := i;
1094 Break;
1095 end;
1096 end;
1097 if iErr < 0
1098 then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)')
1099 else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr));
1100
1101 Expect( stm.Position = stm.Size, 'Stream position after read');
1102
Jens Geyer7bb44a32014-02-07 22:24:37 +01001103
Jens Geyer21366942013-12-30 22:04:51 +01001104 // Solidus can be encoded in two ways. Make sure we can read both
1105 stm.Position := 0;
1106 stm.Size := 0;
1107 stm.WriteString(SOLIDUS_JSON_DATA);
1108 stm.Position := 0;
1109 prot := TJSONProtocolImpl.Create(
1110 TStreamTransportImpl.Create(
1111 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1112 Expect( prot.ReadString = SOLIDUS_EXCPECTED, 'Solidus encoding');
1113
1114
Jens Geyer7bb44a32014-02-07 22:24:37 +01001115 // Widechars should work too. Do they?
1116 // After writing, we ensure that we are able to read it back
1117 // We can't assume hex-encoding, since (nearly) any Unicode char is valid JSON
1118 stm.Position := 0;
1119 stm.Size := 0;
1120 prot := TJSONProtocolImpl.Create(
1121 TStreamTransportImpl.Create(
1122 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001123 prot.WriteString( G_CLEF_AND_CYRILLIC_TEXT);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001124 stm.Position := 0;
1125 prot := TJSONProtocolImpl.Create(
1126 TStreamTransportImpl.Create(
1127 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001128 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Writing JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001129
1130 // Widechars should work with hex-encoding too. Do they?
1131 stm.Position := 0;
1132 stm.Size := 0;
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001133 stm.WriteString( G_CLEF_AND_CYRILLIC_JSON);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001134 stm.Position := 0;
1135 prot := TJSONProtocolImpl.Create(
1136 TStreamTransportImpl.Create(
1137 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001138 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Reading JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001139
1140
Roger Meier3bef8c22012-10-06 06:58:00 +00001141 finally
1142 stm.Free;
1143 prot := nil; //-> Release
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001144 StartTestGroup( '', test_Unknown); // no more tests here
Roger Meier3bef8c22012-10-06 06:58:00 +00001145 end;
1146end;
1147
1148
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001149procedure TClientThread.StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +00001150begin
1151 FTestGroup := aGroup;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001152 FCurrentTest := aTest;
1153
1154 Include( FExecuted, aTest);
1155
Roger Meier3bef8c22012-10-06 06:58:00 +00001156 if FTestGroup <> '' then begin
1157 Console.WriteLine('');
1158 Console.WriteLine( aGroup+' tests');
1159 Console.WriteLine( StringOfChar('-',60));
1160 end;
1161end;
1162
1163
1164procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
1165begin
1166 if aTestResult then begin
1167 Inc(FSuccesses);
1168 Console.WriteLine( aTestInfo+': passed');
1169 end
1170 else begin
1171 FErrors.Add( FTestGroup+': '+aTestInfo);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001172 Include( FFailed, FCurrentTest);
Roger Meier3bef8c22012-10-06 06:58:00 +00001173 Console.WriteLine( aTestInfo+': *** FAILED ***');
1174
1175 // We have a failed test!
1176 // -> issue DebugBreak ONLY if a debugger is attached,
1177 // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise
Jens Geyer9f7f11e2016-04-14 21:37:11 +02001178 if IsDebuggerPresent
1179 then {$IFDEF CPUX64} DebugBreak {$ELSE} asm int 3 end {$ENDIF};
Roger Meier3bef8c22012-10-06 06:58:00 +00001180 end;
1181end;
1182
1183
1184procedure TClientThread.ReportResults;
1185var nTotal : Integer;
1186 sLine : string;
1187begin
1188 // prevent us from stupid DIV/0 errors
1189 nTotal := FSuccesses + FErrors.Count;
1190 if nTotal = 0 then begin
1191 Console.WriteLine('No results logged');
1192 Exit;
1193 end;
1194
1195 Console.WriteLine('');
1196 Console.WriteLine( StringOfChar('=',60));
1197 Console.WriteLine( IntToStr(nTotal)+' tests performed');
1198 Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)');
1199 Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)');
1200 Console.WriteLine( StringOfChar('=',60));
1201 if FErrors.Count > 0 then begin
1202 Console.WriteLine('FAILED TESTS:');
1203 for sLine in FErrors do Console.WriteLine('- '+sLine);
1204 Console.WriteLine( StringOfChar('=',60));
1205 InterlockedIncrement( ExitCode); // return <> 0 on errors
1206 end;
1207 Console.WriteLine('');
1208end;
1209
1210
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001211function TClientThread.CalculateExitCode : Byte;
1212var test : TTestGroup;
1213begin
1214 result := EXITCODE_SUCCESS;
1215 for test := Low(TTestGroup) to High(TTestGroup) do begin
1216 if (test in FFailed) or not (test in FExecuted)
1217 then result := result or MAP_FAILURES_TO_EXITCODE_BITS[test];
1218 end;
1219end;
1220
1221
Roger Meier3bef8c22012-10-06 06:58:00 +00001222constructor TClientThread.Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
1223begin
1224 inherited Create( True );
1225 FNumIteration := ANumIteration;
1226 FTransport := ATransport;
1227 FProtocol := AProtocol;
1228 FConsole := TThreadConsole.Create( Self );
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001229 FCurrentTest := test_Unknown;
Roger Meier3bef8c22012-10-06 06:58:00 +00001230
1231 // error list: keep correct order, allow for duplicates
1232 FErrors := TStringList.Create;
1233 FErrors.Sorted := FALSE;
1234 FErrors.Duplicates := dupAccept;
1235end;
1236
1237destructor TClientThread.Destroy;
1238begin
1239 FreeAndNil( FConsole);
1240 FreeAndNil( FErrors);
1241 inherited;
1242end;
1243
1244procedure TClientThread.Execute;
1245var
1246 i : Integer;
1247 proc : TThreadProcedure;
1248begin
1249 // perform all tests
1250 try
Jens Geyer7bb44a32014-02-07 22:24:37 +01001251 JSONProtocolReadWriteTest;
Roger Meier3bef8c22012-10-06 06:58:00 +00001252 for i := 0 to FNumIteration - 1 do
1253 begin
1254 ClientTest;
Roger Meier3bef8c22012-10-06 06:58:00 +00001255 end;
1256 except
1257 on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"');
1258 end;
1259
1260 // report the outcome
1261 ReportResults;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001262 SetReturnValue( CalculateExitCode);
Roger Meier3bef8c22012-10-06 06:58:00 +00001263
1264 // shutdown
1265 proc := procedure
1266 begin
1267 if FTransport <> nil then
1268 begin
1269 FTransport.Close;
1270 FTransport := nil;
1271 end;
1272 end;
1273
1274 Synchronize( proc );
1275end;
1276
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001277
Roger Meier3bef8c22012-10-06 06:58:00 +00001278{ TThreadConsole }
1279
1280constructor TThreadConsole.Create(AThread: TThread);
1281begin
Jens Geyer718f6ee2013-09-06 21:02:34 +02001282 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +00001283 FThread := AThread;
1284end;
1285
1286procedure TThreadConsole.Write(const S: string);
1287var
1288 proc : TThreadProcedure;
1289begin
1290 proc := procedure
1291 begin
1292 Console.Write( S );
1293 end;
1294 TThread.Synchronize( FThread, proc);
1295end;
1296
1297procedure TThreadConsole.WriteLine(const S: string);
1298var
1299 proc : TThreadProcedure;
1300begin
1301 proc := procedure
1302 begin
1303 Console.WriteLine( S );
1304 end;
1305 TThread.Synchronize( FThread, proc);
1306end;
1307
1308initialization
1309begin
1310 TTestClient.FNumIteration := 1;
1311 TTestClient.FNumThread := 1;
1312end;
1313
1314end.