blob: df0b4c572f53b9fc6a2a6dbbc6d8bb0a1736fec4 [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 Thrift.Configuration,
Jens Geyer3d556242018-01-24 19:14:32 +010057 Thrift.Collections;
Roger Meier3bef8c22012-10-06 06:58:00 +000058
59type
60 TThreadConsole = class
61 private
62 FThread : TThread;
63 public
64 procedure Write( const S : string);
65 procedure WriteLine( const S : string);
66 constructor Create( AThread: TThread);
67 end;
68
Jens Geyeraf7ecd62018-06-22 22:41:27 +020069 TTestSetup = record
70 protType : TKnownProtocol;
71 endpoint : TEndpointTransport;
72 layered : TLayeredTransports;
73 useSSL : Boolean; // include where appropriate (TLayeredTransport?)
74 host : string;
75 port : Integer;
76 sPipeName : string;
77 hAnonRead, hAnonWrite : THandle;
78 end;
79
Roger Meier3bef8c22012-10-06 06:58:00 +000080 TClientThread = class( TThread )
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020081 private type
82 TTestGroup = (
83 test_Unknown,
84 test_BaseTypes,
85 test_Structs,
86 test_Containers,
87 test_Exceptions
88 // new values here
89 );
90 TTestGroups = set of TTestGroup;
91
Jens Geyer85827152018-01-12 21:20:59 +010092 TTestSize = (
93 Empty, // Edge case: the zero-length empty binary
94 Normal, // Fairly small array of usual size (256 bytes)
95 ByteArrayTest, // THRIFT-4454 Large writes/reads may cause range check errors in debug mode
Jens Geyerbd1a2732019-06-26 22:52:44 +020096 PipeWriteLimit, // THRIFT-4372 Pipe write operations across a network are limited to 65,535 bytes per write.
Jens Geyer2646bd62019-11-09 23:24:52 +010097 FifteenMB // quite a bit of data, but still below the default max frame size
Jens Geyer85827152018-01-12 21:20:59 +010098 );
99
Roger Meier3bef8c22012-10-06 06:58:00 +0000100 private
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200101 FSetup : TTestSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +0000102 FTransport : ITransport;
103 FProtocol : IProtocol;
104 FNumIteration : Integer;
105 FConsole : TThreadConsole;
106
107 // test reporting, will be refactored out into separate class later
108 FTestGroup : string;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200109 FCurrentTest : TTestGroup;
Roger Meier3bef8c22012-10-06 06:58:00 +0000110 FSuccesses : Integer;
111 FErrors : TStringList;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200112 FFailed : TTestGroups;
113 FExecuted : TTestGroups;
114 procedure StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +0000115 procedure Expect( aTestResult : Boolean; const aTestInfo : string);
116 procedure ReportResults;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100117 function CalculateExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000118
119 procedure ClientTest;
Jens Geyer14f5d502017-12-09 13:47:09 +0100120 {$IFDEF SupportsAsync}
121 procedure ClientAsyncTest;
122 {$ENDIF}
123
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200124 procedure InitializeProtocolTransportStack;
125 procedure ShutdownProtocolTransportStack;
Jens Geyera019cda2019-11-09 23:24:52 +0100126 function InitializeHttpTransport( const aTimeoutSetting : Integer; const aConfig : IThriftConfiguration = nil) : IHTTPClient;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200127
Roger Meier3bef8c22012-10-06 06:58:00 +0000128 procedure JSONProtocolReadWriteTest;
Jens Geyer85827152018-01-12 21:20:59 +0100129 function PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes;
Jens Geyer718f6ee2013-09-06 21:02:34 +0200130 {$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200131 procedure StressTest(const client : TThriftTest.Iface);
Jens Geyer718f6ee2013-09-06 21:02:34 +0200132 {$ENDIF}
Jens Geyerf7904452017-07-26 15:02:12 +0200133 {$IFDEF Win64}
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200134 procedure UseInterlockedExchangeAdd64;
Jens Geyerf7904452017-07-26 15:02:12 +0200135 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000136 protected
137 procedure Execute; override;
138 public
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200139 constructor Create( const aSetup : TTestSetup; const aNumIteration: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +0000140 destructor Destroy; override;
141 end;
142
143 TTestClient = class
144 private
145 class var
146 FNumIteration : Integer;
147 FNumThread : Integer;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200148
149 class procedure PrintCmdLineHelp;
150 class procedure InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000151 public
Jens Geyeraeda9872020-03-22 15:01:28 +0100152 class function Execute( const arguments: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000153 end;
154
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200155
Roger Meier3bef8c22012-10-06 06:58:00 +0000156implementation
157
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200158const
159 EXITCODE_SUCCESS = $00; // no errors bits set
160 //
161 EXITCODE_FAILBIT_BASETYPES = $01;
162 EXITCODE_FAILBIT_STRUCTS = $02;
163 EXITCODE_FAILBIT_CONTAINERS = $04;
164 EXITCODE_FAILBIT_EXCEPTIONS = $08;
165
166 MAP_FAILURES_TO_EXITCODE_BITS : array[TClientThread.TTestGroup] of Byte = (
167 EXITCODE_SUCCESS, // no bits here
168 EXITCODE_FAILBIT_BASETYPES,
169 EXITCODE_FAILBIT_STRUCTS,
170 EXITCODE_FAILBIT_CONTAINERS,
171 EXITCODE_FAILBIT_EXCEPTIONS
172 );
173
174
175
Roger Meier3bef8c22012-10-06 06:58:00 +0000176function BoolToString( b : Boolean) : string;
177// overrides global BoolToString()
178begin
179 if b
180 then result := 'true'
181 else result := 'false';
182end;
183
184// not available in all versions, so make sure we have this one imported
185function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent';
186
187{ TTestClient }
188
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200189class procedure TTestClient.PrintCmdLineHelp;
190const HELPTEXT = ' [options]'#10
191 + #10
192 + 'Allowed options:'#10
Jens Geyeraeda9872020-03-22 15:01:28 +0100193 + ' -h | --help Produces this help message'#10
194 + ' --host=arg (localhost) Host to connect'#10
195 + ' --port=arg (9090) Port number to connect'#10
196 + ' --pipe=arg Windows Named Pipe (e.g. MyThriftPipe)'#10
197 + ' --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)'#10
198 + ' --transport=arg (sockets) Transport: buffered, framed, http, winhttp'#10
199 + ' --protocol=arg (binary) Protocol: binary, compact, json'#10
200 + ' --ssl Encrypted Transport using SSL'#10
201 + ' -n=num | --testloops=num (1) Number of Tests'#10
202 + ' -t=num | --threads=num (1) Number of Test threads'#10
203 + ' --performance Run the built-in performance test (no other arguments)'#10
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200204 ;
205begin
206 Writeln( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + HELPTEXT);
207end;
208
209class procedure TTestClient.InvalidArgs;
210begin
211 Console.WriteLine( 'Invalid args.');
212 Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + ' -h for more information');
213 Abort;
214end;
215
Jens Geyeraeda9872020-03-22 15:01:28 +0100216class function TTestClient.Execute(const arguments: array of string) : Byte;
217
218 function IsSwitch( const aArgument, aSwitch : string; out sValue : string) : Boolean;
219 begin
220 sValue := '';
221 result := (Copy( aArgument, 1, Length(aSwitch)) = aSwitch);
222 if result then begin
223 if (Copy( aArgument, 1, Length(aSwitch)+1) = (aSwitch+'='))
224 then sValue := Copy( aArgument, Length(aSwitch)+2, MAXINT);
225 end;
226 end;
227
Roger Meier3bef8c22012-10-06 06:58:00 +0000228var
Jens Geyeraeda9872020-03-22 15:01:28 +0100229 iArg : Integer;
Jens Geyer14f5d502017-12-09 13:47:09 +0100230 threadExitCode : Byte;
Jens Geyeraeda9872020-03-22 15:01:28 +0100231 sArg, sValue : string;
Roger Meier3bef8c22012-10-06 06:58:00 +0000232 threads : array of TThread;
233 dtStart : TDateTime;
234 test : Integer;
235 thread : TThread;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200236 setup : TTestSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +0000237begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200238 // init record
239 with setup do begin
240 protType := prot_Binary;
241 endpoint := trns_Sockets;
242 layered := [];
243 useSSL := FALSE;
244 host := 'localhost';
245 port := 9090;
246 sPipeName := '';
247 hAnonRead := INVALID_HANDLE_VALUE;
248 hAnonWrite := INVALID_HANDLE_VALUE;
249 end;
250
Roger Meier3bef8c22012-10-06 06:58:00 +0000251 try
Jens Geyeraeda9872020-03-22 15:01:28 +0100252 iArg := 0;
253 while iArg < Length(arguments) do begin
254 sArg := arguments[iArg];
255 Inc(iArg);
Roger Meier3bef8c22012-10-06 06:58:00 +0000256
Jens Geyeraeda9872020-03-22 15:01:28 +0100257 if IsSwitch( sArg, '-h', sValue)
258 or IsSwitch( sArg, '--help', sValue)
259 then begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200260 // -h [ --help ] produce help message
261 PrintCmdLineHelp;
262 result := $FF; // all tests failed
263 Exit;
264 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100265 else if IsSwitch( sArg, '--host', sValue) then begin
Jens Geyerb360b652014-09-28 01:55:46 +0200266 // --host arg (=localhost) Host to connect
Jens Geyeraeda9872020-03-22 15:01:28 +0100267 setup.host := sValue;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200268 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100269 else if IsSwitch( sArg, '--port', sValue) then begin
Jens Geyerb360b652014-09-28 01:55:46 +0200270 // --port arg (=9090) Port number to connect
Jens Geyeraeda9872020-03-22 15:01:28 +0100271 setup.port := StrToIntDef(sValue,0);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200272 if setup.port <= 0 then InvalidArgs;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200273 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100274 else if IsSwitch( sArg, '--domain-socket', sValue) then begin
Jens Geyerb360b652014-09-28 01:55:46 +0200275 // --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200276 raise Exception.Create('domain-socket not supported');
277 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100278 // --pipe arg Windows Named Pipe (e.g. MyThriftPipe)
279 else if IsSwitch( sArg, '--pipe', sValue) then begin
Jens Geyer4a33b182020-03-22 13:46:34 +0100280 // --pipe arg Windows Named Pipe (e.g. MyThriftPipe)
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200281 setup.endpoint := trns_NamedPipes;
Jens Geyeraeda9872020-03-22 15:01:28 +0100282 setup.sPipeName := sValue;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200283 Console.WriteLine('Using named pipe ('+setup.sPipeName+')');
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200284 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100285 else if IsSwitch( sArg, '--anon-pipes', sValue) then begin
Jens Geyerb360b652014-09-28 01:55:46 +0200286 // --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200287 setup.endpoint := trns_AnonPipes;
Jens Geyeraeda9872020-03-22 15:01:28 +0100288 setup.hAnonRead := THandle( StrToIntDef( arguments[iArg], Integer(INVALID_HANDLE_VALUE)));
289 Inc(iArg);
290 setup.hAnonWrite := THandle( StrToIntDef( arguments[iArg], Integer(INVALID_HANDLE_VALUE)));
291 Inc(iArg);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200292 Console.WriteLine('Using anonymous pipes ('+IntToStr(Integer(setup.hAnonRead))+' and '+IntToStr(Integer(setup.hAnonWrite))+')');
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200293 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100294 else if IsSwitch( sArg, '--transport', sValue) then begin
Jens Geyer02230912019-04-03 01:12:51 +0200295 // --transport arg (=sockets) Transport: buffered, framed, http, winhttp, evhttp
Jens Geyeraeda9872020-03-22 15:01:28 +0100296 if sValue = 'buffered' then Include( setup.layered, trns_Buffered)
297 else if sValue = 'framed' then Include( setup.layered, trns_Framed)
298 else if sValue = 'http' then setup.endpoint := trns_MsXmlHttp
299 else if sValue = 'winhttp' then setup.endpoint := trns_WinHttp
300 else if sValue = 'evhttp' then setup.endpoint := trns_EvHttp // recognized, but not supported
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200301 else InvalidArgs;
302 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100303 else if IsSwitch( sArg, '--protocol', sValue) then begin
Jens Geyerb360b652014-09-28 01:55:46 +0200304 // --protocol arg (=binary) Protocol: binary, compact, json
Jens Geyeraeda9872020-03-22 15:01:28 +0100305 if sValue = 'binary' then setup.protType := prot_Binary
306 else if sValue = 'compact' then setup.protType := prot_Compact
307 else if sValue = 'json' then setup.protType := prot_JSON
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200308 else InvalidArgs;
309 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100310 else if IsSwitch( sArg, '--ssl', sValue) then begin
Jens Geyerb360b652014-09-28 01:55:46 +0200311 // --ssl Encrypted Transport using SSL
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200312 setup.useSSL := TRUE;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200313
314 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100315 else if IsSwitch( sArg, '-n', sValue) or IsSwitch( sArg, '--testloops', sValue) then begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200316 // -n [ --testloops ] arg (=1) Number of Tests
Jens Geyeraeda9872020-03-22 15:01:28 +0100317 FNumIteration := StrToIntDef( sValue, 0);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200318 if FNumIteration <= 0
319 then InvalidArgs;
320
321 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100322 else if IsSwitch( sArg, '-t', sValue) or IsSwitch( sArg, '--threads', sValue) then begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200323 // -t [ --threads ] arg (=1) Number of Test threads
Jens Geyeraeda9872020-03-22 15:01:28 +0100324 FNumThread := StrToIntDef( sValue, 0);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200325 if FNumThread <= 0
326 then InvalidArgs;
327 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100328 else if IsSwitch( sArg, '--performance', sValue) then begin
Jens Geyerb342bd92019-06-03 20:27:00 +0200329 result := TPerformanceTests.Execute;
330 Exit;
331 end
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200332 else begin
333 InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000334 end;
335 end;
336
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200337
Roger Meier79655fb2012-10-20 20:59:41 +0000338 // In the anonymous pipes mode the client is launched by the test server
339 // -> behave nicely and allow for attaching a debugger to this process
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200340 if (setup.endpoint = trns_AnonPipes) and not IsDebuggerPresent
Roger Meier79655fb2012-10-20 20:59:41 +0000341 then MessageBox( 0, 'Attach Debugger and/or click OK to continue.',
342 'Thrift TestClient (Delphi)',
343 MB_OK or MB_ICONEXCLAMATION);
344
Roger Meier3bef8c22012-10-06 06:58:00 +0000345 SetLength( threads, FNumThread);
346 dtStart := Now;
347
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200348 // layered transports are not really meant to be stacked upon each other
349 if (trns_Framed in setup.layered) then begin
350 Console.WriteLine('Using framed transport');
351 end
352 else if (trns_Buffered in setup.layered) then begin
353 Console.WriteLine('Using buffered transport');
354 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000355
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200356 Console.WriteLine(THRIFT_PROTOCOLS[setup.protType]+' protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000357
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200358 for test := 0 to FNumThread - 1 do begin
359 thread := TClientThread.Create( setup, FNumIteration);
Roger Meier3bef8c22012-10-06 06:58:00 +0000360 threads[test] := thread;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200361 thread.Start;
Roger Meier3bef8c22012-10-06 06:58:00 +0000362 end;
363
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200364 result := 0;
365 for test := 0 to FNumThread - 1 do begin
Jens Geyer14f5d502017-12-09 13:47:09 +0100366 threadExitCode := threads[test].WaitFor;
367 result := result or threadExitCode;
Jens Geyer14f5d502017-12-09 13:47:09 +0100368 threads[test].Free;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200369 threads[test] := nil;
Jens Geyer14f5d502017-12-09 13:47:09 +0100370 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000371
372 Console.Write('Total time: ' + IntToStr( MilliSecondsBetween(Now, dtStart)));
373
374 except
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200375 on E: EAbort do raise;
376 on E: Exception do begin
377 Console.WriteLine( E.Message + #10 + E.StackTrace);
378 raise;
Roger Meier3bef8c22012-10-06 06:58:00 +0000379 end;
380 end;
381
382 Console.WriteLine('');
383 Console.WriteLine('done!');
384end;
385
386{ TClientThread }
387
388procedure TClientThread.ClientTest;
389var
390 client : TThriftTest.Iface;
391 s : string;
392 i8 : ShortInt;
393 i32 : Integer;
394 i64 : Int64;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100395 binOut,binIn : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000396 dub : Double;
397 o : IXtruct;
398 o2 : IXtruct2;
399 i : IXtruct;
400 i2 : IXtruct2;
401 mapout : IThriftDictionary<Integer,Integer>;
402 mapin : IThriftDictionary<Integer,Integer>;
403 strmapout : IThriftDictionary<string,string>;
404 strmapin : IThriftDictionary<string,string>;
405 j : Integer;
406 first : Boolean;
407 key : Integer;
408 strkey : string;
409 listout : IThriftList<Integer>;
410 listin : IThriftList<Integer>;
411 setout : IHashSet<Integer>;
412 setin : IHashSet<Integer>;
413 ret : TNumberz;
414 uid : Int64;
415 mm : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
416 pos : IThriftDictionary<Integer, Integer>;
417 neg : IThriftDictionary<Integer, Integer>;
418 m2 : IThriftDictionary<Integer, Integer>;
419 k2 : Integer;
420 insane : IInsanity;
421 truck : IXtruct;
422 whoa : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
423 key64 : Int64;
424 val : IThriftDictionary<TNumberz, IInsanity>;
425 k2_2 : TNumberz;
426 k3 : TNumberz;
427 v2 : IInsanity;
428 userMap : IThriftDictionary<TNumberz, Int64>;
429 xtructs : IThriftList<IXtruct>;
430 x : IXtruct;
431 arg0 : ShortInt;
432 arg1 : Integer;
433 arg2 : Int64;
434 arg3 : IThriftDictionary<SmallInt, string>;
435 arg4 : TNumberz;
436 arg5 : Int64;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200437 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000438 StartTick : Cardinal;
439 k : Integer;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200440 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000441 hello, goodbye : IXtruct;
442 crazy : IInsanity;
443 looney : IInsanity;
444 first_map : IThriftDictionary<TNumberz, IInsanity>;
445 second_map : IThriftDictionary<TNumberz, IInsanity>;
Jens Geyer540e3462016-12-28 14:25:41 +0100446 pair : TPair<TNumberz, TUserId>;
Jens Geyer85827152018-01-12 21:20:59 +0100447 testsize : TTestSize;
Roger Meier3bef8c22012-10-06 06:58:00 +0000448begin
449 client := TThriftTest.TClient.Create( FProtocol);
450 FTransport.Open;
451
Jens Geyer06045cf2013-03-27 20:26:25 +0200452 {$IFDEF StressTest}
453 StressTest( client);
454 {$ENDIF StressTest}
455
Jens Geyer17c3ad92017-09-05 20:31:27 +0200456 {$IFDEF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000457 // in-depth exception test
458 // (1) do we get an exception at all?
459 // (2) do we get the right exception?
460 // (3) does the exception contain the expected data?
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200461 StartTestGroup( 'testException', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000462 // case 1: exception type declared in IDL at the function call
463 try
464 client.testException('Xception');
465 Expect( FALSE, 'testException(''Xception''): must trow an exception');
466 except
467 on e:TXception do begin
468 Expect( e.ErrorCode = 1001, 'error code');
469 Expect( e.Message_ = 'Xception', 'error message');
470 Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ );
471 end;
472 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200473 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000474 end;
475
Jens Geyercc8c2c62021-03-29 22:38:30 +0200476 // re-open connection if needed
477 if not FTransport.IsOpen
478 then FTransport.Open;
479
Roger Meier3bef8c22012-10-06 06:58:00 +0000480 // 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;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200492 on e:TException do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
493 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000494 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+'"');
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200507 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000508 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200509 {$ENDIF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000510
Jens Geyercc8c2c62021-03-29 22:38:30 +0200511 // re-open connection if needed
512 if not FTransport.IsOpen
513 then FTransport.Open;
Roger Meier3bef8c22012-10-06 06:58:00 +0000514
515 // simple things
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200516 StartTestGroup( 'simple Thrift calls', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000517 client.testVoid();
518 Expect( TRUE, 'testVoid()'); // success := no exception
519
Jens Geyer39ba6b72015-09-22 00:00:49 +0200520 s := BoolToString( client.testBool(TRUE));
521 Expect( s = BoolToString(TRUE), 'testBool(TRUE) = '+s);
522 s := BoolToString( client.testBool(FALSE));
523 Expect( s = BoolToString(FALSE), 'testBool(FALSE) = '+s);
524
Roger Meier3bef8c22012-10-06 06:58:00 +0000525 s := client.testString('Test');
526 Expect( s = 'Test', 'testString(''Test'') = "'+s+'"');
527
Jens Geyercf892d42017-09-09 10:08:22 +0200528 s := client.testString(''); // empty string
529 Expect( s = '', 'testString('''') = "'+s+'"');
530
Jens Geyer06045cf2013-03-27 20:26:25 +0200531 s := client.testString(HUGE_TEST_STRING);
532 Expect( length(s) = length(HUGE_TEST_STRING),
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100533 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
Jens Geyer06045cf2013-03-27 20:26:25 +0200534 +'=> length(result) = '+IntToStr(Length(s)));
535
Roger Meier3bef8c22012-10-06 06:58:00 +0000536 i8 := client.testByte(1);
537 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
538
539 i32 := client.testI32(-1);
540 Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32));
541
542 Console.WriteLine('testI64(-34359738368)');
543 i64 := client.testI64(-34359738368);
544 Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
545
Jens Geyerd4df9172017-10-25 22:30:23 +0200546 // random binary small
Jens Geyer85827152018-01-12 21:20:59 +0100547 for testsize := Low(TTestSize) to High(TTestSize) do begin
548 binOut := PrepareBinaryData( TRUE, testsize);
Jens Geyerbd1a2732019-06-26 22:52:44 +0200549 Console.WriteLine('testBinary('+IntToStr(Length(binOut))+' bytes)');
Jens Geyer85827152018-01-12 21:20:59 +0100550 try
551 binIn := client.testBinary(binOut);
Jens Geyerbd1a2732019-06-26 22:52:44 +0200552 Expect( Length(binOut) = Length(binIn), 'testBinary('+IntToStr(Length(binOut))+' bytes): '+IntToStr(Length(binIn))+' bytes received');
Jens Geyer85827152018-01-12 21:20:59 +0100553 i32 := Min( Length(binOut), Length(binIn));
Jens Geyerbd1a2732019-06-26 22:52:44 +0200554 Expect( CompareMem( binOut, binIn, i32), 'testBinary('+IntToStr(Length(binOut))+' bytes): validating received data');
Jens Geyer85827152018-01-12 21:20:59 +0100555 except
556 on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message);
557 on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message);
558 end;
Jens Geyercf892d42017-09-09 10:08:22 +0200559 end;
560
Roger Meier3bef8c22012-10-06 06:58:00 +0000561 Console.WriteLine('testDouble(5.325098235)');
562 dub := client.testDouble(5.325098235);
563 Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
564
565 // structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200566 StartTestGroup( 'testStruct', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000567 Console.WriteLine('testStruct({''Zero'', 1, -3, -5})');
568 o := TXtructImpl.Create;
569 o.String_thing := 'Zero';
570 o.Byte_thing := 1;
571 o.I32_thing := -3;
572 o.I64_thing := -5;
573 i := client.testStruct(o);
574 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
575 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
576 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
577 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
578 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
579 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
580 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
581 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
582
583 // nested structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200584 StartTestGroup( 'testNest', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000585 Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})');
586 o2 := TXtruct2Impl.Create;
587 o2.Byte_thing := 1;
588 o2.Struct_thing := o;
589 o2.I32_thing := 5;
590 i2 := client.testNest(o2);
591 i := i2.Struct_thing;
592 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
593 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
594 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
595 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
596 Expect( i2.Byte_thing = 1, 'i2.Byte_thing = '+IntToStr(i2.Byte_thing));
597 Expect( i2.I32_thing = 5, 'i2.I32_thing = '+IntToStr(i2.I32_thing));
598 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
599 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
600 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
601 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
602 Expect( i2.__isset_Byte_thing, 'i2.__isset_Byte_thing');
603 Expect( i2.__isset_I32_thing, 'i2.__isset_I32_thing');
604
605 // map<type1,type2>: A map of strictly unique keys to values.
606 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200607 StartTestGroup( 'testMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000608 mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
609 for j := 0 to 4 do
610 begin
611 mapout.AddOrSetValue( j, j - 10);
612 end;
613 Console.Write('testMap({');
614 first := True;
615 for key in mapout.Keys do
616 begin
617 if first
618 then first := False
619 else Console.Write( ', ' );
620 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
621 end;
622 Console.WriteLine('})');
623
624 mapin := client.testMap( mapout );
625 Expect( mapin.Count = mapout.Count, 'testMap: mapin.Count = mapout.Count');
626 for j := 0 to 4 do
627 begin
628 Expect( mapout.ContainsKey(j), 'testMap: mapout.ContainsKey('+IntToStr(j)+') = '+BoolToString(mapout.ContainsKey(j)));
629 end;
630 for key in mapin.Keys do
631 begin
632 Expect( mapin[key] = mapout[key], 'testMap: '+IntToStr(key) + ' => ' + IntToStr( mapin[key]));
633 Expect( mapin[key] = key - 10, 'testMap: mapin['+IntToStr(key)+'] = '+IntToStr( mapin[key]));
634 end;
635
636
637 // map<type1,type2>: A map of strictly unique keys to values.
638 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200639 StartTestGroup( 'testStringMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000640 strmapout := TThriftDictionaryImpl<string,string>.Create;
641 for j := 0 to 4 do
642 begin
643 strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10));
644 end;
645 Console.Write('testStringMap({');
646 first := True;
647 for strkey in strmapout.Keys do
648 begin
649 if first
650 then first := False
651 else Console.Write( ', ' );
652 Console.Write( strkey + ' => ' + strmapout[strkey]);
653 end;
654 Console.WriteLine('})');
655
656 strmapin := client.testStringMap( strmapout );
657 Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count');
658 for j := 0 to 4 do
659 begin
660 Expect( strmapout.ContainsKey(IntToStr(j)),
661 'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = '
662 + BoolToString(strmapout.ContainsKey(IntToStr(j))));
663 end;
664 for strkey in strmapin.Keys do
665 begin
666 Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]);
667 Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]);
668 end;
669
670
671 // set<type>: An unordered set of unique elements.
672 // Translates to an STL set, Java HashSet, set in Python, etc.
673 // Note: PHP does not support sets, so it is treated similar to a List
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200674 StartTestGroup( 'testSet', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000675 setout := THashSetImpl<Integer>.Create;
676 for j := -2 to 2 do
677 begin
678 setout.Add( j );
679 end;
680 Console.Write('testSet({');
681 first := True;
682 for j in setout do
683 begin
684 if first
685 then first := False
686 else Console.Write(', ');
687 Console.Write(IntToStr( j));
688 end;
689 Console.WriteLine('})');
690
691 setin := client.testSet(setout);
692 Expect( setin.Count = setout.Count, 'testSet: setin.Count = setout.Count');
693 Expect( setin.Count = 5, 'testSet: setin.Count = '+IntToStr(setin.Count));
694 for j := -2 to 2 do // unordered, we can't rely on the order => test for known elements only
695 begin
696 Expect( setin.Contains(j), 'testSet: setin.Contains('+IntToStr(j)+') => '+BoolToString(setin.Contains(j)));
697 end;
698
699 // list<type>: An ordered list of elements.
700 // Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200701 StartTestGroup( 'testList', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000702 listout := TThriftListImpl<Integer>.Create;
703 listout.Add( +1);
704 listout.Add( -2);
705 listout.Add( +3);
706 listout.Add( -4);
707 listout.Add( 0);
708 Console.Write('testList({');
709 first := True;
710 for j in listout do
711 begin
712 if first
713 then first := False
714 else Console.Write(', ');
715 Console.Write(IntToStr( j));
716 end;
717 Console.WriteLine('})');
718
719 listin := client.testList(listout);
720 Expect( listin.Count = listout.Count, 'testList: listin.Count = listout.Count');
721 Expect( listin.Count = 5, 'testList: listin.Count = '+IntToStr(listin.Count));
722 Expect( listin[0] = +1, 'listin[0] = '+IntToStr( listin[0]));
723 Expect( listin[1] = -2, 'listin[1] = '+IntToStr( listin[1]));
724 Expect( listin[2] = +3, 'listin[2] = '+IntToStr( listin[2]));
725 Expect( listin[3] = -4, 'listin[3] = '+IntToStr( listin[3]));
726 Expect( listin[4] = 0, 'listin[4] = '+IntToStr( listin[4]));
727
728 // enums
729 ret := client.testEnum(TNumberz.ONE);
730 Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret)));
731
732 ret := client.testEnum(TNumberz.TWO);
733 Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret)));
734
735 ret := client.testEnum(TNumberz.THREE);
736 Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret)));
737
738 ret := client.testEnum(TNumberz.FIVE);
739 Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret)));
740
741 ret := client.testEnum(TNumberz.EIGHT);
742 Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret)));
743
744
745 // typedef
746 uid := client.testTypedef(309858235082523);
747 Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid));
748
749
750 // maps of maps
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200751 StartTestGroup( 'testMapMap(1)', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000752 mm := client.testMapMap(1);
753 Console.Write(' = {');
754 for key in mm.Keys do
755 begin
756 Console.Write( IntToStr( key) + ' => {');
757 m2 := mm[key];
758 for k2 in m2.Keys do
759 begin
760 Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', ');
761 end;
762 Console.Write('}, ');
763 end;
764 Console.WriteLine('}');
765
766 // verify result data
767 Expect( mm.Count = 2, 'mm.Count = '+IntToStr(mm.Count));
768 pos := mm[4];
769 neg := mm[-4];
770 for j := 1 to 4 do
771 begin
772 Expect( pos[j] = j, 'pos[j] = '+IntToStr(pos[j]));
773 Expect( neg[-j] = -j, 'neg[-j] = '+IntToStr(neg[-j]));
774 end;
775
776
777
778 // insanity
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200779 StartTestGroup( 'testInsanity', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000780 insane := TInsanityImpl.Create;
781 insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
782 insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000);
783 truck := TXtructImpl.Create;
784 truck.String_thing := 'Truck';
Jens Geyer540e3462016-12-28 14:25:41 +0100785 truck.Byte_thing := -8; // byte is signed
786 truck.I32_thing := 32;
787 truck.I64_thing := 64;
Roger Meier3bef8c22012-10-06 06:58:00 +0000788 insane.Xtructs := TThriftListImpl<IXtruct>.Create;
789 insane.Xtructs.Add( truck );
790 whoa := client.testInsanity( insane );
791 Console.Write(' = {');
792 for key64 in whoa.Keys do
793 begin
794 val := whoa[key64];
795 Console.Write( IntToStr( key64) + ' => {');
796 for k2_2 in val.Keys do
797 begin
798 v2 := val[k2_2];
799 Console.Write( IntToStr( Integer( k2_2)) + ' => {');
800 userMap := v2.UserMap;
801 Console.Write('{');
802 if userMap <> nil then
803 begin
804 for k3 in userMap.Keys do
805 begin
806 Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', ');
807 end;
808 end else
809 begin
810 Console.Write('null');
811 end;
812 Console.Write('}, ');
813 xtructs := v2.Xtructs;
814 Console.Write('{');
815
816 if xtructs <> nil then
817 begin
818 for x in xtructs do
819 begin
820 Console.Write('{"' + x.String_thing + '", ' +
821 IntToStr( x.Byte_thing) + ', ' +
822 IntToStr( x.I32_thing) + ', ' +
823 IntToStr( x.I32_thing) + '}, ');
824 end;
825 end else
826 begin
827 Console.Write('null');
828 end;
829 Console.Write('}');
830 Console.Write('}, ');
831 end;
832 Console.Write('}, ');
833 end;
834 Console.WriteLine('}');
835
Jens Geyer540e3462016-12-28 14:25:41 +0100836 (**
837 * So you think you've got this all worked, out eh?
838 *
839 * Creates a the returned map with these values and prints it out:
840 * { 1 => { 2 => argument,
841 * 3 => argument,
842 * },
843 * 2 => { 6 => <empty Insanity struct>, },
844 * }
845 * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
846 *)
847
Roger Meier3bef8c22012-10-06 06:58:00 +0000848 // verify result data
849 Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count));
850 //
851 first_map := whoa[1];
852 second_map := whoa[2];
853 Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count));
854 Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count));
855 //
856 looney := second_map[TNumberz.SIX];
857 Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney)));
858 Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
859 Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
860 //
861 for ret in [TNumberz.TWO, TNumberz.THREE] do begin
862 crazy := first_map[ret];
863 Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
864
865 Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap));
866 Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs));
867
Jens Geyer540e3462016-12-28 14:25:41 +0100868 Expect( crazy.UserMap.Count = insane.UserMap.Count, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count));
869 for pair in insane.UserMap do begin
870 Expect( crazy.UserMap[pair.Key] = pair.Value, 'crazy.UserMap['+IntToStr(Ord(pair.key))+'] = '+IntToStr(crazy.UserMap[pair.Key]));
871 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000872
Jens Geyer540e3462016-12-28 14:25:41 +0100873 Expect( crazy.Xtructs.Count = insane.Xtructs.Count, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count));
874 for arg0 := 0 to insane.Xtructs.Count-1 do begin
875 hello := insane.Xtructs[arg0];
876 goodbye := crazy.Xtructs[arg0];
877 Expect( goodbye.String_thing = hello.String_thing, 'goodbye.String_thing = '+goodbye.String_thing);
878 Expect( goodbye.Byte_thing = hello.Byte_thing, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing));
879 Expect( goodbye.I32_thing = hello.I32_thing, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing));
880 Expect( goodbye.I64_thing = hello.I64_thing, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing));
881 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000882 end;
883
884
885 // multi args
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200886 StartTestGroup( 'testMulti', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000887 arg0 := 1;
888 arg1 := 2;
889 arg2 := High(Int64);
890 arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
891 arg3.AddOrSetValue( 1, 'one');
892 arg4 := TNumberz.FIVE;
893 arg5 := 5000000;
894 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
895 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
896 arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
897 IntToStr( arg5) + ')');
898
899 i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5);
900 Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"');
901 Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing));
902 Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing));
903 Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing));
904 Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
905 Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
906 Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
907 Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
908
909 // multi exception
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200910 StartTestGroup( 'testMultiException(1)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000911 try
912 i := client.testMultiException( 'need more pizza', 'run out of beer');
913 Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"');
914 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
Jens Geyer6bbbf192014-09-07 01:45:56 +0200915 { this is not necessarily true, these fields are default-serialized
Jens Geyerd5436f52014-10-03 19:50:38 +0200916 Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
Roger Meier3bef8c22012-10-06 06:58:00 +0000917 Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
918 Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
Jens Geyerd5436f52014-10-03 19:50:38 +0200919 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000920 except
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200921 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000922 end;
923
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200924 StartTestGroup( 'testMultiException(Xception)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000925 try
926 i := client.testMultiException( 'Xception', 'second test');
927 Expect( FALSE, 'testMultiException(''Xception''): must trow an exception');
928 except
929 on x:TXception do begin
930 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
931 Expect( x.__isset_Message_, 'x.__isset_Message_ = '+BoolToString(x.__isset_Message_));
932 Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
933 Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"');
934 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200935 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000936 end;
937
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200938 StartTestGroup( 'testMultiException(Xception2)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000939 try
940 i := client.testMultiException( 'Xception2', 'third test');
941 Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception');
942 except
943 on x:TXception2 do begin
944 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
945 Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing));
946 Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
947 Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"');
948 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 +0200949 { this is not necessarily true, these fields are default-serialized
Roger Meier3bef8c22012-10-06 06:58:00 +0000950 Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing));
951 Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing));
952 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 +0200953 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000954 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200955 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000956 end;
957
958
959 // oneway functions
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200960 StartTestGroup( 'Test Oneway(1)', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000961 client.testOneway(1);
962 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
963
964 // call time
Jens Geyer06045cf2013-03-27 20:26:25 +0200965 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000966 StartTestGroup( 'Test Calltime()');
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200967 StartTick := GetTickCount;
Roger Meier3bef8c22012-10-06 06:58:00 +0000968 for k := 0 to 1000 - 1 do
969 begin
970 client.testVoid();
971 end;
972 Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
Jens Geyer06045cf2013-03-27 20:26:25 +0200973 {$ENDIF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000974
975 // no more tests here
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200976 StartTestGroup( '', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000977end;
978
979
Jens Geyer14f5d502017-12-09 13:47:09 +0100980{$IFDEF SupportsAsync}
981procedure TClientThread.ClientAsyncTest;
982var
983 client : TThriftTest.IAsync;
984 s : string;
985 i8 : ShortInt;
986begin
987 StartTestGroup( 'Async Tests', test_Unknown);
988 client := TThriftTest.TClient.Create( FProtocol);
989 FTransport.Open;
990
991 // oneway void functions
992 client.testOnewayAsync(1).Wait;
993 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
994
995 // normal functions
996 s := client.testStringAsync(HUGE_TEST_STRING).Value;
997 Expect( length(s) = length(HUGE_TEST_STRING),
998 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
999 +'=> length(result) = '+IntToStr(Length(s)));
1000
1001 i8 := client.testByte(1).Value;
1002 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
1003end;
1004{$ENDIF}
1005
1006
Jens Geyer718f6ee2013-09-06 21:02:34 +02001007{$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +02001008procedure TClientThread.StressTest(const client : TThriftTest.Iface);
1009begin
1010 while TRUE do begin
1011 try
1012 if not FTransport.IsOpen then FTransport.Open; // re-open connection, server has already closed
1013 try
1014 client.testString('Test');
1015 Write('.');
1016 finally
1017 if FTransport.IsOpen then FTransport.Close;
1018 end;
1019 except
1020 on e:Exception do Writeln(#10+e.message);
1021 end;
1022 end;
1023end;
Jens Geyer718f6ee2013-09-06 21:02:34 +02001024{$ENDIF}
Jens Geyer06045cf2013-03-27 20:26:25 +02001025
Jens Geyerfd1b3582014-12-13 23:42:58 +01001026
Jens Geyer85827152018-01-12 21:20:59 +01001027function TClientThread.PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes;
Jens Geyerd4df9172017-10-25 22:30:23 +02001028var i : Integer;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001029begin
Jens Geyer85827152018-01-12 21:20:59 +01001030 case aSize of
1031 Empty : SetLength( result, 0);
1032 Normal : SetLength( result, $100);
1033 ByteArrayTest : SetLength( result, SizeOf(TByteArray) + 128);
1034 PipeWriteLimit : SetLength( result, 65535 + 128);
Jens Geyer2646bd62019-11-09 23:24:52 +01001035 FifteenMB : SetLength( result, 15 * 1024 * 1024);
Jens Geyer85827152018-01-12 21:20:59 +01001036 else
1037 raise EArgumentException.Create('aSize');
1038 end;
1039
Jens Geyerfd1b3582014-12-13 23:42:58 +01001040 ASSERT( Low(result) = 0);
Jens Geyer85827152018-01-12 21:20:59 +01001041 if Length(result) = 0 then Exit;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001042
1043 // linear distribution, unless random is requested
1044 if not aRandomDist then begin
1045 for i := Low(result) to High(result) do begin
Jens Geyer85827152018-01-12 21:20:59 +01001046 result[i] := i mod $100;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001047 end;
1048 Exit;
1049 end;
1050
1051 // random distribution of all 256 values
1052 FillChar( result[0], Length(result) * SizeOf(result[0]), $0);
Jens Geyerd4df9172017-10-25 22:30:23 +02001053 for i := Low(result) to High(result) do begin
1054 result[i] := Byte( Random($100));
Jens Geyerfd1b3582014-12-13 23:42:58 +01001055 end;
1056end;
1057
1058
Jens Geyerf7904452017-07-26 15:02:12 +02001059{$IFDEF Win64}
1060procedure TClientThread.UseInterlockedExchangeAdd64;
1061var a,b : Int64;
1062begin
1063 a := 1;
1064 b := 2;
1065 Thrift.Utils.InterlockedExchangeAdd64( a,b);
1066 Expect( a = 3, 'InterlockedExchangeAdd64');
1067end;
1068{$ENDIF}
1069
1070
Roger Meier3bef8c22012-10-06 06:58:00 +00001071procedure TClientThread.JSONProtocolReadWriteTest;
1072// Tests only then read/write procedures of the JSON protocol
1073// All tests succeed, if we can read what we wrote before
1074// Note that passing this test does not imply, that our JSON is really compatible to what
1075// other clients or servers expect as the real JSON. This is beyond the scope of this test.
1076var prot : IProtocol;
1077 stm : TStringStream;
Jens Geyer17c3ad92017-09-05 20:31:27 +02001078 list : TThriftList;
Jens Geyera019cda2019-11-09 23:24:52 +01001079 config : IThriftConfiguration;
Jens Geyercf892d42017-09-09 10:08:22 +02001080 binary, binRead, emptyBinary : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +00001081 i,iErr : Integer;
1082const
1083 TEST_SHORT = ShortInt( $FE);
1084 TEST_SMALL = SmallInt( $FEDC);
1085 TEST_LONG = LongInt( $FEDCBA98);
1086 TEST_I64 = Int64( $FEDCBA9876543210);
1087 TEST_DOUBLE = -1.234e-56;
1088 DELTA_DOUBLE = TEST_DOUBLE * 1e-14;
1089 TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars")
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001090 // Test THRIFT-2336 and THRIFT-3404 with U+1D11E (G Clef symbol) and 'Русское Название';
1091 G_CLEF_AND_CYRILLIC_TEXT = #$1d11e' '#$0420#$0443#$0441#$0441#$043a#$043e#$0435' '#$041d#$0430#$0437#$0432#$0430#$043d#$0438#$0435;
1092 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 +01001093 // test both possible solidus encodings
1094 SOLIDUS_JSON_DATA = '"one/two\/three"';
1095 SOLIDUS_EXCPECTED = 'one/two/three';
Roger Meier3bef8c22012-10-06 06:58:00 +00001096begin
1097 stm := TStringStream.Create;
1098 try
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001099 StartTestGroup( 'JsonProtocolTest', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001100
Jens Geyera019cda2019-11-09 23:24:52 +01001101 config := TThriftConfigurationImpl.Create;
1102
Roger Meier3bef8c22012-10-06 06:58:00 +00001103 // prepare binary data
Jens Geyer85827152018-01-12 21:20:59 +01001104 binary := PrepareBinaryData( FALSE, Normal);
Jens Geyercf892d42017-09-09 10:08:22 +02001105 SetLength( emptyBinary, 0); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001106
1107 // output setup
1108 prot := TJSONProtocolImpl.Create(
1109 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001110 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE), config));
Roger Meier3bef8c22012-10-06 06:58:00 +00001111
1112 // write
Jens Geyer17c3ad92017-09-05 20:31:27 +02001113 Init( list, TType.String_, 9);
1114 prot.WriteListBegin( list);
Roger Meier3bef8c22012-10-06 06:58:00 +00001115 prot.WriteBool( TRUE);
1116 prot.WriteBool( FALSE);
1117 prot.WriteByte( TEST_SHORT);
1118 prot.WriteI16( TEST_SMALL);
1119 prot.WriteI32( TEST_LONG);
1120 prot.WriteI64( TEST_I64);
1121 prot.WriteDouble( TEST_DOUBLE);
1122 prot.WriteString( TEST_STRING);
1123 prot.WriteBinary( binary);
Jens Geyercf892d42017-09-09 10:08:22 +02001124 prot.WriteString( ''); // empty string
1125 prot.WriteBinary( emptyBinary); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001126 prot.WriteListEnd;
1127
1128 // input setup
1129 Expect( stm.Position = stm.Size, 'Stream position/length after write');
1130 stm.Position := 0;
1131 prot := TJSONProtocolImpl.Create(
1132 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001133 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil, config));
Roger Meier3bef8c22012-10-06 06:58:00 +00001134
1135 // read and compare
1136 list := prot.ReadListBegin;
1137 Expect( list.ElementType = TType.String_, 'list element type');
1138 Expect( list.Count = 9, 'list element count');
1139 Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE');
1140 Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE');
1141 Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte');
1142 Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16');
1143 Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32');
1144 Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64');
1145 Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble');
1146 Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString');
1147 binRead := prot.ReadBinary;
Jens Geyercf892d42017-09-09 10:08:22 +02001148 Expect( Length(prot.ReadString) = 0, 'WriteString/ReadString (empty string)');
1149 Expect( Length(prot.ReadBinary) = 0, 'empty WriteBinary/ReadBinary (empty data block)');
Roger Meier3bef8c22012-10-06 06:58:00 +00001150 prot.ReadListEnd;
1151
1152 // test binary data
1153 Expect( Length(binary) = Length(binRead), 'Binary data length check');
1154 iErr := -1;
1155 for i := Low(binary) to High(binary) do begin
1156 if binary[i] <> binRead[i] then begin
1157 iErr := i;
1158 Break;
1159 end;
1160 end;
1161 if iErr < 0
1162 then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)')
1163 else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr));
1164
1165 Expect( stm.Position = stm.Size, 'Stream position after read');
1166
Jens Geyer7bb44a32014-02-07 22:24:37 +01001167
Jens Geyer21366942013-12-30 22:04:51 +01001168 // Solidus can be encoded in two ways. Make sure we can read both
1169 stm.Position := 0;
1170 stm.Size := 0;
1171 stm.WriteString(SOLIDUS_JSON_DATA);
1172 stm.Position := 0;
1173 prot := TJSONProtocolImpl.Create(
1174 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001175 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil, config));
Jens Geyer21366942013-12-30 22:04:51 +01001176 Expect( prot.ReadString = SOLIDUS_EXCPECTED, 'Solidus encoding');
1177
1178
Jens Geyer7bb44a32014-02-07 22:24:37 +01001179 // Widechars should work too. Do they?
1180 // After writing, we ensure that we are able to read it back
1181 // We can't assume hex-encoding, since (nearly) any Unicode char is valid JSON
1182 stm.Position := 0;
1183 stm.Size := 0;
1184 prot := TJSONProtocolImpl.Create(
1185 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001186 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE), config));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001187 prot.WriteString( G_CLEF_AND_CYRILLIC_TEXT);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001188 stm.Position := 0;
1189 prot := TJSONProtocolImpl.Create(
1190 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001191 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil, config));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001192 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Writing JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001193
1194 // Widechars should work with hex-encoding too. Do they?
1195 stm.Position := 0;
1196 stm.Size := 0;
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001197 stm.WriteString( G_CLEF_AND_CYRILLIC_JSON);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001198 stm.Position := 0;
1199 prot := TJSONProtocolImpl.Create(
1200 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001201 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil, config));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001202 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Reading JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001203
1204
Roger Meier3bef8c22012-10-06 06:58:00 +00001205 finally
1206 stm.Free;
1207 prot := nil; //-> Release
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001208 StartTestGroup( '', test_Unknown); // no more tests here
Roger Meier3bef8c22012-10-06 06:58:00 +00001209 end;
1210end;
1211
1212
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001213procedure TClientThread.StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +00001214begin
1215 FTestGroup := aGroup;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001216 FCurrentTest := aTest;
1217
1218 Include( FExecuted, aTest);
1219
Roger Meier3bef8c22012-10-06 06:58:00 +00001220 if FTestGroup <> '' then begin
1221 Console.WriteLine('');
1222 Console.WriteLine( aGroup+' tests');
1223 Console.WriteLine( StringOfChar('-',60));
1224 end;
1225end;
1226
1227
1228procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
1229begin
1230 if aTestResult then begin
1231 Inc(FSuccesses);
1232 Console.WriteLine( aTestInfo+': passed');
1233 end
1234 else begin
1235 FErrors.Add( FTestGroup+': '+aTestInfo);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001236 Include( FFailed, FCurrentTest);
Roger Meier3bef8c22012-10-06 06:58:00 +00001237 Console.WriteLine( aTestInfo+': *** FAILED ***');
1238
1239 // We have a failed test!
1240 // -> issue DebugBreak ONLY if a debugger is attached,
1241 // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise
Jens Geyer9f7f11e2016-04-14 21:37:11 +02001242 if IsDebuggerPresent
1243 then {$IFDEF CPUX64} DebugBreak {$ELSE} asm int 3 end {$ENDIF};
Roger Meier3bef8c22012-10-06 06:58:00 +00001244 end;
1245end;
1246
1247
1248procedure TClientThread.ReportResults;
1249var nTotal : Integer;
1250 sLine : string;
1251begin
1252 // prevent us from stupid DIV/0 errors
1253 nTotal := FSuccesses + FErrors.Count;
1254 if nTotal = 0 then begin
1255 Console.WriteLine('No results logged');
1256 Exit;
1257 end;
1258
1259 Console.WriteLine('');
1260 Console.WriteLine( StringOfChar('=',60));
1261 Console.WriteLine( IntToStr(nTotal)+' tests performed');
1262 Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)');
1263 Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)');
1264 Console.WriteLine( StringOfChar('=',60));
1265 if FErrors.Count > 0 then begin
1266 Console.WriteLine('FAILED TESTS:');
1267 for sLine in FErrors do Console.WriteLine('- '+sLine);
1268 Console.WriteLine( StringOfChar('=',60));
1269 InterlockedIncrement( ExitCode); // return <> 0 on errors
1270 end;
1271 Console.WriteLine('');
1272end;
1273
1274
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001275function TClientThread.CalculateExitCode : Byte;
1276var test : TTestGroup;
1277begin
1278 result := EXITCODE_SUCCESS;
1279 for test := Low(TTestGroup) to High(TTestGroup) do begin
1280 if (test in FFailed) or not (test in FExecuted)
1281 then result := result or MAP_FAILURES_TO_EXITCODE_BITS[test];
1282 end;
1283end;
1284
1285
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001286constructor TClientThread.Create( const aSetup : TTestSetup; const aNumIteration: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +00001287begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001288 FSetup := aSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +00001289 FNumIteration := ANumIteration;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001290
Roger Meier3bef8c22012-10-06 06:58:00 +00001291 FConsole := TThreadConsole.Create( Self );
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001292 FCurrentTest := test_Unknown;
Roger Meier3bef8c22012-10-06 06:58:00 +00001293
1294 // error list: keep correct order, allow for duplicates
1295 FErrors := TStringList.Create;
1296 FErrors.Sorted := FALSE;
1297 FErrors.Duplicates := dupAccept;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001298
1299 inherited Create( TRUE);
Roger Meier3bef8c22012-10-06 06:58:00 +00001300end;
1301
1302destructor TClientThread.Destroy;
1303begin
1304 FreeAndNil( FConsole);
1305 FreeAndNil( FErrors);
1306 inherited;
1307end;
1308
1309procedure TClientThread.Execute;
1310var
1311 i : Integer;
Roger Meier3bef8c22012-10-06 06:58:00 +00001312begin
1313 // perform all tests
1314 try
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001315 {$IFDEF Win64}
Jens Geyerf7904452017-07-26 15:02:12 +02001316 UseInterlockedExchangeAdd64;
Jens Geyer14f5d502017-12-09 13:47:09 +01001317 {$ENDIF}
Jens Geyer7bb44a32014-02-07 22:24:37 +01001318 JSONProtocolReadWriteTest;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001319
1320 // must be run in the context of the thread
1321 InitializeProtocolTransportStack;
1322 try
1323 for i := 0 to FNumIteration - 1 do begin
1324 ClientTest;
1325 {$IFDEF SupportsAsync}
1326 ClientAsyncTest;
1327 {$ENDIF}
1328 end;
1329
1330 // report the outcome
1331 ReportResults;
1332 SetReturnValue( CalculateExitCode);
1333
1334 finally
1335 ShutdownProtocolTransportStack;
Roger Meier3bef8c22012-10-06 06:58:00 +00001336 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001337
Roger Meier3bef8c22012-10-06 06:58:00 +00001338 except
1339 on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"');
1340 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001341end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001342
Roger Meier3bef8c22012-10-06 06:58:00 +00001343
Jens Geyera019cda2019-11-09 23:24:52 +01001344function TClientThread.InitializeHttpTransport( const aTimeoutSetting : Integer; const aConfig : IThriftConfiguration) : IHTTPClient;
Jens Geyer83ff7532019-06-06 22:46:03 +02001345var sUrl : string;
1346 comps : URL_COMPONENTS;
1347 dwChars : DWORD;
Jens Geyer02230912019-04-03 01:12:51 +02001348begin
1349 ASSERT( FSetup.endpoint in [trns_MsxmlHttp, trns_WinHttp]);
1350
1351 if FSetup.useSSL
1352 then sUrl := 'https://'
1353 else sUrl := 'http://';
1354
1355 sUrl := sUrl + FSetup.host;
1356
Jens Geyer83ff7532019-06-06 22:46:03 +02001357 // add the port number if necessary and at the right place
1358 FillChar( comps, SizeOf(comps), 0);
1359 comps.dwStructSize := SizeOf(comps);
1360 comps.dwSchemeLength := MAXINT;
1361 comps.dwHostNameLength := MAXINT;
1362 comps.dwUserNameLength := MAXINT;
1363 comps.dwPasswordLength := MAXINT;
1364 comps.dwUrlPathLength := MAXINT;
1365 comps.dwExtraInfoLength := MAXINT;
1366 Win32Check( WinHttpCrackUrl( PChar(sUrl), Length(sUrl), 0, comps));
Jens Geyer02230912019-04-03 01:12:51 +02001367 case FSetup.port of
Jens Geyer83ff7532019-06-06 22:46:03 +02001368 80 : if FSetup.useSSL then comps.nPort := FSetup.port;
1369 443 : if not FSetup.useSSL then comps.nPort := FSetup.port;
Jens Geyer02230912019-04-03 01:12:51 +02001370 else
Jens Geyer83ff7532019-06-06 22:46:03 +02001371 if FSetup.port > 0 then comps.nPort := FSetup.port;
Jens Geyer02230912019-04-03 01:12:51 +02001372 end;
Jens Geyer83ff7532019-06-06 22:46:03 +02001373 dwChars := Length(sUrl) + 64;
1374 SetLength( sUrl, dwChars);
1375 Win32Check( WinHttpCreateUrl( comps, 0, @sUrl[1], dwChars));
1376 SetLength( sUrl, dwChars);
1377
Jens Geyer02230912019-04-03 01:12:51 +02001378
1379 Console.WriteLine('Target URL: '+sUrl);
1380 case FSetup.endpoint of
Jens Geyera019cda2019-11-09 23:24:52 +01001381 trns_MsxmlHttp : result := TMsxmlHTTPClientImpl.Create( sUrl, aConfig);
1382 trns_WinHttp : result := TWinHTTPClientImpl.Create( sUrl, aConfig);
Jens Geyer02230912019-04-03 01:12:51 +02001383 else
1384 raise Exception.Create(ENDPOINT_TRANSPORTS[FSetup.endpoint]+' unhandled case');
1385 end;
1386
1387 result.DnsResolveTimeout := aTimeoutSetting;
1388 result.ConnectionTimeout := aTimeoutSetting;
1389 result.SendTimeout := aTimeoutSetting;
1390 result.ReadTimeout := aTimeoutSetting;
1391end;
1392
1393
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001394procedure TClientThread.InitializeProtocolTransportStack;
Jens Geyer02230912019-04-03 01:12:51 +02001395var streamtrans : IStreamTransport;
Jens Geyer47f63172019-06-06 22:42:58 +02001396 canSSL : Boolean;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001397const
1398 DEBUG_TIMEOUT = 30 * 1000;
1399 RELEASE_TIMEOUT = DEFAULT_THRIFT_TIMEOUT;
1400 PIPE_TIMEOUT = RELEASE_TIMEOUT;
1401 HTTP_TIMEOUTS = 10 * 1000;
1402begin
1403 // needed for HTTP clients as they utilize the MSXML COM components
1404 OleCheck( CoInitialize( nil));
1405
Jens Geyer47f63172019-06-06 22:42:58 +02001406 canSSL := FALSE;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001407 case FSetup.endpoint of
1408 trns_Sockets: begin
1409 Console.WriteLine('Using sockets ('+FSetup.host+' port '+IntToStr(FSetup.port)+')');
Jens Geyera019cda2019-11-09 23:24:52 +01001410 streamtrans := TSocketImpl.Create( FSetup.host, FSetup.port);
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001411 FTransport := streamtrans;
1412 end;
1413
Jens Geyer02230912019-04-03 01:12:51 +02001414 trns_MsxmlHttp,
1415 trns_WinHttp: begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001416 Console.WriteLine('Using HTTPClient');
Jens Geyer02230912019-04-03 01:12:51 +02001417 FTransport := InitializeHttpTransport( HTTP_TIMEOUTS);
Jens Geyer47f63172019-06-06 22:42:58 +02001418 canSSL := TRUE;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001419 end;
1420
1421 trns_EvHttp: begin
1422 raise Exception.Create(ENDPOINT_TRANSPORTS[FSetup.endpoint]+' transport not implemented');
1423 end;
1424
1425 trns_NamedPipes: begin
1426 streamtrans := TNamedPipeTransportClientEndImpl.Create( FSetup.sPipeName, 0, nil, PIPE_TIMEOUT, PIPE_TIMEOUT);
1427 FTransport := streamtrans;
1428 end;
1429
1430 trns_AnonPipes: begin
Jens Geyera019cda2019-11-09 23:24:52 +01001431 streamtrans := TAnonymousPipeTransportImpl.Create( FSetup.hAnonRead, FSetup.hAnonWrite, FALSE, PIPE_TIMEOUT);
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001432 FTransport := streamtrans;
1433 end;
1434
1435 else
1436 raise Exception.Create('Unhandled endpoint transport');
1437 end;
1438 ASSERT( FTransport <> nil);
1439
1440 // layered transports are not really meant to be stacked upon each other
1441 if (trns_Framed in FSetup.layered) then begin
1442 FTransport := TFramedTransportImpl.Create( FTransport);
1443 end
1444 else if (trns_Buffered in FSetup.layered) and (streamtrans <> nil) then begin
1445 FTransport := TBufferedTransportImpl.Create( streamtrans, 32); // small buffer to test read()
1446 end;
1447
Jens Geyer47f63172019-06-06 22:42:58 +02001448 if FSetup.useSSL and not canSSL then begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001449 raise Exception.Create('SSL/TLS not implemented');
1450 end;
1451
1452 // create protocol instance, default to BinaryProtocol
Jens Geyer3b686532021-07-01 23:04:08 +02001453 FProtocol := PROTOCOL_CLASSES[FSetup.protType].Create(FTransport);
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001454 ASSERT( (FTransport <> nil) and (FProtocol <> nil));
1455end;
1456
1457
1458procedure TClientThread.ShutdownProtocolTransportStack;
1459begin
1460 try
1461 FProtocol := nil;
1462
1463 if FTransport <> nil then begin
Roger Meier3bef8c22012-10-06 06:58:00 +00001464 FTransport.Close;
1465 FTransport := nil;
1466 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001467
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001468 finally
1469 CoUninitialize;
1470 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001471end;
1472
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001473
Roger Meier3bef8c22012-10-06 06:58:00 +00001474{ TThreadConsole }
1475
1476constructor TThreadConsole.Create(AThread: TThread);
1477begin
Jens Geyer718f6ee2013-09-06 21:02:34 +02001478 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +00001479 FThread := AThread;
1480end;
1481
1482procedure TThreadConsole.Write(const S: string);
1483var
1484 proc : TThreadProcedure;
1485begin
1486 proc := procedure
1487 begin
1488 Console.Write( S );
1489 end;
1490 TThread.Synchronize( FThread, proc);
1491end;
1492
1493procedure TThreadConsole.WriteLine(const S: string);
1494var
1495 proc : TThreadProcedure;
1496begin
1497 proc := procedure
1498 begin
1499 Console.WriteLine( S );
1500 end;
1501 TThread.Synchronize( FThread, proc);
1502end;
1503
1504initialization
1505begin
1506 TTestClient.FNumIteration := 1;
1507 TTestClient.FNumThread := 1;
1508end;
1509
1510end.