blob: a6567fa7eb60ab9490cce2e15cfd3e6a6c040961 [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>;
Jens Geyer540e3462016-12-28 14:25:41 +0100452 pair : TPair<TNumberz, TUserId>;
Roger Meier3bef8c22012-10-06 06:58:00 +0000453begin
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';
Jens Geyer540e3462016-12-28 14:25:41 +0100775 truck.Byte_thing := -8; // byte is signed
776 truck.I32_thing := 32;
777 truck.I64_thing := 64;
Roger Meier3bef8c22012-10-06 06:58:00 +0000778 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
Jens Geyer540e3462016-12-28 14:25:41 +0100826 (**
827 * So you think you've got this all worked, out eh?
828 *
829 * Creates a the returned map with these values and prints it out:
830 * { 1 => { 2 => argument,
831 * 3 => argument,
832 * },
833 * 2 => { 6 => <empty Insanity struct>, },
834 * }
835 * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
836 *)
837
Roger Meier3bef8c22012-10-06 06:58:00 +0000838 // verify result data
839 Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count));
840 //
841 first_map := whoa[1];
842 second_map := whoa[2];
843 Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count));
844 Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count));
845 //
846 looney := second_map[TNumberz.SIX];
847 Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney)));
848 Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
849 Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
850 //
851 for ret in [TNumberz.TWO, TNumberz.THREE] do begin
852 crazy := first_map[ret];
853 Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
854
855 Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap));
856 Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs));
857
Jens Geyer540e3462016-12-28 14:25:41 +0100858 Expect( crazy.UserMap.Count = insane.UserMap.Count, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count));
859 for pair in insane.UserMap do begin
860 Expect( crazy.UserMap[pair.Key] = pair.Value, 'crazy.UserMap['+IntToStr(Ord(pair.key))+'] = '+IntToStr(crazy.UserMap[pair.Key]));
861 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000862
Jens Geyer540e3462016-12-28 14:25:41 +0100863 Expect( crazy.Xtructs.Count = insane.Xtructs.Count, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count));
864 for arg0 := 0 to insane.Xtructs.Count-1 do begin
865 hello := insane.Xtructs[arg0];
866 goodbye := crazy.Xtructs[arg0];
867 Expect( goodbye.String_thing = hello.String_thing, 'goodbye.String_thing = '+goodbye.String_thing);
868 Expect( goodbye.Byte_thing = hello.Byte_thing, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing));
869 Expect( goodbye.I32_thing = hello.I32_thing, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing));
870 Expect( goodbye.I64_thing = hello.I64_thing, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing));
871 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000872 end;
873
874
875 // multi args
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200876 StartTestGroup( 'testMulti', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000877 arg0 := 1;
878 arg1 := 2;
879 arg2 := High(Int64);
880 arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
881 arg3.AddOrSetValue( 1, 'one');
882 arg4 := TNumberz.FIVE;
883 arg5 := 5000000;
884 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
885 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
886 arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
887 IntToStr( arg5) + ')');
888
889 i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5);
890 Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"');
891 Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing));
892 Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing));
893 Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing));
894 Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
895 Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
896 Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
897 Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
898
899 // multi exception
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200900 StartTestGroup( 'testMultiException(1)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000901 try
902 i := client.testMultiException( 'need more pizza', 'run out of beer');
903 Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"');
904 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
Jens Geyer6bbbf192014-09-07 01:45:56 +0200905 { this is not necessarily true, these fields are default-serialized
Jens Geyerd5436f52014-10-03 19:50:38 +0200906 Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
Roger Meier3bef8c22012-10-06 06:58:00 +0000907 Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
908 Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
Jens Geyerd5436f52014-10-03 19:50:38 +0200909 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000910 except
911 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
912 end;
913
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200914 StartTestGroup( 'testMultiException(Xception)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000915 try
916 i := client.testMultiException( 'Xception', 'second test');
917 Expect( FALSE, 'testMultiException(''Xception''): must trow an exception');
918 except
919 on x:TXception do begin
920 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
921 Expect( x.__isset_Message_, 'x.__isset_Message_ = '+BoolToString(x.__isset_Message_));
922 Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
923 Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"');
924 end;
925 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
926 end;
927
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200928 StartTestGroup( 'testMultiException(Xception2)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000929 try
930 i := client.testMultiException( 'Xception2', 'third test');
931 Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception');
932 except
933 on x:TXception2 do begin
934 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
935 Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing));
936 Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
937 Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"');
938 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 +0200939 { this is not necessarily true, these fields are default-serialized
Roger Meier3bef8c22012-10-06 06:58:00 +0000940 Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing));
941 Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing));
942 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 +0200943 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000944 end;
945 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
946 end;
947
948
949 // oneway functions
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200950 StartTestGroup( 'Test Oneway(1)', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000951 client.testOneway(1);
952 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
953
954 // call time
Jens Geyer06045cf2013-03-27 20:26:25 +0200955 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000956 StartTestGroup( 'Test Calltime()');
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200957 StartTick := GetTickCount;
Roger Meier3bef8c22012-10-06 06:58:00 +0000958 for k := 0 to 1000 - 1 do
959 begin
960 client.testVoid();
961 end;
962 Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
Jens Geyer06045cf2013-03-27 20:26:25 +0200963 {$ENDIF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000964
965 // no more tests here
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200966 StartTestGroup( '', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000967end;
968
969
Jens Geyer718f6ee2013-09-06 21:02:34 +0200970{$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200971procedure TClientThread.StressTest(const client : TThriftTest.Iface);
972begin
973 while TRUE do begin
974 try
975 if not FTransport.IsOpen then FTransport.Open; // re-open connection, server has already closed
976 try
977 client.testString('Test');
978 Write('.');
979 finally
980 if FTransport.IsOpen then FTransport.Close;
981 end;
982 except
983 on e:Exception do Writeln(#10+e.message);
984 end;
985 end;
986end;
Jens Geyer718f6ee2013-09-06 21:02:34 +0200987{$ENDIF}
Jens Geyer06045cf2013-03-27 20:26:25 +0200988
Jens Geyerfd1b3582014-12-13 23:42:58 +0100989
990function TClientThread.PrepareBinaryData( aRandomDist : Boolean = FALSE) : TBytes;
991var i, nextPos : Integer;
992begin
993 SetLength( result, $100);
994 ASSERT( Low(result) = 0);
995
996 // linear distribution, unless random is requested
997 if not aRandomDist then begin
998 for i := Low(result) to High(result) do begin
999 result[i] := i;
1000 end;
1001 Exit;
1002 end;
1003
1004 // random distribution of all 256 values
1005 FillChar( result[0], Length(result) * SizeOf(result[0]), $0);
1006 i := 1;
1007 while i < Length(result) do begin
1008 nextPos := Byte( Random($100));
1009 if result[nextPos] = 0 then begin // unused?
1010 result[nextPos] := i;
1011 Inc(i);
1012 end;
1013 end;
1014end;
1015
1016
Roger Meier3bef8c22012-10-06 06:58:00 +00001017procedure TClientThread.JSONProtocolReadWriteTest;
1018// Tests only then read/write procedures of the JSON protocol
1019// All tests succeed, if we can read what we wrote before
1020// Note that passing this test does not imply, that our JSON is really compatible to what
1021// other clients or servers expect as the real JSON. This is beyond the scope of this test.
1022var prot : IProtocol;
1023 stm : TStringStream;
1024 list : IList;
1025 binary, binRead : TBytes;
1026 i,iErr : Integer;
1027const
1028 TEST_SHORT = ShortInt( $FE);
1029 TEST_SMALL = SmallInt( $FEDC);
1030 TEST_LONG = LongInt( $FEDCBA98);
1031 TEST_I64 = Int64( $FEDCBA9876543210);
1032 TEST_DOUBLE = -1.234e-56;
1033 DELTA_DOUBLE = TEST_DOUBLE * 1e-14;
1034 TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars")
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001035 // Test THRIFT-2336 and THRIFT-3404 with U+1D11E (G Clef symbol) and 'Русское Название';
1036 G_CLEF_AND_CYRILLIC_TEXT = #$1d11e' '#$0420#$0443#$0441#$0441#$043a#$043e#$0435' '#$041d#$0430#$0437#$0432#$0430#$043d#$0438#$0435;
1037 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 +01001038 // test both possible solidus encodings
1039 SOLIDUS_JSON_DATA = '"one/two\/three"';
1040 SOLIDUS_EXCPECTED = 'one/two/three';
Roger Meier3bef8c22012-10-06 06:58:00 +00001041begin
1042 stm := TStringStream.Create;
1043 try
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001044 StartTestGroup( 'JsonProtocolTest', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001045
1046 // prepare binary data
Jens Geyerfd1b3582014-12-13 23:42:58 +01001047 binary := PrepareBinaryData( FALSE);
Roger Meier3bef8c22012-10-06 06:58:00 +00001048
1049 // output setup
1050 prot := TJSONProtocolImpl.Create(
1051 TStreamTransportImpl.Create(
1052 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
1053
1054 // write
1055 prot.WriteListBegin( TListImpl.Create( TType.String_, 9));
1056 prot.WriteBool( TRUE);
1057 prot.WriteBool( FALSE);
1058 prot.WriteByte( TEST_SHORT);
1059 prot.WriteI16( TEST_SMALL);
1060 prot.WriteI32( TEST_LONG);
1061 prot.WriteI64( TEST_I64);
1062 prot.WriteDouble( TEST_DOUBLE);
1063 prot.WriteString( TEST_STRING);
1064 prot.WriteBinary( binary);
1065 prot.WriteListEnd;
1066
1067 // input setup
1068 Expect( stm.Position = stm.Size, 'Stream position/length after write');
1069 stm.Position := 0;
1070 prot := TJSONProtocolImpl.Create(
1071 TStreamTransportImpl.Create(
1072 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1073
1074 // read and compare
1075 list := prot.ReadListBegin;
1076 Expect( list.ElementType = TType.String_, 'list element type');
1077 Expect( list.Count = 9, 'list element count');
1078 Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE');
1079 Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE');
1080 Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte');
1081 Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16');
1082 Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32');
1083 Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64');
1084 Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble');
1085 Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString');
1086 binRead := prot.ReadBinary;
1087 prot.ReadListEnd;
1088
1089 // test binary data
1090 Expect( Length(binary) = Length(binRead), 'Binary data length check');
1091 iErr := -1;
1092 for i := Low(binary) to High(binary) do begin
1093 if binary[i] <> binRead[i] then begin
1094 iErr := i;
1095 Break;
1096 end;
1097 end;
1098 if iErr < 0
1099 then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)')
1100 else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr));
1101
1102 Expect( stm.Position = stm.Size, 'Stream position after read');
1103
Jens Geyer7bb44a32014-02-07 22:24:37 +01001104
Jens Geyer21366942013-12-30 22:04:51 +01001105 // Solidus can be encoded in two ways. Make sure we can read both
1106 stm.Position := 0;
1107 stm.Size := 0;
1108 stm.WriteString(SOLIDUS_JSON_DATA);
1109 stm.Position := 0;
1110 prot := TJSONProtocolImpl.Create(
1111 TStreamTransportImpl.Create(
1112 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
1113 Expect( prot.ReadString = SOLIDUS_EXCPECTED, 'Solidus encoding');
1114
1115
Jens Geyer7bb44a32014-02-07 22:24:37 +01001116 // Widechars should work too. Do they?
1117 // After writing, we ensure that we are able to read it back
1118 // We can't assume hex-encoding, since (nearly) any Unicode char is valid JSON
1119 stm.Position := 0;
1120 stm.Size := 0;
1121 prot := TJSONProtocolImpl.Create(
1122 TStreamTransportImpl.Create(
1123 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001124 prot.WriteString( G_CLEF_AND_CYRILLIC_TEXT);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001125 stm.Position := 0;
1126 prot := TJSONProtocolImpl.Create(
1127 TStreamTransportImpl.Create(
1128 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001129 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Writing JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001130
1131 // Widechars should work with hex-encoding too. Do they?
1132 stm.Position := 0;
1133 stm.Size := 0;
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001134 stm.WriteString( G_CLEF_AND_CYRILLIC_JSON);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001135 stm.Position := 0;
1136 prot := TJSONProtocolImpl.Create(
1137 TStreamTransportImpl.Create(
1138 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001139 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Reading JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001140
1141
Roger Meier3bef8c22012-10-06 06:58:00 +00001142 finally
1143 stm.Free;
1144 prot := nil; //-> Release
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001145 StartTestGroup( '', test_Unknown); // no more tests here
Roger Meier3bef8c22012-10-06 06:58:00 +00001146 end;
1147end;
1148
1149
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001150procedure TClientThread.StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +00001151begin
1152 FTestGroup := aGroup;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001153 FCurrentTest := aTest;
1154
1155 Include( FExecuted, aTest);
1156
Roger Meier3bef8c22012-10-06 06:58:00 +00001157 if FTestGroup <> '' then begin
1158 Console.WriteLine('');
1159 Console.WriteLine( aGroup+' tests');
1160 Console.WriteLine( StringOfChar('-',60));
1161 end;
1162end;
1163
1164
1165procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
1166begin
1167 if aTestResult then begin
1168 Inc(FSuccesses);
1169 Console.WriteLine( aTestInfo+': passed');
1170 end
1171 else begin
1172 FErrors.Add( FTestGroup+': '+aTestInfo);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001173 Include( FFailed, FCurrentTest);
Roger Meier3bef8c22012-10-06 06:58:00 +00001174 Console.WriteLine( aTestInfo+': *** FAILED ***');
1175
1176 // We have a failed test!
1177 // -> issue DebugBreak ONLY if a debugger is attached,
1178 // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise
Jens Geyer9f7f11e2016-04-14 21:37:11 +02001179 if IsDebuggerPresent
1180 then {$IFDEF CPUX64} DebugBreak {$ELSE} asm int 3 end {$ENDIF};
Roger Meier3bef8c22012-10-06 06:58:00 +00001181 end;
1182end;
1183
1184
1185procedure TClientThread.ReportResults;
1186var nTotal : Integer;
1187 sLine : string;
1188begin
1189 // prevent us from stupid DIV/0 errors
1190 nTotal := FSuccesses + FErrors.Count;
1191 if nTotal = 0 then begin
1192 Console.WriteLine('No results logged');
1193 Exit;
1194 end;
1195
1196 Console.WriteLine('');
1197 Console.WriteLine( StringOfChar('=',60));
1198 Console.WriteLine( IntToStr(nTotal)+' tests performed');
1199 Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)');
1200 Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)');
1201 Console.WriteLine( StringOfChar('=',60));
1202 if FErrors.Count > 0 then begin
1203 Console.WriteLine('FAILED TESTS:');
1204 for sLine in FErrors do Console.WriteLine('- '+sLine);
1205 Console.WriteLine( StringOfChar('=',60));
1206 InterlockedIncrement( ExitCode); // return <> 0 on errors
1207 end;
1208 Console.WriteLine('');
1209end;
1210
1211
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001212function TClientThread.CalculateExitCode : Byte;
1213var test : TTestGroup;
1214begin
1215 result := EXITCODE_SUCCESS;
1216 for test := Low(TTestGroup) to High(TTestGroup) do begin
1217 if (test in FFailed) or not (test in FExecuted)
1218 then result := result or MAP_FAILURES_TO_EXITCODE_BITS[test];
1219 end;
1220end;
1221
1222
Roger Meier3bef8c22012-10-06 06:58:00 +00001223constructor TClientThread.Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
1224begin
1225 inherited Create( True );
1226 FNumIteration := ANumIteration;
1227 FTransport := ATransport;
1228 FProtocol := AProtocol;
1229 FConsole := TThreadConsole.Create( Self );
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001230 FCurrentTest := test_Unknown;
Roger Meier3bef8c22012-10-06 06:58:00 +00001231
1232 // error list: keep correct order, allow for duplicates
1233 FErrors := TStringList.Create;
1234 FErrors.Sorted := FALSE;
1235 FErrors.Duplicates := dupAccept;
1236end;
1237
1238destructor TClientThread.Destroy;
1239begin
1240 FreeAndNil( FConsole);
1241 FreeAndNil( FErrors);
1242 inherited;
1243end;
1244
1245procedure TClientThread.Execute;
1246var
1247 i : Integer;
1248 proc : TThreadProcedure;
1249begin
1250 // perform all tests
1251 try
Jens Geyer7bb44a32014-02-07 22:24:37 +01001252 JSONProtocolReadWriteTest;
Roger Meier3bef8c22012-10-06 06:58:00 +00001253 for i := 0 to FNumIteration - 1 do
1254 begin
1255 ClientTest;
Roger Meier3bef8c22012-10-06 06:58:00 +00001256 end;
1257 except
1258 on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"');
1259 end;
1260
1261 // report the outcome
1262 ReportResults;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001263 SetReturnValue( CalculateExitCode);
Roger Meier3bef8c22012-10-06 06:58:00 +00001264
1265 // shutdown
1266 proc := procedure
1267 begin
1268 if FTransport <> nil then
1269 begin
1270 FTransport.Close;
1271 FTransport := nil;
1272 end;
1273 end;
1274
1275 Synchronize( proc );
1276end;
1277
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001278
Roger Meier3bef8c22012-10-06 06:58:00 +00001279{ TThreadConsole }
1280
1281constructor TThreadConsole.Create(AThread: TThread);
1282begin
Jens Geyer718f6ee2013-09-06 21:02:34 +02001283 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +00001284 FThread := AThread;
1285end;
1286
1287procedure TThreadConsole.Write(const S: string);
1288var
1289 proc : TThreadProcedure;
1290begin
1291 proc := procedure
1292 begin
1293 Console.Write( S );
1294 end;
1295 TThread.Synchronize( FThread, proc);
1296end;
1297
1298procedure TThreadConsole.WriteLine(const S: string);
1299var
1300 proc : TThreadProcedure;
1301begin
1302 proc := procedure
1303 begin
1304 Console.WriteLine( S );
1305 end;
1306 TThread.Synchronize( FThread, proc);
1307end;
1308
1309initialization
1310begin
1311 TTestClient.FNumIteration := 1;
1312 TTestClient.FNumThread := 1;
1313end;
1314
1315end.