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