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