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