blob: 1b09d3cf450c9fc2c697a6239b094d7e1ae7c74a [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 Geyer62445c12022-06-29 00:00:00 +020037 Classes, Windows, SysUtils, Math, ActiveX, ComObj,
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
Jens Geyer48d3bef2022-09-08 21:48:41 +020060 TClientThread = class;
61
62 TThreadConsole = class(TThriftConsole)
63 strict private
64 FThread : TClientThread;
65 FLogThreadID : Boolean;
Roger Meier3bef8c22012-10-06 06:58:00 +000066 public
Jens Geyer48d3bef2022-09-08 21:48:41 +020067 constructor Create( const aThread: TClientThread; const aLogThreadID : Boolean);
68
69 procedure Write( const S: string); override;
70 procedure WriteLine( const S: string); override;
Roger Meier3bef8c22012-10-06 06:58:00 +000071 end;
72
Jens Geyeraf7ecd62018-06-22 22:41:27 +020073 TTestSetup = record
74 protType : TKnownProtocol;
75 endpoint : TEndpointTransport;
76 layered : TLayeredTransports;
77 useSSL : Boolean; // include where appropriate (TLayeredTransport?)
78 host : string;
79 port : Integer;
80 sPipeName : string;
81 hAnonRead, hAnonWrite : THandle;
82 end;
83
Roger Meier3bef8c22012-10-06 06:58:00 +000084 TClientThread = class( TThread )
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020085 private type
86 TTestGroup = (
87 test_Unknown,
88 test_BaseTypes,
89 test_Structs,
90 test_Containers,
91 test_Exceptions
92 // new values here
93 );
94 TTestGroups = set of TTestGroup;
95
Jens Geyer85827152018-01-12 21:20:59 +010096 TTestSize = (
97 Empty, // Edge case: the zero-length empty binary
98 Normal, // Fairly small array of usual size (256 bytes)
99 ByteArrayTest, // THRIFT-4454 Large writes/reads may cause range check errors in debug mode
Jens Geyerbd1a2732019-06-26 22:52:44 +0200100 PipeWriteLimit, // THRIFT-4372 Pipe write operations across a network are limited to 65,535 bytes per write.
Jens Geyer2646bd62019-11-09 23:24:52 +0100101 FifteenMB // quite a bit of data, but still below the default max frame size
Jens Geyer85827152018-01-12 21:20:59 +0100102 );
103
Jens Geyer48d3bef2022-09-08 21:48:41 +0200104 strict private
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200105 FSetup : TTestSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +0000106 FTransport : ITransport;
107 FProtocol : IProtocol;
Jens Geyer48d3bef2022-09-08 21:48:41 +0200108 FNumIterations : Integer;
109
110 FThreadNo : Integer;
Roger Meier3bef8c22012-10-06 06:58:00 +0000111 FConsole : TThreadConsole;
112
113 // test reporting, will be refactored out into separate class later
114 FTestGroup : string;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200115 FCurrentTest : TTestGroup;
Roger Meier3bef8c22012-10-06 06:58:00 +0000116 FSuccesses : Integer;
117 FErrors : TStringList;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200118 FFailed : TTestGroups;
119 FExecuted : TTestGroups;
120 procedure StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +0000121 procedure Expect( aTestResult : Boolean; const aTestInfo : string);
122 procedure ReportResults;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100123 function CalculateExitCode : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000124
125 procedure ClientTest;
Jens Geyer14f5d502017-12-09 13:47:09 +0100126 {$IFDEF SupportsAsync}
127 procedure ClientAsyncTest;
128 {$ENDIF}
129
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200130 procedure InitializeProtocolTransportStack;
131 procedure ShutdownProtocolTransportStack;
Jens Geyera019cda2019-11-09 23:24:52 +0100132 function InitializeHttpTransport( const aTimeoutSetting : Integer; const aConfig : IThriftConfiguration = nil) : IHTTPClient;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200133
Roger Meier3bef8c22012-10-06 06:58:00 +0000134 procedure JSONProtocolReadWriteTest;
Jens Geyer85827152018-01-12 21:20:59 +0100135 function PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes;
Jens Geyer718f6ee2013-09-06 21:02:34 +0200136 {$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +0200137 procedure StressTest(const client : TThriftTest.Iface);
Jens Geyer718f6ee2013-09-06 21:02:34 +0200138 {$ENDIF}
Jens Geyerf7904452017-07-26 15:02:12 +0200139 {$IFDEF Win64}
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200140 procedure UseInterlockedExchangeAdd64;
Jens Geyerf7904452017-07-26 15:02:12 +0200141 {$ENDIF}
Jens Geyer48d3bef2022-09-08 21:48:41 +0200142
143 strict protected
Roger Meier3bef8c22012-10-06 06:58:00 +0000144 procedure Execute; override;
Jens Geyer48d3bef2022-09-08 21:48:41 +0200145 property Console : TThreadConsole read FConsole;
146
Roger Meier3bef8c22012-10-06 06:58:00 +0000147 public
Jens Geyer48d3bef2022-09-08 21:48:41 +0200148 constructor Create( const aSetup : TTestSetup; const aNumIteration, aThreadNo: Integer; const aLogThreadID : Boolean);
Roger Meier3bef8c22012-10-06 06:58:00 +0000149 destructor Destroy; override;
Jens Geyer48d3bef2022-09-08 21:48:41 +0200150
151 property ThreadNo : Integer read FThreadNo;
Roger Meier3bef8c22012-10-06 06:58:00 +0000152 end;
153
154 TTestClient = class
155 private
156 class var
Jens Geyer48d3bef2022-09-08 21:48:41 +0200157 FNumIterations : Integer;
158 FNumThreads : Integer;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200159
160 class procedure PrintCmdLineHelp;
161 class procedure InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000162 public
Jens Geyeraeda9872020-03-22 15:01:28 +0100163 class function Execute( const arguments: array of string) : Byte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000164 end;
165
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200166
Roger Meier3bef8c22012-10-06 06:58:00 +0000167implementation
168
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200169const
170 EXITCODE_SUCCESS = $00; // no errors bits set
171 //
172 EXITCODE_FAILBIT_BASETYPES = $01;
173 EXITCODE_FAILBIT_STRUCTS = $02;
174 EXITCODE_FAILBIT_CONTAINERS = $04;
175 EXITCODE_FAILBIT_EXCEPTIONS = $08;
176
177 MAP_FAILURES_TO_EXITCODE_BITS : array[TClientThread.TTestGroup] of Byte = (
178 EXITCODE_SUCCESS, // no bits here
179 EXITCODE_FAILBIT_BASETYPES,
180 EXITCODE_FAILBIT_STRUCTS,
181 EXITCODE_FAILBIT_CONTAINERS,
182 EXITCODE_FAILBIT_EXCEPTIONS
183 );
184
185
186
Roger Meier3bef8c22012-10-06 06:58:00 +0000187function BoolToString( b : Boolean) : string;
188// overrides global BoolToString()
189begin
190 if b
191 then result := 'true'
192 else result := 'false';
193end;
194
195// not available in all versions, so make sure we have this one imported
196function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent';
197
198{ TTestClient }
199
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200200class procedure TTestClient.PrintCmdLineHelp;
201const HELPTEXT = ' [options]'#10
202 + #10
203 + 'Allowed options:'#10
Jens Geyeraeda9872020-03-22 15:01:28 +0100204 + ' -h | --help Produces this help message'#10
205 + ' --host=arg (localhost) Host to connect'#10
206 + ' --port=arg (9090) Port number to connect'#10
207 + ' --pipe=arg Windows Named Pipe (e.g. MyThriftPipe)'#10
208 + ' --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)'#10
209 + ' --transport=arg (sockets) Transport: buffered, framed, http, winhttp'#10
210 + ' --protocol=arg (binary) Protocol: binary, compact, json'#10
211 + ' --ssl Encrypted Transport using SSL'#10
212 + ' -n=num | --testloops=num (1) Number of Tests'#10
213 + ' -t=num | --threads=num (1) Number of Test threads'#10
214 + ' --performance Run the built-in performance test (no other arguments)'#10
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200215 ;
216begin
217 Writeln( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + HELPTEXT);
218end;
219
220class procedure TTestClient.InvalidArgs;
221begin
222 Console.WriteLine( 'Invalid args.');
223 Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + ' -h for more information');
224 Abort;
225end;
226
Jens Geyeraeda9872020-03-22 15:01:28 +0100227class function TTestClient.Execute(const arguments: array of string) : Byte;
228
229 function IsSwitch( const aArgument, aSwitch : string; out sValue : string) : Boolean;
230 begin
231 sValue := '';
232 result := (Copy( aArgument, 1, Length(aSwitch)) = aSwitch);
233 if result then begin
234 if (Copy( aArgument, 1, Length(aSwitch)+1) = (aSwitch+'='))
235 then sValue := Copy( aArgument, Length(aSwitch)+2, MAXINT);
236 end;
237 end;
238
Roger Meier3bef8c22012-10-06 06:58:00 +0000239var
Jens Geyeraeda9872020-03-22 15:01:28 +0100240 iArg : Integer;
Jens Geyer14f5d502017-12-09 13:47:09 +0100241 threadExitCode : Byte;
Jens Geyeraeda9872020-03-22 15:01:28 +0100242 sArg, sValue : string;
Roger Meier3bef8c22012-10-06 06:58:00 +0000243 threads : array of TThread;
244 dtStart : TDateTime;
245 test : Integer;
246 thread : TThread;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200247 setup : TTestSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +0000248begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200249 // init record
250 with setup do begin
251 protType := prot_Binary;
252 endpoint := trns_Sockets;
253 layered := [];
254 useSSL := FALSE;
255 host := 'localhost';
256 port := 9090;
257 sPipeName := '';
258 hAnonRead := INVALID_HANDLE_VALUE;
259 hAnonWrite := INVALID_HANDLE_VALUE;
260 end;
261
Roger Meier3bef8c22012-10-06 06:58:00 +0000262 try
Jens Geyeraeda9872020-03-22 15:01:28 +0100263 iArg := 0;
264 while iArg < Length(arguments) do begin
265 sArg := arguments[iArg];
266 Inc(iArg);
Roger Meier3bef8c22012-10-06 06:58:00 +0000267
Jens Geyeraeda9872020-03-22 15:01:28 +0100268 if IsSwitch( sArg, '-h', sValue)
269 or IsSwitch( sArg, '--help', sValue)
270 then begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200271 // -h [ --help ] produce help message
272 PrintCmdLineHelp;
273 result := $FF; // all tests failed
274 Exit;
275 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100276 else if IsSwitch( sArg, '--host', sValue) then begin
Jens Geyerb360b652014-09-28 01:55:46 +0200277 // --host arg (=localhost) Host to connect
Jens Geyeraeda9872020-03-22 15:01:28 +0100278 setup.host := sValue;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200279 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100280 else if IsSwitch( sArg, '--port', sValue) then begin
Jens Geyerb360b652014-09-28 01:55:46 +0200281 // --port arg (=9090) Port number to connect
Jens Geyeraeda9872020-03-22 15:01:28 +0100282 setup.port := StrToIntDef(sValue,0);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200283 if setup.port <= 0 then InvalidArgs;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200284 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100285 else if IsSwitch( sArg, '--domain-socket', sValue) then begin
Jens Geyerb360b652014-09-28 01:55:46 +0200286 // --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200287 raise Exception.Create('domain-socket not supported');
288 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100289 // --pipe arg Windows Named Pipe (e.g. MyThriftPipe)
290 else if IsSwitch( sArg, '--pipe', sValue) then begin
Jens Geyer4a33b182020-03-22 13:46:34 +0100291 // --pipe arg Windows Named Pipe (e.g. MyThriftPipe)
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200292 setup.endpoint := trns_NamedPipes;
Jens Geyeraeda9872020-03-22 15:01:28 +0100293 setup.sPipeName := sValue;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200294 Console.WriteLine('Using named pipe ('+setup.sPipeName+')');
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200295 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100296 else if IsSwitch( sArg, '--anon-pipes', sValue) then begin
Jens Geyerb360b652014-09-28 01:55:46 +0200297 // --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200298 setup.endpoint := trns_AnonPipes;
Jens Geyeraeda9872020-03-22 15:01:28 +0100299 setup.hAnonRead := THandle( StrToIntDef( arguments[iArg], Integer(INVALID_HANDLE_VALUE)));
300 Inc(iArg);
301 setup.hAnonWrite := THandle( StrToIntDef( arguments[iArg], Integer(INVALID_HANDLE_VALUE)));
302 Inc(iArg);
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200303 Console.WriteLine('Using anonymous pipes ('+IntToStr(Integer(setup.hAnonRead))+' and '+IntToStr(Integer(setup.hAnonWrite))+')');
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200304 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100305 else if IsSwitch( sArg, '--transport', sValue) then begin
Jens Geyer02230912019-04-03 01:12:51 +0200306 // --transport arg (=sockets) Transport: buffered, framed, http, winhttp, evhttp
Jens Geyeraeda9872020-03-22 15:01:28 +0100307 if sValue = 'buffered' then Include( setup.layered, trns_Buffered)
308 else if sValue = 'framed' then Include( setup.layered, trns_Framed)
309 else if sValue = 'http' then setup.endpoint := trns_MsXmlHttp
310 else if sValue = 'winhttp' then setup.endpoint := trns_WinHttp
311 else if sValue = 'evhttp' then setup.endpoint := trns_EvHttp // recognized, but not supported
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200312 else InvalidArgs;
313 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100314 else if IsSwitch( sArg, '--protocol', sValue) then begin
Jens Geyerb360b652014-09-28 01:55:46 +0200315 // --protocol arg (=binary) Protocol: binary, compact, json
Jens Geyeraeda9872020-03-22 15:01:28 +0100316 if sValue = 'binary' then setup.protType := prot_Binary
317 else if sValue = 'compact' then setup.protType := prot_Compact
318 else if sValue = 'json' then setup.protType := prot_JSON
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200319 else InvalidArgs;
320 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100321 else if IsSwitch( sArg, '--ssl', sValue) then begin
Jens Geyerb360b652014-09-28 01:55:46 +0200322 // --ssl Encrypted Transport using SSL
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200323 setup.useSSL := TRUE;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200324
325 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100326 else if IsSwitch( sArg, '-n', sValue) or IsSwitch( sArg, '--testloops', sValue) then begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200327 // -n [ --testloops ] arg (=1) Number of Tests
Jens Geyer48d3bef2022-09-08 21:48:41 +0200328 FNumIterations := StrToIntDef( sValue, 0);
329 if FNumIterations <= 0
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200330 then InvalidArgs;
331
332 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100333 else if IsSwitch( sArg, '-t', sValue) or IsSwitch( sArg, '--threads', sValue) then begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200334 // -t [ --threads ] arg (=1) Number of Test threads
Jens Geyer48d3bef2022-09-08 21:48:41 +0200335 FNumThreads := StrToIntDef( sValue, 0);
336 if FNumThreads <= 0
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200337 then InvalidArgs;
338 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100339 else if IsSwitch( sArg, '--performance', sValue) then begin
Jens Geyerb342bd92019-06-03 20:27:00 +0200340 result := TPerformanceTests.Execute;
341 Exit;
342 end
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200343 else begin
344 InvalidArgs;
Roger Meier3bef8c22012-10-06 06:58:00 +0000345 end;
346 end;
347
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200348
Roger Meier79655fb2012-10-20 20:59:41 +0000349 // In the anonymous pipes mode the client is launched by the test server
350 // -> behave nicely and allow for attaching a debugger to this process
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200351 if (setup.endpoint = trns_AnonPipes) and not IsDebuggerPresent
Roger Meier79655fb2012-10-20 20:59:41 +0000352 then MessageBox( 0, 'Attach Debugger and/or click OK to continue.',
353 'Thrift TestClient (Delphi)',
354 MB_OK or MB_ICONEXCLAMATION);
355
Jens Geyer48d3bef2022-09-08 21:48:41 +0200356 SetLength( threads, FNumThreads);
Roger Meier3bef8c22012-10-06 06:58:00 +0000357 dtStart := Now;
358
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200359 // layered transports are not really meant to be stacked upon each other
360 if (trns_Framed in setup.layered) then begin
361 Console.WriteLine('Using framed transport');
362 end
363 else if (trns_Buffered in setup.layered) then begin
364 Console.WriteLine('Using buffered transport');
365 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000366
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200367 Console.WriteLine(THRIFT_PROTOCOLS[setup.protType]+' protocol');
Roger Meier3bef8c22012-10-06 06:58:00 +0000368
Jens Geyer48d3bef2022-09-08 21:48:41 +0200369 if FNumThreads <> 1
370 then Console.WriteLine(IntToStr(FNumThreads)+' client threads');
371
372 if FNumIterations <> 1
373 then Console.WriteLine(IntToStr(FNumIterations)+' iterations');
374
375 for test := 0 to FNumThreads - 1 do begin
376 thread := TClientThread.Create( setup, FNumIterations, test, FNumThreads<>1);
Roger Meier3bef8c22012-10-06 06:58:00 +0000377 threads[test] := thread;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200378 thread.Start;
Roger Meier3bef8c22012-10-06 06:58:00 +0000379 end;
380
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200381 result := 0;
Jens Geyer48d3bef2022-09-08 21:48:41 +0200382 for test := 0 to FNumThreads - 1 do begin
Jens Geyer14f5d502017-12-09 13:47:09 +0100383 threadExitCode := threads[test].WaitFor;
384 result := result or threadExitCode;
Jens Geyer14f5d502017-12-09 13:47:09 +0100385 threads[test].Free;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200386 threads[test] := nil;
Jens Geyer14f5d502017-12-09 13:47:09 +0100387 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000388
389 Console.Write('Total time: ' + IntToStr( MilliSecondsBetween(Now, dtStart)));
390
391 except
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200392 on E: EAbort do raise;
393 on E: Exception do begin
394 Console.WriteLine( E.Message + #10 + E.StackTrace);
395 raise;
Roger Meier3bef8c22012-10-06 06:58:00 +0000396 end;
397 end;
398
399 Console.WriteLine('');
400 Console.WriteLine('done!');
401end;
402
403{ TClientThread }
404
405procedure TClientThread.ClientTest;
406var
407 client : TThriftTest.Iface;
408 s : string;
409 i8 : ShortInt;
410 i32 : Integer;
411 i64 : Int64;
Jens Geyerfd1b3582014-12-13 23:42:58 +0100412 binOut,binIn : TBytes;
Jens Geyer62445c12022-06-29 00:00:00 +0200413 guidIn, guidOut : TGuid;
Roger Meier3bef8c22012-10-06 06:58:00 +0000414 dub : Double;
415 o : IXtruct;
416 o2 : IXtruct2;
417 i : IXtruct;
418 i2 : IXtruct2;
419 mapout : IThriftDictionary<Integer,Integer>;
420 mapin : IThriftDictionary<Integer,Integer>;
421 strmapout : IThriftDictionary<string,string>;
422 strmapin : IThriftDictionary<string,string>;
423 j : Integer;
424 first : Boolean;
425 key : Integer;
426 strkey : string;
427 listout : IThriftList<Integer>;
428 listin : IThriftList<Integer>;
Jens Geyer6a797b92022-09-05 13:55:37 +0200429 setout : IThriftHashSet<Integer>;
430 setin : IThriftHashSet<Integer>;
Roger Meier3bef8c22012-10-06 06:58:00 +0000431 ret : TNumberz;
432 uid : Int64;
433 mm : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
434 pos : IThriftDictionary<Integer, Integer>;
435 neg : IThriftDictionary<Integer, Integer>;
436 m2 : IThriftDictionary<Integer, Integer>;
437 k2 : Integer;
438 insane : IInsanity;
439 truck : IXtruct;
440 whoa : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
441 key64 : Int64;
442 val : IThriftDictionary<TNumberz, IInsanity>;
443 k2_2 : TNumberz;
444 k3 : TNumberz;
445 v2 : IInsanity;
446 userMap : IThriftDictionary<TNumberz, Int64>;
447 xtructs : IThriftList<IXtruct>;
448 x : IXtruct;
449 arg0 : ShortInt;
450 arg1 : Integer;
451 arg2 : Int64;
452 arg3 : IThriftDictionary<SmallInt, string>;
453 arg4 : TNumberz;
454 arg5 : Int64;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200455 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000456 StartTick : Cardinal;
457 k : Integer;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200458 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000459 hello, goodbye : IXtruct;
460 crazy : IInsanity;
461 looney : IInsanity;
462 first_map : IThriftDictionary<TNumberz, IInsanity>;
463 second_map : IThriftDictionary<TNumberz, IInsanity>;
Jens Geyer540e3462016-12-28 14:25:41 +0100464 pair : TPair<TNumberz, TUserId>;
Jens Geyer85827152018-01-12 21:20:59 +0100465 testsize : TTestSize;
Roger Meier3bef8c22012-10-06 06:58:00 +0000466begin
467 client := TThriftTest.TClient.Create( FProtocol);
468 FTransport.Open;
469
Jens Geyer06045cf2013-03-27 20:26:25 +0200470 {$IFDEF StressTest}
471 StressTest( client);
472 {$ENDIF StressTest}
473
Jens Geyer17c3ad92017-09-05 20:31:27 +0200474 {$IFDEF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000475 // in-depth exception test
476 // (1) do we get an exception at all?
477 // (2) do we get the right exception?
478 // (3) does the exception contain the expected data?
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200479 StartTestGroup( 'testException', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000480 // case 1: exception type declared in IDL at the function call
481 try
482 client.testException('Xception');
483 Expect( FALSE, 'testException(''Xception''): must trow an exception');
484 except
485 on e:TXception do begin
486 Expect( e.ErrorCode = 1001, 'error code');
487 Expect( e.Message_ = 'Xception', 'error message');
488 Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ );
489 end;
490 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200491 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000492 end;
493
Jens Geyercc8c2c62021-03-29 22:38:30 +0200494 // re-open connection if needed
495 if not FTransport.IsOpen
496 then FTransport.Open;
497
Roger Meier3bef8c22012-10-06 06:58:00 +0000498 // case 2: exception type NOT declared in IDL at the function call
499 // this will close the connection
500 try
501 client.testException('TException');
502 Expect( FALSE, 'testException(''TException''): must trow an exception');
503 except
504 on e:TTransportException do begin
505 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Roger Meier3bef8c22012-10-06 06:58:00 +0000506 end;
Jens Geyer6bbbf192014-09-07 01:45:56 +0200507 on e:TApplicationException do begin
508 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Jens Geyer6bbbf192014-09-07 01:45:56 +0200509 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200510 on e:TException do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
511 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000512 end;
513
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100514
Jens Geyer2ad6c302015-02-26 19:38:53 +0100515 if FTransport.IsOpen then FTransport.Close;
516 FTransport.Open; // re-open connection, server has already closed
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100517
Jens Geyer2ad6c302015-02-26 19:38:53 +0100518
Roger Meier3bef8c22012-10-06 06:58:00 +0000519 // case 3: no exception
520 try
521 client.testException('something');
522 Expect( TRUE, 'testException(''something''): must not trow an exception');
523 except
524 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200525 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000526 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200527 {$ENDIF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000528
Jens Geyercc8c2c62021-03-29 22:38:30 +0200529 // re-open connection if needed
530 if not FTransport.IsOpen
531 then FTransport.Open;
Roger Meier3bef8c22012-10-06 06:58:00 +0000532
533 // simple things
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200534 StartTestGroup( 'simple Thrift calls', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000535 client.testVoid();
536 Expect( TRUE, 'testVoid()'); // success := no exception
537
Jens Geyer39ba6b72015-09-22 00:00:49 +0200538 s := BoolToString( client.testBool(TRUE));
539 Expect( s = BoolToString(TRUE), 'testBool(TRUE) = '+s);
540 s := BoolToString( client.testBool(FALSE));
541 Expect( s = BoolToString(FALSE), 'testBool(FALSE) = '+s);
542
Roger Meier3bef8c22012-10-06 06:58:00 +0000543 s := client.testString('Test');
544 Expect( s = 'Test', 'testString(''Test'') = "'+s+'"');
545
Jens Geyercf892d42017-09-09 10:08:22 +0200546 s := client.testString(''); // empty string
547 Expect( s = '', 'testString('''') = "'+s+'"');
548
Jens Geyer06045cf2013-03-27 20:26:25 +0200549 s := client.testString(HUGE_TEST_STRING);
550 Expect( length(s) = length(HUGE_TEST_STRING),
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100551 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
Jens Geyer06045cf2013-03-27 20:26:25 +0200552 +'=> length(result) = '+IntToStr(Length(s)));
553
Roger Meier3bef8c22012-10-06 06:58:00 +0000554 i8 := client.testByte(1);
555 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
556
557 i32 := client.testI32(-1);
558 Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32));
559
560 Console.WriteLine('testI64(-34359738368)');
561 i64 := client.testI64(-34359738368);
562 Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
563
Jens Geyer62445c12022-06-29 00:00:00 +0200564 guidOut := StringToGUID('{00112233-4455-6677-8899-AABBCCDDEEFF}');
565 Console.WriteLine('testUuid('+GUIDToString(guidOut)+')');
566 try
567 guidIn := client.testUuid(guidOut);
568 Expect( IsEqualGUID(guidIn, guidOut), 'testUuid('+GUIDToString(guidOut)+') = '+GUIDToString(guidIn));
569 except
570 on e:TApplicationException do Console.WriteLine('testUuid(): '+e.Message);
571 on e:Exception do Expect( FALSE, 'testUuid(): Unexpected exception "'+e.ClassName+'": '+e.Message);
572 end;
573
Jens Geyerd4df9172017-10-25 22:30:23 +0200574 // random binary small
Jens Geyer85827152018-01-12 21:20:59 +0100575 for testsize := Low(TTestSize) to High(TTestSize) do begin
576 binOut := PrepareBinaryData( TRUE, testsize);
Jens Geyerbd1a2732019-06-26 22:52:44 +0200577 Console.WriteLine('testBinary('+IntToStr(Length(binOut))+' bytes)');
Jens Geyer85827152018-01-12 21:20:59 +0100578 try
579 binIn := client.testBinary(binOut);
Jens Geyerbd1a2732019-06-26 22:52:44 +0200580 Expect( Length(binOut) = Length(binIn), 'testBinary('+IntToStr(Length(binOut))+' bytes): '+IntToStr(Length(binIn))+' bytes received');
Jens Geyer85827152018-01-12 21:20:59 +0100581 i32 := Min( Length(binOut), Length(binIn));
Jens Geyerbd1a2732019-06-26 22:52:44 +0200582 Expect( CompareMem( binOut, binIn, i32), 'testBinary('+IntToStr(Length(binOut))+' bytes): validating received data');
Jens Geyer85827152018-01-12 21:20:59 +0100583 except
584 on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message);
585 on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message);
586 end;
Jens Geyercf892d42017-09-09 10:08:22 +0200587 end;
588
Roger Meier3bef8c22012-10-06 06:58:00 +0000589 Console.WriteLine('testDouble(5.325098235)');
590 dub := client.testDouble(5.325098235);
591 Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
592
593 // structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200594 StartTestGroup( 'testStruct', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000595 Console.WriteLine('testStruct({''Zero'', 1, -3, -5})');
596 o := TXtructImpl.Create;
597 o.String_thing := 'Zero';
598 o.Byte_thing := 1;
599 o.I32_thing := -3;
600 o.I64_thing := -5;
601 i := client.testStruct(o);
602 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
603 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
604 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
605 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
606 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
607 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
608 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
609 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
610
611 // nested structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200612 StartTestGroup( 'testNest', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000613 Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})');
614 o2 := TXtruct2Impl.Create;
615 o2.Byte_thing := 1;
616 o2.Struct_thing := o;
617 o2.I32_thing := 5;
618 i2 := client.testNest(o2);
619 i := i2.Struct_thing;
620 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
621 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
622 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
623 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
624 Expect( i2.Byte_thing = 1, 'i2.Byte_thing = '+IntToStr(i2.Byte_thing));
625 Expect( i2.I32_thing = 5, 'i2.I32_thing = '+IntToStr(i2.I32_thing));
626 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
627 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
628 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
629 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
630 Expect( i2.__isset_Byte_thing, 'i2.__isset_Byte_thing');
631 Expect( i2.__isset_I32_thing, 'i2.__isset_I32_thing');
632
633 // map<type1,type2>: A map of strictly unique keys to values.
634 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200635 StartTestGroup( 'testMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000636 mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
637 for j := 0 to 4 do
638 begin
639 mapout.AddOrSetValue( j, j - 10);
640 end;
641 Console.Write('testMap({');
642 first := True;
643 for key in mapout.Keys do
644 begin
645 if first
646 then first := False
647 else Console.Write( ', ' );
648 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
649 end;
650 Console.WriteLine('})');
651
652 mapin := client.testMap( mapout );
653 Expect( mapin.Count = mapout.Count, 'testMap: mapin.Count = mapout.Count');
654 for j := 0 to 4 do
655 begin
656 Expect( mapout.ContainsKey(j), 'testMap: mapout.ContainsKey('+IntToStr(j)+') = '+BoolToString(mapout.ContainsKey(j)));
657 end;
658 for key in mapin.Keys do
659 begin
660 Expect( mapin[key] = mapout[key], 'testMap: '+IntToStr(key) + ' => ' + IntToStr( mapin[key]));
661 Expect( mapin[key] = key - 10, 'testMap: mapin['+IntToStr(key)+'] = '+IntToStr( mapin[key]));
662 end;
663
664
665 // map<type1,type2>: A map of strictly unique keys to values.
666 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200667 StartTestGroup( 'testStringMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000668 strmapout := TThriftDictionaryImpl<string,string>.Create;
669 for j := 0 to 4 do
670 begin
671 strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10));
672 end;
673 Console.Write('testStringMap({');
674 first := True;
675 for strkey in strmapout.Keys do
676 begin
677 if first
678 then first := False
679 else Console.Write( ', ' );
680 Console.Write( strkey + ' => ' + strmapout[strkey]);
681 end;
682 Console.WriteLine('})');
683
684 strmapin := client.testStringMap( strmapout );
685 Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count');
686 for j := 0 to 4 do
687 begin
688 Expect( strmapout.ContainsKey(IntToStr(j)),
689 'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = '
690 + BoolToString(strmapout.ContainsKey(IntToStr(j))));
691 end;
692 for strkey in strmapin.Keys do
693 begin
694 Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]);
695 Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]);
696 end;
697
698
699 // set<type>: An unordered set of unique elements.
700 // Translates to an STL set, Java HashSet, set in Python, etc.
701 // Note: PHP does not support sets, so it is treated similar to a List
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200702 StartTestGroup( 'testSet', test_Containers);
Jens Geyer6a797b92022-09-05 13:55:37 +0200703 setout := TThriftHashSetImpl<Integer>.Create;
Roger Meier3bef8c22012-10-06 06:58:00 +0000704 for j := -2 to 2 do
705 begin
706 setout.Add( j );
707 end;
708 Console.Write('testSet({');
709 first := True;
710 for j in setout 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 setin := client.testSet(setout);
720 Expect( setin.Count = setout.Count, 'testSet: setin.Count = setout.Count');
721 Expect( setin.Count = 5, 'testSet: setin.Count = '+IntToStr(setin.Count));
722 for j := -2 to 2 do // unordered, we can't rely on the order => test for known elements only
723 begin
724 Expect( setin.Contains(j), 'testSet: setin.Contains('+IntToStr(j)+') => '+BoolToString(setin.Contains(j)));
725 end;
726
727 // list<type>: An ordered list of elements.
728 // Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200729 StartTestGroup( 'testList', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000730 listout := TThriftListImpl<Integer>.Create;
731 listout.Add( +1);
732 listout.Add( -2);
733 listout.Add( +3);
734 listout.Add( -4);
735 listout.Add( 0);
736 Console.Write('testList({');
737 first := True;
738 for j in listout do
739 begin
740 if first
741 then first := False
742 else Console.Write(', ');
743 Console.Write(IntToStr( j));
744 end;
745 Console.WriteLine('})');
746
747 listin := client.testList(listout);
748 Expect( listin.Count = listout.Count, 'testList: listin.Count = listout.Count');
749 Expect( listin.Count = 5, 'testList: listin.Count = '+IntToStr(listin.Count));
750 Expect( listin[0] = +1, 'listin[0] = '+IntToStr( listin[0]));
751 Expect( listin[1] = -2, 'listin[1] = '+IntToStr( listin[1]));
752 Expect( listin[2] = +3, 'listin[2] = '+IntToStr( listin[2]));
753 Expect( listin[3] = -4, 'listin[3] = '+IntToStr( listin[3]));
754 Expect( listin[4] = 0, 'listin[4] = '+IntToStr( listin[4]));
755
756 // enums
757 ret := client.testEnum(TNumberz.ONE);
758 Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret)));
759
760 ret := client.testEnum(TNumberz.TWO);
761 Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret)));
762
763 ret := client.testEnum(TNumberz.THREE);
764 Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret)));
765
766 ret := client.testEnum(TNumberz.FIVE);
767 Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret)));
768
769 ret := client.testEnum(TNumberz.EIGHT);
770 Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret)));
771
772
773 // typedef
774 uid := client.testTypedef(309858235082523);
775 Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid));
776
777
778 // maps of maps
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200779 StartTestGroup( 'testMapMap(1)', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000780 mm := client.testMapMap(1);
781 Console.Write(' = {');
782 for key in mm.Keys do
783 begin
784 Console.Write( IntToStr( key) + ' => {');
785 m2 := mm[key];
786 for k2 in m2.Keys do
787 begin
788 Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', ');
789 end;
790 Console.Write('}, ');
791 end;
792 Console.WriteLine('}');
793
794 // verify result data
795 Expect( mm.Count = 2, 'mm.Count = '+IntToStr(mm.Count));
796 pos := mm[4];
797 neg := mm[-4];
798 for j := 1 to 4 do
799 begin
800 Expect( pos[j] = j, 'pos[j] = '+IntToStr(pos[j]));
801 Expect( neg[-j] = -j, 'neg[-j] = '+IntToStr(neg[-j]));
802 end;
803
804
805
806 // insanity
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200807 StartTestGroup( 'testInsanity', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000808 insane := TInsanityImpl.Create;
809 insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
810 insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000);
811 truck := TXtructImpl.Create;
812 truck.String_thing := 'Truck';
Jens Geyer540e3462016-12-28 14:25:41 +0100813 truck.Byte_thing := -8; // byte is signed
814 truck.I32_thing := 32;
815 truck.I64_thing := 64;
Roger Meier3bef8c22012-10-06 06:58:00 +0000816 insane.Xtructs := TThriftListImpl<IXtruct>.Create;
817 insane.Xtructs.Add( truck );
818 whoa := client.testInsanity( insane );
819 Console.Write(' = {');
820 for key64 in whoa.Keys do
821 begin
822 val := whoa[key64];
823 Console.Write( IntToStr( key64) + ' => {');
824 for k2_2 in val.Keys do
825 begin
826 v2 := val[k2_2];
827 Console.Write( IntToStr( Integer( k2_2)) + ' => {');
828 userMap := v2.UserMap;
829 Console.Write('{');
830 if userMap <> nil then
831 begin
832 for k3 in userMap.Keys do
833 begin
834 Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', ');
835 end;
836 end else
837 begin
838 Console.Write('null');
839 end;
840 Console.Write('}, ');
841 xtructs := v2.Xtructs;
842 Console.Write('{');
843
844 if xtructs <> nil then
845 begin
846 for x in xtructs do
847 begin
848 Console.Write('{"' + x.String_thing + '", ' +
849 IntToStr( x.Byte_thing) + ', ' +
850 IntToStr( x.I32_thing) + ', ' +
851 IntToStr( x.I32_thing) + '}, ');
852 end;
853 end else
854 begin
855 Console.Write('null');
856 end;
857 Console.Write('}');
858 Console.Write('}, ');
859 end;
860 Console.Write('}, ');
861 end;
862 Console.WriteLine('}');
863
Jens Geyer540e3462016-12-28 14:25:41 +0100864 (**
865 * So you think you've got this all worked, out eh?
866 *
867 * Creates a the returned map with these values and prints it out:
868 * { 1 => { 2 => argument,
869 * 3 => argument,
870 * },
871 * 2 => { 6 => <empty Insanity struct>, },
872 * }
873 * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
874 *)
875
Roger Meier3bef8c22012-10-06 06:58:00 +0000876 // verify result data
877 Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count));
878 //
879 first_map := whoa[1];
880 second_map := whoa[2];
881 Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count));
882 Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count));
883 //
884 looney := second_map[TNumberz.SIX];
885 Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney)));
886 Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
887 Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
888 //
889 for ret in [TNumberz.TWO, TNumberz.THREE] do begin
890 crazy := first_map[ret];
891 Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
892
893 Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap));
894 Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs));
895
Jens Geyer540e3462016-12-28 14:25:41 +0100896 Expect( crazy.UserMap.Count = insane.UserMap.Count, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count));
897 for pair in insane.UserMap do begin
898 Expect( crazy.UserMap[pair.Key] = pair.Value, 'crazy.UserMap['+IntToStr(Ord(pair.key))+'] = '+IntToStr(crazy.UserMap[pair.Key]));
899 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000900
Jens Geyer540e3462016-12-28 14:25:41 +0100901 Expect( crazy.Xtructs.Count = insane.Xtructs.Count, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count));
902 for arg0 := 0 to insane.Xtructs.Count-1 do begin
903 hello := insane.Xtructs[arg0];
904 goodbye := crazy.Xtructs[arg0];
905 Expect( goodbye.String_thing = hello.String_thing, 'goodbye.String_thing = '+goodbye.String_thing);
906 Expect( goodbye.Byte_thing = hello.Byte_thing, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing));
907 Expect( goodbye.I32_thing = hello.I32_thing, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing));
908 Expect( goodbye.I64_thing = hello.I64_thing, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing));
909 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000910 end;
911
912
913 // multi args
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200914 StartTestGroup( 'testMulti', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000915 arg0 := 1;
916 arg1 := 2;
917 arg2 := High(Int64);
918 arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
919 arg3.AddOrSetValue( 1, 'one');
920 arg4 := TNumberz.FIVE;
921 arg5 := 5000000;
922 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
923 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
924 arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
925 IntToStr( arg5) + ')');
926
927 i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5);
928 Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"');
929 Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing));
930 Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing));
931 Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing));
932 Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
933 Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
934 Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
935 Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
936
937 // multi exception
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200938 StartTestGroup( 'testMultiException(1)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000939 try
940 i := client.testMultiException( 'need more pizza', 'run out of beer');
941 Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"');
942 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
Jens Geyer6bbbf192014-09-07 01:45:56 +0200943 { this is not necessarily true, these fields are default-serialized
Jens Geyerd5436f52014-10-03 19:50:38 +0200944 Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
Roger Meier3bef8c22012-10-06 06:58:00 +0000945 Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
946 Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
Jens Geyerd5436f52014-10-03 19:50:38 +0200947 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000948 except
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200949 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000950 end;
951
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200952 StartTestGroup( 'testMultiException(Xception)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000953 try
954 i := client.testMultiException( 'Xception', 'second test');
955 Expect( FALSE, 'testMultiException(''Xception''): must trow an exception');
956 except
957 on x:TXception do begin
958 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
Jens Geyercd7a2aa2022-10-17 14:21:18 +0200959 Expect( x.__isset_Message, 'x.__isset_Message = '+BoolToString(x.__isset_Message));
Roger Meier3bef8c22012-10-06 06:58:00 +0000960 Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
961 Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"');
962 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200963 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000964 end;
965
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200966 StartTestGroup( 'testMultiException(Xception2)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000967 try
968 i := client.testMultiException( 'Xception2', 'third test');
969 Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception');
970 except
971 on x:TXception2 do begin
972 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
973 Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing));
974 Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
975 Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"');
976 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 +0200977 { this is not necessarily true, these fields are default-serialized
Roger Meier3bef8c22012-10-06 06:58:00 +0000978 Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing));
979 Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing));
980 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 +0200981 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000982 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200983 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000984 end;
985
986
987 // oneway functions
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200988 StartTestGroup( 'Test Oneway(1)', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000989 client.testOneway(1);
990 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
991
992 // call time
Jens Geyer06045cf2013-03-27 20:26:25 +0200993 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000994 StartTestGroup( 'Test Calltime()');
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200995 StartTick := GetTickCount;
Roger Meier3bef8c22012-10-06 06:58:00 +0000996 for k := 0 to 1000 - 1 do
997 begin
998 client.testVoid();
999 end;
1000 Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
Jens Geyer06045cf2013-03-27 20:26:25 +02001001 {$ENDIF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +00001002
1003 // no more tests here
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001004 StartTestGroup( '', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001005end;
1006
1007
Jens Geyer14f5d502017-12-09 13:47:09 +01001008{$IFDEF SupportsAsync}
1009procedure TClientThread.ClientAsyncTest;
1010var
1011 client : TThriftTest.IAsync;
1012 s : string;
1013 i8 : ShortInt;
1014begin
1015 StartTestGroup( 'Async Tests', test_Unknown);
1016 client := TThriftTest.TClient.Create( FProtocol);
1017 FTransport.Open;
1018
1019 // oneway void functions
1020 client.testOnewayAsync(1).Wait;
1021 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
1022
1023 // normal functions
1024 s := client.testStringAsync(HUGE_TEST_STRING).Value;
1025 Expect( length(s) = length(HUGE_TEST_STRING),
1026 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
1027 +'=> length(result) = '+IntToStr(Length(s)));
1028
1029 i8 := client.testByte(1).Value;
1030 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
1031end;
1032{$ENDIF}
1033
1034
Jens Geyer718f6ee2013-09-06 21:02:34 +02001035{$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +02001036procedure TClientThread.StressTest(const client : TThriftTest.Iface);
1037begin
1038 while TRUE do begin
1039 try
1040 if not FTransport.IsOpen then FTransport.Open; // re-open connection, server has already closed
1041 try
1042 client.testString('Test');
1043 Write('.');
1044 finally
1045 if FTransport.IsOpen then FTransport.Close;
1046 end;
1047 except
1048 on e:Exception do Writeln(#10+e.message);
1049 end;
1050 end;
1051end;
Jens Geyer718f6ee2013-09-06 21:02:34 +02001052{$ENDIF}
Jens Geyer06045cf2013-03-27 20:26:25 +02001053
Jens Geyerfd1b3582014-12-13 23:42:58 +01001054
Jens Geyer85827152018-01-12 21:20:59 +01001055function TClientThread.PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes;
Jens Geyerd4df9172017-10-25 22:30:23 +02001056var i : Integer;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001057begin
Jens Geyer85827152018-01-12 21:20:59 +01001058 case aSize of
1059 Empty : SetLength( result, 0);
1060 Normal : SetLength( result, $100);
1061 ByteArrayTest : SetLength( result, SizeOf(TByteArray) + 128);
1062 PipeWriteLimit : SetLength( result, 65535 + 128);
Jens Geyer2646bd62019-11-09 23:24:52 +01001063 FifteenMB : SetLength( result, 15 * 1024 * 1024);
Jens Geyer85827152018-01-12 21:20:59 +01001064 else
1065 raise EArgumentException.Create('aSize');
1066 end;
1067
Jens Geyerfd1b3582014-12-13 23:42:58 +01001068 ASSERT( Low(result) = 0);
Jens Geyer85827152018-01-12 21:20:59 +01001069 if Length(result) = 0 then Exit;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001070
1071 // linear distribution, unless random is requested
1072 if not aRandomDist then begin
1073 for i := Low(result) to High(result) do begin
Jens Geyer85827152018-01-12 21:20:59 +01001074 result[i] := i mod $100;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001075 end;
1076 Exit;
1077 end;
1078
1079 // random distribution of all 256 values
1080 FillChar( result[0], Length(result) * SizeOf(result[0]), $0);
Jens Geyerd4df9172017-10-25 22:30:23 +02001081 for i := Low(result) to High(result) do begin
1082 result[i] := Byte( Random($100));
Jens Geyerfd1b3582014-12-13 23:42:58 +01001083 end;
1084end;
1085
1086
Jens Geyerf7904452017-07-26 15:02:12 +02001087{$IFDEF Win64}
1088procedure TClientThread.UseInterlockedExchangeAdd64;
1089var a,b : Int64;
1090begin
1091 a := 1;
1092 b := 2;
1093 Thrift.Utils.InterlockedExchangeAdd64( a,b);
1094 Expect( a = 3, 'InterlockedExchangeAdd64');
1095end;
1096{$ENDIF}
1097
1098
Roger Meier3bef8c22012-10-06 06:58:00 +00001099procedure TClientThread.JSONProtocolReadWriteTest;
1100// Tests only then read/write procedures of the JSON protocol
1101// All tests succeed, if we can read what we wrote before
1102// Note that passing this test does not imply, that our JSON is really compatible to what
1103// other clients or servers expect as the real JSON. This is beyond the scope of this test.
1104var prot : IProtocol;
1105 stm : TStringStream;
Jens Geyer17c3ad92017-09-05 20:31:27 +02001106 list : TThriftList;
Jens Geyera019cda2019-11-09 23:24:52 +01001107 config : IThriftConfiguration;
Jens Geyercf892d42017-09-09 10:08:22 +02001108 binary, binRead, emptyBinary : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +00001109 i,iErr : Integer;
1110const
1111 TEST_SHORT = ShortInt( $FE);
1112 TEST_SMALL = SmallInt( $FEDC);
1113 TEST_LONG = LongInt( $FEDCBA98);
1114 TEST_I64 = Int64( $FEDCBA9876543210);
1115 TEST_DOUBLE = -1.234e-56;
1116 DELTA_DOUBLE = TEST_DOUBLE * 1e-14;
1117 TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars")
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001118 // Test THRIFT-2336 and THRIFT-3404 with U+1D11E (G Clef symbol) and 'Русское Название';
1119 G_CLEF_AND_CYRILLIC_TEXT = #$1d11e' '#$0420#$0443#$0441#$0441#$043a#$043e#$0435' '#$041d#$0430#$0437#$0432#$0430#$043d#$0438#$0435;
1120 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 +01001121 // test both possible solidus encodings
1122 SOLIDUS_JSON_DATA = '"one/two\/three"';
1123 SOLIDUS_EXCPECTED = 'one/two/three';
Roger Meier3bef8c22012-10-06 06:58:00 +00001124begin
1125 stm := TStringStream.Create;
1126 try
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001127 StartTestGroup( 'JsonProtocolTest', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001128
Jens Geyera019cda2019-11-09 23:24:52 +01001129 config := TThriftConfigurationImpl.Create;
1130
Roger Meier3bef8c22012-10-06 06:58:00 +00001131 // prepare binary data
Jens Geyer85827152018-01-12 21:20:59 +01001132 binary := PrepareBinaryData( FALSE, Normal);
Jens Geyercf892d42017-09-09 10:08:22 +02001133 SetLength( emptyBinary, 0); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001134
1135 // output setup
1136 prot := TJSONProtocolImpl.Create(
1137 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001138 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE), config));
Roger Meier3bef8c22012-10-06 06:58:00 +00001139
1140 // write
Jens Geyer17c3ad92017-09-05 20:31:27 +02001141 Init( list, TType.String_, 9);
1142 prot.WriteListBegin( list);
Roger Meier3bef8c22012-10-06 06:58:00 +00001143 prot.WriteBool( TRUE);
1144 prot.WriteBool( FALSE);
1145 prot.WriteByte( TEST_SHORT);
1146 prot.WriteI16( TEST_SMALL);
1147 prot.WriteI32( TEST_LONG);
1148 prot.WriteI64( TEST_I64);
1149 prot.WriteDouble( TEST_DOUBLE);
1150 prot.WriteString( TEST_STRING);
1151 prot.WriteBinary( binary);
Jens Geyercf892d42017-09-09 10:08:22 +02001152 prot.WriteString( ''); // empty string
1153 prot.WriteBinary( emptyBinary); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001154 prot.WriteListEnd;
1155
1156 // input setup
1157 Expect( stm.Position = stm.Size, 'Stream position/length after write');
1158 stm.Position := 0;
1159 prot := TJSONProtocolImpl.Create(
1160 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001161 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil, config));
Roger Meier3bef8c22012-10-06 06:58:00 +00001162
1163 // read and compare
1164 list := prot.ReadListBegin;
1165 Expect( list.ElementType = TType.String_, 'list element type');
1166 Expect( list.Count = 9, 'list element count');
1167 Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE');
1168 Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE');
1169 Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte');
1170 Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16');
1171 Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32');
1172 Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64');
1173 Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble');
1174 Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString');
1175 binRead := prot.ReadBinary;
Jens Geyercf892d42017-09-09 10:08:22 +02001176 Expect( Length(prot.ReadString) = 0, 'WriteString/ReadString (empty string)');
1177 Expect( Length(prot.ReadBinary) = 0, 'empty WriteBinary/ReadBinary (empty data block)');
Roger Meier3bef8c22012-10-06 06:58:00 +00001178 prot.ReadListEnd;
1179
1180 // test binary data
1181 Expect( Length(binary) = Length(binRead), 'Binary data length check');
1182 iErr := -1;
1183 for i := Low(binary) to High(binary) do begin
1184 if binary[i] <> binRead[i] then begin
1185 iErr := i;
1186 Break;
1187 end;
1188 end;
1189 if iErr < 0
1190 then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)')
1191 else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr));
1192
1193 Expect( stm.Position = stm.Size, 'Stream position after read');
1194
Jens Geyer7bb44a32014-02-07 22:24:37 +01001195
Jens Geyer21366942013-12-30 22:04:51 +01001196 // Solidus can be encoded in two ways. Make sure we can read both
1197 stm.Position := 0;
1198 stm.Size := 0;
1199 stm.WriteString(SOLIDUS_JSON_DATA);
1200 stm.Position := 0;
1201 prot := TJSONProtocolImpl.Create(
1202 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001203 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil, config));
Jens Geyer21366942013-12-30 22:04:51 +01001204 Expect( prot.ReadString = SOLIDUS_EXCPECTED, 'Solidus encoding');
1205
1206
Jens Geyer7bb44a32014-02-07 22:24:37 +01001207 // Widechars should work too. Do they?
1208 // After writing, we ensure that we are able to read it back
1209 // We can't assume hex-encoding, since (nearly) any Unicode char is valid JSON
1210 stm.Position := 0;
1211 stm.Size := 0;
1212 prot := TJSONProtocolImpl.Create(
1213 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001214 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE), config));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001215 prot.WriteString( G_CLEF_AND_CYRILLIC_TEXT);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001216 stm.Position := 0;
1217 prot := TJSONProtocolImpl.Create(
1218 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001219 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil, config));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001220 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Writing JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001221
1222 // Widechars should work with hex-encoding too. Do they?
1223 stm.Position := 0;
1224 stm.Size := 0;
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001225 stm.WriteString( G_CLEF_AND_CYRILLIC_JSON);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001226 stm.Position := 0;
1227 prot := TJSONProtocolImpl.Create(
1228 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001229 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil, config));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001230 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Reading JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001231
1232
Roger Meier3bef8c22012-10-06 06:58:00 +00001233 finally
1234 stm.Free;
1235 prot := nil; //-> Release
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001236 StartTestGroup( '', test_Unknown); // no more tests here
Roger Meier3bef8c22012-10-06 06:58:00 +00001237 end;
1238end;
1239
1240
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001241procedure TClientThread.StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +00001242begin
1243 FTestGroup := aGroup;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001244 FCurrentTest := aTest;
1245
1246 Include( FExecuted, aTest);
1247
Roger Meier3bef8c22012-10-06 06:58:00 +00001248 if FTestGroup <> '' then begin
1249 Console.WriteLine('');
1250 Console.WriteLine( aGroup+' tests');
1251 Console.WriteLine( StringOfChar('-',60));
1252 end;
1253end;
1254
1255
1256procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
1257begin
1258 if aTestResult then begin
1259 Inc(FSuccesses);
1260 Console.WriteLine( aTestInfo+': passed');
1261 end
1262 else begin
1263 FErrors.Add( FTestGroup+': '+aTestInfo);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001264 Include( FFailed, FCurrentTest);
Roger Meier3bef8c22012-10-06 06:58:00 +00001265 Console.WriteLine( aTestInfo+': *** FAILED ***');
1266
1267 // We have a failed test!
1268 // -> issue DebugBreak ONLY if a debugger is attached,
1269 // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise
Jens Geyer9f7f11e2016-04-14 21:37:11 +02001270 if IsDebuggerPresent
1271 then {$IFDEF CPUX64} DebugBreak {$ELSE} asm int 3 end {$ENDIF};
Roger Meier3bef8c22012-10-06 06:58:00 +00001272 end;
1273end;
1274
1275
1276procedure TClientThread.ReportResults;
1277var nTotal : Integer;
1278 sLine : string;
1279begin
1280 // prevent us from stupid DIV/0 errors
1281 nTotal := FSuccesses + FErrors.Count;
1282 if nTotal = 0 then begin
1283 Console.WriteLine('No results logged');
1284 Exit;
1285 end;
1286
1287 Console.WriteLine('');
1288 Console.WriteLine( StringOfChar('=',60));
1289 Console.WriteLine( IntToStr(nTotal)+' tests performed');
1290 Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)');
1291 Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)');
1292 Console.WriteLine( StringOfChar('=',60));
1293 if FErrors.Count > 0 then begin
1294 Console.WriteLine('FAILED TESTS:');
1295 for sLine in FErrors do Console.WriteLine('- '+sLine);
1296 Console.WriteLine( StringOfChar('=',60));
1297 InterlockedIncrement( ExitCode); // return <> 0 on errors
1298 end;
1299 Console.WriteLine('');
1300end;
1301
1302
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001303function TClientThread.CalculateExitCode : Byte;
1304var test : TTestGroup;
1305begin
1306 result := EXITCODE_SUCCESS;
1307 for test := Low(TTestGroup) to High(TTestGroup) do begin
1308 if (test in FFailed) or not (test in FExecuted)
1309 then result := result or MAP_FAILURES_TO_EXITCODE_BITS[test];
1310 end;
1311end;
1312
1313
Jens Geyer48d3bef2022-09-08 21:48:41 +02001314constructor TClientThread.Create( const aSetup : TTestSetup; const aNumIteration, aThreadNo: Integer; const aLogThreadID : Boolean);
Roger Meier3bef8c22012-10-06 06:58:00 +00001315begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001316 FSetup := aSetup;
Jens Geyer48d3bef2022-09-08 21:48:41 +02001317 FThreadNo := aThreadNo;
1318 FNumIterations := aNumIteration;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001319
Jens Geyer48d3bef2022-09-08 21:48:41 +02001320 FConsole := TThreadConsole.Create( Self, aLogThreadID);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001321 FCurrentTest := test_Unknown;
Roger Meier3bef8c22012-10-06 06:58:00 +00001322
1323 // error list: keep correct order, allow for duplicates
1324 FErrors := TStringList.Create;
1325 FErrors.Sorted := FALSE;
1326 FErrors.Duplicates := dupAccept;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001327
1328 inherited Create( TRUE);
Roger Meier3bef8c22012-10-06 06:58:00 +00001329end;
1330
1331destructor TClientThread.Destroy;
1332begin
1333 FreeAndNil( FConsole);
1334 FreeAndNil( FErrors);
1335 inherited;
1336end;
1337
1338procedure TClientThread.Execute;
1339var
1340 i : Integer;
Roger Meier3bef8c22012-10-06 06:58:00 +00001341begin
1342 // perform all tests
1343 try
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001344 {$IFDEF Win64}
Jens Geyerf7904452017-07-26 15:02:12 +02001345 UseInterlockedExchangeAdd64;
Jens Geyer14f5d502017-12-09 13:47:09 +01001346 {$ENDIF}
Jens Geyer7bb44a32014-02-07 22:24:37 +01001347 JSONProtocolReadWriteTest;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001348
1349 // must be run in the context of the thread
1350 InitializeProtocolTransportStack;
1351 try
Jens Geyer48d3bef2022-09-08 21:48:41 +02001352 for i := 0 to FNumIterations - 1 do begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001353 ClientTest;
1354 {$IFDEF SupportsAsync}
1355 ClientAsyncTest;
1356 {$ENDIF}
1357 end;
1358
1359 // report the outcome
1360 ReportResults;
1361 SetReturnValue( CalculateExitCode);
1362
1363 finally
1364 ShutdownProtocolTransportStack;
Roger Meier3bef8c22012-10-06 06:58:00 +00001365 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001366
Roger Meier3bef8c22012-10-06 06:58:00 +00001367 except
1368 on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"');
1369 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001370end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001371
Roger Meier3bef8c22012-10-06 06:58:00 +00001372
Jens Geyera019cda2019-11-09 23:24:52 +01001373function TClientThread.InitializeHttpTransport( const aTimeoutSetting : Integer; const aConfig : IThriftConfiguration) : IHTTPClient;
Jens Geyer83ff7532019-06-06 22:46:03 +02001374var sUrl : string;
1375 comps : URL_COMPONENTS;
1376 dwChars : DWORD;
Jens Geyer02230912019-04-03 01:12:51 +02001377begin
1378 ASSERT( FSetup.endpoint in [trns_MsxmlHttp, trns_WinHttp]);
1379
1380 if FSetup.useSSL
1381 then sUrl := 'https://'
1382 else sUrl := 'http://';
1383
1384 sUrl := sUrl + FSetup.host;
1385
Jens Geyer83ff7532019-06-06 22:46:03 +02001386 // add the port number if necessary and at the right place
1387 FillChar( comps, SizeOf(comps), 0);
1388 comps.dwStructSize := SizeOf(comps);
1389 comps.dwSchemeLength := MAXINT;
1390 comps.dwHostNameLength := MAXINT;
1391 comps.dwUserNameLength := MAXINT;
1392 comps.dwPasswordLength := MAXINT;
1393 comps.dwUrlPathLength := MAXINT;
1394 comps.dwExtraInfoLength := MAXINT;
1395 Win32Check( WinHttpCrackUrl( PChar(sUrl), Length(sUrl), 0, comps));
Jens Geyer02230912019-04-03 01:12:51 +02001396 case FSetup.port of
Jens Geyer83ff7532019-06-06 22:46:03 +02001397 80 : if FSetup.useSSL then comps.nPort := FSetup.port;
1398 443 : if not FSetup.useSSL then comps.nPort := FSetup.port;
Jens Geyer02230912019-04-03 01:12:51 +02001399 else
Jens Geyer83ff7532019-06-06 22:46:03 +02001400 if FSetup.port > 0 then comps.nPort := FSetup.port;
Jens Geyer02230912019-04-03 01:12:51 +02001401 end;
Jens Geyer83ff7532019-06-06 22:46:03 +02001402 dwChars := Length(sUrl) + 64;
1403 SetLength( sUrl, dwChars);
1404 Win32Check( WinHttpCreateUrl( comps, 0, @sUrl[1], dwChars));
1405 SetLength( sUrl, dwChars);
1406
Jens Geyer02230912019-04-03 01:12:51 +02001407
1408 Console.WriteLine('Target URL: '+sUrl);
1409 case FSetup.endpoint of
Jens Geyera019cda2019-11-09 23:24:52 +01001410 trns_MsxmlHttp : result := TMsxmlHTTPClientImpl.Create( sUrl, aConfig);
1411 trns_WinHttp : result := TWinHTTPClientImpl.Create( sUrl, aConfig);
Jens Geyer02230912019-04-03 01:12:51 +02001412 else
1413 raise Exception.Create(ENDPOINT_TRANSPORTS[FSetup.endpoint]+' unhandled case');
1414 end;
1415
1416 result.DnsResolveTimeout := aTimeoutSetting;
1417 result.ConnectionTimeout := aTimeoutSetting;
1418 result.SendTimeout := aTimeoutSetting;
1419 result.ReadTimeout := aTimeoutSetting;
1420end;
1421
1422
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001423procedure TClientThread.InitializeProtocolTransportStack;
Jens Geyer02230912019-04-03 01:12:51 +02001424var streamtrans : IStreamTransport;
Jens Geyer47f63172019-06-06 22:42:58 +02001425 canSSL : Boolean;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001426const
1427 DEBUG_TIMEOUT = 30 * 1000;
1428 RELEASE_TIMEOUT = DEFAULT_THRIFT_TIMEOUT;
1429 PIPE_TIMEOUT = RELEASE_TIMEOUT;
1430 HTTP_TIMEOUTS = 10 * 1000;
1431begin
1432 // needed for HTTP clients as they utilize the MSXML COM components
1433 OleCheck( CoInitialize( nil));
1434
Jens Geyer47f63172019-06-06 22:42:58 +02001435 canSSL := FALSE;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001436 case FSetup.endpoint of
1437 trns_Sockets: begin
1438 Console.WriteLine('Using sockets ('+FSetup.host+' port '+IntToStr(FSetup.port)+')');
Jens Geyera019cda2019-11-09 23:24:52 +01001439 streamtrans := TSocketImpl.Create( FSetup.host, FSetup.port);
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001440 FTransport := streamtrans;
1441 end;
1442
Jens Geyer02230912019-04-03 01:12:51 +02001443 trns_MsxmlHttp,
1444 trns_WinHttp: begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001445 Console.WriteLine('Using HTTPClient');
Jens Geyer02230912019-04-03 01:12:51 +02001446 FTransport := InitializeHttpTransport( HTTP_TIMEOUTS);
Jens Geyer47f63172019-06-06 22:42:58 +02001447 canSSL := TRUE;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001448 end;
1449
1450 trns_EvHttp: begin
1451 raise Exception.Create(ENDPOINT_TRANSPORTS[FSetup.endpoint]+' transport not implemented');
1452 end;
1453
1454 trns_NamedPipes: begin
1455 streamtrans := TNamedPipeTransportClientEndImpl.Create( FSetup.sPipeName, 0, nil, PIPE_TIMEOUT, PIPE_TIMEOUT);
1456 FTransport := streamtrans;
1457 end;
1458
1459 trns_AnonPipes: begin
Jens Geyera019cda2019-11-09 23:24:52 +01001460 streamtrans := TAnonymousPipeTransportImpl.Create( FSetup.hAnonRead, FSetup.hAnonWrite, FALSE, PIPE_TIMEOUT);
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001461 FTransport := streamtrans;
1462 end;
1463
1464 else
1465 raise Exception.Create('Unhandled endpoint transport');
1466 end;
1467 ASSERT( FTransport <> nil);
1468
1469 // layered transports are not really meant to be stacked upon each other
1470 if (trns_Framed in FSetup.layered) then begin
1471 FTransport := TFramedTransportImpl.Create( FTransport);
1472 end
1473 else if (trns_Buffered in FSetup.layered) and (streamtrans <> nil) then begin
1474 FTransport := TBufferedTransportImpl.Create( streamtrans, 32); // small buffer to test read()
1475 end;
1476
Jens Geyer47f63172019-06-06 22:42:58 +02001477 if FSetup.useSSL and not canSSL then begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001478 raise Exception.Create('SSL/TLS not implemented');
1479 end;
1480
1481 // create protocol instance, default to BinaryProtocol
Jens Geyer3b686532021-07-01 23:04:08 +02001482 FProtocol := PROTOCOL_CLASSES[FSetup.protType].Create(FTransport);
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001483 ASSERT( (FTransport <> nil) and (FProtocol <> nil));
1484end;
1485
1486
1487procedure TClientThread.ShutdownProtocolTransportStack;
1488begin
1489 try
1490 FProtocol := nil;
1491
1492 if FTransport <> nil then begin
Roger Meier3bef8c22012-10-06 06:58:00 +00001493 FTransport.Close;
1494 FTransport := nil;
1495 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001496
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001497 finally
1498 CoUninitialize;
1499 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001500end;
1501
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001502
Roger Meier3bef8c22012-10-06 06:58:00 +00001503{ TThreadConsole }
1504
Jens Geyer48d3bef2022-09-08 21:48:41 +02001505constructor TThreadConsole.Create( const aThread: TClientThread; const aLogThreadID : Boolean);
Roger Meier3bef8c22012-10-06 06:58:00 +00001506begin
Jens Geyer718f6ee2013-09-06 21:02:34 +02001507 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +00001508 FThread := AThread;
Jens Geyer48d3bef2022-09-08 21:48:41 +02001509 FLogThreadID := aLogThreadID;
Roger Meier3bef8c22012-10-06 06:58:00 +00001510end;
1511
1512procedure TThreadConsole.Write(const S: string);
Roger Meier3bef8c22012-10-06 06:58:00 +00001513begin
Jens Geyer48d3bef2022-09-08 21:48:41 +02001514 if FLogThreadID
1515 then ConsoleHelper.Console.Write( IntToStr(FThread.ThreadNo)+'> '+S)
1516 else ConsoleHelper.Console.Write( S);
Roger Meier3bef8c22012-10-06 06:58:00 +00001517end;
1518
1519procedure TThreadConsole.WriteLine(const S: string);
Roger Meier3bef8c22012-10-06 06:58:00 +00001520begin
Jens Geyer48d3bef2022-09-08 21:48:41 +02001521 if FLogThreadID
1522 then ConsoleHelper.Console.WriteLine( IntToStr(FThread.ThreadNo)+'> '+S)
1523 else ConsoleHelper.Console.WriteLine( S);
Roger Meier3bef8c22012-10-06 06:58:00 +00001524end;
1525
Jens Geyer48d3bef2022-09-08 21:48:41 +02001526
Roger Meier3bef8c22012-10-06 06:58:00 +00001527initialization
Jens Geyer48d3bef2022-09-08 21:48:41 +02001528 TTestClient.FNumIterations := 1;
1529 TTestClient.FNumThreads := 1;
Roger Meier3bef8c22012-10-06 06:58:00 +00001530
1531end.