blob: 86235ebaf54c828560c8531994bc0c62b08af3e2 [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
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;
Jens Geyer62445c12022-06-29 00:00:00 +0200396 guidIn, guidOut : TGuid;
Roger Meier3bef8c22012-10-06 06:58:00 +0000397 dub : Double;
398 o : IXtruct;
399 o2 : IXtruct2;
400 i : IXtruct;
401 i2 : IXtruct2;
402 mapout : IThriftDictionary<Integer,Integer>;
403 mapin : IThriftDictionary<Integer,Integer>;
404 strmapout : IThriftDictionary<string,string>;
405 strmapin : IThriftDictionary<string,string>;
406 j : Integer;
407 first : Boolean;
408 key : Integer;
409 strkey : string;
410 listout : IThriftList<Integer>;
411 listin : IThriftList<Integer>;
Jens Geyer6a797b92022-09-05 13:55:37 +0200412 setout : IThriftHashSet<Integer>;
413 setin : IThriftHashSet<Integer>;
Roger Meier3bef8c22012-10-06 06:58:00 +0000414 ret : TNumberz;
415 uid : Int64;
416 mm : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
417 pos : IThriftDictionary<Integer, Integer>;
418 neg : IThriftDictionary<Integer, Integer>;
419 m2 : IThriftDictionary<Integer, Integer>;
420 k2 : Integer;
421 insane : IInsanity;
422 truck : IXtruct;
423 whoa : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
424 key64 : Int64;
425 val : IThriftDictionary<TNumberz, IInsanity>;
426 k2_2 : TNumberz;
427 k3 : TNumberz;
428 v2 : IInsanity;
429 userMap : IThriftDictionary<TNumberz, Int64>;
430 xtructs : IThriftList<IXtruct>;
431 x : IXtruct;
432 arg0 : ShortInt;
433 arg1 : Integer;
434 arg2 : Int64;
435 arg3 : IThriftDictionary<SmallInt, string>;
436 arg4 : TNumberz;
437 arg5 : Int64;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200438 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000439 StartTick : Cardinal;
440 k : Integer;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200441 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +0000442 hello, goodbye : IXtruct;
443 crazy : IInsanity;
444 looney : IInsanity;
445 first_map : IThriftDictionary<TNumberz, IInsanity>;
446 second_map : IThriftDictionary<TNumberz, IInsanity>;
Jens Geyer540e3462016-12-28 14:25:41 +0100447 pair : TPair<TNumberz, TUserId>;
Jens Geyer85827152018-01-12 21:20:59 +0100448 testsize : TTestSize;
Roger Meier3bef8c22012-10-06 06:58:00 +0000449begin
450 client := TThriftTest.TClient.Create( FProtocol);
451 FTransport.Open;
452
Jens Geyer06045cf2013-03-27 20:26:25 +0200453 {$IFDEF StressTest}
454 StressTest( client);
455 {$ENDIF StressTest}
456
Jens Geyer17c3ad92017-09-05 20:31:27 +0200457 {$IFDEF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000458 // in-depth exception test
459 // (1) do we get an exception at all?
460 // (2) do we get the right exception?
461 // (3) does the exception contain the expected data?
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200462 StartTestGroup( 'testException', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000463 // case 1: exception type declared in IDL at the function call
464 try
465 client.testException('Xception');
466 Expect( FALSE, 'testException(''Xception''): must trow an exception');
467 except
468 on e:TXception do begin
469 Expect( e.ErrorCode = 1001, 'error code');
470 Expect( e.Message_ = 'Xception', 'error message');
471 Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ );
472 end;
473 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200474 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000475 end;
476
Jens Geyercc8c2c62021-03-29 22:38:30 +0200477 // re-open connection if needed
478 if not FTransport.IsOpen
479 then FTransport.Open;
480
Roger Meier3bef8c22012-10-06 06:58:00 +0000481 // case 2: exception type NOT declared in IDL at the function call
482 // this will close the connection
483 try
484 client.testException('TException');
485 Expect( FALSE, 'testException(''TException''): must trow an exception');
486 except
487 on e:TTransportException do begin
488 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Roger Meier3bef8c22012-10-06 06:58:00 +0000489 end;
Jens Geyer6bbbf192014-09-07 01:45:56 +0200490 on e:TApplicationException do begin
491 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
Jens Geyer6bbbf192014-09-07 01:45:56 +0200492 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200493 on e:TException do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
494 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000495 end;
496
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100497
Jens Geyer2ad6c302015-02-26 19:38:53 +0100498 if FTransport.IsOpen then FTransport.Close;
499 FTransport.Open; // re-open connection, server has already closed
Jens Geyer6f6aa8a2016-03-10 19:47:12 +0100500
Jens Geyer2ad6c302015-02-26 19:38:53 +0100501
Roger Meier3bef8c22012-10-06 06:58:00 +0000502 // case 3: no exception
503 try
504 client.testException('something');
505 Expect( TRUE, 'testException(''something''): must not trow an exception');
506 except
507 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200508 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000509 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200510 {$ENDIF Exceptions}
Roger Meier3bef8c22012-10-06 06:58:00 +0000511
Jens Geyercc8c2c62021-03-29 22:38:30 +0200512 // re-open connection if needed
513 if not FTransport.IsOpen
514 then FTransport.Open;
Roger Meier3bef8c22012-10-06 06:58:00 +0000515
516 // simple things
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200517 StartTestGroup( 'simple Thrift calls', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000518 client.testVoid();
519 Expect( TRUE, 'testVoid()'); // success := no exception
520
Jens Geyer39ba6b72015-09-22 00:00:49 +0200521 s := BoolToString( client.testBool(TRUE));
522 Expect( s = BoolToString(TRUE), 'testBool(TRUE) = '+s);
523 s := BoolToString( client.testBool(FALSE));
524 Expect( s = BoolToString(FALSE), 'testBool(FALSE) = '+s);
525
Roger Meier3bef8c22012-10-06 06:58:00 +0000526 s := client.testString('Test');
527 Expect( s = 'Test', 'testString(''Test'') = "'+s+'"');
528
Jens Geyercf892d42017-09-09 10:08:22 +0200529 s := client.testString(''); // empty string
530 Expect( s = '', 'testString('''') = "'+s+'"');
531
Jens Geyer06045cf2013-03-27 20:26:25 +0200532 s := client.testString(HUGE_TEST_STRING);
533 Expect( length(s) = length(HUGE_TEST_STRING),
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100534 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
Jens Geyer06045cf2013-03-27 20:26:25 +0200535 +'=> length(result) = '+IntToStr(Length(s)));
536
Roger Meier3bef8c22012-10-06 06:58:00 +0000537 i8 := client.testByte(1);
538 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
539
540 i32 := client.testI32(-1);
541 Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32));
542
543 Console.WriteLine('testI64(-34359738368)');
544 i64 := client.testI64(-34359738368);
545 Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
546
Jens Geyer62445c12022-06-29 00:00:00 +0200547 guidOut := StringToGUID('{00112233-4455-6677-8899-AABBCCDDEEFF}');
548 Console.WriteLine('testUuid('+GUIDToString(guidOut)+')');
549 try
550 guidIn := client.testUuid(guidOut);
551 Expect( IsEqualGUID(guidIn, guidOut), 'testUuid('+GUIDToString(guidOut)+') = '+GUIDToString(guidIn));
552 except
553 on e:TApplicationException do Console.WriteLine('testUuid(): '+e.Message);
554 on e:Exception do Expect( FALSE, 'testUuid(): Unexpected exception "'+e.ClassName+'": '+e.Message);
555 end;
556
Jens Geyerd4df9172017-10-25 22:30:23 +0200557 // random binary small
Jens Geyer85827152018-01-12 21:20:59 +0100558 for testsize := Low(TTestSize) to High(TTestSize) do begin
559 binOut := PrepareBinaryData( TRUE, testsize);
Jens Geyerbd1a2732019-06-26 22:52:44 +0200560 Console.WriteLine('testBinary('+IntToStr(Length(binOut))+' bytes)');
Jens Geyer85827152018-01-12 21:20:59 +0100561 try
562 binIn := client.testBinary(binOut);
Jens Geyerbd1a2732019-06-26 22:52:44 +0200563 Expect( Length(binOut) = Length(binIn), 'testBinary('+IntToStr(Length(binOut))+' bytes): '+IntToStr(Length(binIn))+' bytes received');
Jens Geyer85827152018-01-12 21:20:59 +0100564 i32 := Min( Length(binOut), Length(binIn));
Jens Geyerbd1a2732019-06-26 22:52:44 +0200565 Expect( CompareMem( binOut, binIn, i32), 'testBinary('+IntToStr(Length(binOut))+' bytes): validating received data');
Jens Geyer85827152018-01-12 21:20:59 +0100566 except
567 on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message);
568 on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message);
569 end;
Jens Geyercf892d42017-09-09 10:08:22 +0200570 end;
571
Roger Meier3bef8c22012-10-06 06:58:00 +0000572 Console.WriteLine('testDouble(5.325098235)');
573 dub := client.testDouble(5.325098235);
574 Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
575
576 // structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200577 StartTestGroup( 'testStruct', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000578 Console.WriteLine('testStruct({''Zero'', 1, -3, -5})');
579 o := TXtructImpl.Create;
580 o.String_thing := 'Zero';
581 o.Byte_thing := 1;
582 o.I32_thing := -3;
583 o.I64_thing := -5;
584 i := client.testStruct(o);
585 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
586 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
587 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
588 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
589 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
590 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
591 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
592 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
593
594 // nested structs
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200595 StartTestGroup( 'testNest', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000596 Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})');
597 o2 := TXtruct2Impl.Create;
598 o2.Byte_thing := 1;
599 o2.Struct_thing := o;
600 o2.I32_thing := 5;
601 i2 := client.testNest(o2);
602 i := i2.Struct_thing;
603 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
604 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
605 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
606 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
607 Expect( i2.Byte_thing = 1, 'i2.Byte_thing = '+IntToStr(i2.Byte_thing));
608 Expect( i2.I32_thing = 5, 'i2.I32_thing = '+IntToStr(i2.I32_thing));
609 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
610 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
611 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
612 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
613 Expect( i2.__isset_Byte_thing, 'i2.__isset_Byte_thing');
614 Expect( i2.__isset_I32_thing, 'i2.__isset_I32_thing');
615
616 // map<type1,type2>: A map of strictly unique keys to values.
617 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200618 StartTestGroup( 'testMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000619 mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
620 for j := 0 to 4 do
621 begin
622 mapout.AddOrSetValue( j, j - 10);
623 end;
624 Console.Write('testMap({');
625 first := True;
626 for key in mapout.Keys do
627 begin
628 if first
629 then first := False
630 else Console.Write( ', ' );
631 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
632 end;
633 Console.WriteLine('})');
634
635 mapin := client.testMap( mapout );
636 Expect( mapin.Count = mapout.Count, 'testMap: mapin.Count = mapout.Count');
637 for j := 0 to 4 do
638 begin
639 Expect( mapout.ContainsKey(j), 'testMap: mapout.ContainsKey('+IntToStr(j)+') = '+BoolToString(mapout.ContainsKey(j)));
640 end;
641 for key in mapin.Keys do
642 begin
643 Expect( mapin[key] = mapout[key], 'testMap: '+IntToStr(key) + ' => ' + IntToStr( mapin[key]));
644 Expect( mapin[key] = key - 10, 'testMap: mapin['+IntToStr(key)+'] = '+IntToStr( mapin[key]));
645 end;
646
647
648 // map<type1,type2>: A map of strictly unique keys to values.
649 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200650 StartTestGroup( 'testStringMap', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000651 strmapout := TThriftDictionaryImpl<string,string>.Create;
652 for j := 0 to 4 do
653 begin
654 strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10));
655 end;
656 Console.Write('testStringMap({');
657 first := True;
658 for strkey in strmapout.Keys do
659 begin
660 if first
661 then first := False
662 else Console.Write( ', ' );
663 Console.Write( strkey + ' => ' + strmapout[strkey]);
664 end;
665 Console.WriteLine('})');
666
667 strmapin := client.testStringMap( strmapout );
668 Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count');
669 for j := 0 to 4 do
670 begin
671 Expect( strmapout.ContainsKey(IntToStr(j)),
672 'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = '
673 + BoolToString(strmapout.ContainsKey(IntToStr(j))));
674 end;
675 for strkey in strmapin.Keys do
676 begin
677 Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]);
678 Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]);
679 end;
680
681
682 // set<type>: An unordered set of unique elements.
683 // Translates to an STL set, Java HashSet, set in Python, etc.
684 // Note: PHP does not support sets, so it is treated similar to a List
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200685 StartTestGroup( 'testSet', test_Containers);
Jens Geyer6a797b92022-09-05 13:55:37 +0200686 setout := TThriftHashSetImpl<Integer>.Create;
Roger Meier3bef8c22012-10-06 06:58:00 +0000687 for j := -2 to 2 do
688 begin
689 setout.Add( j );
690 end;
691 Console.Write('testSet({');
692 first := True;
693 for j in setout do
694 begin
695 if first
696 then first := False
697 else Console.Write(', ');
698 Console.Write(IntToStr( j));
699 end;
700 Console.WriteLine('})');
701
702 setin := client.testSet(setout);
703 Expect( setin.Count = setout.Count, 'testSet: setin.Count = setout.Count');
704 Expect( setin.Count = 5, 'testSet: setin.Count = '+IntToStr(setin.Count));
705 for j := -2 to 2 do // unordered, we can't rely on the order => test for known elements only
706 begin
707 Expect( setin.Contains(j), 'testSet: setin.Contains('+IntToStr(j)+') => '+BoolToString(setin.Contains(j)));
708 end;
709
710 // list<type>: An ordered list of elements.
711 // Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200712 StartTestGroup( 'testList', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000713 listout := TThriftListImpl<Integer>.Create;
714 listout.Add( +1);
715 listout.Add( -2);
716 listout.Add( +3);
717 listout.Add( -4);
718 listout.Add( 0);
719 Console.Write('testList({');
720 first := True;
721 for j in listout do
722 begin
723 if first
724 then first := False
725 else Console.Write(', ');
726 Console.Write(IntToStr( j));
727 end;
728 Console.WriteLine('})');
729
730 listin := client.testList(listout);
731 Expect( listin.Count = listout.Count, 'testList: listin.Count = listout.Count');
732 Expect( listin.Count = 5, 'testList: listin.Count = '+IntToStr(listin.Count));
733 Expect( listin[0] = +1, 'listin[0] = '+IntToStr( listin[0]));
734 Expect( listin[1] = -2, 'listin[1] = '+IntToStr( listin[1]));
735 Expect( listin[2] = +3, 'listin[2] = '+IntToStr( listin[2]));
736 Expect( listin[3] = -4, 'listin[3] = '+IntToStr( listin[3]));
737 Expect( listin[4] = 0, 'listin[4] = '+IntToStr( listin[4]));
738
739 // enums
740 ret := client.testEnum(TNumberz.ONE);
741 Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret)));
742
743 ret := client.testEnum(TNumberz.TWO);
744 Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret)));
745
746 ret := client.testEnum(TNumberz.THREE);
747 Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret)));
748
749 ret := client.testEnum(TNumberz.FIVE);
750 Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret)));
751
752 ret := client.testEnum(TNumberz.EIGHT);
753 Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret)));
754
755
756 // typedef
757 uid := client.testTypedef(309858235082523);
758 Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid));
759
760
761 // maps of maps
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200762 StartTestGroup( 'testMapMap(1)', test_Containers);
Roger Meier3bef8c22012-10-06 06:58:00 +0000763 mm := client.testMapMap(1);
764 Console.Write(' = {');
765 for key in mm.Keys do
766 begin
767 Console.Write( IntToStr( key) + ' => {');
768 m2 := mm[key];
769 for k2 in m2.Keys do
770 begin
771 Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', ');
772 end;
773 Console.Write('}, ');
774 end;
775 Console.WriteLine('}');
776
777 // verify result data
778 Expect( mm.Count = 2, 'mm.Count = '+IntToStr(mm.Count));
779 pos := mm[4];
780 neg := mm[-4];
781 for j := 1 to 4 do
782 begin
783 Expect( pos[j] = j, 'pos[j] = '+IntToStr(pos[j]));
784 Expect( neg[-j] = -j, 'neg[-j] = '+IntToStr(neg[-j]));
785 end;
786
787
788
789 // insanity
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200790 StartTestGroup( 'testInsanity', test_Structs);
Roger Meier3bef8c22012-10-06 06:58:00 +0000791 insane := TInsanityImpl.Create;
792 insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
793 insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000);
794 truck := TXtructImpl.Create;
795 truck.String_thing := 'Truck';
Jens Geyer540e3462016-12-28 14:25:41 +0100796 truck.Byte_thing := -8; // byte is signed
797 truck.I32_thing := 32;
798 truck.I64_thing := 64;
Roger Meier3bef8c22012-10-06 06:58:00 +0000799 insane.Xtructs := TThriftListImpl<IXtruct>.Create;
800 insane.Xtructs.Add( truck );
801 whoa := client.testInsanity( insane );
802 Console.Write(' = {');
803 for key64 in whoa.Keys do
804 begin
805 val := whoa[key64];
806 Console.Write( IntToStr( key64) + ' => {');
807 for k2_2 in val.Keys do
808 begin
809 v2 := val[k2_2];
810 Console.Write( IntToStr( Integer( k2_2)) + ' => {');
811 userMap := v2.UserMap;
812 Console.Write('{');
813 if userMap <> nil then
814 begin
815 for k3 in userMap.Keys do
816 begin
817 Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', ');
818 end;
819 end else
820 begin
821 Console.Write('null');
822 end;
823 Console.Write('}, ');
824 xtructs := v2.Xtructs;
825 Console.Write('{');
826
827 if xtructs <> nil then
828 begin
829 for x in xtructs do
830 begin
831 Console.Write('{"' + x.String_thing + '", ' +
832 IntToStr( x.Byte_thing) + ', ' +
833 IntToStr( x.I32_thing) + ', ' +
834 IntToStr( x.I32_thing) + '}, ');
835 end;
836 end else
837 begin
838 Console.Write('null');
839 end;
840 Console.Write('}');
841 Console.Write('}, ');
842 end;
843 Console.Write('}, ');
844 end;
845 Console.WriteLine('}');
846
Jens Geyer540e3462016-12-28 14:25:41 +0100847 (**
848 * So you think you've got this all worked, out eh?
849 *
850 * Creates a the returned map with these values and prints it out:
851 * { 1 => { 2 => argument,
852 * 3 => argument,
853 * },
854 * 2 => { 6 => <empty Insanity struct>, },
855 * }
856 * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
857 *)
858
Roger Meier3bef8c22012-10-06 06:58:00 +0000859 // verify result data
860 Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count));
861 //
862 first_map := whoa[1];
863 second_map := whoa[2];
864 Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count));
865 Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count));
866 //
867 looney := second_map[TNumberz.SIX];
868 Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney)));
869 Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
870 Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
871 //
872 for ret in [TNumberz.TWO, TNumberz.THREE] do begin
873 crazy := first_map[ret];
874 Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
875
876 Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap));
877 Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs));
878
Jens Geyer540e3462016-12-28 14:25:41 +0100879 Expect( crazy.UserMap.Count = insane.UserMap.Count, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count));
880 for pair in insane.UserMap do begin
881 Expect( crazy.UserMap[pair.Key] = pair.Value, 'crazy.UserMap['+IntToStr(Ord(pair.key))+'] = '+IntToStr(crazy.UserMap[pair.Key]));
882 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000883
Jens Geyer540e3462016-12-28 14:25:41 +0100884 Expect( crazy.Xtructs.Count = insane.Xtructs.Count, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count));
885 for arg0 := 0 to insane.Xtructs.Count-1 do begin
886 hello := insane.Xtructs[arg0];
887 goodbye := crazy.Xtructs[arg0];
888 Expect( goodbye.String_thing = hello.String_thing, 'goodbye.String_thing = '+goodbye.String_thing);
889 Expect( goodbye.Byte_thing = hello.Byte_thing, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing));
890 Expect( goodbye.I32_thing = hello.I32_thing, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing));
891 Expect( goodbye.I64_thing = hello.I64_thing, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing));
892 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000893 end;
894
895
896 // multi args
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200897 StartTestGroup( 'testMulti', test_BaseTypes);
Roger Meier3bef8c22012-10-06 06:58:00 +0000898 arg0 := 1;
899 arg1 := 2;
900 arg2 := High(Int64);
901 arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
902 arg3.AddOrSetValue( 1, 'one');
903 arg4 := TNumberz.FIVE;
904 arg5 := 5000000;
905 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
906 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
907 arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
908 IntToStr( arg5) + ')');
909
910 i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5);
911 Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"');
912 Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing));
913 Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing));
914 Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing));
915 Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
916 Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
917 Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
918 Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
919
920 // multi exception
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200921 StartTestGroup( 'testMultiException(1)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000922 try
923 i := client.testMultiException( 'need more pizza', 'run out of beer');
924 Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"');
925 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
Jens Geyer6bbbf192014-09-07 01:45:56 +0200926 { this is not necessarily true, these fields are default-serialized
Jens Geyerd5436f52014-10-03 19:50:38 +0200927 Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
Roger Meier3bef8c22012-10-06 06:58:00 +0000928 Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
929 Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
Jens Geyerd5436f52014-10-03 19:50:38 +0200930 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000931 except
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200932 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000933 end;
934
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200935 StartTestGroup( 'testMultiException(Xception)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000936 try
937 i := client.testMultiException( 'Xception', 'second test');
938 Expect( FALSE, 'testMultiException(''Xception''): must trow an exception');
939 except
940 on x:TXception do begin
941 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
942 Expect( x.__isset_Message_, 'x.__isset_Message_ = '+BoolToString(x.__isset_Message_));
943 Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
944 Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"');
945 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200946 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000947 end;
948
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200949 StartTestGroup( 'testMultiException(Xception2)', test_Exceptions);
Roger Meier3bef8c22012-10-06 06:58:00 +0000950 try
951 i := client.testMultiException( 'Xception2', 'third test');
952 Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception');
953 except
954 on x:TXception2 do begin
955 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
956 Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing));
957 Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
958 Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"');
959 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 +0200960 { this is not necessarily true, these fields are default-serialized
Roger Meier3bef8c22012-10-06 06:58:00 +0000961 Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing));
962 Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing));
963 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 +0200964 }
Roger Meier3bef8c22012-10-06 06:58:00 +0000965 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +0200966 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message);
Roger Meier3bef8c22012-10-06 06:58:00 +0000967 end;
968
969
970 // oneway functions
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200971 StartTestGroup( 'Test Oneway(1)', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000972 client.testOneway(1);
973 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
974
975 // call time
Jens Geyer06045cf2013-03-27 20:26:25 +0200976 {$IFDEF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000977 StartTestGroup( 'Test Calltime()');
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200978 StartTick := GetTickCount;
Roger Meier3bef8c22012-10-06 06:58:00 +0000979 for k := 0 to 1000 - 1 do
980 begin
981 client.testVoid();
982 end;
983 Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
Jens Geyer06045cf2013-03-27 20:26:25 +0200984 {$ENDIF PerfTest}
Roger Meier3bef8c22012-10-06 06:58:00 +0000985
986 // no more tests here
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200987 StartTestGroup( '', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +0000988end;
989
990
Jens Geyer14f5d502017-12-09 13:47:09 +0100991{$IFDEF SupportsAsync}
992procedure TClientThread.ClientAsyncTest;
993var
994 client : TThriftTest.IAsync;
995 s : string;
996 i8 : ShortInt;
997begin
998 StartTestGroup( 'Async Tests', test_Unknown);
999 client := TThriftTest.TClient.Create( FProtocol);
1000 FTransport.Open;
1001
1002 // oneway void functions
1003 client.testOnewayAsync(1).Wait;
1004 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
1005
1006 // normal functions
1007 s := client.testStringAsync(HUGE_TEST_STRING).Value;
1008 Expect( length(s) = length(HUGE_TEST_STRING),
1009 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') '
1010 +'=> length(result) = '+IntToStr(Length(s)));
1011
1012 i8 := client.testByte(1).Value;
1013 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
1014end;
1015{$ENDIF}
1016
1017
Jens Geyer718f6ee2013-09-06 21:02:34 +02001018{$IFDEF StressTest}
Jens Geyer06045cf2013-03-27 20:26:25 +02001019procedure TClientThread.StressTest(const client : TThriftTest.Iface);
1020begin
1021 while TRUE do begin
1022 try
1023 if not FTransport.IsOpen then FTransport.Open; // re-open connection, server has already closed
1024 try
1025 client.testString('Test');
1026 Write('.');
1027 finally
1028 if FTransport.IsOpen then FTransport.Close;
1029 end;
1030 except
1031 on e:Exception do Writeln(#10+e.message);
1032 end;
1033 end;
1034end;
Jens Geyer718f6ee2013-09-06 21:02:34 +02001035{$ENDIF}
Jens Geyer06045cf2013-03-27 20:26:25 +02001036
Jens Geyerfd1b3582014-12-13 23:42:58 +01001037
Jens Geyer85827152018-01-12 21:20:59 +01001038function TClientThread.PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes;
Jens Geyerd4df9172017-10-25 22:30:23 +02001039var i : Integer;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001040begin
Jens Geyer85827152018-01-12 21:20:59 +01001041 case aSize of
1042 Empty : SetLength( result, 0);
1043 Normal : SetLength( result, $100);
1044 ByteArrayTest : SetLength( result, SizeOf(TByteArray) + 128);
1045 PipeWriteLimit : SetLength( result, 65535 + 128);
Jens Geyer2646bd62019-11-09 23:24:52 +01001046 FifteenMB : SetLength( result, 15 * 1024 * 1024);
Jens Geyer85827152018-01-12 21:20:59 +01001047 else
1048 raise EArgumentException.Create('aSize');
1049 end;
1050
Jens Geyerfd1b3582014-12-13 23:42:58 +01001051 ASSERT( Low(result) = 0);
Jens Geyer85827152018-01-12 21:20:59 +01001052 if Length(result) = 0 then Exit;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001053
1054 // linear distribution, unless random is requested
1055 if not aRandomDist then begin
1056 for i := Low(result) to High(result) do begin
Jens Geyer85827152018-01-12 21:20:59 +01001057 result[i] := i mod $100;
Jens Geyerfd1b3582014-12-13 23:42:58 +01001058 end;
1059 Exit;
1060 end;
1061
1062 // random distribution of all 256 values
1063 FillChar( result[0], Length(result) * SizeOf(result[0]), $0);
Jens Geyerd4df9172017-10-25 22:30:23 +02001064 for i := Low(result) to High(result) do begin
1065 result[i] := Byte( Random($100));
Jens Geyerfd1b3582014-12-13 23:42:58 +01001066 end;
1067end;
1068
1069
Jens Geyerf7904452017-07-26 15:02:12 +02001070{$IFDEF Win64}
1071procedure TClientThread.UseInterlockedExchangeAdd64;
1072var a,b : Int64;
1073begin
1074 a := 1;
1075 b := 2;
1076 Thrift.Utils.InterlockedExchangeAdd64( a,b);
1077 Expect( a = 3, 'InterlockedExchangeAdd64');
1078end;
1079{$ENDIF}
1080
1081
Roger Meier3bef8c22012-10-06 06:58:00 +00001082procedure TClientThread.JSONProtocolReadWriteTest;
1083// Tests only then read/write procedures of the JSON protocol
1084// All tests succeed, if we can read what we wrote before
1085// Note that passing this test does not imply, that our JSON is really compatible to what
1086// other clients or servers expect as the real JSON. This is beyond the scope of this test.
1087var prot : IProtocol;
1088 stm : TStringStream;
Jens Geyer17c3ad92017-09-05 20:31:27 +02001089 list : TThriftList;
Jens Geyera019cda2019-11-09 23:24:52 +01001090 config : IThriftConfiguration;
Jens Geyercf892d42017-09-09 10:08:22 +02001091 binary, binRead, emptyBinary : TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +00001092 i,iErr : Integer;
1093const
1094 TEST_SHORT = ShortInt( $FE);
1095 TEST_SMALL = SmallInt( $FEDC);
1096 TEST_LONG = LongInt( $FEDCBA98);
1097 TEST_I64 = Int64( $FEDCBA9876543210);
1098 TEST_DOUBLE = -1.234e-56;
1099 DELTA_DOUBLE = TEST_DOUBLE * 1e-14;
1100 TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars")
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001101 // Test THRIFT-2336 and THRIFT-3404 with U+1D11E (G Clef symbol) and 'Русское Название';
1102 G_CLEF_AND_CYRILLIC_TEXT = #$1d11e' '#$0420#$0443#$0441#$0441#$043a#$043e#$0435' '#$041d#$0430#$0437#$0432#$0430#$043d#$0438#$0435;
1103 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 +01001104 // test both possible solidus encodings
1105 SOLIDUS_JSON_DATA = '"one/two\/three"';
1106 SOLIDUS_EXCPECTED = 'one/two/three';
Roger Meier3bef8c22012-10-06 06:58:00 +00001107begin
1108 stm := TStringStream.Create;
1109 try
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001110 StartTestGroup( 'JsonProtocolTest', test_Unknown);
Roger Meier3bef8c22012-10-06 06:58:00 +00001111
Jens Geyera019cda2019-11-09 23:24:52 +01001112 config := TThriftConfigurationImpl.Create;
1113
Roger Meier3bef8c22012-10-06 06:58:00 +00001114 // prepare binary data
Jens Geyer85827152018-01-12 21:20:59 +01001115 binary := PrepareBinaryData( FALSE, Normal);
Jens Geyercf892d42017-09-09 10:08:22 +02001116 SetLength( emptyBinary, 0); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001117
1118 // output setup
1119 prot := TJSONProtocolImpl.Create(
1120 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001121 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE), config));
Roger Meier3bef8c22012-10-06 06:58:00 +00001122
1123 // write
Jens Geyer17c3ad92017-09-05 20:31:27 +02001124 Init( list, TType.String_, 9);
1125 prot.WriteListBegin( list);
Roger Meier3bef8c22012-10-06 06:58:00 +00001126 prot.WriteBool( TRUE);
1127 prot.WriteBool( FALSE);
1128 prot.WriteByte( TEST_SHORT);
1129 prot.WriteI16( TEST_SMALL);
1130 prot.WriteI32( TEST_LONG);
1131 prot.WriteI64( TEST_I64);
1132 prot.WriteDouble( TEST_DOUBLE);
1133 prot.WriteString( TEST_STRING);
1134 prot.WriteBinary( binary);
Jens Geyercf892d42017-09-09 10:08:22 +02001135 prot.WriteString( ''); // empty string
1136 prot.WriteBinary( emptyBinary); // empty binary data block
Roger Meier3bef8c22012-10-06 06:58:00 +00001137 prot.WriteListEnd;
1138
1139 // input setup
1140 Expect( stm.Position = stm.Size, 'Stream position/length after write');
1141 stm.Position := 0;
1142 prot := TJSONProtocolImpl.Create(
1143 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001144 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil, config));
Roger Meier3bef8c22012-10-06 06:58:00 +00001145
1146 // read and compare
1147 list := prot.ReadListBegin;
1148 Expect( list.ElementType = TType.String_, 'list element type');
1149 Expect( list.Count = 9, 'list element count');
1150 Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE');
1151 Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE');
1152 Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte');
1153 Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16');
1154 Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32');
1155 Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64');
1156 Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble');
1157 Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString');
1158 binRead := prot.ReadBinary;
Jens Geyercf892d42017-09-09 10:08:22 +02001159 Expect( Length(prot.ReadString) = 0, 'WriteString/ReadString (empty string)');
1160 Expect( Length(prot.ReadBinary) = 0, 'empty WriteBinary/ReadBinary (empty data block)');
Roger Meier3bef8c22012-10-06 06:58:00 +00001161 prot.ReadListEnd;
1162
1163 // test binary data
1164 Expect( Length(binary) = Length(binRead), 'Binary data length check');
1165 iErr := -1;
1166 for i := Low(binary) to High(binary) do begin
1167 if binary[i] <> binRead[i] then begin
1168 iErr := i;
1169 Break;
1170 end;
1171 end;
1172 if iErr < 0
1173 then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)')
1174 else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr));
1175
1176 Expect( stm.Position = stm.Size, 'Stream position after read');
1177
Jens Geyer7bb44a32014-02-07 22:24:37 +01001178
Jens Geyer21366942013-12-30 22:04:51 +01001179 // Solidus can be encoded in two ways. Make sure we can read both
1180 stm.Position := 0;
1181 stm.Size := 0;
1182 stm.WriteString(SOLIDUS_JSON_DATA);
1183 stm.Position := 0;
1184 prot := TJSONProtocolImpl.Create(
1185 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001186 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil, config));
Jens Geyer21366942013-12-30 22:04:51 +01001187 Expect( prot.ReadString = SOLIDUS_EXCPECTED, 'Solidus encoding');
1188
1189
Jens Geyer7bb44a32014-02-07 22:24:37 +01001190 // Widechars should work too. Do they?
1191 // After writing, we ensure that we are able to read it back
1192 // We can't assume hex-encoding, since (nearly) any Unicode char is valid JSON
1193 stm.Position := 0;
1194 stm.Size := 0;
1195 prot := TJSONProtocolImpl.Create(
1196 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001197 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE), config));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001198 prot.WriteString( G_CLEF_AND_CYRILLIC_TEXT);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001199 stm.Position := 0;
1200 prot := TJSONProtocolImpl.Create(
1201 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001202 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil, config));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001203 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Writing JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001204
1205 // Widechars should work with hex-encoding too. Do they?
1206 stm.Position := 0;
1207 stm.Size := 0;
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001208 stm.WriteString( G_CLEF_AND_CYRILLIC_JSON);
Jens Geyer7bb44a32014-02-07 22:24:37 +01001209 stm.Position := 0;
1210 prot := TJSONProtocolImpl.Create(
1211 TStreamTransportImpl.Create(
Jens Geyera019cda2019-11-09 23:24:52 +01001212 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil, config));
Phongphan Phutthaa6509f72015-10-31 01:09:47 +07001213 Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Reading JSON with chars > 8 bit');
Jens Geyer7bb44a32014-02-07 22:24:37 +01001214
1215
Roger Meier3bef8c22012-10-06 06:58:00 +00001216 finally
1217 stm.Free;
1218 prot := nil; //-> Release
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001219 StartTestGroup( '', test_Unknown); // no more tests here
Roger Meier3bef8c22012-10-06 06:58:00 +00001220 end;
1221end;
1222
1223
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001224procedure TClientThread.StartTestGroup( const aGroup : string; const aTest : TTestGroup);
Roger Meier3bef8c22012-10-06 06:58:00 +00001225begin
1226 FTestGroup := aGroup;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001227 FCurrentTest := aTest;
1228
1229 Include( FExecuted, aTest);
1230
Roger Meier3bef8c22012-10-06 06:58:00 +00001231 if FTestGroup <> '' then begin
1232 Console.WriteLine('');
1233 Console.WriteLine( aGroup+' tests');
1234 Console.WriteLine( StringOfChar('-',60));
1235 end;
1236end;
1237
1238
1239procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
1240begin
1241 if aTestResult then begin
1242 Inc(FSuccesses);
1243 Console.WriteLine( aTestInfo+': passed');
1244 end
1245 else begin
1246 FErrors.Add( FTestGroup+': '+aTestInfo);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001247 Include( FFailed, FCurrentTest);
Roger Meier3bef8c22012-10-06 06:58:00 +00001248 Console.WriteLine( aTestInfo+': *** FAILED ***');
1249
1250 // We have a failed test!
1251 // -> issue DebugBreak ONLY if a debugger is attached,
1252 // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise
Jens Geyer9f7f11e2016-04-14 21:37:11 +02001253 if IsDebuggerPresent
1254 then {$IFDEF CPUX64} DebugBreak {$ELSE} asm int 3 end {$ENDIF};
Roger Meier3bef8c22012-10-06 06:58:00 +00001255 end;
1256end;
1257
1258
1259procedure TClientThread.ReportResults;
1260var nTotal : Integer;
1261 sLine : string;
1262begin
1263 // prevent us from stupid DIV/0 errors
1264 nTotal := FSuccesses + FErrors.Count;
1265 if nTotal = 0 then begin
1266 Console.WriteLine('No results logged');
1267 Exit;
1268 end;
1269
1270 Console.WriteLine('');
1271 Console.WriteLine( StringOfChar('=',60));
1272 Console.WriteLine( IntToStr(nTotal)+' tests performed');
1273 Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)');
1274 Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)');
1275 Console.WriteLine( StringOfChar('=',60));
1276 if FErrors.Count > 0 then begin
1277 Console.WriteLine('FAILED TESTS:');
1278 for sLine in FErrors do Console.WriteLine('- '+sLine);
1279 Console.WriteLine( StringOfChar('=',60));
1280 InterlockedIncrement( ExitCode); // return <> 0 on errors
1281 end;
1282 Console.WriteLine('');
1283end;
1284
1285
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001286function TClientThread.CalculateExitCode : Byte;
1287var test : TTestGroup;
1288begin
1289 result := EXITCODE_SUCCESS;
1290 for test := Low(TTestGroup) to High(TTestGroup) do begin
1291 if (test in FFailed) or not (test in FExecuted)
1292 then result := result or MAP_FAILURES_TO_EXITCODE_BITS[test];
1293 end;
1294end;
1295
1296
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001297constructor TClientThread.Create( const aSetup : TTestSetup; const aNumIteration: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +00001298begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001299 FSetup := aSetup;
Roger Meier3bef8c22012-10-06 06:58:00 +00001300 FNumIteration := ANumIteration;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001301
Roger Meier3bef8c22012-10-06 06:58:00 +00001302 FConsole := TThreadConsole.Create( Self );
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001303 FCurrentTest := test_Unknown;
Roger Meier3bef8c22012-10-06 06:58:00 +00001304
1305 // error list: keep correct order, allow for duplicates
1306 FErrors := TStringList.Create;
1307 FErrors.Sorted := FALSE;
1308 FErrors.Duplicates := dupAccept;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001309
1310 inherited Create( TRUE);
Roger Meier3bef8c22012-10-06 06:58:00 +00001311end;
1312
1313destructor TClientThread.Destroy;
1314begin
1315 FreeAndNil( FConsole);
1316 FreeAndNil( FErrors);
1317 inherited;
1318end;
1319
1320procedure TClientThread.Execute;
1321var
1322 i : Integer;
Roger Meier3bef8c22012-10-06 06:58:00 +00001323begin
1324 // perform all tests
1325 try
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001326 {$IFDEF Win64}
Jens Geyerf7904452017-07-26 15:02:12 +02001327 UseInterlockedExchangeAdd64;
Jens Geyer14f5d502017-12-09 13:47:09 +01001328 {$ENDIF}
Jens Geyer7bb44a32014-02-07 22:24:37 +01001329 JSONProtocolReadWriteTest;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001330
1331 // must be run in the context of the thread
1332 InitializeProtocolTransportStack;
1333 try
1334 for i := 0 to FNumIteration - 1 do begin
1335 ClientTest;
1336 {$IFDEF SupportsAsync}
1337 ClientAsyncTest;
1338 {$ENDIF}
1339 end;
1340
1341 // report the outcome
1342 ReportResults;
1343 SetReturnValue( CalculateExitCode);
1344
1345 finally
1346 ShutdownProtocolTransportStack;
Roger Meier3bef8c22012-10-06 06:58:00 +00001347 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001348
Roger Meier3bef8c22012-10-06 06:58:00 +00001349 except
1350 on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"');
1351 end;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001352end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001353
Roger Meier3bef8c22012-10-06 06:58:00 +00001354
Jens Geyera019cda2019-11-09 23:24:52 +01001355function TClientThread.InitializeHttpTransport( const aTimeoutSetting : Integer; const aConfig : IThriftConfiguration) : IHTTPClient;
Jens Geyer83ff7532019-06-06 22:46:03 +02001356var sUrl : string;
1357 comps : URL_COMPONENTS;
1358 dwChars : DWORD;
Jens Geyer02230912019-04-03 01:12:51 +02001359begin
1360 ASSERT( FSetup.endpoint in [trns_MsxmlHttp, trns_WinHttp]);
1361
1362 if FSetup.useSSL
1363 then sUrl := 'https://'
1364 else sUrl := 'http://';
1365
1366 sUrl := sUrl + FSetup.host;
1367
Jens Geyer83ff7532019-06-06 22:46:03 +02001368 // add the port number if necessary and at the right place
1369 FillChar( comps, SizeOf(comps), 0);
1370 comps.dwStructSize := SizeOf(comps);
1371 comps.dwSchemeLength := MAXINT;
1372 comps.dwHostNameLength := MAXINT;
1373 comps.dwUserNameLength := MAXINT;
1374 comps.dwPasswordLength := MAXINT;
1375 comps.dwUrlPathLength := MAXINT;
1376 comps.dwExtraInfoLength := MAXINT;
1377 Win32Check( WinHttpCrackUrl( PChar(sUrl), Length(sUrl), 0, comps));
Jens Geyer02230912019-04-03 01:12:51 +02001378 case FSetup.port of
Jens Geyer83ff7532019-06-06 22:46:03 +02001379 80 : if FSetup.useSSL then comps.nPort := FSetup.port;
1380 443 : if not FSetup.useSSL then comps.nPort := FSetup.port;
Jens Geyer02230912019-04-03 01:12:51 +02001381 else
Jens Geyer83ff7532019-06-06 22:46:03 +02001382 if FSetup.port > 0 then comps.nPort := FSetup.port;
Jens Geyer02230912019-04-03 01:12:51 +02001383 end;
Jens Geyer83ff7532019-06-06 22:46:03 +02001384 dwChars := Length(sUrl) + 64;
1385 SetLength( sUrl, dwChars);
1386 Win32Check( WinHttpCreateUrl( comps, 0, @sUrl[1], dwChars));
1387 SetLength( sUrl, dwChars);
1388
Jens Geyer02230912019-04-03 01:12:51 +02001389
1390 Console.WriteLine('Target URL: '+sUrl);
1391 case FSetup.endpoint of
Jens Geyera019cda2019-11-09 23:24:52 +01001392 trns_MsxmlHttp : result := TMsxmlHTTPClientImpl.Create( sUrl, aConfig);
1393 trns_WinHttp : result := TWinHTTPClientImpl.Create( sUrl, aConfig);
Jens Geyer02230912019-04-03 01:12:51 +02001394 else
1395 raise Exception.Create(ENDPOINT_TRANSPORTS[FSetup.endpoint]+' unhandled case');
1396 end;
1397
1398 result.DnsResolveTimeout := aTimeoutSetting;
1399 result.ConnectionTimeout := aTimeoutSetting;
1400 result.SendTimeout := aTimeoutSetting;
1401 result.ReadTimeout := aTimeoutSetting;
1402end;
1403
1404
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001405procedure TClientThread.InitializeProtocolTransportStack;
Jens Geyer02230912019-04-03 01:12:51 +02001406var streamtrans : IStreamTransport;
Jens Geyer47f63172019-06-06 22:42:58 +02001407 canSSL : Boolean;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001408const
1409 DEBUG_TIMEOUT = 30 * 1000;
1410 RELEASE_TIMEOUT = DEFAULT_THRIFT_TIMEOUT;
1411 PIPE_TIMEOUT = RELEASE_TIMEOUT;
1412 HTTP_TIMEOUTS = 10 * 1000;
1413begin
1414 // needed for HTTP clients as they utilize the MSXML COM components
1415 OleCheck( CoInitialize( nil));
1416
Jens Geyer47f63172019-06-06 22:42:58 +02001417 canSSL := FALSE;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001418 case FSetup.endpoint of
1419 trns_Sockets: begin
1420 Console.WriteLine('Using sockets ('+FSetup.host+' port '+IntToStr(FSetup.port)+')');
Jens Geyera019cda2019-11-09 23:24:52 +01001421 streamtrans := TSocketImpl.Create( FSetup.host, FSetup.port);
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001422 FTransport := streamtrans;
1423 end;
1424
Jens Geyer02230912019-04-03 01:12:51 +02001425 trns_MsxmlHttp,
1426 trns_WinHttp: begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001427 Console.WriteLine('Using HTTPClient');
Jens Geyer02230912019-04-03 01:12:51 +02001428 FTransport := InitializeHttpTransport( HTTP_TIMEOUTS);
Jens Geyer47f63172019-06-06 22:42:58 +02001429 canSSL := TRUE;
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001430 end;
1431
1432 trns_EvHttp: begin
1433 raise Exception.Create(ENDPOINT_TRANSPORTS[FSetup.endpoint]+' transport not implemented');
1434 end;
1435
1436 trns_NamedPipes: begin
1437 streamtrans := TNamedPipeTransportClientEndImpl.Create( FSetup.sPipeName, 0, nil, PIPE_TIMEOUT, PIPE_TIMEOUT);
1438 FTransport := streamtrans;
1439 end;
1440
1441 trns_AnonPipes: begin
Jens Geyera019cda2019-11-09 23:24:52 +01001442 streamtrans := TAnonymousPipeTransportImpl.Create( FSetup.hAnonRead, FSetup.hAnonWrite, FALSE, PIPE_TIMEOUT);
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001443 FTransport := streamtrans;
1444 end;
1445
1446 else
1447 raise Exception.Create('Unhandled endpoint transport');
1448 end;
1449 ASSERT( FTransport <> nil);
1450
1451 // layered transports are not really meant to be stacked upon each other
1452 if (trns_Framed in FSetup.layered) then begin
1453 FTransport := TFramedTransportImpl.Create( FTransport);
1454 end
1455 else if (trns_Buffered in FSetup.layered) and (streamtrans <> nil) then begin
1456 FTransport := TBufferedTransportImpl.Create( streamtrans, 32); // small buffer to test read()
1457 end;
1458
Jens Geyer47f63172019-06-06 22:42:58 +02001459 if FSetup.useSSL and not canSSL then begin
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001460 raise Exception.Create('SSL/TLS not implemented');
1461 end;
1462
1463 // create protocol instance, default to BinaryProtocol
Jens Geyer3b686532021-07-01 23:04:08 +02001464 FProtocol := PROTOCOL_CLASSES[FSetup.protType].Create(FTransport);
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001465 ASSERT( (FTransport <> nil) and (FProtocol <> nil));
1466end;
1467
1468
1469procedure TClientThread.ShutdownProtocolTransportStack;
1470begin
1471 try
1472 FProtocol := nil;
1473
1474 if FTransport <> nil then begin
Roger Meier3bef8c22012-10-06 06:58:00 +00001475 FTransport.Close;
1476 FTransport := nil;
1477 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001478
Jens Geyeraf7ecd62018-06-22 22:41:27 +02001479 finally
1480 CoUninitialize;
1481 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001482end;
1483
Jens Geyerf8a1b7a2014-09-24 00:26:46 +02001484
Roger Meier3bef8c22012-10-06 06:58:00 +00001485{ TThreadConsole }
1486
1487constructor TThreadConsole.Create(AThread: TThread);
1488begin
Jens Geyer718f6ee2013-09-06 21:02:34 +02001489 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +00001490 FThread := AThread;
1491end;
1492
1493procedure TThreadConsole.Write(const S: string);
1494var
1495 proc : TThreadProcedure;
1496begin
1497 proc := procedure
1498 begin
1499 Console.Write( S );
1500 end;
1501 TThread.Synchronize( FThread, proc);
1502end;
1503
1504procedure TThreadConsole.WriteLine(const S: string);
1505var
1506 proc : TThreadProcedure;
1507begin
1508 proc := procedure
1509 begin
1510 Console.WriteLine( S );
1511 end;
1512 TThread.Synchronize( FThread, proc);
1513end;
1514
1515initialization
1516begin
1517 TTestClient.FNumIteration := 1;
1518 TTestClient.FNumThread := 1;
1519end;
1520
1521end.