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