Jens Geyer | 7bb44a3 | 2014-02-07 22:24:37 +0100 | [diff] [blame] | 1 | (* |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 2 | * 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 | |
| 20 | unit TestClient; |
| 21 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 22 | {$I ../src/Thrift.Defines.inc} |
| 23 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 24 | {.$DEFINE StressTest} // activate to stress-test the server with frequent connects/disconnects |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 25 | {.$DEFINE PerfTest} // activate the performance test |
| 26 | {$DEFINE Exceptions} // activate the exceptions test (or disable while debugging) |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 27 | |
Jens Geyer | 14f5d50 | 2017-12-09 13:47:09 +0100 | [diff] [blame] | 28 | {$if CompilerVersion >= 28} |
| 29 | {$DEFINE SupportsAsync} |
| 30 | {$ifend} |
| 31 | |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 32 | {$WARN SYMBOL_PLATFORM OFF} // Win32Check |
| 33 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 34 | interface |
| 35 | |
| 36 | uses |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 37 | Windows, SysUtils, Classes, Math, ComObj, ActiveX, |
Jens Geyer | 14f5d50 | 2017-12-09 13:47:09 +0100 | [diff] [blame] | 38 | {$IFDEF SupportsAsync} System.Threading, {$ENDIF} |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 39 | DateUtils, |
| 40 | Generics.Collections, |
| 41 | TestConstants, |
Jens Geyer | 3d55624 | 2018-01-24 19:14:32 +0100 | [diff] [blame] | 42 | ConsoleHelper, |
Jens Geyer | b342bd9 | 2019-06-03 20:27:00 +0200 | [diff] [blame] | 43 | PerfTests, |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 44 | Thrift, |
Jens Geyer | f0e6331 | 2015-03-01 18:47:49 +0100 | [diff] [blame] | 45 | Thrift.Protocol.Compact, |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 46 | Thrift.Protocol.JSON, |
| 47 | Thrift.Protocol, |
| 48 | Thrift.Transport.Pipes, |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 49 | Thrift.Transport.WinHTTP, |
| 50 | Thrift.Transport.MsxmlHTTP, |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 51 | Thrift.Transport, |
| 52 | Thrift.Stream, |
| 53 | Thrift.Test, |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 54 | Thrift.WinHTTP, |
Jens Geyer | f790445 | 2017-07-26 15:02:12 +0200 | [diff] [blame] | 55 | Thrift.Utils, |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 56 | Thrift.Configuration, |
Jens Geyer | 3d55624 | 2018-01-24 19:14:32 +0100 | [diff] [blame] | 57 | Thrift.Collections; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 58 | |
| 59 | type |
| 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 Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 69 | 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 Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 80 | TClientThread = class( TThread ) |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 81 | 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 Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 92 | 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 Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 96 | PipeWriteLimit, // THRIFT-4372 Pipe write operations across a network are limited to 65,535 bytes per write. |
Jens Geyer | 2646bd6 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 97 | FifteenMB // quite a bit of data, but still below the default max frame size |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 98 | ); |
| 99 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 100 | private |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 101 | FSetup : TTestSetup; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 102 | 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 Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 109 | FCurrentTest : TTestGroup; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 110 | FSuccesses : Integer; |
| 111 | FErrors : TStringList; |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 112 | FFailed : TTestGroups; |
| 113 | FExecuted : TTestGroups; |
| 114 | procedure StartTestGroup( const aGroup : string; const aTest : TTestGroup); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 115 | procedure Expect( aTestResult : Boolean; const aTestInfo : string); |
| 116 | procedure ReportResults; |
Jens Geyer | fd1b358 | 2014-12-13 23:42:58 +0100 | [diff] [blame] | 117 | function CalculateExitCode : Byte; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 118 | |
| 119 | procedure ClientTest; |
Jens Geyer | 14f5d50 | 2017-12-09 13:47:09 +0100 | [diff] [blame] | 120 | {$IFDEF SupportsAsync} |
| 121 | procedure ClientAsyncTest; |
| 122 | {$ENDIF} |
| 123 | |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 124 | procedure InitializeProtocolTransportStack; |
| 125 | procedure ShutdownProtocolTransportStack; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 126 | function InitializeHttpTransport( const aTimeoutSetting : Integer; const aConfig : IThriftConfiguration = nil) : IHTTPClient; |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 127 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 128 | procedure JSONProtocolReadWriteTest; |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 129 | function PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes; |
Jens Geyer | 718f6ee | 2013-09-06 21:02:34 +0200 | [diff] [blame] | 130 | {$IFDEF StressTest} |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 131 | procedure StressTest(const client : TThriftTest.Iface); |
Jens Geyer | 718f6ee | 2013-09-06 21:02:34 +0200 | [diff] [blame] | 132 | {$ENDIF} |
Jens Geyer | f790445 | 2017-07-26 15:02:12 +0200 | [diff] [blame] | 133 | {$IFDEF Win64} |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 134 | procedure UseInterlockedExchangeAdd64; |
Jens Geyer | f790445 | 2017-07-26 15:02:12 +0200 | [diff] [blame] | 135 | {$ENDIF} |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 136 | protected |
| 137 | procedure Execute; override; |
| 138 | public |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 139 | constructor Create( const aSetup : TTestSetup; const aNumIteration: Integer); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 140 | destructor Destroy; override; |
| 141 | end; |
| 142 | |
| 143 | TTestClient = class |
| 144 | private |
| 145 | class var |
| 146 | FNumIteration : Integer; |
| 147 | FNumThread : Integer; |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 148 | |
| 149 | class procedure PrintCmdLineHelp; |
| 150 | class procedure InvalidArgs; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 151 | public |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 152 | class function Execute( const arguments: array of string) : Byte; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 153 | end; |
| 154 | |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 155 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 156 | implementation |
| 157 | |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 158 | const |
| 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 Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 176 | function BoolToString( b : Boolean) : string; |
| 177 | // overrides global BoolToString() |
| 178 | begin |
| 179 | if b |
| 180 | then result := 'true' |
| 181 | else result := 'false'; |
| 182 | end; |
| 183 | |
| 184 | // not available in all versions, so make sure we have this one imported |
| 185 | function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent'; |
| 186 | |
| 187 | { TTestClient } |
| 188 | |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 189 | class procedure TTestClient.PrintCmdLineHelp; |
| 190 | const HELPTEXT = ' [options]'#10 |
| 191 | + #10 |
| 192 | + 'Allowed options:'#10 |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 193 | + ' -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 Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 204 | ; |
| 205 | begin |
| 206 | Writeln( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + HELPTEXT); |
| 207 | end; |
| 208 | |
| 209 | class procedure TTestClient.InvalidArgs; |
| 210 | begin |
| 211 | Console.WriteLine( 'Invalid args.'); |
| 212 | Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + ' -h for more information'); |
| 213 | Abort; |
| 214 | end; |
| 215 | |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 216 | class 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 Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 228 | var |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 229 | iArg : Integer; |
Jens Geyer | 14f5d50 | 2017-12-09 13:47:09 +0100 | [diff] [blame] | 230 | threadExitCode : Byte; |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 231 | sArg, sValue : string; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 232 | threads : array of TThread; |
| 233 | dtStart : TDateTime; |
| 234 | test : Integer; |
| 235 | thread : TThread; |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 236 | setup : TTestSetup; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 237 | begin |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 238 | // 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 Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 251 | try |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 252 | iArg := 0; |
| 253 | while iArg < Length(arguments) do begin |
| 254 | sArg := arguments[iArg]; |
| 255 | Inc(iArg); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 256 | |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 257 | if IsSwitch( sArg, '-h', sValue) |
| 258 | or IsSwitch( sArg, '--help', sValue) |
| 259 | then begin |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 260 | // -h [ --help ] produce help message |
| 261 | PrintCmdLineHelp; |
| 262 | result := $FF; // all tests failed |
| 263 | Exit; |
| 264 | end |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 265 | else if IsSwitch( sArg, '--host', sValue) then begin |
Jens Geyer | b360b65 | 2014-09-28 01:55:46 +0200 | [diff] [blame] | 266 | // --host arg (=localhost) Host to connect |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 267 | setup.host := sValue; |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 268 | end |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 269 | else if IsSwitch( sArg, '--port', sValue) then begin |
Jens Geyer | b360b65 | 2014-09-28 01:55:46 +0200 | [diff] [blame] | 270 | // --port arg (=9090) Port number to connect |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 271 | setup.port := StrToIntDef(sValue,0); |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 272 | if setup.port <= 0 then InvalidArgs; |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 273 | end |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 274 | else if IsSwitch( sArg, '--domain-socket', sValue) then begin |
Jens Geyer | b360b65 | 2014-09-28 01:55:46 +0200 | [diff] [blame] | 275 | // --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 276 | raise Exception.Create('domain-socket not supported'); |
| 277 | end |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 278 | // --pipe arg Windows Named Pipe (e.g. MyThriftPipe) |
| 279 | else if IsSwitch( sArg, '--pipe', sValue) then begin |
Jens Geyer | 4a33b18 | 2020-03-22 13:46:34 +0100 | [diff] [blame] | 280 | // --pipe arg Windows Named Pipe (e.g. MyThriftPipe) |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 281 | setup.endpoint := trns_NamedPipes; |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 282 | setup.sPipeName := sValue; |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 283 | Console.WriteLine('Using named pipe ('+setup.sPipeName+')'); |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 284 | end |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 285 | else if IsSwitch( sArg, '--anon-pipes', sValue) then begin |
Jens Geyer | b360b65 | 2014-09-28 01:55:46 +0200 | [diff] [blame] | 286 | // --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles) |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 287 | setup.endpoint := trns_AnonPipes; |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 288 | 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 Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 292 | Console.WriteLine('Using anonymous pipes ('+IntToStr(Integer(setup.hAnonRead))+' and '+IntToStr(Integer(setup.hAnonWrite))+')'); |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 293 | end |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 294 | else if IsSwitch( sArg, '--transport', sValue) then begin |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 295 | // --transport arg (=sockets) Transport: buffered, framed, http, winhttp, evhttp |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 296 | 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 Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 301 | else InvalidArgs; |
| 302 | end |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 303 | else if IsSwitch( sArg, '--protocol', sValue) then begin |
Jens Geyer | b360b65 | 2014-09-28 01:55:46 +0200 | [diff] [blame] | 304 | // --protocol arg (=binary) Protocol: binary, compact, json |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 305 | 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 Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 308 | else InvalidArgs; |
| 309 | end |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 310 | else if IsSwitch( sArg, '--ssl', sValue) then begin |
Jens Geyer | b360b65 | 2014-09-28 01:55:46 +0200 | [diff] [blame] | 311 | // --ssl Encrypted Transport using SSL |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 312 | setup.useSSL := TRUE; |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 313 | |
| 314 | end |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 315 | else if IsSwitch( sArg, '-n', sValue) or IsSwitch( sArg, '--testloops', sValue) then begin |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 316 | // -n [ --testloops ] arg (=1) Number of Tests |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 317 | FNumIteration := StrToIntDef( sValue, 0); |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 318 | if FNumIteration <= 0 |
| 319 | then InvalidArgs; |
| 320 | |
| 321 | end |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 322 | else if IsSwitch( sArg, '-t', sValue) or IsSwitch( sArg, '--threads', sValue) then begin |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 323 | // -t [ --threads ] arg (=1) Number of Test threads |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 324 | FNumThread := StrToIntDef( sValue, 0); |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 325 | if FNumThread <= 0 |
| 326 | then InvalidArgs; |
| 327 | end |
Jens Geyer | aeda987 | 2020-03-22 15:01:28 +0100 | [diff] [blame] | 328 | else if IsSwitch( sArg, '--performance', sValue) then begin |
Jens Geyer | b342bd9 | 2019-06-03 20:27:00 +0200 | [diff] [blame] | 329 | result := TPerformanceTests.Execute; |
| 330 | Exit; |
| 331 | end |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 332 | else begin |
| 333 | InvalidArgs; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 334 | end; |
| 335 | end; |
| 336 | |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 337 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 338 | // 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 Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 340 | if (setup.endpoint = trns_AnonPipes) and not IsDebuggerPresent |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 341 | then MessageBox( 0, 'Attach Debugger and/or click OK to continue.', |
| 342 | 'Thrift TestClient (Delphi)', |
| 343 | MB_OK or MB_ICONEXCLAMATION); |
| 344 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 345 | SetLength( threads, FNumThread); |
| 346 | dtStart := Now; |
| 347 | |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 348 | // 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 Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 355 | |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 356 | Console.WriteLine(THRIFT_PROTOCOLS[setup.protType]+' protocol'); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 357 | |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 358 | for test := 0 to FNumThread - 1 do begin |
| 359 | thread := TClientThread.Create( setup, FNumIteration); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 360 | threads[test] := thread; |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 361 | thread.Start; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 362 | end; |
| 363 | |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 364 | result := 0; |
| 365 | for test := 0 to FNumThread - 1 do begin |
Jens Geyer | 14f5d50 | 2017-12-09 13:47:09 +0100 | [diff] [blame] | 366 | threadExitCode := threads[test].WaitFor; |
| 367 | result := result or threadExitCode; |
Jens Geyer | 14f5d50 | 2017-12-09 13:47:09 +0100 | [diff] [blame] | 368 | threads[test].Free; |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 369 | threads[test] := nil; |
Jens Geyer | 14f5d50 | 2017-12-09 13:47:09 +0100 | [diff] [blame] | 370 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 371 | |
| 372 | Console.Write('Total time: ' + IntToStr( MilliSecondsBetween(Now, dtStart))); |
| 373 | |
| 374 | except |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 375 | on E: EAbort do raise; |
| 376 | on E: Exception do begin |
| 377 | Console.WriteLine( E.Message + #10 + E.StackTrace); |
| 378 | raise; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 379 | end; |
| 380 | end; |
| 381 | |
| 382 | Console.WriteLine(''); |
| 383 | Console.WriteLine('done!'); |
| 384 | end; |
| 385 | |
| 386 | { TClientThread } |
| 387 | |
| 388 | procedure TClientThread.ClientTest; |
| 389 | var |
| 390 | client : TThriftTest.Iface; |
| 391 | s : string; |
| 392 | i8 : ShortInt; |
| 393 | i32 : Integer; |
| 394 | i64 : Int64; |
Jens Geyer | fd1b358 | 2014-12-13 23:42:58 +0100 | [diff] [blame] | 395 | binOut,binIn : TBytes; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 396 | dub : Double; |
| 397 | o : IXtruct; |
| 398 | o2 : IXtruct2; |
| 399 | i : IXtruct; |
| 400 | i2 : IXtruct2; |
| 401 | mapout : IThriftDictionary<Integer,Integer>; |
| 402 | mapin : IThriftDictionary<Integer,Integer>; |
| 403 | strmapout : IThriftDictionary<string,string>; |
| 404 | strmapin : IThriftDictionary<string,string>; |
| 405 | j : Integer; |
| 406 | first : Boolean; |
| 407 | key : Integer; |
| 408 | strkey : string; |
| 409 | listout : IThriftList<Integer>; |
| 410 | listin : IThriftList<Integer>; |
| 411 | setout : IHashSet<Integer>; |
| 412 | setin : IHashSet<Integer>; |
| 413 | ret : TNumberz; |
| 414 | uid : Int64; |
| 415 | mm : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>; |
| 416 | pos : IThriftDictionary<Integer, Integer>; |
| 417 | neg : IThriftDictionary<Integer, Integer>; |
| 418 | m2 : IThriftDictionary<Integer, Integer>; |
| 419 | k2 : Integer; |
| 420 | insane : IInsanity; |
| 421 | truck : IXtruct; |
| 422 | whoa : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>; |
| 423 | key64 : Int64; |
| 424 | val : IThriftDictionary<TNumberz, IInsanity>; |
| 425 | k2_2 : TNumberz; |
| 426 | k3 : TNumberz; |
| 427 | v2 : IInsanity; |
| 428 | userMap : IThriftDictionary<TNumberz, Int64>; |
| 429 | xtructs : IThriftList<IXtruct>; |
| 430 | x : IXtruct; |
| 431 | arg0 : ShortInt; |
| 432 | arg1 : Integer; |
| 433 | arg2 : Int64; |
| 434 | arg3 : IThriftDictionary<SmallInt, string>; |
| 435 | arg4 : TNumberz; |
| 436 | arg5 : Int64; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 437 | {$IFDEF PerfTest} |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 438 | StartTick : Cardinal; |
| 439 | k : Integer; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 440 | {$ENDIF} |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 441 | hello, goodbye : IXtruct; |
| 442 | crazy : IInsanity; |
| 443 | looney : IInsanity; |
| 444 | first_map : IThriftDictionary<TNumberz, IInsanity>; |
| 445 | second_map : IThriftDictionary<TNumberz, IInsanity>; |
Jens Geyer | 540e346 | 2016-12-28 14:25:41 +0100 | [diff] [blame] | 446 | pair : TPair<TNumberz, TUserId>; |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 447 | testsize : TTestSize; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 448 | begin |
| 449 | client := TThriftTest.TClient.Create( FProtocol); |
| 450 | FTransport.Open; |
| 451 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 452 | {$IFDEF StressTest} |
| 453 | StressTest( client); |
| 454 | {$ENDIF StressTest} |
| 455 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 456 | {$IFDEF Exceptions} |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 457 | // in-depth exception test |
| 458 | // (1) do we get an exception at all? |
| 459 | // (2) do we get the right exception? |
| 460 | // (3) does the exception contain the expected data? |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 461 | StartTestGroup( 'testException', test_Exceptions); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 462 | // case 1: exception type declared in IDL at the function call |
| 463 | try |
| 464 | client.testException('Xception'); |
| 465 | Expect( FALSE, 'testException(''Xception''): must trow an exception'); |
| 466 | except |
| 467 | on e:TXception do begin |
| 468 | Expect( e.ErrorCode = 1001, 'error code'); |
| 469 | Expect( e.Message_ = 'Xception', 'error message'); |
| 470 | Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ ); |
| 471 | end; |
| 472 | on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"'); |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 473 | on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 474 | end; |
| 475 | |
Jens Geyer | cc8c2c6 | 2021-03-29 22:38:30 +0200 | [diff] [blame] | 476 | // re-open connection if needed |
| 477 | if not FTransport.IsOpen |
| 478 | then FTransport.Open; |
| 479 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 480 | // case 2: exception type NOT declared in IDL at the function call |
| 481 | // this will close the connection |
| 482 | try |
| 483 | client.testException('TException'); |
| 484 | Expect( FALSE, 'testException(''TException''): must trow an exception'); |
| 485 | except |
| 486 | on e:TTransportException do begin |
| 487 | Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 488 | end; |
Jens Geyer | 6bbbf19 | 2014-09-07 01:45:56 +0200 | [diff] [blame] | 489 | on e:TApplicationException do begin |
| 490 | Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get |
Jens Geyer | 6bbbf19 | 2014-09-07 01:45:56 +0200 | [diff] [blame] | 491 | end; |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 492 | on e:TException do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message); |
| 493 | on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 494 | end; |
| 495 | |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 496 | |
Jens Geyer | 2ad6c30 | 2015-02-26 19:38:53 +0100 | [diff] [blame] | 497 | if FTransport.IsOpen then FTransport.Close; |
| 498 | FTransport.Open; // re-open connection, server has already closed |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 499 | |
Jens Geyer | 2ad6c30 | 2015-02-26 19:38:53 +0100 | [diff] [blame] | 500 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 501 | // case 3: no exception |
| 502 | try |
| 503 | client.testException('something'); |
| 504 | Expect( TRUE, 'testException(''something''): must not trow an exception'); |
| 505 | except |
| 506 | on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"'); |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 507 | on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 508 | end; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 509 | {$ENDIF Exceptions} |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 510 | |
Jens Geyer | cc8c2c6 | 2021-03-29 22:38:30 +0200 | [diff] [blame] | 511 | // re-open connection if needed |
| 512 | if not FTransport.IsOpen |
| 513 | then FTransport.Open; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 514 | |
| 515 | // simple things |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 516 | StartTestGroup( 'simple Thrift calls', test_BaseTypes); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 517 | client.testVoid(); |
| 518 | Expect( TRUE, 'testVoid()'); // success := no exception |
| 519 | |
Jens Geyer | 39ba6b7 | 2015-09-22 00:00:49 +0200 | [diff] [blame] | 520 | s := BoolToString( client.testBool(TRUE)); |
| 521 | Expect( s = BoolToString(TRUE), 'testBool(TRUE) = '+s); |
| 522 | s := BoolToString( client.testBool(FALSE)); |
| 523 | Expect( s = BoolToString(FALSE), 'testBool(FALSE) = '+s); |
| 524 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 525 | s := client.testString('Test'); |
| 526 | Expect( s = 'Test', 'testString(''Test'') = "'+s+'"'); |
| 527 | |
Jens Geyer | cf892d4 | 2017-09-09 10:08:22 +0200 | [diff] [blame] | 528 | s := client.testString(''); // empty string |
| 529 | Expect( s = '', 'testString('''') = "'+s+'"'); |
| 530 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 531 | s := client.testString(HUGE_TEST_STRING); |
| 532 | Expect( length(s) = length(HUGE_TEST_STRING), |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 533 | 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') ' |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 534 | +'=> length(result) = '+IntToStr(Length(s))); |
| 535 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 536 | i8 := client.testByte(1); |
| 537 | Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 )); |
| 538 | |
| 539 | i32 := client.testI32(-1); |
| 540 | Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32)); |
| 541 | |
| 542 | Console.WriteLine('testI64(-34359738368)'); |
| 543 | i64 := client.testI64(-34359738368); |
| 544 | Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64)); |
| 545 | |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 546 | // random binary small |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 547 | for testsize := Low(TTestSize) to High(TTestSize) do begin |
| 548 | binOut := PrepareBinaryData( TRUE, testsize); |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 549 | Console.WriteLine('testBinary('+IntToStr(Length(binOut))+' bytes)'); |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 550 | try |
| 551 | binIn := client.testBinary(binOut); |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 552 | Expect( Length(binOut) = Length(binIn), 'testBinary('+IntToStr(Length(binOut))+' bytes): '+IntToStr(Length(binIn))+' bytes received'); |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 553 | i32 := Min( Length(binOut), Length(binIn)); |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 554 | Expect( CompareMem( binOut, binIn, i32), 'testBinary('+IntToStr(Length(binOut))+' bytes): validating received data'); |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 555 | except |
| 556 | on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message); |
| 557 | on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message); |
| 558 | end; |
Jens Geyer | cf892d4 | 2017-09-09 10:08:22 +0200 | [diff] [blame] | 559 | end; |
| 560 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 561 | Console.WriteLine('testDouble(5.325098235)'); |
| 562 | dub := client.testDouble(5.325098235); |
| 563 | Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub)); |
| 564 | |
| 565 | // structs |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 566 | StartTestGroup( 'testStruct', test_Structs); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 567 | Console.WriteLine('testStruct({''Zero'', 1, -3, -5})'); |
| 568 | o := TXtructImpl.Create; |
| 569 | o.String_thing := 'Zero'; |
| 570 | o.Byte_thing := 1; |
| 571 | o.I32_thing := -3; |
| 572 | o.I64_thing := -5; |
| 573 | i := client.testStruct(o); |
| 574 | Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"'); |
| 575 | Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing)); |
| 576 | Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing)); |
| 577 | Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing)); |
| 578 | Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing)); |
| 579 | Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing)); |
| 580 | Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing)); |
| 581 | Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing)); |
| 582 | |
| 583 | // nested structs |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 584 | StartTestGroup( 'testNest', test_Structs); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 585 | Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})'); |
| 586 | o2 := TXtruct2Impl.Create; |
| 587 | o2.Byte_thing := 1; |
| 588 | o2.Struct_thing := o; |
| 589 | o2.I32_thing := 5; |
| 590 | i2 := client.testNest(o2); |
| 591 | i := i2.Struct_thing; |
| 592 | Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"'); |
| 593 | Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing)); |
| 594 | Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing)); |
| 595 | Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing)); |
| 596 | Expect( i2.Byte_thing = 1, 'i2.Byte_thing = '+IntToStr(i2.Byte_thing)); |
| 597 | Expect( i2.I32_thing = 5, 'i2.I32_thing = '+IntToStr(i2.I32_thing)); |
| 598 | Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing)); |
| 599 | Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing)); |
| 600 | Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing)); |
| 601 | Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing)); |
| 602 | Expect( i2.__isset_Byte_thing, 'i2.__isset_Byte_thing'); |
| 603 | Expect( i2.__isset_I32_thing, 'i2.__isset_I32_thing'); |
| 604 | |
| 605 | // map<type1,type2>: A map of strictly unique keys to values. |
| 606 | // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc. |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 607 | StartTestGroup( 'testMap', test_Containers); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 608 | mapout := TThriftDictionaryImpl<Integer,Integer>.Create; |
| 609 | for j := 0 to 4 do |
| 610 | begin |
| 611 | mapout.AddOrSetValue( j, j - 10); |
| 612 | end; |
| 613 | Console.Write('testMap({'); |
| 614 | first := True; |
| 615 | for key in mapout.Keys do |
| 616 | begin |
| 617 | if first |
| 618 | then first := False |
| 619 | else Console.Write( ', ' ); |
| 620 | Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key])); |
| 621 | end; |
| 622 | Console.WriteLine('})'); |
| 623 | |
| 624 | mapin := client.testMap( mapout ); |
| 625 | Expect( mapin.Count = mapout.Count, 'testMap: mapin.Count = mapout.Count'); |
| 626 | for j := 0 to 4 do |
| 627 | begin |
| 628 | Expect( mapout.ContainsKey(j), 'testMap: mapout.ContainsKey('+IntToStr(j)+') = '+BoolToString(mapout.ContainsKey(j))); |
| 629 | end; |
| 630 | for key in mapin.Keys do |
| 631 | begin |
| 632 | Expect( mapin[key] = mapout[key], 'testMap: '+IntToStr(key) + ' => ' + IntToStr( mapin[key])); |
| 633 | Expect( mapin[key] = key - 10, 'testMap: mapin['+IntToStr(key)+'] = '+IntToStr( mapin[key])); |
| 634 | end; |
| 635 | |
| 636 | |
| 637 | // map<type1,type2>: A map of strictly unique keys to values. |
| 638 | // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc. |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 639 | StartTestGroup( 'testStringMap', test_Containers); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 640 | strmapout := TThriftDictionaryImpl<string,string>.Create; |
| 641 | for j := 0 to 4 do |
| 642 | begin |
| 643 | strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10)); |
| 644 | end; |
| 645 | Console.Write('testStringMap({'); |
| 646 | first := True; |
| 647 | for strkey in strmapout.Keys do |
| 648 | begin |
| 649 | if first |
| 650 | then first := False |
| 651 | else Console.Write( ', ' ); |
| 652 | Console.Write( strkey + ' => ' + strmapout[strkey]); |
| 653 | end; |
| 654 | Console.WriteLine('})'); |
| 655 | |
| 656 | strmapin := client.testStringMap( strmapout ); |
| 657 | Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count'); |
| 658 | for j := 0 to 4 do |
| 659 | begin |
| 660 | Expect( strmapout.ContainsKey(IntToStr(j)), |
| 661 | 'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = ' |
| 662 | + BoolToString(strmapout.ContainsKey(IntToStr(j)))); |
| 663 | end; |
| 664 | for strkey in strmapin.Keys do |
| 665 | begin |
| 666 | Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]); |
| 667 | Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]); |
| 668 | end; |
| 669 | |
| 670 | |
| 671 | // set<type>: An unordered set of unique elements. |
| 672 | // Translates to an STL set, Java HashSet, set in Python, etc. |
| 673 | // Note: PHP does not support sets, so it is treated similar to a List |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 674 | StartTestGroup( 'testSet', test_Containers); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 675 | setout := THashSetImpl<Integer>.Create; |
| 676 | for j := -2 to 2 do |
| 677 | begin |
| 678 | setout.Add( j ); |
| 679 | end; |
| 680 | Console.Write('testSet({'); |
| 681 | first := True; |
| 682 | for j in setout do |
| 683 | begin |
| 684 | if first |
| 685 | then first := False |
| 686 | else Console.Write(', '); |
| 687 | Console.Write(IntToStr( j)); |
| 688 | end; |
| 689 | Console.WriteLine('})'); |
| 690 | |
| 691 | setin := client.testSet(setout); |
| 692 | Expect( setin.Count = setout.Count, 'testSet: setin.Count = setout.Count'); |
| 693 | Expect( setin.Count = 5, 'testSet: setin.Count = '+IntToStr(setin.Count)); |
| 694 | for j := -2 to 2 do // unordered, we can't rely on the order => test for known elements only |
| 695 | begin |
| 696 | Expect( setin.Contains(j), 'testSet: setin.Contains('+IntToStr(j)+') => '+BoolToString(setin.Contains(j))); |
| 697 | end; |
| 698 | |
| 699 | // list<type>: An ordered list of elements. |
| 700 | // Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc. |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 701 | StartTestGroup( 'testList', test_Containers); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 702 | listout := TThriftListImpl<Integer>.Create; |
| 703 | listout.Add( +1); |
| 704 | listout.Add( -2); |
| 705 | listout.Add( +3); |
| 706 | listout.Add( -4); |
| 707 | listout.Add( 0); |
| 708 | Console.Write('testList({'); |
| 709 | first := True; |
| 710 | for j in listout do |
| 711 | begin |
| 712 | if first |
| 713 | then first := False |
| 714 | else Console.Write(', '); |
| 715 | Console.Write(IntToStr( j)); |
| 716 | end; |
| 717 | Console.WriteLine('})'); |
| 718 | |
| 719 | listin := client.testList(listout); |
| 720 | Expect( listin.Count = listout.Count, 'testList: listin.Count = listout.Count'); |
| 721 | Expect( listin.Count = 5, 'testList: listin.Count = '+IntToStr(listin.Count)); |
| 722 | Expect( listin[0] = +1, 'listin[0] = '+IntToStr( listin[0])); |
| 723 | Expect( listin[1] = -2, 'listin[1] = '+IntToStr( listin[1])); |
| 724 | Expect( listin[2] = +3, 'listin[2] = '+IntToStr( listin[2])); |
| 725 | Expect( listin[3] = -4, 'listin[3] = '+IntToStr( listin[3])); |
| 726 | Expect( listin[4] = 0, 'listin[4] = '+IntToStr( listin[4])); |
| 727 | |
| 728 | // enums |
| 729 | ret := client.testEnum(TNumberz.ONE); |
| 730 | Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret))); |
| 731 | |
| 732 | ret := client.testEnum(TNumberz.TWO); |
| 733 | Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret))); |
| 734 | |
| 735 | ret := client.testEnum(TNumberz.THREE); |
| 736 | Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret))); |
| 737 | |
| 738 | ret := client.testEnum(TNumberz.FIVE); |
| 739 | Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret))); |
| 740 | |
| 741 | ret := client.testEnum(TNumberz.EIGHT); |
| 742 | Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret))); |
| 743 | |
| 744 | |
| 745 | // typedef |
| 746 | uid := client.testTypedef(309858235082523); |
| 747 | Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid)); |
| 748 | |
| 749 | |
| 750 | // maps of maps |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 751 | StartTestGroup( 'testMapMap(1)', test_Containers); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 752 | mm := client.testMapMap(1); |
| 753 | Console.Write(' = {'); |
| 754 | for key in mm.Keys do |
| 755 | begin |
| 756 | Console.Write( IntToStr( key) + ' => {'); |
| 757 | m2 := mm[key]; |
| 758 | for k2 in m2.Keys do |
| 759 | begin |
| 760 | Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', '); |
| 761 | end; |
| 762 | Console.Write('}, '); |
| 763 | end; |
| 764 | Console.WriteLine('}'); |
| 765 | |
| 766 | // verify result data |
| 767 | Expect( mm.Count = 2, 'mm.Count = '+IntToStr(mm.Count)); |
| 768 | pos := mm[4]; |
| 769 | neg := mm[-4]; |
| 770 | for j := 1 to 4 do |
| 771 | begin |
| 772 | Expect( pos[j] = j, 'pos[j] = '+IntToStr(pos[j])); |
| 773 | Expect( neg[-j] = -j, 'neg[-j] = '+IntToStr(neg[-j])); |
| 774 | end; |
| 775 | |
| 776 | |
| 777 | |
| 778 | // insanity |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 779 | StartTestGroup( 'testInsanity', test_Structs); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 780 | insane := TInsanityImpl.Create; |
| 781 | insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create; |
| 782 | insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000); |
| 783 | truck := TXtructImpl.Create; |
| 784 | truck.String_thing := 'Truck'; |
Jens Geyer | 540e346 | 2016-12-28 14:25:41 +0100 | [diff] [blame] | 785 | truck.Byte_thing := -8; // byte is signed |
| 786 | truck.I32_thing := 32; |
| 787 | truck.I64_thing := 64; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 788 | insane.Xtructs := TThriftListImpl<IXtruct>.Create; |
| 789 | insane.Xtructs.Add( truck ); |
| 790 | whoa := client.testInsanity( insane ); |
| 791 | Console.Write(' = {'); |
| 792 | for key64 in whoa.Keys do |
| 793 | begin |
| 794 | val := whoa[key64]; |
| 795 | Console.Write( IntToStr( key64) + ' => {'); |
| 796 | for k2_2 in val.Keys do |
| 797 | begin |
| 798 | v2 := val[k2_2]; |
| 799 | Console.Write( IntToStr( Integer( k2_2)) + ' => {'); |
| 800 | userMap := v2.UserMap; |
| 801 | Console.Write('{'); |
| 802 | if userMap <> nil then |
| 803 | begin |
| 804 | for k3 in userMap.Keys do |
| 805 | begin |
| 806 | Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', '); |
| 807 | end; |
| 808 | end else |
| 809 | begin |
| 810 | Console.Write('null'); |
| 811 | end; |
| 812 | Console.Write('}, '); |
| 813 | xtructs := v2.Xtructs; |
| 814 | Console.Write('{'); |
| 815 | |
| 816 | if xtructs <> nil then |
| 817 | begin |
| 818 | for x in xtructs do |
| 819 | begin |
| 820 | Console.Write('{"' + x.String_thing + '", ' + |
| 821 | IntToStr( x.Byte_thing) + ', ' + |
| 822 | IntToStr( x.I32_thing) + ', ' + |
| 823 | IntToStr( x.I32_thing) + '}, '); |
| 824 | end; |
| 825 | end else |
| 826 | begin |
| 827 | Console.Write('null'); |
| 828 | end; |
| 829 | Console.Write('}'); |
| 830 | Console.Write('}, '); |
| 831 | end; |
| 832 | Console.Write('}, '); |
| 833 | end; |
| 834 | Console.WriteLine('}'); |
| 835 | |
Jens Geyer | 540e346 | 2016-12-28 14:25:41 +0100 | [diff] [blame] | 836 | (** |
| 837 | * So you think you've got this all worked, out eh? |
| 838 | * |
| 839 | * Creates a the returned map with these values and prints it out: |
| 840 | * { 1 => { 2 => argument, |
| 841 | * 3 => argument, |
| 842 | * }, |
| 843 | * 2 => { 6 => <empty Insanity struct>, }, |
| 844 | * } |
| 845 | * @return map<UserId, map<Numberz,Insanity>> - a map with the above values |
| 846 | *) |
| 847 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 848 | // verify result data |
| 849 | Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count)); |
| 850 | // |
| 851 | first_map := whoa[1]; |
| 852 | second_map := whoa[2]; |
| 853 | Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count)); |
| 854 | Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count)); |
| 855 | // |
| 856 | looney := second_map[TNumberz.SIX]; |
| 857 | Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney))); |
| 858 | Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap)); |
| 859 | Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs)); |
| 860 | // |
| 861 | for ret in [TNumberz.TWO, TNumberz.THREE] do begin |
| 862 | crazy := first_map[ret]; |
| 863 | Console.WriteLine('first_map['+intToStr(Ord(ret))+']'); |
| 864 | |
| 865 | Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap)); |
| 866 | Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs)); |
| 867 | |
Jens Geyer | 540e346 | 2016-12-28 14:25:41 +0100 | [diff] [blame] | 868 | Expect( crazy.UserMap.Count = insane.UserMap.Count, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count)); |
| 869 | for pair in insane.UserMap do begin |
| 870 | Expect( crazy.UserMap[pair.Key] = pair.Value, 'crazy.UserMap['+IntToStr(Ord(pair.key))+'] = '+IntToStr(crazy.UserMap[pair.Key])); |
| 871 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 872 | |
Jens Geyer | 540e346 | 2016-12-28 14:25:41 +0100 | [diff] [blame] | 873 | Expect( crazy.Xtructs.Count = insane.Xtructs.Count, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count)); |
| 874 | for arg0 := 0 to insane.Xtructs.Count-1 do begin |
| 875 | hello := insane.Xtructs[arg0]; |
| 876 | goodbye := crazy.Xtructs[arg0]; |
| 877 | Expect( goodbye.String_thing = hello.String_thing, 'goodbye.String_thing = '+goodbye.String_thing); |
| 878 | Expect( goodbye.Byte_thing = hello.Byte_thing, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing)); |
| 879 | Expect( goodbye.I32_thing = hello.I32_thing, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing)); |
| 880 | Expect( goodbye.I64_thing = hello.I64_thing, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing)); |
| 881 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 882 | end; |
| 883 | |
| 884 | |
| 885 | // multi args |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 886 | StartTestGroup( 'testMulti', test_BaseTypes); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 887 | arg0 := 1; |
| 888 | arg1 := 2; |
| 889 | arg2 := High(Int64); |
| 890 | arg3 := TThriftDictionaryImpl<SmallInt, string>.Create; |
| 891 | arg3.AddOrSetValue( 1, 'one'); |
| 892 | arg4 := TNumberz.FIVE; |
| 893 | arg5 := 5000000; |
| 894 | Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' + |
| 895 | IntToStr( arg1) + ',' + IntToStr( arg2) + ',' + |
| 896 | arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' + |
| 897 | IntToStr( arg5) + ')'); |
| 898 | |
| 899 | i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5); |
| 900 | Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"'); |
| 901 | Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing)); |
| 902 | Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing)); |
| 903 | Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing)); |
| 904 | Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing)); |
| 905 | Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing)); |
| 906 | Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing)); |
| 907 | Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing)); |
| 908 | |
| 909 | // multi exception |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 910 | StartTestGroup( 'testMultiException(1)', test_Exceptions); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 911 | try |
| 912 | i := client.testMultiException( 'need more pizza', 'run out of beer'); |
| 913 | Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"'); |
| 914 | Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing)); |
Jens Geyer | 6bbbf19 | 2014-09-07 01:45:56 +0200 | [diff] [blame] | 915 | { this is not necessarily true, these fields are default-serialized |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 916 | Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing)); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 917 | Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing)); |
| 918 | Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing)); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 919 | } |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 920 | except |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 921 | on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 922 | end; |
| 923 | |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 924 | StartTestGroup( 'testMultiException(Xception)', test_Exceptions); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 925 | try |
| 926 | i := client.testMultiException( 'Xception', 'second test'); |
| 927 | Expect( FALSE, 'testMultiException(''Xception''): must trow an exception'); |
| 928 | except |
| 929 | on x:TXception do begin |
| 930 | Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode)); |
| 931 | Expect( x.__isset_Message_, 'x.__isset_Message_ = '+BoolToString(x.__isset_Message_)); |
| 932 | Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode)); |
| 933 | Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"'); |
| 934 | end; |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 935 | on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 936 | end; |
| 937 | |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 938 | StartTestGroup( 'testMultiException(Xception2)', test_Exceptions); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 939 | try |
| 940 | i := client.testMultiException( 'Xception2', 'third test'); |
| 941 | Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception'); |
| 942 | except |
| 943 | on x:TXception2 do begin |
| 944 | Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode)); |
| 945 | Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing)); |
| 946 | Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode)); |
| 947 | Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"'); |
| 948 | Expect( x.Struct_thing.__isset_String_thing, 'x.Struct_thing.__isset_String_thing = '+BoolToString(x.Struct_thing.__isset_String_thing)); |
Jens Geyer | 6bbbf19 | 2014-09-07 01:45:56 +0200 | [diff] [blame] | 949 | { this is not necessarily true, these fields are default-serialized |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 950 | Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing)); |
| 951 | Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing)); |
| 952 | Expect( not x.Struct_thing.__isset_I64_thing, 'x.Struct_thing.__isset_I64_thing = '+BoolToString(x.Struct_thing.__isset_I64_thing)); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 953 | } |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 954 | end; |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 955 | on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'": '+e.Message); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 956 | end; |
| 957 | |
| 958 | |
| 959 | // oneway functions |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 960 | StartTestGroup( 'Test Oneway(1)', test_Unknown); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 961 | client.testOneway(1); |
| 962 | Expect( TRUE, 'Test Oneway(1)'); // success := no exception |
| 963 | |
| 964 | // call time |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 965 | {$IFDEF PerfTest} |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 966 | StartTestGroup( 'Test Calltime()'); |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 967 | StartTick := GetTickCount; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 968 | for k := 0 to 1000 - 1 do |
| 969 | begin |
| 970 | client.testVoid(); |
| 971 | end; |
| 972 | Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' ); |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 973 | {$ENDIF PerfTest} |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 974 | |
| 975 | // no more tests here |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 976 | StartTestGroup( '', test_Unknown); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 977 | end; |
| 978 | |
| 979 | |
Jens Geyer | 14f5d50 | 2017-12-09 13:47:09 +0100 | [diff] [blame] | 980 | {$IFDEF SupportsAsync} |
| 981 | procedure TClientThread.ClientAsyncTest; |
| 982 | var |
| 983 | client : TThriftTest.IAsync; |
| 984 | s : string; |
| 985 | i8 : ShortInt; |
| 986 | begin |
| 987 | StartTestGroup( 'Async Tests', test_Unknown); |
| 988 | client := TThriftTest.TClient.Create( FProtocol); |
| 989 | FTransport.Open; |
| 990 | |
| 991 | // oneway void functions |
| 992 | client.testOnewayAsync(1).Wait; |
| 993 | Expect( TRUE, 'Test Oneway(1)'); // success := no exception |
| 994 | |
| 995 | // normal functions |
| 996 | s := client.testStringAsync(HUGE_TEST_STRING).Value; |
| 997 | Expect( length(s) = length(HUGE_TEST_STRING), |
| 998 | 'testString( length(HUGE_TEST_STRING) = '+IntToStr(Length(HUGE_TEST_STRING))+') ' |
| 999 | +'=> length(result) = '+IntToStr(Length(s))); |
| 1000 | |
| 1001 | i8 := client.testByte(1).Value; |
| 1002 | Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 )); |
| 1003 | end; |
| 1004 | {$ENDIF} |
| 1005 | |
| 1006 | |
Jens Geyer | 718f6ee | 2013-09-06 21:02:34 +0200 | [diff] [blame] | 1007 | {$IFDEF StressTest} |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 1008 | procedure TClientThread.StressTest(const client : TThriftTest.Iface); |
| 1009 | begin |
| 1010 | while TRUE do begin |
| 1011 | try |
| 1012 | if not FTransport.IsOpen then FTransport.Open; // re-open connection, server has already closed |
| 1013 | try |
| 1014 | client.testString('Test'); |
| 1015 | Write('.'); |
| 1016 | finally |
| 1017 | if FTransport.IsOpen then FTransport.Close; |
| 1018 | end; |
| 1019 | except |
| 1020 | on e:Exception do Writeln(#10+e.message); |
| 1021 | end; |
| 1022 | end; |
| 1023 | end; |
Jens Geyer | 718f6ee | 2013-09-06 21:02:34 +0200 | [diff] [blame] | 1024 | {$ENDIF} |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 1025 | |
Jens Geyer | fd1b358 | 2014-12-13 23:42:58 +0100 | [diff] [blame] | 1026 | |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 1027 | function TClientThread.PrepareBinaryData( aRandomDist : Boolean; aSize : TTestSize) : TBytes; |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 1028 | var i : Integer; |
Jens Geyer | fd1b358 | 2014-12-13 23:42:58 +0100 | [diff] [blame] | 1029 | begin |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 1030 | case aSize of |
| 1031 | Empty : SetLength( result, 0); |
| 1032 | Normal : SetLength( result, $100); |
| 1033 | ByteArrayTest : SetLength( result, SizeOf(TByteArray) + 128); |
| 1034 | PipeWriteLimit : SetLength( result, 65535 + 128); |
Jens Geyer | 2646bd6 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1035 | FifteenMB : SetLength( result, 15 * 1024 * 1024); |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 1036 | else |
| 1037 | raise EArgumentException.Create('aSize'); |
| 1038 | end; |
| 1039 | |
Jens Geyer | fd1b358 | 2014-12-13 23:42:58 +0100 | [diff] [blame] | 1040 | ASSERT( Low(result) = 0); |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 1041 | if Length(result) = 0 then Exit; |
Jens Geyer | fd1b358 | 2014-12-13 23:42:58 +0100 | [diff] [blame] | 1042 | |
| 1043 | // linear distribution, unless random is requested |
| 1044 | if not aRandomDist then begin |
| 1045 | for i := Low(result) to High(result) do begin |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 1046 | result[i] := i mod $100; |
Jens Geyer | fd1b358 | 2014-12-13 23:42:58 +0100 | [diff] [blame] | 1047 | end; |
| 1048 | Exit; |
| 1049 | end; |
| 1050 | |
| 1051 | // random distribution of all 256 values |
| 1052 | FillChar( result[0], Length(result) * SizeOf(result[0]), $0); |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 1053 | for i := Low(result) to High(result) do begin |
| 1054 | result[i] := Byte( Random($100)); |
Jens Geyer | fd1b358 | 2014-12-13 23:42:58 +0100 | [diff] [blame] | 1055 | end; |
| 1056 | end; |
| 1057 | |
| 1058 | |
Jens Geyer | f790445 | 2017-07-26 15:02:12 +0200 | [diff] [blame] | 1059 | {$IFDEF Win64} |
| 1060 | procedure TClientThread.UseInterlockedExchangeAdd64; |
| 1061 | var a,b : Int64; |
| 1062 | begin |
| 1063 | a := 1; |
| 1064 | b := 2; |
| 1065 | Thrift.Utils.InterlockedExchangeAdd64( a,b); |
| 1066 | Expect( a = 3, 'InterlockedExchangeAdd64'); |
| 1067 | end; |
| 1068 | {$ENDIF} |
| 1069 | |
| 1070 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1071 | procedure TClientThread.JSONProtocolReadWriteTest; |
| 1072 | // Tests only then read/write procedures of the JSON protocol |
| 1073 | // All tests succeed, if we can read what we wrote before |
| 1074 | // Note that passing this test does not imply, that our JSON is really compatible to what |
| 1075 | // other clients or servers expect as the real JSON. This is beyond the scope of this test. |
| 1076 | var prot : IProtocol; |
| 1077 | stm : TStringStream; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1078 | list : TThriftList; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1079 | config : IThriftConfiguration; |
Jens Geyer | cf892d4 | 2017-09-09 10:08:22 +0200 | [diff] [blame] | 1080 | binary, binRead, emptyBinary : TBytes; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1081 | i,iErr : Integer; |
| 1082 | const |
| 1083 | TEST_SHORT = ShortInt( $FE); |
| 1084 | TEST_SMALL = SmallInt( $FEDC); |
| 1085 | TEST_LONG = LongInt( $FEDCBA98); |
| 1086 | TEST_I64 = Int64( $FEDCBA9876543210); |
| 1087 | TEST_DOUBLE = -1.234e-56; |
| 1088 | DELTA_DOUBLE = TEST_DOUBLE * 1e-14; |
| 1089 | TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars") |
Phongphan Phuttha | a6509f7 | 2015-10-31 01:09:47 +0700 | [diff] [blame] | 1090 | // Test THRIFT-2336 and THRIFT-3404 with U+1D11E (G Clef symbol) and 'Русское Название'; |
| 1091 | G_CLEF_AND_CYRILLIC_TEXT = #$1d11e' '#$0420#$0443#$0441#$0441#$043a#$043e#$0435' '#$041d#$0430#$0437#$0432#$0430#$043d#$0438#$0435; |
| 1092 | G_CLEF_AND_CYRILLIC_JSON = '"\ud834\udd1e \u0420\u0443\u0441\u0441\u043a\u043e\u0435 \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435"'; |
Jens Geyer | 2136694 | 2013-12-30 22:04:51 +0100 | [diff] [blame] | 1093 | // test both possible solidus encodings |
| 1094 | SOLIDUS_JSON_DATA = '"one/two\/three"'; |
| 1095 | SOLIDUS_EXCPECTED = 'one/two/three'; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1096 | begin |
| 1097 | stm := TStringStream.Create; |
| 1098 | try |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 1099 | StartTestGroup( 'JsonProtocolTest', test_Unknown); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1100 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1101 | config := TThriftConfigurationImpl.Create; |
| 1102 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1103 | // prepare binary data |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 1104 | binary := PrepareBinaryData( FALSE, Normal); |
Jens Geyer | cf892d4 | 2017-09-09 10:08:22 +0200 | [diff] [blame] | 1105 | SetLength( emptyBinary, 0); // empty binary data block |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1106 | |
| 1107 | // output setup |
| 1108 | prot := TJSONProtocolImpl.Create( |
| 1109 | TStreamTransportImpl.Create( |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1110 | nil, TThriftStreamAdapterDelphi.Create( stm, FALSE), config)); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1111 | |
| 1112 | // write |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1113 | Init( list, TType.String_, 9); |
| 1114 | prot.WriteListBegin( list); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1115 | prot.WriteBool( TRUE); |
| 1116 | prot.WriteBool( FALSE); |
| 1117 | prot.WriteByte( TEST_SHORT); |
| 1118 | prot.WriteI16( TEST_SMALL); |
| 1119 | prot.WriteI32( TEST_LONG); |
| 1120 | prot.WriteI64( TEST_I64); |
| 1121 | prot.WriteDouble( TEST_DOUBLE); |
| 1122 | prot.WriteString( TEST_STRING); |
| 1123 | prot.WriteBinary( binary); |
Jens Geyer | cf892d4 | 2017-09-09 10:08:22 +0200 | [diff] [blame] | 1124 | prot.WriteString( ''); // empty string |
| 1125 | prot.WriteBinary( emptyBinary); // empty binary data block |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1126 | prot.WriteListEnd; |
| 1127 | |
| 1128 | // input setup |
| 1129 | Expect( stm.Position = stm.Size, 'Stream position/length after write'); |
| 1130 | stm.Position := 0; |
| 1131 | prot := TJSONProtocolImpl.Create( |
| 1132 | TStreamTransportImpl.Create( |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1133 | TThriftStreamAdapterDelphi.Create( stm, FALSE), nil, config)); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1134 | |
| 1135 | // read and compare |
| 1136 | list := prot.ReadListBegin; |
| 1137 | Expect( list.ElementType = TType.String_, 'list element type'); |
| 1138 | Expect( list.Count = 9, 'list element count'); |
| 1139 | Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE'); |
| 1140 | Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE'); |
| 1141 | Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte'); |
| 1142 | Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16'); |
| 1143 | Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32'); |
| 1144 | Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64'); |
| 1145 | Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble'); |
| 1146 | Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString'); |
| 1147 | binRead := prot.ReadBinary; |
Jens Geyer | cf892d4 | 2017-09-09 10:08:22 +0200 | [diff] [blame] | 1148 | Expect( Length(prot.ReadString) = 0, 'WriteString/ReadString (empty string)'); |
| 1149 | Expect( Length(prot.ReadBinary) = 0, 'empty WriteBinary/ReadBinary (empty data block)'); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1150 | prot.ReadListEnd; |
| 1151 | |
| 1152 | // test binary data |
| 1153 | Expect( Length(binary) = Length(binRead), 'Binary data length check'); |
| 1154 | iErr := -1; |
| 1155 | for i := Low(binary) to High(binary) do begin |
| 1156 | if binary[i] <> binRead[i] then begin |
| 1157 | iErr := i; |
| 1158 | Break; |
| 1159 | end; |
| 1160 | end; |
| 1161 | if iErr < 0 |
| 1162 | then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)') |
| 1163 | else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr)); |
| 1164 | |
| 1165 | Expect( stm.Position = stm.Size, 'Stream position after read'); |
| 1166 | |
Jens Geyer | 7bb44a3 | 2014-02-07 22:24:37 +0100 | [diff] [blame] | 1167 | |
Jens Geyer | 2136694 | 2013-12-30 22:04:51 +0100 | [diff] [blame] | 1168 | // Solidus can be encoded in two ways. Make sure we can read both |
| 1169 | stm.Position := 0; |
| 1170 | stm.Size := 0; |
| 1171 | stm.WriteString(SOLIDUS_JSON_DATA); |
| 1172 | stm.Position := 0; |
| 1173 | prot := TJSONProtocolImpl.Create( |
| 1174 | TStreamTransportImpl.Create( |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1175 | TThriftStreamAdapterDelphi.Create( stm, FALSE), nil, config)); |
Jens Geyer | 2136694 | 2013-12-30 22:04:51 +0100 | [diff] [blame] | 1176 | Expect( prot.ReadString = SOLIDUS_EXCPECTED, 'Solidus encoding'); |
| 1177 | |
| 1178 | |
Jens Geyer | 7bb44a3 | 2014-02-07 22:24:37 +0100 | [diff] [blame] | 1179 | // Widechars should work too. Do they? |
| 1180 | // After writing, we ensure that we are able to read it back |
| 1181 | // We can't assume hex-encoding, since (nearly) any Unicode char is valid JSON |
| 1182 | stm.Position := 0; |
| 1183 | stm.Size := 0; |
| 1184 | prot := TJSONProtocolImpl.Create( |
| 1185 | TStreamTransportImpl.Create( |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1186 | nil, TThriftStreamAdapterDelphi.Create( stm, FALSE), config)); |
Phongphan Phuttha | a6509f7 | 2015-10-31 01:09:47 +0700 | [diff] [blame] | 1187 | prot.WriteString( G_CLEF_AND_CYRILLIC_TEXT); |
Jens Geyer | 7bb44a3 | 2014-02-07 22:24:37 +0100 | [diff] [blame] | 1188 | stm.Position := 0; |
| 1189 | prot := TJSONProtocolImpl.Create( |
| 1190 | TStreamTransportImpl.Create( |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1191 | TThriftStreamAdapterDelphi.Create( stm, FALSE), nil, config)); |
Phongphan Phuttha | a6509f7 | 2015-10-31 01:09:47 +0700 | [diff] [blame] | 1192 | Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Writing JSON with chars > 8 bit'); |
Jens Geyer | 7bb44a3 | 2014-02-07 22:24:37 +0100 | [diff] [blame] | 1193 | |
| 1194 | // Widechars should work with hex-encoding too. Do they? |
| 1195 | stm.Position := 0; |
| 1196 | stm.Size := 0; |
Phongphan Phuttha | a6509f7 | 2015-10-31 01:09:47 +0700 | [diff] [blame] | 1197 | stm.WriteString( G_CLEF_AND_CYRILLIC_JSON); |
Jens Geyer | 7bb44a3 | 2014-02-07 22:24:37 +0100 | [diff] [blame] | 1198 | stm.Position := 0; |
| 1199 | prot := TJSONProtocolImpl.Create( |
| 1200 | TStreamTransportImpl.Create( |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1201 | TThriftStreamAdapterDelphi.Create( stm, FALSE), nil, config)); |
Phongphan Phuttha | a6509f7 | 2015-10-31 01:09:47 +0700 | [diff] [blame] | 1202 | Expect( prot.ReadString = G_CLEF_AND_CYRILLIC_TEXT, 'Reading JSON with chars > 8 bit'); |
Jens Geyer | 7bb44a3 | 2014-02-07 22:24:37 +0100 | [diff] [blame] | 1203 | |
| 1204 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1205 | finally |
| 1206 | stm.Free; |
| 1207 | prot := nil; //-> Release |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 1208 | StartTestGroup( '', test_Unknown); // no more tests here |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1209 | end; |
| 1210 | end; |
| 1211 | |
| 1212 | |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 1213 | procedure TClientThread.StartTestGroup( const aGroup : string; const aTest : TTestGroup); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1214 | begin |
| 1215 | FTestGroup := aGroup; |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 1216 | FCurrentTest := aTest; |
| 1217 | |
| 1218 | Include( FExecuted, aTest); |
| 1219 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1220 | if FTestGroup <> '' then begin |
| 1221 | Console.WriteLine(''); |
| 1222 | Console.WriteLine( aGroup+' tests'); |
| 1223 | Console.WriteLine( StringOfChar('-',60)); |
| 1224 | end; |
| 1225 | end; |
| 1226 | |
| 1227 | |
| 1228 | procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string); |
| 1229 | begin |
| 1230 | if aTestResult then begin |
| 1231 | Inc(FSuccesses); |
| 1232 | Console.WriteLine( aTestInfo+': passed'); |
| 1233 | end |
| 1234 | else begin |
| 1235 | FErrors.Add( FTestGroup+': '+aTestInfo); |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 1236 | Include( FFailed, FCurrentTest); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1237 | Console.WriteLine( aTestInfo+': *** FAILED ***'); |
| 1238 | |
| 1239 | // We have a failed test! |
| 1240 | // -> issue DebugBreak ONLY if a debugger is attached, |
| 1241 | // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1242 | if IsDebuggerPresent |
| 1243 | then {$IFDEF CPUX64} DebugBreak {$ELSE} asm int 3 end {$ENDIF}; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1244 | end; |
| 1245 | end; |
| 1246 | |
| 1247 | |
| 1248 | procedure TClientThread.ReportResults; |
| 1249 | var nTotal : Integer; |
| 1250 | sLine : string; |
| 1251 | begin |
| 1252 | // prevent us from stupid DIV/0 errors |
| 1253 | nTotal := FSuccesses + FErrors.Count; |
| 1254 | if nTotal = 0 then begin |
| 1255 | Console.WriteLine('No results logged'); |
| 1256 | Exit; |
| 1257 | end; |
| 1258 | |
| 1259 | Console.WriteLine(''); |
| 1260 | Console.WriteLine( StringOfChar('=',60)); |
| 1261 | Console.WriteLine( IntToStr(nTotal)+' tests performed'); |
| 1262 | Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)'); |
| 1263 | Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)'); |
| 1264 | Console.WriteLine( StringOfChar('=',60)); |
| 1265 | if FErrors.Count > 0 then begin |
| 1266 | Console.WriteLine('FAILED TESTS:'); |
| 1267 | for sLine in FErrors do Console.WriteLine('- '+sLine); |
| 1268 | Console.WriteLine( StringOfChar('=',60)); |
| 1269 | InterlockedIncrement( ExitCode); // return <> 0 on errors |
| 1270 | end; |
| 1271 | Console.WriteLine(''); |
| 1272 | end; |
| 1273 | |
| 1274 | |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 1275 | function TClientThread.CalculateExitCode : Byte; |
| 1276 | var test : TTestGroup; |
| 1277 | begin |
| 1278 | result := EXITCODE_SUCCESS; |
| 1279 | for test := Low(TTestGroup) to High(TTestGroup) do begin |
| 1280 | if (test in FFailed) or not (test in FExecuted) |
| 1281 | then result := result or MAP_FAILURES_TO_EXITCODE_BITS[test]; |
| 1282 | end; |
| 1283 | end; |
| 1284 | |
| 1285 | |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 1286 | constructor TClientThread.Create( const aSetup : TTestSetup; const aNumIteration: Integer); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1287 | begin |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 1288 | FSetup := aSetup; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1289 | FNumIteration := ANumIteration; |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 1290 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1291 | FConsole := TThreadConsole.Create( Self ); |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 1292 | FCurrentTest := test_Unknown; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1293 | |
| 1294 | // error list: keep correct order, allow for duplicates |
| 1295 | FErrors := TStringList.Create; |
| 1296 | FErrors.Sorted := FALSE; |
| 1297 | FErrors.Duplicates := dupAccept; |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 1298 | |
| 1299 | inherited Create( TRUE); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1300 | end; |
| 1301 | |
| 1302 | destructor TClientThread.Destroy; |
| 1303 | begin |
| 1304 | FreeAndNil( FConsole); |
| 1305 | FreeAndNil( FErrors); |
| 1306 | inherited; |
| 1307 | end; |
| 1308 | |
| 1309 | procedure TClientThread.Execute; |
| 1310 | var |
| 1311 | i : Integer; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1312 | begin |
| 1313 | // perform all tests |
| 1314 | try |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 1315 | {$IFDEF Win64} |
Jens Geyer | f790445 | 2017-07-26 15:02:12 +0200 | [diff] [blame] | 1316 | UseInterlockedExchangeAdd64; |
Jens Geyer | 14f5d50 | 2017-12-09 13:47:09 +0100 | [diff] [blame] | 1317 | {$ENDIF} |
Jens Geyer | 7bb44a3 | 2014-02-07 22:24:37 +0100 | [diff] [blame] | 1318 | JSONProtocolReadWriteTest; |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 1319 | |
| 1320 | // must be run in the context of the thread |
| 1321 | InitializeProtocolTransportStack; |
| 1322 | try |
| 1323 | for i := 0 to FNumIteration - 1 do begin |
| 1324 | ClientTest; |
| 1325 | {$IFDEF SupportsAsync} |
| 1326 | ClientAsyncTest; |
| 1327 | {$ENDIF} |
| 1328 | end; |
| 1329 | |
| 1330 | // report the outcome |
| 1331 | ReportResults; |
| 1332 | SetReturnValue( CalculateExitCode); |
| 1333 | |
| 1334 | finally |
| 1335 | ShutdownProtocolTransportStack; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1336 | end; |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 1337 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1338 | except |
| 1339 | on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"'); |
| 1340 | end; |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 1341 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1342 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1343 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1344 | function TClientThread.InitializeHttpTransport( const aTimeoutSetting : Integer; const aConfig : IThriftConfiguration) : IHTTPClient; |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 1345 | var sUrl : string; |
| 1346 | comps : URL_COMPONENTS; |
| 1347 | dwChars : DWORD; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 1348 | begin |
| 1349 | ASSERT( FSetup.endpoint in [trns_MsxmlHttp, trns_WinHttp]); |
| 1350 | |
| 1351 | if FSetup.useSSL |
| 1352 | then sUrl := 'https://' |
| 1353 | else sUrl := 'http://'; |
| 1354 | |
| 1355 | sUrl := sUrl + FSetup.host; |
| 1356 | |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 1357 | // add the port number if necessary and at the right place |
| 1358 | FillChar( comps, SizeOf(comps), 0); |
| 1359 | comps.dwStructSize := SizeOf(comps); |
| 1360 | comps.dwSchemeLength := MAXINT; |
| 1361 | comps.dwHostNameLength := MAXINT; |
| 1362 | comps.dwUserNameLength := MAXINT; |
| 1363 | comps.dwPasswordLength := MAXINT; |
| 1364 | comps.dwUrlPathLength := MAXINT; |
| 1365 | comps.dwExtraInfoLength := MAXINT; |
| 1366 | Win32Check( WinHttpCrackUrl( PChar(sUrl), Length(sUrl), 0, comps)); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 1367 | case FSetup.port of |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 1368 | 80 : if FSetup.useSSL then comps.nPort := FSetup.port; |
| 1369 | 443 : if not FSetup.useSSL then comps.nPort := FSetup.port; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 1370 | else |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 1371 | if FSetup.port > 0 then comps.nPort := FSetup.port; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 1372 | end; |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 1373 | dwChars := Length(sUrl) + 64; |
| 1374 | SetLength( sUrl, dwChars); |
| 1375 | Win32Check( WinHttpCreateUrl( comps, 0, @sUrl[1], dwChars)); |
| 1376 | SetLength( sUrl, dwChars); |
| 1377 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 1378 | |
| 1379 | Console.WriteLine('Target URL: '+sUrl); |
| 1380 | case FSetup.endpoint of |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1381 | trns_MsxmlHttp : result := TMsxmlHTTPClientImpl.Create( sUrl, aConfig); |
| 1382 | trns_WinHttp : result := TWinHTTPClientImpl.Create( sUrl, aConfig); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 1383 | else |
| 1384 | raise Exception.Create(ENDPOINT_TRANSPORTS[FSetup.endpoint]+' unhandled case'); |
| 1385 | end; |
| 1386 | |
| 1387 | result.DnsResolveTimeout := aTimeoutSetting; |
| 1388 | result.ConnectionTimeout := aTimeoutSetting; |
| 1389 | result.SendTimeout := aTimeoutSetting; |
| 1390 | result.ReadTimeout := aTimeoutSetting; |
| 1391 | end; |
| 1392 | |
| 1393 | |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 1394 | procedure TClientThread.InitializeProtocolTransportStack; |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 1395 | var streamtrans : IStreamTransport; |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 1396 | canSSL : Boolean; |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 1397 | const |
| 1398 | DEBUG_TIMEOUT = 30 * 1000; |
| 1399 | RELEASE_TIMEOUT = DEFAULT_THRIFT_TIMEOUT; |
| 1400 | PIPE_TIMEOUT = RELEASE_TIMEOUT; |
| 1401 | HTTP_TIMEOUTS = 10 * 1000; |
| 1402 | begin |
| 1403 | // needed for HTTP clients as they utilize the MSXML COM components |
| 1404 | OleCheck( CoInitialize( nil)); |
| 1405 | |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 1406 | canSSL := FALSE; |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 1407 | case FSetup.endpoint of |
| 1408 | trns_Sockets: begin |
| 1409 | Console.WriteLine('Using sockets ('+FSetup.host+' port '+IntToStr(FSetup.port)+')'); |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1410 | streamtrans := TSocketImpl.Create( FSetup.host, FSetup.port); |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 1411 | FTransport := streamtrans; |
| 1412 | end; |
| 1413 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 1414 | trns_MsxmlHttp, |
| 1415 | trns_WinHttp: begin |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 1416 | Console.WriteLine('Using HTTPClient'); |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 1417 | FTransport := InitializeHttpTransport( HTTP_TIMEOUTS); |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 1418 | canSSL := TRUE; |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 1419 | end; |
| 1420 | |
| 1421 | trns_EvHttp: begin |
| 1422 | raise Exception.Create(ENDPOINT_TRANSPORTS[FSetup.endpoint]+' transport not implemented'); |
| 1423 | end; |
| 1424 | |
| 1425 | trns_NamedPipes: begin |
| 1426 | streamtrans := TNamedPipeTransportClientEndImpl.Create( FSetup.sPipeName, 0, nil, PIPE_TIMEOUT, PIPE_TIMEOUT); |
| 1427 | FTransport := streamtrans; |
| 1428 | end; |
| 1429 | |
| 1430 | trns_AnonPipes: begin |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1431 | streamtrans := TAnonymousPipeTransportImpl.Create( FSetup.hAnonRead, FSetup.hAnonWrite, FALSE, PIPE_TIMEOUT); |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 1432 | FTransport := streamtrans; |
| 1433 | end; |
| 1434 | |
| 1435 | else |
| 1436 | raise Exception.Create('Unhandled endpoint transport'); |
| 1437 | end; |
| 1438 | ASSERT( FTransport <> nil); |
| 1439 | |
| 1440 | // layered transports are not really meant to be stacked upon each other |
| 1441 | if (trns_Framed in FSetup.layered) then begin |
| 1442 | FTransport := TFramedTransportImpl.Create( FTransport); |
| 1443 | end |
| 1444 | else if (trns_Buffered in FSetup.layered) and (streamtrans <> nil) then begin |
| 1445 | FTransport := TBufferedTransportImpl.Create( streamtrans, 32); // small buffer to test read() |
| 1446 | end; |
| 1447 | |
Jens Geyer | 47f6317 | 2019-06-06 22:42:58 +0200 | [diff] [blame] | 1448 | if FSetup.useSSL and not canSSL then begin |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 1449 | raise Exception.Create('SSL/TLS not implemented'); |
| 1450 | end; |
| 1451 | |
| 1452 | // create protocol instance, default to BinaryProtocol |
Jens Geyer | 3b68653 | 2021-07-01 23:04:08 +0200 | [diff] [blame] | 1453 | FProtocol := PROTOCOL_CLASSES[FSetup.protType].Create(FTransport); |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 1454 | ASSERT( (FTransport <> nil) and (FProtocol <> nil)); |
| 1455 | end; |
| 1456 | |
| 1457 | |
| 1458 | procedure TClientThread.ShutdownProtocolTransportStack; |
| 1459 | begin |
| 1460 | try |
| 1461 | FProtocol := nil; |
| 1462 | |
| 1463 | if FTransport <> nil then begin |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1464 | FTransport.Close; |
| 1465 | FTransport := nil; |
| 1466 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1467 | |
Jens Geyer | af7ecd6 | 2018-06-22 22:41:27 +0200 | [diff] [blame] | 1468 | finally |
| 1469 | CoUninitialize; |
| 1470 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1471 | end; |
| 1472 | |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 1473 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1474 | { TThreadConsole } |
| 1475 | |
| 1476 | constructor TThreadConsole.Create(AThread: TThread); |
| 1477 | begin |
Jens Geyer | 718f6ee | 2013-09-06 21:02:34 +0200 | [diff] [blame] | 1478 | inherited Create; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1479 | FThread := AThread; |
| 1480 | end; |
| 1481 | |
| 1482 | procedure TThreadConsole.Write(const S: string); |
| 1483 | var |
| 1484 | proc : TThreadProcedure; |
| 1485 | begin |
| 1486 | proc := procedure |
| 1487 | begin |
| 1488 | Console.Write( S ); |
| 1489 | end; |
| 1490 | TThread.Synchronize( FThread, proc); |
| 1491 | end; |
| 1492 | |
| 1493 | procedure TThreadConsole.WriteLine(const S: string); |
| 1494 | var |
| 1495 | proc : TThreadProcedure; |
| 1496 | begin |
| 1497 | proc := procedure |
| 1498 | begin |
| 1499 | Console.WriteLine( S ); |
| 1500 | end; |
| 1501 | TThread.Synchronize( FThread, proc); |
| 1502 | end; |
| 1503 | |
| 1504 | initialization |
| 1505 | begin |
| 1506 | TTestClient.FNumIteration := 1; |
| 1507 | TTestClient.FNumThread := 1; |
| 1508 | end; |
| 1509 | |
| 1510 | end. |