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