blob: ebc0796fedbbdbfeea8112dcbe0d6411c5891f28 [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
Jens Geyer17c3ad92017-09-05 20:31:27 +020025{.$DEFINE PerfTest} // activate the performance test
26{$DEFINE Exceptions} // activate the exceptions test (or disable while debugging)
Jens Geyer06045cf2013-03-27 20:26:25 +020027
Jens Geyer14f5d502017-12-09 13:47:09 +010028{$if CompilerVersion >= 28}
29{$DEFINE SupportsAsync}
30{$ifend}
31
Jens Geyer47f63172019-06-06 22:42:58 +020032{$WARN SYMBOL_PLATFORM OFF} // Win32Check
33
Roger Meier3bef8c22012-10-06 06:58:00 +000034interface
35
36uses
Jens Geyeraf7ecd62018-06-22 22:41:27 +020037 Windows, SysUtils, Classes, Math, ComObj, ActiveX,
Jens Geyer14f5d502017-12-09 13:47:09 +010038 {$IFDEF SupportsAsync} System.Threading, {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +000039 DateUtils,
40 Generics.Collections,
41 TestConstants,
Jens Geyer3d556242018-01-24 19:14:32 +010042 ConsoleHelper,
Jens Geyerb342bd92019-06-03 20:27:00 +020043 PerfTests,
Roger Meier3bef8c22012-10-06 06:58:00 +000044 Thrift,
Jens Geyerf0e63312015-03-01 18:47:49 +010045 Thrift.Protocol.Compact,
Roger Meier3bef8c22012-10-06 06:58:00 +000046 Thrift.Protocol.JSON,
47 Thrift.Protocol,
48 Thrift.Transport.Pipes,
Jens Geyer02230912019-04-03 01:12:51 +020049 Thrift.Transport.WinHTTP,
50 Thrift.Transport.MsxmlHTTP,
Roger Meier3bef8c22012-10-06 06:58:00 +000051 Thrift.Transport,
52 Thrift.Stream,
53 Thrift.Test,
Jens Geyer83ff7532019-06-06 22:46:03 +020054 Thrift.WinHTTP,
Jens Geyerf7904452017-07-26 15:02:12 +020055 Thrift.Utils,
Jens Geyera019cda2019-11-09 23:24:52 +010056
57 Thrift.Configuration,
Jens Geyer3d556242018-01-24 19:14:32 +010058 Thrift.Collections;
Roger Meier3bef8c22012-10-06 06:58:00 +000059
60type
61 TThreadConsole = class
62 private
63 FThread : TThread;
64 public
65 procedure Write( const S : string);
66 procedure WriteLine( const S : string);
67 constructor Create( AThread: TThread);
68 end;
69
Jens Geyeraf7ecd62018-06-22 22:41:27 +020070 TTestSetup = record
71 protType : TKnownProtocol;
72 endpoint : TEndpointTransport;
73 layered : TLayeredTransports;
74 useSSL : Boolean; // include where appropriate (TLayeredTransport?)
75 host : string;
76 port : Integer;
77 sPipeName : string;
78 hAnonRead, hAnonWrite : THandle;
79 end;
80
Roger Meier3bef8c22012-10-06 06:58:00 +000081 TClientThread = class( TThread )
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020082 private type
83 TTestGroup = (
84 test_Unknown,
85 test_BaseTypes,
86 test_Structs,
87 test_Containers,
88 test_Exceptions
89 // new values here
90 );
91 TTestGroups = set of TTestGroup;
92
Jens Geyer85827152018-01-12 21:20:59 +010093 TTestSize = (
94 Empty, // Edge case: the zero-length empty binary
95 Normal, // Fairly small array of usual size (256 bytes)
96 ByteArrayTest, // THRIFT-4454 Large writes/reads may cause range check errors in debug mode
Jens Geyerbd1a2732019-06-26 22:52:44 +020097 PipeWriteLimit, // THRIFT-4372 Pipe write operations across a network are limited to 65,535 bytes per write.
Jens Geyer2646bd62019-11-09 23:24:52 +010098 FifteenMB // quite a bit of data, but still below the default max frame size
Jens Geyer85827152018-01-12 21:20:59 +010099 );
100
Roger Meier3bef8c22012-10-06 06:58:00 +0000101 private
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200102 FSetup : TTestSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +0000103 FTransport : ITransport;
104 FProtocol : IProtocol;
105 FNumIteration : Integer;
106 FConsole : TThreadConsole;
107
108 // test reporting, will be refactored out into separate class later
109 FTestGroup : string;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200110 FCurrentTest : TTestGroup;
Roger Meier3bef8c22012-10-06 06:58:00 +0000111 FSuccesses : Integer;
112 FErrors : TStringList;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200113 FFailed : TTestGroups;
114 FExecuted : TTestGroups;
115 procedure StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +0000116 procedure Expect( aTestResult : Boolean; const aTestInfo : string);
117 procedure ReportResults;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100118 function CalculateExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000119
120 procedure ClientTest;
Jens Geyer14f5d502017-12-09 13:47:09 +0100121 {$IFDEF SupportsAsync}
122 procedure ClientAsyncTest;
123 {$ENDIF}
124
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200125 procedure InitializeProtocolTransportStack;
126 procedure ShutdownProtocolTransportStack;
Jens Geyera019cda2019-11-09 23:24:52 +0100127 function InitializeHttpTransport( const aTimeoutSetting : Integer; const aConfig : IThriftConfiguration = nil) : IHTTPClient;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200128
Roger Meier3bef8c22012-10-06 06:58:00 +0000129 procedure JSONProtocolReadWriteTest;
Jens Geyer85827152018-01-12 21:20:59 +0100130 function PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes;
Jens Geyer718f6ee2013-09-06 21:02:34 +0200131 {$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200132 procedure StressTest(const client : TThriftTest.Iface);
Jens Geyer718f6ee2013-09-06 21:02:34 +0200133 {$ENDIF}
Jens Geyerf7904452017-07-26 15:02:12 +0200134 {$IFDEF Win64}
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200135 procedure UseInterlockedExchangeAdd64;
Jens Geyerf7904452017-07-26 15:02:12 +0200136 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000137 protected
138 procedure Execute; override;
139 public
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200140 constructor Create( const aSetup : TTestSetup; const aNumIteration: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +0000141 destructor Destroy; override;
142 end;
143
144 TTestClient = class
145 private
146 class var
147 FNumIteration : Integer;
148 FNumThread : Integer;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200149
150 class procedure PrintCmdLineHelp;
151 class procedure InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000152 public
Jens Geyeraeda9872020-03-22 15:01:28 +0100153 class function Execute( const arguments: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000154 end;
155
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200156
Roger Meier3bef8c22012-10-06 06:58:00 +0000157implementation
158
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200159const
160 EXITCODE_SUCCESS = $00; // no errors bits set
161 //
162 EXITCODE_FAILBIT_BASETYPES = $01;
163 EXITCODE_FAILBIT_STRUCTS = $02;
164 EXITCODE_FAILBIT_CONTAINERS = $04;
165 EXITCODE_FAILBIT_EXCEPTIONS = $08;
166
167 MAP_FAILURES_TO_EXITCODE_BITS : array[TClientThread.TTestGroup] of Byte = (
168 EXITCODE_SUCCESS, // no bits here
169 EXITCODE_FAILBIT_BASETYPES,
170 EXITCODE_FAILBIT_STRUCTS,
171 EXITCODE_FAILBIT_CONTAINERS,
172 EXITCODE_FAILBIT_EXCEPTIONS
173 );
174
175
176
Roger Meier3bef8c22012-10-06 06:58:00 +0000177function BoolToString( b : Boolean) : string;
178// overrides global BoolToString()
179begin
180 if b
181 then result := 'true'
182 else result := 'false';
183end;
184
185// not available in all versions, so make sure we have this one imported
186function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent';
187
188{ TTestClient }
189
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200190class procedure TTestClient.PrintCmdLineHelp;
191const HELPTEXT = ' [options]'#10
192 + #10
193 + 'Allowed options:'#10
Jens Geyeraeda9872020-03-22 15:01:28 +0100194 + ' -h | --help Produces this help message'#10
195 + ' --host=arg (localhost) Host to connect'#10
196 + ' --port=arg (9090) Port number to connect'#10
197 + ' --pipe=arg Windows Named Pipe (e.g. MyThriftPipe)'#10
198 + ' --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)'#10
199 + ' --transport=arg (sockets) Transport: buffered, framed, http, winhttp'#10
200 + ' --protocol=arg (binary) Protocol: binary, compact, json'#10
201 + ' --ssl Encrypted Transport using SSL'#10
202 + ' -n=num | --testloops=num (1) Number of Tests'#10
203 + ' -t=num | --threads=num (1) Number of Test threads'#10
204 + ' --performance Run the built-in performance test (no other arguments)'#10
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200205 ;
206begin
207 Writeln( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + HELPTEXT);
208end;
209
210class procedure TTestClient.InvalidArgs;
211begin
212 Console.WriteLine( 'Invalid args.');
213 Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + ' -h for more information');
214 Abort;
215end;
216
Jens Geyeraeda9872020-03-22 15:01:28 +0100217class function TTestClient.Execute(const arguments: array of string) : Byte;
218
219 function IsSwitch( const aArgument, aSwitch : string; out sValue : string) : Boolean;
220 begin
221 sValue := '';
222 result := (Copy( aArgument, 1, Length(aSwitch)) = aSwitch);
223 if result then begin
224 if (Copy( aArgument, 1, Length(aSwitch)+1) = (aSwitch+'='))
225 then sValue := Copy( aArgument, Length(aSwitch)+2, MAXINT);
226 end;
227 end;
228
Roger Meier3bef8c22012-10-06 06:58:00 +0000229var
Jens Geyeraeda9872020-03-22 15:01:28 +0100230 iArg : Integer;
Jens Geyer14f5d502017-12-09 13:47:09 +0100231 threadExitCode : Byte;
Jens Geyeraeda9872020-03-22 15:01:28 +0100232 sArg, sValue : string;
Roger Meier3bef8c22012-10-06 06:58:00 +0000233 threads : array of TThread;
234 dtStart : TDateTime;
235 test : Integer;
236 thread : TThread;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200237 setup : TTestSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +0000238begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200239 // init record
240 with setup do begin
241 protType := prot_Binary;
242 endpoint := trns_Sockets;
243 layered := [];
244 useSSL := FALSE;
245 host := 'localhost';
246 port := 9090;
247 sPipeName := '';
248 hAnonRead := INVALID_HANDLE_VALUE;
249 hAnonWrite := INVALID_HANDLE_VALUE;
250 end;
251
Roger Meier3bef8c22012-10-06 06:58:00 +0000252 try
Jens Geyeraeda9872020-03-22 15:01:28 +0100253 iArg := 0;
254 while iArg < Length(arguments) do begin
255 sArg := arguments[iArg];
256 Inc(iArg);
Roger Meier3bef8c22012-10-06 06:58:00 +0000257
Jens Geyeraeda9872020-03-22 15:01:28 +0100258 if IsSwitch( sArg, '-h', sValue)
259 or IsSwitch( sArg, '--help', sValue)
260 then begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200261 // -h [ --help ] produce help message
262 PrintCmdLineHelp;
263 result := $FF; // all tests failed
264 Exit;
265 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100266 else if IsSwitch( sArg, '--host', sValue) then begin
Jens Geyerb360b652014-09-28 01:55:46 +0200267 // --host arg (=localhost) Host to connect
Jens Geyeraeda9872020-03-22 15:01:28 +0100268 setup.host := sValue;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200269 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100270 else if IsSwitch( sArg, '--port', sValue) then begin
Jens Geyerb360b652014-09-28 01:55:46 +0200271 // --port arg (=9090) Port number to connect
Jens Geyeraeda9872020-03-22 15:01:28 +0100272 setup.port := StrToIntDef(sValue,0);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200273 if setup.port <= 0 then InvalidArgs;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200274 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100275 else if IsSwitch( sArg, '--domain-socket', sValue) then begin
Jens Geyerb360b652014-09-28 01:55:46 +0200276 // --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200277 raise Exception.Create('domain-socket not supported');
278 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100279 // --pipe arg Windows Named Pipe (e.g. MyThriftPipe)
280 else if IsSwitch( sArg, '--pipe', sValue) then begin
Jens Geyer4a33b182020-03-22 13:46:34 +0100281 // --pipe arg Windows Named Pipe (e.g. MyThriftPipe)
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200282 setup.endpoint := trns_NamedPipes;
Jens Geyeraeda9872020-03-22 15:01:28 +0100283 setup.sPipeName := sValue;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200284 Console.WriteLine('Using named pipe ('+setup.sPipeName+')');
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200285 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100286 else if IsSwitch( sArg, '--anon-pipes', sValue) then begin
Jens Geyerb360b652014-09-28 01:55:46 +0200287 // --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200288 setup.endpoint := trns_AnonPipes;
Jens Geyeraeda9872020-03-22 15:01:28 +0100289 setup.hAnonRead := THandle( StrToIntDef( arguments[iArg], Integer(INVALID_HANDLE_VALUE)));
290 Inc(iArg);
291 setup.hAnonWrite := THandle( StrToIntDef( arguments[iArg], Integer(INVALID_HANDLE_VALUE)));
292 Inc(iArg);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200293 Console.WriteLine('Using anonymous pipes ('+IntToStr(Integer(setup.hAnonRead))+' and '+IntToStr(Integer(setup.hAnonWrite))+')');
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200294 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100295 else if IsSwitch( sArg, '--transport', sValue) then begin
Jens Geyer02230912019-04-03 01:12:51 +0200296 // --transport arg (=sockets) Transport: buffered, framed, http, winhttp, evhttp
Jens Geyeraeda9872020-03-22 15:01:28 +0100297 if sValue = 'buffered' then Include( setup.layered, trns_Buffered)
298 else if sValue = 'framed' then Include( setup.layered, trns_Framed)
299 else if sValue = 'http' then setup.endpoint := trns_MsXmlHttp
300 else if sValue = 'winhttp' then setup.endpoint := trns_WinHttp
301 else if sValue = 'evhttp' then setup.endpoint := trns_EvHttp // recognized, but not supported
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200302 else InvalidArgs;
303 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100304 else if IsSwitch( sArg, '--protocol', sValue) then begin
Jens Geyerb360b652014-09-28 01:55:46 +0200305 // --protocol arg (=binary) Protocol: binary, compact, json
Jens Geyeraeda9872020-03-22 15:01:28 +0100306 if sValue = 'binary' then setup.protType := prot_Binary
307 else if sValue = 'compact' then setup.protType := prot_Compact
308 else if sValue = 'json' then setup.protType := prot_JSON
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200309 else InvalidArgs;
310 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100311 else if IsSwitch( sArg, '--ssl', sValue) then begin
Jens Geyerb360b652014-09-28 01:55:46 +0200312 // --ssl Encrypted Transport using SSL
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200313 setup.useSSL := TRUE;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200314
315 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100316 else if IsSwitch( sArg, '-n', sValue) or IsSwitch( sArg, '--testloops', sValue) then begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200317 // -n [ --testloops ] arg (=1) Number of Tests
Jens Geyeraeda9872020-03-22 15:01:28 +0100318 FNumIteration := StrToIntDef( sValue, 0);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200319 if FNumIteration <= 0
320 then InvalidArgs;
321
322 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100323 else if IsSwitch( sArg, '-t', sValue) or IsSwitch( sArg, '--threads', sValue) then begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200324 // -t [ --threads ] arg (=1) Number of Test threads
Jens Geyeraeda9872020-03-22 15:01:28 +0100325 FNumThread := StrToIntDef( sValue, 0);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200326 if FNumThread <= 0
327 then InvalidArgs;
328 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100329 else if IsSwitch( sArg, '--performance', sValue) then begin
Jens Geyerb342bd92019-06-03 20:27:00 +0200330 result := TPerformanceTests.Execute;
331 Exit;
332 end
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200333 else begin
334 InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000335 end;
336 end;
337
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200338
Roger Meier79655fb2012-10-20 20:59:41 +0000339 // In the anonymous pipes mode the client is launched by the test server
340 // -> behave nicely and allow for attaching a debugger to this process
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200341 if (setup.endpoint = trns_AnonPipes) and not IsDebuggerPresent
Roger Meier79655fb2012-10-20 20:59:41 +0000342 then MessageBox( 0, 'Attach Debugger and/or click OK to continue.',
343 'Thrift TestClient (Delphi)',
344 MB_OK or MB_ICONEXCLAMATION);
345
Roger Meier3bef8c22012-10-06 06:58:00 +0000346 SetLength( threads, FNumThread);
347 dtStart := Now;
348
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200349 // layered transports are not really meant to be stacked upon each other
350 if (trns_Framed in setup.layered) then begin
351 Console.WriteLine('Using framed transport');
352 end
353 else if (trns_Buffered in setup.layered) then begin
354 Console.WriteLine('Using buffered transport');
355 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000356
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200357 Console.WriteLine(THRIFT_PROTOCOLS[setup.protType]+' protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000358
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200359 for test := 0 to FNumThread - 1 do begin
360 thread := TClientThread.Create( setup, FNumIteration);
Roger Meier3bef8c22012-10-06 06:58:00 +0000361 threads[test] := thread;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200362 thread.Start;
Roger Meier3bef8c22012-10-06 06:58:00 +0000363 end;
364
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200365 result := 0;
366 for test := 0 to FNumThread - 1 do begin
Jens Geyer14f5d502017-12-09 13:47:09 +0100367 threadExitCode := threads[test].WaitFor;
368 result := result or threadExitCode;
Jens Geyer14f5d502017-12-09 13:47:09 +0100369 threads[test].Free;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200370 threads[test] := nil;
Jens Geyer14f5d502017-12-09 13:47:09 +0100371 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000372
373 Console.Write('Total time: ' + IntToStr( MilliSecondsBetween(Now, dtStart)));
374
375 except
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200376 on E: EAbort do raise;
377 on E: Exception do begin
378 Console.WriteLine( E.Message + #10 + E.StackTrace);
379 raise;
Roger Meier3bef8c22012-10-06 06:58:00 +0000380 end;
381 end;
382
383 Console.WriteLine('');
384 Console.WriteLine('done!');
385end;
386
387{ TClientThread }
388
389procedure TClientThread.ClientTest;
390var
391 client : TThriftTest.Iface;
392 s : string;
393 i8 : ShortInt;
394 i32 : Integer;
395 i64 : Int64;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100396 binOut,binIn : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000397 dub : Double;
398 o : IXtruct;
399 o2 : IXtruct2;
400 i : IXtruct;
401 i2 : IXtruct2;
402 mapout : IThriftDictionary<Integer,Integer>;
403 mapin : IThriftDictionary<Integer,Integer>;
404 strmapout : IThriftDictionary<string,string>;
405 strmapin : IThriftDictionary<string,string>;
406 j : Integer;
407 first : Boolean;
408 key : Integer;
409 strkey : string;
410 listout : IThriftList<Integer>;
411 listin : IThriftList<Integer>;
412 setout : IHashSet<Integer>;
413 setin : IHashSet<Integer>;
414 ret : TNumberz;
415 uid : Int64;
416 mm : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
417 pos : IThriftDictionary<Integer, Integer>;
418 neg : IThriftDictionary<Integer, Integer>;
419 m2 : IThriftDictionary<Integer, Integer>;
420 k2 : Integer;
421 insane : IInsanity;
422 truck : IXtruct;
423 whoa : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
424 key64 : Int64;
425 val : IThriftDictionary<TNumberz, IInsanity>;
426 k2_2 : TNumberz;
427 k3 : TNumberz;
428 v2 : IInsanity;
429 userMap : IThriftDictionary<TNumberz, Int64>;
430 xtructs : IThriftList<IXtruct>;
431 x : IXtruct;
432 arg0 : ShortInt;
433 arg1 : Integer;
434 arg2 : Int64;
435 arg3 : IThriftDictionary<SmallInt, string>;
436 arg4 : TNumberz;
437 arg5 : Int64;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200438 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000439 StartTick : Cardinal;
440 k : Integer;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200441 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000442 hello, goodbye : IXtruct;
443 crazy : IInsanity;
444 looney : IInsanity;
445 first_map : IThriftDictionary<TNumberz, IInsanity>;
446 second_map : IThriftDictionary<TNumberz, IInsanity>;
Jens Geyer540e3462016-12-28 14:25:41 +0100447 pair : TPair<TNumberz, TUserId>;
Jens Geyer85827152018-01-12 21:20:59 +0100448 testsize : TTestSize;
Roger Meier3bef8c22012-10-06 06:58:00 +0000449begin
450 client := TThriftTest.TClient.Create( FProtocol);
451 FTransport.Open;
452
Jens Geyer06045cf2013-03-27 20:26:25 +0200453 {$IFDEF StressTest}
454 StressTest( client);
455 {$ENDIF StressTest}
456
Jens Geyer17c3ad92017-09-05 20:31:27 +0200457 {$IFDEF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000458 // in-depth exception test
459 // (1) do we get an exception at all?
460 // (2) do we get the right exception?
461 // (3) does the exception contain the expected data?
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200462 StartTestGroup( 'testException', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000463 // case 1: exception type declared in IDL at the function call
464 try
465 client.testException('Xception');
466 Expect( FALSE, 'testException(''Xception''): must trow an exception');
467 except
468 on e:TXception do begin
469 Expect( e.ErrorCode = 1001, 'error code');
470 Expect( e.Message_ = 'Xception', 'error message');
471 Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ );
472 end;
473 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200474 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000475 end;
476
Jens Geyercc8c2c62021-03-29 22:38:30 +0200477 // re-open connection if needed
478 if not FTransport.IsOpen
479 then FTransport.Open;
480
Roger Meier3bef8c22012-10-06 06:58:00 +0000481 // case 2: exception type NOT declared in IDL at the function call
482 // this will close the connection
483 try
484 client.testException('TException');
485 Expect( FALSE, 'testException(''TException''): must trow an exception');
486 except
487 on e:TTransportException do begin
488 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Roger Meier3bef8c22012-10-06 06:58:00 +0000489 end;
Jens Geyer6bbbf192014-09-07 01:45:56 +0200490 on e:TApplicationException do begin
491 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Jens Geyer6bbbf192014-09-07 01:45:56 +0200492 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200493 on e:TException do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
494 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000495 end;
496
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100497
Jens Geyer2ad6c302015-02-26 19:38:53 +0100498 if FTransport.IsOpen then FTransport.Close;
499 FTransport.Open; // re-open connection, server has already closed
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100500
Jens Geyer2ad6c302015-02-26 19:38:53 +0100501
Roger Meier3bef8c22012-10-06 06:58:00 +0000502 // case 3: no exception
503 try
504 client.testException('something');
505 Expect( TRUE, 'testException(''something''): must not trow an exception');
506 except
507 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200508 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000509 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200510 {$ENDIF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000511
Jens Geyercc8c2c62021-03-29 22:38:30 +0200512 // re-open connection if needed
513 if not FTransport.IsOpen
514 then FTransport.Open;
Roger Meier3bef8c22012-10-06 06:58:00 +0000515
516 // simple things
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200517 StartTestGroup( 'simple Thrift calls', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000518 client.testVoid();
519 Expect( TRUE, 'testVoid()'); // success := no exception
520
Jens Geyer39ba6b72015-09-22 00:00:49 +0200521 s := BoolToString( client.testBool(TRUE));
522 Expect( s = BoolToString(TRUE), 'testBool(TRUE) = '+s);
523 s := BoolToString( client.testBool(FALSE));
524 Expect( s = BoolToString(FALSE), 'testBool(FALSE) = '+s);
525
Roger Meier3bef8c22012-10-06 06:58:00 +0000526 s := client.testString('Test');
527 Expect( s = 'Test', 'testString(''Test'') = "'+s+'"');
528
Jens Geyercf892d42017-09-09 10:08:22 +0200529 s := client.testString(''); // empty string
530 Expect( s = '', 'testString('''') = "'+s+'"');
531
Jens Geyer06045cf2013-03-27 20:26:25 +0200532 s := client.testString(HUGE_TEST_STRING);
533 Expect( length(s) = length(HUGE_TEST_STRING),
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100534 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
Jens Geyer06045cf2013-03-27 20:26:25 +0200535 +'=> length(result) = '+IntToStr(Length(s)));
536
Roger Meier3bef8c22012-10-06 06:58:00 +0000537 i8 := client.testByte(1);
538 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
539
540 i32 := client.testI32(-1);
541 Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32));
542
543 Console.WriteLine('testI64(-34359738368)');
544 i64 := client.testI64(-34359738368);
545 Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
546
Jens Geyerd4df9172017-10-25 22:30:23 +0200547 // random binary small
Jens Geyer85827152018-01-12 21:20:59 +0100548 for testsize := Low(TTestSize) to High(TTestSize) do begin
549 binOut := PrepareBinaryData( TRUE, testsize);
Jens Geyerbd1a2732019-06-26 22:52:44 +0200550 Console.WriteLine('testBinary('+IntToStr(Length(binOut))+' bytes)');
Jens Geyer85827152018-01-12 21:20:59 +0100551 try
552 binIn := client.testBinary(binOut);
Jens Geyerbd1a2732019-06-26 22:52:44 +0200553 Expect( Length(binOut) = Length(binIn), 'testBinary('+IntToStr(Length(binOut))+' bytes): '+IntToStr(Length(binIn))+' bytes received');
Jens Geyer85827152018-01-12 21:20:59 +0100554 i32 := Min( Length(binOut), Length(binIn));
Jens Geyerbd1a2732019-06-26 22:52:44 +0200555 Expect( CompareMem( binOut, binIn, i32), 'testBinary('+IntToStr(Length(binOut))+' bytes): validating received data');
Jens Geyer85827152018-01-12 21:20:59 +0100556 except
557 on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message);
558 on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message);
559 end;
Jens Geyercf892d42017-09-09 10:08:22 +0200560 end;
561
Roger Meier3bef8c22012-10-06 06:58:00 +0000562 Console.WriteLine('testDouble(5.325098235)');
563 dub := client.testDouble(5.325098235);
564 Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
565
566 // structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200567 StartTestGroup( 'testStruct', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000568 Console.WriteLine('testStruct({''Zero'', 1, -3, -5})');
569 o := TXtructImpl.Create;
570 o.String_thing := 'Zero';
571 o.Byte_thing := 1;
572 o.I32_thing := -3;
573 o.I64_thing := -5;
574 i := client.testStruct(o);
575 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
576 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
577 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
578 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
579 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
580 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
581 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
582 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
583
584 // nested structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200585 StartTestGroup( 'testNest', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000586 Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})');
587 o2 := TXtruct2Impl.Create;
588 o2.Byte_thing := 1;
589 o2.Struct_thing := o;
590 o2.I32_thing := 5;
591 i2 := client.testNest(o2);
592 i := i2.Struct_thing;
593 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
594 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
595 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
596 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
597 Expect( i2.Byte_thing = 1, 'i2.Byte_thing = '+IntToStr(i2.Byte_thing));
598 Expect( i2.I32_thing = 5, 'i2.I32_thing = '+IntToStr(i2.I32_thing));
599 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
600 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
601 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
602 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
603 Expect( i2.__isset_Byte_thing, 'i2.__isset_Byte_thing');
604 Expect( i2.__isset_I32_thing, 'i2.__isset_I32_thing');
605
606 // map<type1,type2>: A map of strictly unique keys to values.
607 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200608 StartTestGroup( 'testMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000609 mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
610 for j := 0 to 4 do
611 begin
612 mapout.AddOrSetValue( j, j - 10);
613 end;
614 Console.Write('testMap({');
615 first := True;
616 for key in mapout.Keys do
617 begin
618 if first
619 then first := False
620 else Console.Write( ', ' );
621 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
622 end;
623 Console.WriteLine('})');
624
625 mapin := client.testMap( mapout );
626 Expect( mapin.Count = mapout.Count, 'testMap: mapin.Count = mapout.Count');
627 for j := 0 to 4 do
628 begin
629 Expect( mapout.ContainsKey(j), 'testMap: mapout.ContainsKey('+IntToStr(j)+') = '+BoolToString(mapout.ContainsKey(j)));
630 end;
631 for key in mapin.Keys do
632 begin
633 Expect( mapin[key] = mapout[key], 'testMap: '+IntToStr(key) + ' => ' + IntToStr( mapin[key]));
634 Expect( mapin[key] = key - 10, 'testMap: mapin['+IntToStr(key)+'] = '+IntToStr( mapin[key]));
635 end;
636
637
638 // map<type1,type2>: A map of strictly unique keys to values.
639 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200640 StartTestGroup( 'testStringMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000641 strmapout := TThriftDictionaryImpl<string,string>.Create;
642 for j := 0 to 4 do
643 begin
644 strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10));
645 end;
646 Console.Write('testStringMap({');
647 first := True;
648 for strkey in strmapout.Keys do
649 begin
650 if first
651 then first := False
652 else Console.Write( ', ' );
653 Console.Write( strkey + ' => ' + strmapout[strkey]);
654 end;
655 Console.WriteLine('})');
656
657 strmapin := client.testStringMap( strmapout );
658 Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count');
659 for j := 0 to 4 do
660 begin
661 Expect( strmapout.ContainsKey(IntToStr(j)),
662 'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = '
663 + BoolToString(strmapout.ContainsKey(IntToStr(j))));
664 end;
665 for strkey in strmapin.Keys do
666 begin
667 Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]);
668 Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]);
669 end;
670
671
672 // set<type>: An unordered set of unique elements.
673 // Translates to an STL set, Java HashSet, set in Python, etc.
674 // Note: PHP does not support sets, so it is treated similar to a List
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200675 StartTestGroup( 'testSet', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000676 setout := THashSetImpl<Integer>.Create;
677 for j := -2 to 2 do
678 begin
679 setout.Add( j );
680 end;
681 Console.Write('testSet({');
682 first := True;
683 for j in setout do
684 begin
685 if first
686 then first := False
687 else Console.Write(', ');
688 Console.Write(IntToStr( j));
689 end;
690 Console.WriteLine('})');
691
692 setin := client.testSet(setout);
693 Expect( setin.Count = setout.Count, 'testSet: setin.Count = setout.Count');
694 Expect( setin.Count = 5, 'testSet: setin.Count = '+IntToStr(setin.Count));
695 for j := -2 to 2 do // unordered, we can't rely on the order => test for known elements only
696 begin
697 Expect( setin.Contains(j), 'testSet: setin.Contains('+IntToStr(j)+') => '+BoolToString(setin.Contains(j)));
698 end;
699
700 // list<type>: An ordered list of elements.
701 // Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200702 StartTestGroup( 'testList', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000703 listout := TThriftListImpl<Integer>.Create;
704 listout.Add( +1);
705 listout.Add( -2);
706 listout.Add( +3);
707 listout.Add( -4);
708 listout.Add( 0);
709 Console.Write('testList({');
710 first := True;
711 for j in listout do
712 begin
713 if first
714 then first := False
715 else Console.Write(', ');
716 Console.Write(IntToStr( j));
717 end;
718 Console.WriteLine('})');
719
720 listin := client.testList(listout);
721 Expect( listin.Count = listout.Count, 'testList: listin.Count = listout.Count');
722 Expect( listin.Count = 5, 'testList: listin.Count = '+IntToStr(listin.Count));
723 Expect( listin[0] = +1, 'listin[0] = '+IntToStr( listin[0]));
724 Expect( listin[1] = -2, 'listin[1] = '+IntToStr( listin[1]));
725 Expect( listin[2] = +3, 'listin[2] = '+IntToStr( listin[2]));
726 Expect( listin[3] = -4, 'listin[3] = '+IntToStr( listin[3]));
727 Expect( listin[4] = 0, 'listin[4] = '+IntToStr( listin[4]));
728
729 // enums
730 ret := client.testEnum(TNumberz.ONE);
731 Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret)));
732
733 ret := client.testEnum(TNumberz.TWO);
734 Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret)));
735
736 ret := client.testEnum(TNumberz.THREE);
737 Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret)));
738
739 ret := client.testEnum(TNumberz.FIVE);
740 Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret)));
741
742 ret := client.testEnum(TNumberz.EIGHT);
743 Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret)));
744
745
746 // typedef
747 uid := client.testTypedef(309858235082523);
748 Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid));
749
750
751 // maps of maps
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200752 StartTestGroup( 'testMapMap(1)', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000753 mm := client.testMapMap(1);
754 Console.Write(' = {');
755 for key in mm.Keys do
756 begin
757 Console.Write( IntToStr( key) + ' => {');
758 m2 := mm[key];
759 for k2 in m2.Keys do
760 begin
761 Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', ');
762 end;
763 Console.Write('}, ');
764 end;
765 Console.WriteLine('}');
766
767 // verify result data
768 Expect( mm.Count = 2, 'mm.Count = '+IntToStr(mm.Count));
769 pos := mm[4];
770 neg := mm[-4];
771 for j := 1 to 4 do
772 begin
773 Expect( pos[j] = j, 'pos[j] = '+IntToStr(pos[j]));
774 Expect( neg[-j] = -j, 'neg[-j] = '+IntToStr(neg[-j]));
775 end;
776
777
778
779 // insanity
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200780 StartTestGroup( 'testInsanity', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000781 insane := TInsanityImpl.Create;
782 insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
783 insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000);
784 truck := TXtructImpl.Create;
785 truck.String_thing := 'Truck';
Jens Geyer540e3462016-12-28 14:25:41 +0100786 truck.Byte_thing := -8; // byte is signed
787 truck.I32_thing := 32;
788 truck.I64_thing := 64;
Roger Meier3bef8c22012-10-06 06:58:00 +0000789 insane.Xtructs := TThriftListImpl<IXtruct>.Create;
790 insane.Xtructs.Add( truck );
791 whoa := client.testInsanity( insane );
792 Console.Write(' = {');
793 for key64 in whoa.Keys do
794 begin
795 val := whoa[key64];
796 Console.Write( IntToStr( key64) + ' => {');
797 for k2_2 in val.Keys do
798 begin
799 v2 := val[k2_2];
800 Console.Write( IntToStr( Integer( k2_2)) + ' => {');
801 userMap := v2.UserMap;
802 Console.Write('{');
803 if userMap <> nil then
804 begin
805 for k3 in userMap.Keys do
806 begin
807 Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', ');
808 end;
809 end else
810 begin
811 Console.Write('null');
812 end;
813 Console.Write('}, ');
814 xtructs := v2.Xtructs;
815 Console.Write('{');
816
817 if xtructs <> nil then
818 begin
819 for x in xtructs do
820 begin
821 Console.Write('{"' + x.String_thing + '", ' +
822 IntToStr( x.Byte_thing) + ', ' +
823 IntToStr( x.I32_thing) + ', ' +
824 IntToStr( x.I32_thing) + '}, ');
825 end;
826 end else
827 begin
828 Console.Write('null');
829 end;
830 Console.Write('}');
831 Console.Write('}, ');
832 end;
833 Console.Write('}, ');
834 end;
835 Console.WriteLine('}');
836
Jens Geyer540e3462016-12-28 14:25:41 +0100837 (**
838 * So you think you've got this all worked, out eh?
839 *
840 * Creates a the returned map with these values and prints it out:
841 * { 1 => { 2 => argument,
842 * 3 => argument,
843 * },
844 * 2 => { 6 => <empty Insanity struct>, },
845 * }
846 * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
847 *)
848
Roger Meier3bef8c22012-10-06 06:58:00 +0000849 // verify result data
850 Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count));
851 //
852 first_map := whoa[1];
853 second_map := whoa[2];
854 Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count));
855 Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count));
856 //
857 looney := second_map[TNumberz.SIX];
858 Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney)));
859 Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
860 Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
861 //
862 for ret in [TNumberz.TWO, TNumberz.THREE] do begin
863 crazy := first_map[ret];
864 Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
865
866 Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap));
867 Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs));
868
Jens Geyer540e3462016-12-28 14:25:41 +0100869 Expect( crazy.UserMap.Count = insane.UserMap.Count, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count));
870 for pair in insane.UserMap do begin
871 Expect( crazy.UserMap[pair.Key] = pair.Value, 'crazy.UserMap['+IntToStr(Ord(pair.key))+'] = '+IntToStr(crazy.UserMap[pair.Key]));
872 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000873
Jens Geyer540e3462016-12-28 14:25:41 +0100874 Expect( crazy.Xtructs.Count = insane.Xtructs.Count, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count));
875 for arg0 := 0 to insane.Xtructs.Count-1 do begin
876 hello := insane.Xtructs[arg0];
877 goodbye := crazy.Xtructs[arg0];
878 Expect( goodbye.String_thing = hello.String_thing, 'goodbye.String_thing = '+goodbye.String_thing);
879 Expect( goodbye.Byte_thing = hello.Byte_thing, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing));
880 Expect( goodbye.I32_thing = hello.I32_thing, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing));
881 Expect( goodbye.I64_thing = hello.I64_thing, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing));
882 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000883 end;
884
885
886 // multi args
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200887 StartTestGroup( 'testMulti', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000888 arg0 := 1;
889 arg1 := 2;
890 arg2 := High(Int64);
891 arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
892 arg3.AddOrSetValue( 1, 'one');
893 arg4 := TNumberz.FIVE;
894 arg5 := 5000000;
895 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
896 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
897 arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
898 IntToStr( arg5) + ')');
899
900 i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5);
901 Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"');
902 Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing));
903 Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing));
904 Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing));
905 Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
906 Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
907 Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
908 Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
909
910 // multi exception
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200911 StartTestGroup( 'testMultiException(1)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000912 try
913 i := client.testMultiException( 'need more pizza', 'run out of beer');
914 Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"');
915 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
Jens Geyer6bbbf192014-09-07 01:45:56 +0200916 { this is not necessarily true, these fields are default-serialized
Jens Geyerd5436f52014-10-03 19:50:38 +0200917 Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
Roger Meier3bef8c22012-10-06 06:58:00 +0000918 Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
919 Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
Jens Geyerd5436f52014-10-03 19:50:38 +0200920 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000921 except
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200922 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000923 end;
924
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200925 StartTestGroup( 'testMultiException(Xception)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000926 try
927 i := client.testMultiException( 'Xception', 'second test');
928 Expect( FALSE, 'testMultiException(''Xception''): must trow an exception');
929 except
930 on x:TXception do begin
931 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
932 Expect( x.__isset_Message_, 'x.__isset_Message_ = '+BoolToString(x.__isset_Message_));
933 Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
934 Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"');
935 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200936 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000937 end;
938
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200939 StartTestGroup( 'testMultiException(Xception2)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000940 try
941 i := client.testMultiException( 'Xception2', 'third test');
942 Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception');
943 except
944 on x:TXception2 do begin
945 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
946 Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing));
947 Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
948 Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"');
949 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 +0200950 { this is not necessarily true, these fields are default-serialized
Roger Meier3bef8c22012-10-06 06:58:00 +0000951 Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing));
952 Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing));
953 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 +0200954 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000955 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200956 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000957 end;
958
959
960 // oneway functions
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200961 StartTestGroup( 'Test Oneway(1)', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000962 client.testOneway(1);
963 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
964
965 // call time
Jens Geyer06045cf2013-03-27 20:26:25 +0200966 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000967 StartTestGroup( 'Test Calltime()');
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200968 StartTick := GetTickCount;
Roger Meier3bef8c22012-10-06 06:58:00 +0000969 for k := 0 to 1000 - 1 do
970 begin
971 client.testVoid();
972 end;
973 Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
Jens Geyer06045cf2013-03-27 20:26:25 +0200974 {$ENDIF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000975
976 // no more tests here
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200977 StartTestGroup( '', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000978end;
979
980
Jens Geyer14f5d502017-12-09 13:47:09 +0100981{$IFDEF SupportsAsync}
982procedure TClientThread.ClientAsyncTest;
983var
984 client : TThriftTest.IAsync;
985 s : string;
986 i8 : ShortInt;
987begin
988 StartTestGroup( 'Async Tests', test_Unknown);
989 client := TThriftTest.TClient.Create( FProtocol);
990 FTransport.Open;
991
992 // oneway void functions
993 client.testOnewayAsync(1).Wait;
994 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
995
996 // normal functions
997 s := client.testStringAsync(HUGE_TEST_STRING).Value;
998 Expect( length(s) = length(HUGE_TEST_STRING),
999 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
1000 +'=> length(result) = '+IntToStr(Length(s)));
1001
1002 i8 := client.testByte(1).Value;
1003 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
1004end;
1005{$ENDIF}
1006
1007
Jens Geyer718f6ee2013-09-06 21:02:34 +02001008{$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +02001009procedure TClientThread.StressTest(const client : TThriftTest.Iface);
1010begin
1011 while TRUE do begin
1012 try
1013 if not FTransport.IsOpen then FTransport.Open; // re-open connection, server has already closed
1014 try
1015 client.testString('Test');
1016 Write('.');
1017 finally
1018 if FTransport.IsOpen then FTransport.Close;
1019 end;
1020 except
1021 on e:Exception do Writeln(#10+e.message);
1022 end;
1023 end;
1024end;
Jens Geyer718f6ee2013-09-06 21:02:34 +02001025{$ENDIF}
Jens Geyer06045cf2013-03-27 20:26:25 +02001026
Jens Geyerfd1b3582014-12-13 23:42:58 +01001027
Jens Geyer85827152018-01-12 21:20:59 +01001028function TClientThread.PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes;
Jens Geyerd4df9172017-10-25 22:30:23 +02001029var i : Integer;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001030begin
Jens Geyer85827152018-01-12 21:20:59 +01001031 case aSize of
1032 Empty : SetLength( result, 0);
1033 Normal : SetLength( result, $100);
1034 ByteArrayTest : SetLength( result, SizeOf(TByteArray) + 128);
1035 PipeWriteLimit : SetLength( result, 65535 + 128);
Jens Geyer2646bd62019-11-09 23:24:52 +01001036 FifteenMB : SetLength( result, 15 * 1024 * 1024);
Jens Geyer85827152018-01-12 21:20:59 +01001037 else
1038 raise EArgumentException.Create('aSize');
1039 end;
1040
Jens Geyerfd1b3582014-12-13 23:42:58 +01001041 ASSERT( Low(result) = 0);
Jens Geyer85827152018-01-12 21:20:59 +01001042 if Length(result) = 0 then Exit;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001043
1044 // linear distribution, unless random is requested
1045 if not aRandomDist then begin
1046 for i := Low(result) to High(result) do begin
Jens Geyer85827152018-01-12 21:20:59 +01001047 result[i] := i mod $100;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001048 end;
1049 Exit;
1050 end;
1051
1052 // random distribution of all 256 values
1053 FillChar( result[0], Length(result) * SizeOf(result[0]), $0);
Jens Geyerd4df9172017-10-25 22:30:23 +02001054 for i := Low(result) to High(result) do begin
1055 result[i] := Byte( Random($100));
Jens Geyerfd1b3582014-12-13 23:42:58 +01001056 end;
1057end;
1058
1059
Jens Geyerf7904452017-07-26 15:02:12 +02001060{$IFDEF Win64}
1061procedure TClientThread.UseInterlockedExchangeAdd64;
1062var a,b : Int64;
1063begin
1064 a := 1;
1065 b := 2;
1066 Thrift.Utils.InterlockedExchangeAdd64( a,b);
1067 Expect( a = 3, 'InterlockedExchangeAdd64');
1068end;
1069{$ENDIF}
1070
1071
Roger Meier3bef8c22012-10-06 06:58:00 +00001072procedure TClientThread.JSONProtocolReadWriteTest;
1073// Tests only then read/write procedures of the JSON protocol
1074// All tests succeed, if we can read what we wrote before
1075// Note that passing this test does not imply, that our JSON is really compatible to what
1076// other clients or servers expect as the real JSON. This is beyond the scope of this test.
1077var prot : IProtocol;
1078 stm : TStringStream;
Jens Geyer17c3ad92017-09-05 20:31:27 +02001079 list : TThriftList;
Jens Geyera019cda2019-11-09 23:24:52 +01001080 config : IThriftConfiguration;
Jens Geyercf892d42017-09-09 10:08:22 +02001081 binary, binRead, emptyBinary : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +00001082 i,iErr : Integer;
1083const
1084 TEST_SHORT = ShortInt( $FE);
1085 TEST_SMALL = SmallInt( $FEDC);
1086 TEST_LONG = LongInt( $FEDCBA98);
1087 TEST_I64 = Int64( $FEDCBA9876543210);
1088 TEST_DOUBLE = -1.234e-56;
1089 DELTA_DOUBLE = TEST_DOUBLE * 1e-14;
1090 TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars")
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001091 // Test THRIFT-2336 and THRIFT-3404 with U+1D11E (G Clef symbol) and 'Русское Название';
1092 G_CLEF_AND_CYRILLIC_TEXT = #$1d11e' '#$0420#$0443#$0441#$0441#$043a#$043e#$0435' '#$041d#$0430#$0437#$0432#$0430#$043d#$0438#$0435;
1093 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 +01001094 // test both possible solidus encodings
1095 SOLIDUS_JSON_DATA = '"one/two\/three"';
1096 SOLIDUS_EXCPECTED = 'one/two/three';
Roger Meier3bef8c22012-10-06 06:58:00 +00001097begin
1098 stm := TStringStream.Create;
1099 try
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001100 StartTestGroup( 'JsonProtocolTest', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001101
Jens Geyera019cda2019-11-09 23:24:52 +01001102 config := TThriftConfigurationImpl.Create;
1103
Roger Meier3bef8c22012-10-06 06:58:00 +00001104 // prepare binary data
Jens Geyer85827152018-01-12 21:20:59 +01001105 binary := PrepareBinaryData( FALSE, Normal);
Jens Geyercf892d42017-09-09 10:08:22 +02001106 SetLength( emptyBinary, 0); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001107
1108 // output setup
1109 prot := TJSONProtocolImpl.Create(
1110 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001111 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE), config));
Roger Meier3bef8c22012-10-06 06:58:00 +00001112
1113 // write
Jens Geyer17c3ad92017-09-05 20:31:27 +02001114 Init( list, TType.String_, 9);
1115 prot.WriteListBegin( list);
Roger Meier3bef8c22012-10-06 06:58:00 +00001116 prot.WriteBool( TRUE);
1117 prot.WriteBool( FALSE);
1118 prot.WriteByte( TEST_SHORT);
1119 prot.WriteI16( TEST_SMALL);
1120 prot.WriteI32( TEST_LONG);
1121 prot.WriteI64( TEST_I64);
1122 prot.WriteDouble( TEST_DOUBLE);
1123 prot.WriteString( TEST_STRING);
1124 prot.WriteBinary( binary);
Jens Geyercf892d42017-09-09 10:08:22 +02001125 prot.WriteString( ''); // empty string
1126 prot.WriteBinary( emptyBinary); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001127 prot.WriteListEnd;
1128
1129 // input setup
1130 Expect( stm.Position = stm.Size, 'Stream position/length after write');
1131 stm.Position := 0;
1132 prot := TJSONProtocolImpl.Create(
1133 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001134 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil, config));
Roger Meier3bef8c22012-10-06 06:58:00 +00001135
1136 // read and compare
1137 list := prot.ReadListBegin;
1138 Expect( list.ElementType = TType.String_, 'list element type');
1139 Expect( list.Count = 9, 'list element count');
1140 Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE');
1141 Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE');
1142 Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte');
1143 Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16');
1144 Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32');
1145 Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64');
1146 Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble');
1147 Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString');
1148 binRead := prot.ReadBinary;
Jens Geyercf892d42017-09-09 10:08:22 +02001149 Expect( Length(prot.ReadString) = 0, 'WriteString/ReadString (empty string)');
1150 Expect( Length(prot.ReadBinary) = 0, 'empty WriteBinary/ReadBinary (empty data block)');
Roger Meier3bef8c22012-10-06 06:58:00 +00001151 prot.ReadListEnd;
1152
1153 // test binary data
1154 Expect( Length(binary) = Length(binRead), 'Binary data length check');
1155 iErr := -1;
1156 for i := Low(binary) to High(binary) do begin
1157 if binary[i] <> binRead[i] then begin
1158 iErr := i;
1159 Break;
1160 end;
1161 end;
1162 if iErr < 0
1163 then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)')
1164 else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr));
1165
1166 Expect( stm.Position = stm.Size, 'Stream position after read');
1167
Jens Geyer7bb44a32014-02-07 22:24:37 +01001168
Jens Geyer21366942013-12-30 22:04:51 +01001169 // Solidus can be encoded in two ways. Make sure we can read both
1170 stm.Position := 0;
1171 stm.Size := 0;
1172 stm.WriteString(SOLIDUS_JSON_DATA);
1173 stm.Position := 0;
1174 prot := TJSONProtocolImpl.Create(
1175 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001176 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil, config));
Jens Geyer21366942013-12-30 22:04:51 +01001177 Expect( prot.ReadString = SOLIDUS_EXCPECTED, 'Solidus encoding');
1178
1179
Jens Geyer7bb44a32014-02-07 22:24:37 +01001180 // Widechars should work too. Do they?
1181 // After writing, we ensure that we are able to read it back
1182 // We can't assume hex-encoding, since (nearly) any Unicode char is valid JSON
1183 stm.Position := 0;
1184 stm.Size := 0;
1185 prot := TJSONProtocolImpl.Create(
1186 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001187 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE), config));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001188 prot.WriteString( G_CLEF_AND_CYRILLIC_TEXT);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001189 stm.Position := 0;
1190 prot := TJSONProtocolImpl.Create(
1191 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001192 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil, config));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001193 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Writing JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001194
1195 // Widechars should work with hex-encoding too. Do they?
1196 stm.Position := 0;
1197 stm.Size := 0;
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001198 stm.WriteString( G_CLEF_AND_CYRILLIC_JSON);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001199 stm.Position := 0;
1200 prot := TJSONProtocolImpl.Create(
1201 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001202 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil, config));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001203 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Reading JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001204
1205
Roger Meier3bef8c22012-10-06 06:58:00 +00001206 finally
1207 stm.Free;
1208 prot := nil; //-> Release
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001209 StartTestGroup( '', test_Unknown); // no more tests here
Roger Meier3bef8c22012-10-06 06:58:00 +00001210 end;
1211end;
1212
1213
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001214procedure TClientThread.StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +00001215begin
1216 FTestGroup := aGroup;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001217 FCurrentTest := aTest;
1218
1219 Include( FExecuted, aTest);
1220
Roger Meier3bef8c22012-10-06 06:58:00 +00001221 if FTestGroup <> '' then begin
1222 Console.WriteLine('');
1223 Console.WriteLine( aGroup+' tests');
1224 Console.WriteLine( StringOfChar('-',60));
1225 end;
1226end;
1227
1228
1229procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
1230begin
1231 if aTestResult then begin
1232 Inc(FSuccesses);
1233 Console.WriteLine( aTestInfo+': passed');
1234 end
1235 else begin
1236 FErrors.Add( FTestGroup+': '+aTestInfo);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001237 Include( FFailed, FCurrentTest);
Roger Meier3bef8c22012-10-06 06:58:00 +00001238 Console.WriteLine( aTestInfo+': *** FAILED ***');
1239
1240 // We have a failed test!
1241 // -> issue DebugBreak ONLY if a debugger is attached,
1242 // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise
Jens Geyer9f7f11e2016-04-14 21:37:11 +02001243 if IsDebuggerPresent
1244 then {$IFDEF CPUX64} DebugBreak {$ELSE} asm int 3 end {$ENDIF};
Roger Meier3bef8c22012-10-06 06:58:00 +00001245 end;
1246end;
1247
1248
1249procedure TClientThread.ReportResults;
1250var nTotal : Integer;
1251 sLine : string;
1252begin
1253 // prevent us from stupid DIV/0 errors
1254 nTotal := FSuccesses + FErrors.Count;
1255 if nTotal = 0 then begin
1256 Console.WriteLine('No results logged');
1257 Exit;
1258 end;
1259
1260 Console.WriteLine('');
1261 Console.WriteLine( StringOfChar('=',60));
1262 Console.WriteLine( IntToStr(nTotal)+' tests performed');
1263 Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)');
1264 Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)');
1265 Console.WriteLine( StringOfChar('=',60));
1266 if FErrors.Count > 0 then begin
1267 Console.WriteLine('FAILED TESTS:');
1268 for sLine in FErrors do Console.WriteLine('- '+sLine);
1269 Console.WriteLine( StringOfChar('=',60));
1270 InterlockedIncrement( ExitCode); // return <> 0 on errors
1271 end;
1272 Console.WriteLine('');
1273end;
1274
1275
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001276function TClientThread.CalculateExitCode : Byte;
1277var test : TTestGroup;
1278begin
1279 result := EXITCODE_SUCCESS;
1280 for test := Low(TTestGroup) to High(TTestGroup) do begin
1281 if (test in FFailed) or not (test in FExecuted)
1282 then result := result or MAP_FAILURES_TO_EXITCODE_BITS[test];
1283 end;
1284end;
1285
1286
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001287constructor TClientThread.Create( const aSetup : TTestSetup; const aNumIteration: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +00001288begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001289 FSetup := aSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +00001290 FNumIteration := ANumIteration;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001291
Roger Meier3bef8c22012-10-06 06:58:00 +00001292 FConsole := TThreadConsole.Create( Self );
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001293 FCurrentTest := test_Unknown;
Roger Meier3bef8c22012-10-06 06:58:00 +00001294
1295 // error list: keep correct order, allow for duplicates
1296 FErrors := TStringList.Create;
1297 FErrors.Sorted := FALSE;
1298 FErrors.Duplicates := dupAccept;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001299
1300 inherited Create( TRUE);
Roger Meier3bef8c22012-10-06 06:58:00 +00001301end;
1302
1303destructor TClientThread.Destroy;
1304begin
1305 FreeAndNil( FConsole);
1306 FreeAndNil( FErrors);
1307 inherited;
1308end;
1309
1310procedure TClientThread.Execute;
1311var
1312 i : Integer;
Roger Meier3bef8c22012-10-06 06:58:00 +00001313begin
1314 // perform all tests
1315 try
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001316 {$IFDEF Win64}
Jens Geyerf7904452017-07-26 15:02:12 +02001317 UseInterlockedExchangeAdd64;
Jens Geyer14f5d502017-12-09 13:47:09 +01001318 {$ENDIF}
Jens Geyer7bb44a32014-02-07 22:24:37 +01001319 JSONProtocolReadWriteTest;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001320
1321 // must be run in the context of the thread
1322 InitializeProtocolTransportStack;
1323 try
1324 for i := 0 to FNumIteration - 1 do begin
1325 ClientTest;
1326 {$IFDEF SupportsAsync}
1327 ClientAsyncTest;
1328 {$ENDIF}
1329 end;
1330
1331 // report the outcome
1332 ReportResults;
1333 SetReturnValue( CalculateExitCode);
1334
1335 finally
1336 ShutdownProtocolTransportStack;
Roger Meier3bef8c22012-10-06 06:58:00 +00001337 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001338
Roger Meier3bef8c22012-10-06 06:58:00 +00001339 except
1340 on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"');
1341 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001342end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001343
Roger Meier3bef8c22012-10-06 06:58:00 +00001344
Jens Geyera019cda2019-11-09 23:24:52 +01001345function TClientThread.InitializeHttpTransport( const aTimeoutSetting : Integer; const aConfig : IThriftConfiguration) : IHTTPClient;
Jens Geyer83ff7532019-06-06 22:46:03 +02001346var sUrl : string;
1347 comps : URL_COMPONENTS;
1348 dwChars : DWORD;
Jens Geyer02230912019-04-03 01:12:51 +02001349begin
1350 ASSERT( FSetup.endpoint in [trns_MsxmlHttp, trns_WinHttp]);
1351
1352 if FSetup.useSSL
1353 then sUrl := 'https://'
1354 else sUrl := 'http://';
1355
1356 sUrl := sUrl + FSetup.host;
1357
Jens Geyer83ff7532019-06-06 22:46:03 +02001358 // add the port number if necessary and at the right place
1359 FillChar( comps, SizeOf(comps), 0);
1360 comps.dwStructSize := SizeOf(comps);
1361 comps.dwSchemeLength := MAXINT;
1362 comps.dwHostNameLength := MAXINT;
1363 comps.dwUserNameLength := MAXINT;
1364 comps.dwPasswordLength := MAXINT;
1365 comps.dwUrlPathLength := MAXINT;
1366 comps.dwExtraInfoLength := MAXINT;
1367 Win32Check( WinHttpCrackUrl( PChar(sUrl), Length(sUrl), 0, comps));
Jens Geyer02230912019-04-03 01:12:51 +02001368 case FSetup.port of
Jens Geyer83ff7532019-06-06 22:46:03 +02001369 80 : if FSetup.useSSL then comps.nPort := FSetup.port;
1370 443 : if not FSetup.useSSL then comps.nPort := FSetup.port;
Jens Geyer02230912019-04-03 01:12:51 +02001371 else
Jens Geyer83ff7532019-06-06 22:46:03 +02001372 if FSetup.port > 0 then comps.nPort := FSetup.port;
Jens Geyer02230912019-04-03 01:12:51 +02001373 end;
Jens Geyer83ff7532019-06-06 22:46:03 +02001374 dwChars := Length(sUrl) + 64;
1375 SetLength( sUrl, dwChars);
1376 Win32Check( WinHttpCreateUrl( comps, 0, @sUrl[1], dwChars));
1377 SetLength( sUrl, dwChars);
1378
Jens Geyer02230912019-04-03 01:12:51 +02001379
1380 Console.WriteLine('Target URL: '+sUrl);
1381 case FSetup.endpoint of
Jens Geyera019cda2019-11-09 23:24:52 +01001382 trns_MsxmlHttp : result := TMsxmlHTTPClientImpl.Create( sUrl, aConfig);
1383 trns_WinHttp : result := TWinHTTPClientImpl.Create( sUrl, aConfig);
Jens Geyer02230912019-04-03 01:12:51 +02001384 else
1385 raise Exception.Create(ENDPOINT_TRANSPORTS[FSetup.endpoint]+' unhandled case');
1386 end;
1387
1388 result.DnsResolveTimeout := aTimeoutSetting;
1389 result.ConnectionTimeout := aTimeoutSetting;
1390 result.SendTimeout := aTimeoutSetting;
1391 result.ReadTimeout := aTimeoutSetting;
1392end;
1393
1394
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001395procedure TClientThread.InitializeProtocolTransportStack;
Jens Geyer02230912019-04-03 01:12:51 +02001396var streamtrans : IStreamTransport;
Jens Geyer47f63172019-06-06 22:42:58 +02001397 canSSL : Boolean;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001398const
1399 DEBUG_TIMEOUT = 30 * 1000;
1400 RELEASE_TIMEOUT = DEFAULT_THRIFT_TIMEOUT;
1401 PIPE_TIMEOUT = RELEASE_TIMEOUT;
1402 HTTP_TIMEOUTS = 10 * 1000;
1403begin
1404 // needed for HTTP clients as they utilize the MSXML COM components
1405 OleCheck( CoInitialize( nil));
1406
Jens Geyer47f63172019-06-06 22:42:58 +02001407 canSSL := FALSE;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001408 case FSetup.endpoint of
1409 trns_Sockets: begin
1410 Console.WriteLine('Using sockets ('+FSetup.host+' port '+IntToStr(FSetup.port)+')');
Jens Geyera019cda2019-11-09 23:24:52 +01001411 streamtrans := TSocketImpl.Create( FSetup.host, FSetup.port);
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001412 FTransport := streamtrans;
1413 end;
1414
Jens Geyer02230912019-04-03 01:12:51 +02001415 trns_MsxmlHttp,
1416 trns_WinHttp: begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001417 Console.WriteLine('Using HTTPClient');
Jens Geyer02230912019-04-03 01:12:51 +02001418 FTransport := InitializeHttpTransport( HTTP_TIMEOUTS);
Jens Geyer47f63172019-06-06 22:42:58 +02001419 canSSL := TRUE;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001420 end;
1421
1422 trns_EvHttp: begin
1423 raise Exception.Create(ENDPOINT_TRANSPORTS[FSetup.endpoint]+' transport not implemented');
1424 end;
1425
1426 trns_NamedPipes: begin
1427 streamtrans := TNamedPipeTransportClientEndImpl.Create( FSetup.sPipeName, 0, nil, PIPE_TIMEOUT, PIPE_TIMEOUT);
1428 FTransport := streamtrans;
1429 end;
1430
1431 trns_AnonPipes: begin
Jens Geyera019cda2019-11-09 23:24:52 +01001432 streamtrans := TAnonymousPipeTransportImpl.Create( FSetup.hAnonRead, FSetup.hAnonWrite, FALSE, PIPE_TIMEOUT);
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001433 FTransport := streamtrans;
1434 end;
1435
1436 else
1437 raise Exception.Create('Unhandled endpoint transport');
1438 end;
1439 ASSERT( FTransport <> nil);
1440
1441 // layered transports are not really meant to be stacked upon each other
1442 if (trns_Framed in FSetup.layered) then begin
1443 FTransport := TFramedTransportImpl.Create( FTransport);
1444 end
1445 else if (trns_Buffered in FSetup.layered) and (streamtrans <> nil) then begin
1446 FTransport := TBufferedTransportImpl.Create( streamtrans, 32); // small buffer to test read()
1447 end;
1448
Jens Geyer47f63172019-06-06 22:42:58 +02001449 if FSetup.useSSL and not canSSL then begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001450 raise Exception.Create('SSL/TLS not implemented');
1451 end;
1452
1453 // create protocol instance, default to BinaryProtocol
1454 case FSetup.protType of
1455 prot_Binary : FProtocol := TBinaryProtocolImpl.Create( FTransport, BINARY_STRICT_READ, BINARY_STRICT_WRITE);
1456 prot_JSON : FProtocol := TJSONProtocolImpl.Create( FTransport);
1457 prot_Compact : FProtocol := TCompactProtocolImpl.Create( FTransport);
1458 else
1459 raise Exception.Create('Unhandled protocol');
1460 end;
1461
1462 ASSERT( (FTransport <> nil) and (FProtocol <> nil));
1463end;
1464
1465
1466procedure TClientThread.ShutdownProtocolTransportStack;
1467begin
1468 try
1469 FProtocol := nil;
1470
1471 if FTransport <> nil then begin
Roger Meier3bef8c22012-10-06 06:58:00 +00001472 FTransport.Close;
1473 FTransport := nil;
1474 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001475
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001476 finally
1477 CoUninitialize;
1478 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001479end;
1480
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001481
Roger Meier3bef8c22012-10-06 06:58:00 +00001482{ TThreadConsole }
1483
1484constructor TThreadConsole.Create(AThread: TThread);
1485begin
Jens Geyer718f6ee2013-09-06 21:02:34 +02001486 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +00001487 FThread := AThread;
1488end;
1489
1490procedure TThreadConsole.Write(const S: string);
1491var
1492 proc : TThreadProcedure;
1493begin
1494 proc := procedure
1495 begin
1496 Console.Write( S );
1497 end;
1498 TThread.Synchronize( FThread, proc);
1499end;
1500
1501procedure TThreadConsole.WriteLine(const S: string);
1502var
1503 proc : TThreadProcedure;
1504begin
1505 proc := procedure
1506 begin
1507 Console.WriteLine( S );
1508 end;
1509 TThread.Synchronize( FThread, proc);
1510end;
1511
1512initialization
1513begin
1514 TTestClient.FNumIteration := 1;
1515 TTestClient.FNumThread := 1;
1516end;
1517
1518end.