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