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