Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 1 | (*
|
| 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 |
|
| 22 | interface
|
| 23 |
|
| 24 | uses
|
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 25 | Windows, SysUtils, Classes,
|
| 26 | DateUtils,
|
| 27 | Generics.Collections,
|
| 28 | TestConstants,
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 29 | Thrift,
|
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 30 | Thrift.Protocol.JSON,
|
| 31 | Thrift.Protocol,
|
| 32 | Thrift.Transport,
|
| 33 | Thrift.Stream,
|
| 34 | Thrift.Test,
|
| 35 | Thrift.Collections,
|
| 36 | Thrift.Console;
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 37 |
|
| 38 | type
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 39 | TThreadConsole = class
|
| 40 | private
|
| 41 | FThread : TThread;
|
| 42 | public
|
| 43 | procedure Write( const S : string);
|
| 44 | procedure WriteLine( const S : string);
|
| 45 | constructor Create( AThread: TThread);
|
| 46 | end;
|
| 47 |
|
| 48 | TClientThread = class( TThread )
|
| 49 | private
|
| 50 | FTransport : ITransport;
|
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 51 | FProtocol : IProtocol;
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 52 | FNumIteration : Integer;
|
| 53 | FConsole : TThreadConsole;
|
| 54 |
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 55 | // test reporting, will be refactored out into separate class later
|
| 56 | FTestGroup : string;
|
| 57 | FSuccesses : Integer;
|
| 58 | FErrors : TStringList;
|
| 59 | procedure StartTestGroup( const aGroup : string);
|
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 60 | procedure Expect( aTestResult : Boolean; const aTestInfo : string);
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 61 | procedure ReportResults;
|
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 62 |
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 63 | procedure ClientTest;
|
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 64 | procedure JSONProtocolReadWriteTest;
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 65 | protected
|
| 66 | procedure Execute; override;
|
| 67 | public
|
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 68 | constructor Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 69 | destructor Destroy; override;
|
| 70 | end;
|
| 71 |
|
| 72 | TTestClient = class
|
| 73 | private
|
| 74 | class var
|
| 75 | FNumIteration : Integer;
|
| 76 | FNumThread : Integer;
|
| 77 | public
|
| 78 | class procedure Execute( const args: array of string);
|
| 79 | end;
|
| 80 |
|
| 81 | implementation
|
| 82 |
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 83 | function BoolToString( b : Boolean) : string;
|
| 84 | // overrides global BoolToString()
|
| 85 | begin
|
| 86 | if b
|
| 87 | then result := 'true'
|
Jake Farrell | 6fa2b30 | 2011-12-06 00:59:30 +0000 | [diff] [blame] | 88 | else result := 'false'; |
| 89 | end; |
| 90 | |
| 91 | // not available in all versions, so make sure we have this one imported |
| 92 | function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent'; |
| 93 | |
| 94 | { TTestClient } |
| 95 | |
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 96 | class procedure TTestClient.Execute(const args: array of string);
|
| 97 | var
|
| 98 | i : Integer;
|
| 99 | host : string;
|
| 100 | port : Integer;
|
| 101 | url : string;
|
| 102 | bBuffered : Boolean;
|
| 103 | bFramed : Boolean;
|
| 104 | s : string;
|
| 105 | n : Integer;
|
| 106 | threads : array of TThread;
|
| 107 | dtStart : TDateTime;
|
| 108 | test : Integer;
|
| 109 | thread : TThread;
|
| 110 | trans : ITransport;
|
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 111 | prot : IProtocol;
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 112 | streamtrans : IStreamTransport;
|
| 113 | http : IHTTPClient;
|
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 114 | protType, p : TKnownProtocol;
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 115 | begin
|
| 116 | bBuffered := False;;
|
| 117 | bFramed := False;
|
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 118 | protType := prot_Binary;
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 119 | try
|
| 120 | host := 'localhost';
|
| 121 | port := 9090;
|
| 122 | url := '';
|
| 123 | i := 0;
|
| 124 | try
|
| 125 | while ( i < Length(args) ) do
|
| 126 | begin
|
| 127 | try
|
| 128 | if ( args[i] = '-h') then
|
| 129 | begin
|
| 130 | Inc( i );
|
| 131 | s := args[i];
|
| 132 | n := Pos( ':', s);
|
| 133 | if ( n > 0 ) then
|
| 134 | begin
|
| 135 | host := Copy( s, 1, n - 1);
|
| 136 | port := StrToInt( Copy( s, n + 1, MaxInt));
|
| 137 | end else
|
| 138 | begin
|
| 139 | host := s;
|
| 140 | end;
|
| 141 | end else
|
| 142 | if (args[i] = '-u') then
|
| 143 | begin
|
| 144 | Inc( i );
|
| 145 | url := args[i];
|
| 146 | end else
|
| 147 | if (args[i] = '-n') then
|
| 148 | begin
|
| 149 | Inc( i );
|
| 150 | FNumIteration := StrToInt( args[i] );
|
| 151 | end else
|
| 152 | if (args[i] = '-b') then
|
| 153 | begin
|
| 154 | bBuffered := True;
|
| 155 | Console.WriteLine('Using buffered transport');
|
| 156 | end else
|
| 157 | if (args[i] = '-f' ) or ( args[i] = '-framed') then
|
| 158 | begin
|
| 159 | bFramed := True;
|
| 160 | Console.WriteLine('Using framed transport');
|
| 161 | end else
|
| 162 | if (args[i] = '-t') then
|
| 163 | begin
|
| 164 | Inc( i );
|
| 165 | FNumThread := StrToInt( args[i] );
|
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 166 | end else
|
| 167 | if (args[i] = '-prot') then // -prot JSON|binary
|
| 168 | begin
|
| 169 | Inc( i );
|
| 170 | s := args[i];
|
| 171 | for p:= Low(TKnownProtocol) to High(TKnownProtocol) do begin
|
| 172 | if SameText( s, KNOWN_PROTOCOLS[p]) then begin
|
| 173 | protType := p;
|
| 174 | Console.WriteLine('Using '+KNOWN_PROTOCOLS[protType]+' protocol');
|
| 175 | Break;
|
| 176 | end;
|
| 177 | end;
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 178 | end;
|
| 179 | finally
|
| 180 | Inc( i );
|
| 181 | end;
|
| 182 | end;
|
| 183 | except
|
| 184 | on E: Exception do
|
| 185 | begin
|
| 186 | Console.WriteLine( E.Message );
|
| 187 | end;
|
| 188 | end;
|
| 189 |
|
| 190 | SetLength( threads, FNumThread);
|
| 191 | dtStart := Now;
|
| 192 |
|
| 193 | for test := 0 to FNumThread - 1 do
|
| 194 | begin
|
| 195 | if url = '' then
|
| 196 | begin
|
| 197 | streamtrans := TSocketImpl.Create( host, port );
|
| 198 | trans := streamtrans;
|
| 199 | if bBuffered then
|
| 200 | begin
|
| 201 | trans := TBufferedTransportImpl.Create( streamtrans );
|
| 202 | end;
|
| 203 |
|
| 204 | if bFramed then
|
| 205 | begin
|
| 206 | trans := TFramedTransportImpl.Create( trans );
|
| 207 | end;
|
| 208 | end else
|
| 209 | begin
|
| 210 | http := THTTPClientImpl.Create( url );
|
| 211 | trans := http;
|
| 212 | end;
|
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 213 |
|
| 214 | // create protocol instance, default to BinaryProtocol
|
| 215 | case protType of
|
| 216 | prot_Binary: prot := TBinaryProtocolImpl.Create( trans);
|
| 217 | prot_JSON : prot := TJSONProtocolImpl.Create( trans);
|
| 218 | else
|
| 219 | ASSERT( FALSE); // unhandled case!
|
| 220 | prot := TBinaryProtocolImpl.Create( trans); // use default
|
| 221 | end;
|
| 222 |
|
| 223 | thread := TClientThread.Create( trans, prot, FNumIteration);
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 224 | threads[test] := thread;
|
| 225 | {$WARN SYMBOL_DEPRECATED OFF}
|
| 226 | thread.Resume;
|
| 227 | {$WARN SYMBOL_DEPRECATED ON}
|
| 228 | end;
|
| 229 |
|
| 230 | for test := 0 to FNumThread - 1 do
|
| 231 | begin
|
| 232 | threads[test].WaitFor;
|
| 233 | end;
|
| 234 |
|
| 235 | for test := 0 to FNumThread - 1 do
|
| 236 | begin
|
| 237 | threads[test].Free;
|
| 238 | end;
|
| 239 |
|
| 240 | Console.Write('Total time: ' + IntToStr( MilliSecondsBetween(Now, dtStart)));
|
| 241 |
|
| 242 | except
|
| 243 | on E: Exception do
|
| 244 | begin
|
| 245 | Console.WriteLine( E.Message + ' ST: ' + E.StackTrace );
|
| 246 | end;
|
| 247 | end;
|
| 248 |
|
| 249 | Console.WriteLine('');
|
| 250 | Console.WriteLine('done!');
|
| 251 | end;
|
| 252 |
|
| 253 | { TClientThread }
|
| 254 |
|
| 255 | procedure TClientThread.ClientTest;
|
| 256 | var
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 257 | client : TThriftTest.Iface;
|
| 258 | s : string;
|
| 259 | i8 : ShortInt;
|
| 260 | i32 : Integer;
|
| 261 | i64 : Int64;
|
| 262 | dub : Double;
|
| 263 | o : IXtruct;
|
| 264 | o2 : IXtruct2;
|
| 265 | i : IXtruct;
|
| 266 | i2 : IXtruct2;
|
| 267 | mapout : IThriftDictionary<Integer,Integer>;
|
| 268 | mapin : IThriftDictionary<Integer,Integer>;
|
| 269 | j : Integer;
|
| 270 | first : Boolean;
|
| 271 | key : Integer;
|
| 272 | listout : IThriftList<Integer>;
|
| 273 | listin : IThriftList<Integer>;
|
| 274 | setout : IHashSet<Integer>;
|
| 275 | setin : IHashSet<Integer>;
|
| 276 | ret : TNumberz;
|
| 277 | uid : Int64;
|
| 278 | mm : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 279 | pos : IThriftDictionary<Integer, Integer>;
|
| 280 | neg : IThriftDictionary<Integer, Integer>;
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 281 | m2 : IThriftDictionary<Integer, Integer>;
|
| 282 | k2 : Integer;
|
| 283 | insane : IInsanity;
|
| 284 | truck : IXtruct;
|
| 285 | whoa : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
|
| 286 | key64 : Int64;
|
| 287 | val : IThriftDictionary<TNumberz, IInsanity>;
|
| 288 | k2_2 : TNumberz;
|
| 289 | k3 : TNumberz;
|
| 290 | v2 : IInsanity;
|
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 291 | userMap : IThriftDictionary<TNumberz, Int64>;
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 292 | xtructs : IThriftList<IXtruct>;
|
| 293 | x : IXtruct;
|
| 294 | arg0 : ShortInt;
|
| 295 | arg1 : Integer;
|
| 296 | arg2 : Int64;
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 297 | arg3 : IThriftDictionary<SmallInt, string>;
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 298 | arg4 : TNumberz;
|
| 299 | arg5 : Int64;
|
| 300 | StartTick : Cardinal;
|
| 301 | k : Integer;
|
| 302 | proc : TThreadProcedure;
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 303 | hello, goodbye : IXtruct;
|
| 304 | crazy : IInsanity;
|
| 305 | looney : IInsanity;
|
| 306 | first_map : IThriftDictionary<TNumberz, IInsanity>;
|
| 307 | second_map : IThriftDictionary<TNumberz, IInsanity>;
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 308 |
|
| 309 | begin
|
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 310 | client := TThriftTest.TClient.Create( FProtocol);
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 311 | try
|
| 312 | if not FTransport.IsOpen then
|
| 313 | begin
|
| 314 | FTransport.Open;
|
| 315 | end;
|
| 316 | except
|
| 317 | on E: Exception do
|
| 318 | begin
|
| 319 | Console.WriteLine( E.Message );
|
| 320 | Exit;
|
| 321 | end;
|
| 322 | end;
|
| 323 |
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 324 | // in-depth exception test
|
| 325 | // (1) do we get an exception at all?
|
| 326 | // (2) do we get the right exception?
|
| 327 | // (3) does the exception contain the expected data?
|
| 328 | StartTestGroup( 'testException');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 329 | try
|
| 330 | client.testException('Xception');
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 331 | Expect( FALSE, 'testException(''Xception''): must trow an exception');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 332 | except
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 333 | on e:TXception do begin
|
| 334 | Expect( e.ErrorCode = 1001, 'error code');
|
| 335 | Expect( e.Message_ = 'This is an Xception', 'error message');
|
| 336 | Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ );
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 337 | end;
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 338 | on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
|
| 339 | on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 340 | end;
|
| 341 |
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 342 | // simple things
|
| 343 | StartTestGroup( 'simple Thrift calls');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 344 | client.testVoid();
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 345 | Expect( TRUE, 'testVoid()'); // success := no exception
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 346 |
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 347 | s := client.testString('Test');
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 348 | Expect( s = 'Test', 'testString(''Test'') = "'+s+'"');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 349 |
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 350 | i8 := client.testByte(1);
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 351 | Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 352 |
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 353 | i32 := client.testI32(-1);
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 354 | Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32));
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 355 |
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 356 | Console.WriteLine('testI64(-34359738368)');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 357 | i64 := client.testI64(-34359738368);
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 358 | Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 359 |
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 360 | Console.WriteLine('testDouble(5.325098235)');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 361 | dub := client.testDouble(5.325098235);
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 362 | Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 363 |
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 364 | // structs
|
| 365 | StartTestGroup( 'testStruct');
|
| 366 | Console.WriteLine('testStruct({''Zero'', 1, -3, -5})');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 367 | o := TXtructImpl.Create;
|
| 368 | o.String_thing := 'Zero';
|
| 369 | o.Byte_thing := 1;
|
| 370 | o.I32_thing := -3;
|
| 371 | o.I64_thing := -5;
|
| 372 | i := client.testStruct(o);
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 373 | Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
|
| 374 | Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
|
| 375 | Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
|
| 376 | Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
|
| 377 | Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
|
| 378 | Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
|
| 379 | Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
|
| 380 | Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 381 |
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 382 | // nested structs
|
| 383 | StartTestGroup( 'testNest');
|
| 384 | Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 385 | o2 := TXtruct2Impl.Create;
|
| 386 | o2.Byte_thing := 1;
|
| 387 | o2.Struct_thing := o;
|
| 388 | o2.I32_thing := 5;
|
| 389 | i2 := client.testNest(o2);
|
| 390 | i := i2.Struct_thing;
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 391 | Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
|
| 392 | Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
|
| 393 | Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
|
| 394 | Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
|
| 395 | Expect( i2.Byte_thing = 1, 'i2.Byte_thing = '+IntToStr(i2.Byte_thing));
|
| 396 | Expect( i2.I32_thing = 5, 'i2.I32_thing = '+IntToStr(i2.I32_thing));
|
| 397 | Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
|
| 398 | Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
|
| 399 | Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
|
| 400 | Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
|
| 401 | Expect( i2.__isset_Byte_thing, 'i2.__isset_Byte_thing');
|
| 402 | Expect( i2.__isset_I32_thing, 'i2.__isset_I32_thing');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 403 |
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 404 | // map<type1,type2>: A map of strictly unique keys to values.
|
| 405 | // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
|
| 406 | StartTestGroup( 'testMap');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 407 | mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 408 | for j := 0 to 4 do
|
| 409 | begin
|
| 410 | mapout.AddOrSetValue( j, j - 10);
|
| 411 | end;
|
| 412 | Console.Write('testMap({');
|
| 413 | first := True;
|
| 414 | for key in mapout.Keys do
|
| 415 | begin
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 416 | if first
|
| 417 | then first := False
|
| 418 | else Console.Write( ', ' );
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 419 | Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
|
| 420 | end;
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 421 | Console.WriteLine('})');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 422 |
|
| 423 | mapin := client.testMap( mapout );
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 424 | Expect( mapin.Count = mapout.Count, 'testMap: mapin.Count = mapout.Count');
|
| 425 | for j := 0 to 4 do
|
| 426 | begin
|
| 427 | Expect( mapout.ContainsKey(j), 'testMap: mapout.ContainsKey('+IntToStr(j)+') = '+BoolToString(mapout.ContainsKey(j)));
|
| 428 | end;
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 429 | for key in mapin.Keys do
|
| 430 | begin
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 431 | Expect( mapin[key] = mapout[key], 'testMap: '+IntToStr(key) + ' => ' + IntToStr( mapin[key]));
|
| 432 | Expect( mapin[key] = key - 10, 'testMap: mapin['+IntToStr(key)+'] = '+IntToStr( mapin[key]));
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 433 | end;
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 434 |
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 435 |
|
| 436 | // set<type>: An unordered set of unique elements.
|
| 437 | // Translates to an STL set, Java HashSet, set in Python, etc.
|
| 438 | // Note: PHP does not support sets, so it is treated similar to a List
|
| 439 | StartTestGroup( 'testSet');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 440 | setout := THashSetImpl<Integer>.Create;
|
| 441 | for j := -2 to 2 do
|
| 442 | begin
|
| 443 | setout.Add( j );
|
| 444 | end;
|
| 445 | Console.Write('testSet({');
|
| 446 | first := True;
|
| 447 | for j in setout do
|
| 448 | begin
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 449 | if first
|
| 450 | then first := False
|
| 451 | else Console.Write(', ');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 452 | Console.Write(IntToStr( j));
|
| 453 | end;
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 454 | Console.WriteLine('})');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 455 |
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 456 | setin := client.testSet(setout);
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 457 | Expect( setin.Count = setout.Count, 'testSet: setin.Count = setout.Count');
|
| 458 | Expect( setin.Count = 5, 'testSet: setin.Count = '+IntToStr(setin.Count));
|
| 459 | for j := -2 to 2 do // unordered, we can't rely on the order => test for known elements only
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 460 | begin
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 461 | Expect( setin.Contains(j), 'testSet: setin.Contains('+IntToStr(j)+') => '+BoolToString(setin.Contains(j)));
|
| 462 | end;
|
| 463 |
|
| 464 | // list<type>: An ordered list of elements.
|
| 465 | // Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
|
| 466 | StartTestGroup( 'testList');
|
| 467 | listout := TThriftListImpl<Integer>.Create;
|
| 468 | listout.Add( +1);
|
| 469 | listout.Add( -2);
|
| 470 | listout.Add( +3);
|
| 471 | listout.Add( -4);
|
| 472 | listout.Add( 0);
|
| 473 | Console.Write('testList({');
|
| 474 | first := True;
|
| 475 | for j in listout do
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 476 | begin
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 477 | if first
|
| 478 | then first := False
|
| 479 | else Console.Write(', ');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 480 | Console.Write(IntToStr( j));
|
| 481 | end;
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 482 | Console.WriteLine('})');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 483 |
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 484 | listin := client.testList(listout);
|
| 485 | Expect( listin.Count = listout.Count, 'testList: listin.Count = listout.Count');
|
| 486 | Expect( listin.Count = 5, 'testList: listin.Count = '+IntToStr(listin.Count));
|
| 487 | Expect( listin[0] = +1, 'listin[0] = '+IntToStr( listin[0]));
|
| 488 | Expect( listin[1] = -2, 'listin[1] = '+IntToStr( listin[1]));
|
| 489 | Expect( listin[2] = +3, 'listin[2] = '+IntToStr( listin[2]));
|
| 490 | Expect( listin[3] = -4, 'listin[3] = '+IntToStr( listin[3]));
|
| 491 | Expect( listin[4] = 0, 'listin[4] = '+IntToStr( listin[4]));
|
| 492 |
|
| 493 | // enums
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 494 | ret := client.testEnum(TNumberz.ONE);
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 495 | Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret)));
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 496 |
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 497 | ret := client.testEnum(TNumberz.TWO);
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 498 | Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret)));
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 499 |
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 500 | ret := client.testEnum(TNumberz.THREE);
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 501 | Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret)));
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 502 |
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 503 | ret := client.testEnum(TNumberz.FIVE);
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 504 | Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret)));
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 505 |
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 506 | ret := client.testEnum(TNumberz.EIGHT);
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 507 | Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret)));
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 508 |
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 509 |
|
| 510 | // typedef
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 511 | uid := client.testTypedef(309858235082523);
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 512 | Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid));
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 513 |
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 514 |
|
| 515 | // maps of maps
|
| 516 | StartTestGroup( 'testMapMap(1)');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 517 | mm := client.testMapMap(1);
|
| 518 | Console.Write(' = {');
|
| 519 | for key in mm.Keys do
|
| 520 | begin
|
| 521 | Console.Write( IntToStr( key) + ' => {');
|
| 522 | m2 := mm[key];
|
| 523 | for k2 in m2.Keys do
|
| 524 | begin
|
| 525 | Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', ');
|
| 526 | end;
|
| 527 | Console.Write('}, ');
|
| 528 | end;
|
| 529 | Console.WriteLine('}');
|
| 530 |
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 531 | // verify result data
|
| 532 | Expect( mm.Count = 2, 'mm.Count = '+IntToStr(mm.Count));
|
| 533 | pos := mm[4];
|
| 534 | neg := mm[-4];
|
| 535 | for j := 1 to 4 do
|
| 536 | begin
|
| 537 | Expect( pos[j] = j, 'pos[j] = '+IntToStr(pos[j]));
|
| 538 | Expect( neg[-j] = -j, 'neg[-j] = '+IntToStr(neg[-j]));
|
| 539 | end;
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 | // insanity
|
| 544 | StartTestGroup( 'testInsanity');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 545 | insane := TInsanityImpl.Create;
|
| 546 | insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
|
| 547 | insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000);
|
| 548 | truck := TXtructImpl.Create;
|
| 549 | truck.String_thing := 'Truck';
|
| 550 | truck.Byte_thing := 8;
|
| 551 | truck.I32_thing := 8;
|
| 552 | truck.I64_thing := 8;
|
| 553 | insane.Xtructs := TThriftListImpl<IXtruct>.Create;
|
| 554 | insane.Xtructs.Add( truck );
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 555 | whoa := client.testInsanity( insane );
|
| 556 | Console.Write(' = {');
|
| 557 | for key64 in whoa.Keys do
|
| 558 | begin
|
| 559 | val := whoa[key64];
|
| 560 | Console.Write( IntToStr( key64) + ' => {');
|
| 561 | for k2_2 in val.Keys do
|
| 562 | begin
|
| 563 | v2 := val[k2_2];
|
| 564 | Console.Write( IntToStr( Integer( k2_2)) + ' => {');
|
| 565 | userMap := v2.UserMap;
|
| 566 | Console.Write('{');
|
| 567 | if userMap <> nil then
|
| 568 | begin
|
| 569 | for k3 in userMap.Keys do
|
| 570 | begin
|
| 571 | Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', ');
|
| 572 | end;
|
| 573 | end else
|
| 574 | begin
|
| 575 | Console.Write('null');
|
| 576 | end;
|
| 577 | Console.Write('}, ');
|
| 578 | xtructs := v2.Xtructs;
|
| 579 | Console.Write('{');
|
| 580 |
|
| 581 | if xtructs <> nil then
|
| 582 | begin
|
| 583 | for x in xtructs do
|
| 584 | begin
|
| 585 | Console.Write('{"' + x.String_thing + '", ' +
|
| 586 | IntToStr( x.Byte_thing) + ', ' +
|
| 587 | IntToStr( x.I32_thing) + ', ' +
|
| 588 | IntToStr( x.I32_thing) + '}, ');
|
| 589 | end;
|
| 590 | end else
|
| 591 | begin
|
| 592 | Console.Write('null');
|
| 593 | end;
|
| 594 | Console.Write('}');
|
| 595 | Console.Write('}, ');
|
| 596 | end;
|
| 597 | Console.Write('}, ');
|
| 598 | end;
|
| 599 | Console.WriteLine('}');
|
| 600 |
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 601 | // verify result data
|
| 602 | Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count));
|
| 603 | //
|
| 604 | first_map := whoa[1];
|
| 605 | second_map := whoa[2];
|
| 606 | Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count));
|
| 607 | Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count));
|
| 608 | //
|
| 609 | looney := second_map[TNumberz.SIX];
|
| 610 | Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney)));
|
| 611 | Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
|
| 612 | Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
|
| 613 | //
|
| 614 | for ret in [TNumberz.SIX, TNumberz.THREE] do begin
|
| 615 | crazy := first_map[ret];
|
| 616 | Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
|
| 617 |
|
| 618 | Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap));
|
| 619 | Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs));
|
| 620 |
|
| 621 | Expect( crazy.UserMap.Count = 2, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count));
|
| 622 | Expect( crazy.UserMap[TNumberz.FIVE] = 5, 'crazy.UserMap[TNumberz.FIVE] = '+IntToStr(crazy.UserMap[TNumberz.FIVE]));
|
| 623 | Expect( crazy.UserMap[TNumberz.EIGHT] = 8, 'crazy.UserMap[TNumberz.EIGHT] = '+IntToStr(crazy.UserMap[TNumberz.EIGHT]));
|
| 624 |
|
| 625 | Expect( crazy.Xtructs.Count = 2, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count));
|
| 626 | goodbye := crazy.Xtructs[0]; // lists are ordered, so we are allowed to assume this order
|
| 627 | hello := crazy.Xtructs[1];
|
| 628 |
|
| 629 | Expect( goodbye.String_thing = 'Goodbye4', 'goodbye.String_thing = "'+goodbye.String_thing+'"');
|
| 630 | Expect( goodbye.Byte_thing = 4, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing));
|
| 631 | Expect( goodbye.I32_thing = 4, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing));
|
| 632 | Expect( goodbye.I64_thing = 4, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing));
|
| 633 | Expect( goodbye.__isset_String_thing, 'goodbye.__isset_String_thing = '+BoolToString(goodbye.__isset_String_thing));
|
| 634 | Expect( goodbye.__isset_Byte_thing, 'goodbye.__isset_Byte_thing = '+BoolToString(goodbye.__isset_Byte_thing));
|
| 635 | Expect( goodbye.__isset_I32_thing, 'goodbye.__isset_I32_thing = '+BoolToString(goodbye.__isset_I32_thing));
|
| 636 | Expect( goodbye.__isset_I64_thing, 'goodbye.__isset_I64_thing = '+BoolToString(goodbye.__isset_I64_thing));
|
| 637 |
|
| 638 | Expect( hello.String_thing = 'hello', 'hello.String_thing = "'+hello.String_thing+'"');
|
| 639 | Expect( hello.Byte_thing = 2, 'hello.Byte_thing = '+IntToStr(hello.Byte_thing));
|
| 640 | Expect( hello.I32_thing = 2, 'hello.I32_thing = '+IntToStr(hello.I32_thing));
|
| 641 | Expect( hello.I64_thing = 2, 'hello.I64_thing = '+IntToStr(hello.I64_thing));
|
| 642 | Expect( hello.__isset_String_thing, 'hello.__isset_String_thing = '+BoolToString(hello.__isset_String_thing));
|
| 643 | Expect( hello.__isset_Byte_thing, 'hello.__isset_Byte_thing = '+BoolToString(hello.__isset_Byte_thing));
|
| 644 | Expect( hello.__isset_I32_thing, 'hello.__isset_I32_thing = '+BoolToString(hello.__isset_I32_thing));
|
| 645 | Expect( hello.__isset_I64_thing, 'hello.__isset_I64_thing = '+BoolToString(hello.__isset_I64_thing));
|
| 646 | end;
|
| 647 |
|
| 648 |
|
| 649 | // multi args
|
| 650 | StartTestGroup( 'testMulti');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 651 | arg0 := 1;
|
| 652 | arg1 := 2;
|
| 653 | arg2 := High(Int64);
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 654 | arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
|
| 655 | arg3.AddOrSetValue( 1, 'one');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 656 | arg4 := TNumberz.FIVE;
|
| 657 | arg5 := 5000000;
|
| 658 | Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
|
| 659 | IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 660 | arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 661 | IntToStr( arg5) + ')');
|
| 662 |
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 663 | i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5);
|
| 664 | Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"');
|
| 665 | Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing));
|
| 666 | Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing));
|
| 667 | Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing));
|
| 668 | Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
|
| 669 | Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
|
| 670 | Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
|
| 671 | Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
|
| 672 |
|
| 673 | // multi exception
|
| 674 | StartTestGroup( 'testMultiException(1)');
|
| 675 | try
|
| 676 | i := client.testMultiException( 'need more pizza', 'run out of beer');
|
| 677 | Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"');
|
| 678 | Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
|
| 679 | Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
|
| 680 | Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
|
| 681 | Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
|
| 682 | except
|
| 683 | on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
|
| 684 | end;
|
| 685 |
|
| 686 | StartTestGroup( 'testMultiException(Xception)');
|
| 687 | try
|
| 688 | i := client.testMultiException( 'Xception', 'second test');
|
| 689 | Expect( FALSE, 'testMultiException(''Xception''): must trow an exception');
|
| 690 | except
|
| 691 | on x:TXception do begin
|
| 692 | Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
|
| 693 | Expect( x.__isset_Message_, 'x.__isset_Message_ = '+BoolToString(x.__isset_Message_));
|
| 694 | Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
|
| 695 | Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"');
|
| 696 | end;
|
| 697 | on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
|
| 698 | end;
|
| 699 |
|
| 700 | StartTestGroup( 'testMultiException(Xception2)');
|
| 701 | try
|
| 702 | i := client.testMultiException( 'Xception2', 'third test');
|
| 703 | Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception');
|
| 704 | except
|
| 705 | on x:TXception2 do begin
|
| 706 | Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
|
| 707 | Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing));
|
| 708 | Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
|
| 709 | Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"');
|
| 710 | Expect( x.Struct_thing.__isset_String_thing, 'x.Struct_thing.__isset_String_thing = '+BoolToString(x.Struct_thing.__isset_String_thing));
|
| 711 | Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing));
|
| 712 | Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing));
|
| 713 | Expect( not x.Struct_thing.__isset_I64_thing, 'x.Struct_thing.__isset_I64_thing = '+BoolToString(x.Struct_thing.__isset_I64_thing));
|
| 714 | end;
|
| 715 | on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
|
| 716 | end;
|
| 717 |
|
| 718 |
|
| 719 | // oneway functions
|
| 720 | StartTestGroup( 'Test Oneway(1)');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 721 | client.testOneway(1);
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 722 | Expect( TRUE, 'Test Oneway(1)'); // success := no exception
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 723 |
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 724 | // call time
|
| 725 | StartTestGroup( 'Test Calltime()');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 726 | StartTick := GetTIckCount;
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 727 | for k := 0 to 1000 - 1 do
|
| 728 | begin
|
| 729 | client.testVoid();
|
| 730 | end;
|
| 731 | Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
|
| 732 |
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 733 | // no more tests here
|
| 734 | StartTestGroup( '');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 735 | end;
|
| 736 |
|
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 737 |
|
| 738 | procedure TClientThread.JSONProtocolReadWriteTest;
|
| 739 | // Tests only then read/write procedures of the JSON protocol
|
| 740 | // All tests succeed, if we can read what we wrote before
|
| 741 | // Note that passing this test does not imply, that our JSON is really compatible to what
|
| 742 | // other clients or servers expect as the real JSON. This is beyond the scope of this test.
|
| 743 | var prot : IProtocol;
|
| 744 | stm : TStringStream;
|
| 745 | list : IList;
|
| 746 | binary, binRead : TBytes;
|
| 747 | i,iErr : Integer;
|
| 748 | const
|
| 749 | TEST_SHORT = ShortInt( $FE);
|
| 750 | TEST_SMALL = SmallInt( $FEDC);
|
| 751 | TEST_LONG = LongInt( $FEDCBA98);
|
| 752 | TEST_I64 = Int64( $FEDCBA9876543210);
|
| 753 | TEST_DOUBLE = -1.234e-56;
|
| 754 | DELTA_DOUBLE = TEST_DOUBLE * 1e-14;
|
| 755 | TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars")
|
| 756 | begin
|
| 757 | stm := TStringStream.Create;
|
| 758 | try
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 759 | StartTestGroup( 'JsonProtocolTest'); // no more tests here
|
| 760 |
|
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 761 | // prepare binary data
|
| 762 | SetLength( binary, $100);
|
| 763 | for i := Low(binary) to High(binary) do binary[i] := i;
|
| 764 |
|
| 765 | // output setup
|
| 766 | prot := TJSONProtocolImpl.Create(
|
| 767 | TStreamTransportImpl.Create(
|
| 768 | nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
|
| 769 |
|
| 770 | // write
|
| 771 | prot.WriteListBegin( TListImpl.Create( TType.String_, 9));
|
| 772 | prot.WriteBool( TRUE);
|
| 773 | prot.WriteBool( FALSE);
|
| 774 | prot.WriteByte( TEST_SHORT);
|
| 775 | prot.WriteI16( TEST_SMALL);
|
| 776 | prot.WriteI32( TEST_LONG);
|
| 777 | prot.WriteI64( TEST_I64);
|
| 778 | prot.WriteDouble( TEST_DOUBLE);
|
| 779 | prot.WriteString( TEST_STRING);
|
| 780 | prot.WriteBinary( binary);
|
| 781 | prot.WriteListEnd;
|
| 782 |
|
| 783 | // input setup
|
| 784 | Expect( stm.Position = stm.Size, 'Stream position/length after write');
|
| 785 | stm.Position := 0;
|
| 786 | prot := TJSONProtocolImpl.Create(
|
| 787 | TStreamTransportImpl.Create(
|
| 788 | TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
|
| 789 |
|
| 790 | // read and compare
|
| 791 | list := prot.ReadListBegin;
|
| 792 | Expect( list.ElementType = TType.String_, 'list element type');
|
| 793 | Expect( list.Count = 9, 'list element count');
|
| 794 | Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE');
|
| 795 | Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE');
|
| 796 | Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte');
|
| 797 | Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16');
|
| 798 | Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32');
|
| 799 | Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64');
|
| 800 | Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble');
|
| 801 | Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString');
|
| 802 | binRead := prot.ReadBinary;
|
| 803 | prot.ReadListEnd;
|
| 804 |
|
| 805 | // test binary data
|
| 806 | Expect( Length(binary) = Length(binRead), 'Binary data length check');
|
| 807 | iErr := -1;
|
| 808 | for i := Low(binary) to High(binary) do begin
|
| 809 | if binary[i] <> binRead[i] then begin
|
| 810 | iErr := i;
|
| 811 | Break;
|
| 812 | end;
|
| 813 | end;
|
| 814 | if iErr < 0
|
| 815 | then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)')
|
| 816 | else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr));
|
| 817 |
|
| 818 | Expect( stm.Position = stm.Size, 'Stream position after read');
|
| 819 |
|
| 820 | finally
|
| 821 | stm.Free;
|
| 822 | prot := nil; //-> Release
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 823 | StartTestGroup( ''); // no more tests here
|
| 824 | end;
|
| 825 | end;
|
| 826 |
|
| 827 |
|
| 828 | procedure TClientThread.StartTestGroup( const aGroup : string);
|
| 829 | begin
|
| 830 | FTestGroup := aGroup;
|
| 831 | if FTestGroup <> '' then begin
|
| 832 | Console.WriteLine('');
|
| 833 | Console.WriteLine( aGroup+' tests');
|
| 834 | Console.WriteLine( StringOfChar('-',60));
|
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 835 | end;
|
| 836 | end;
|
| 837 |
|
| 838 |
|
| 839 | procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
|
| 840 | begin
|
| 841 | if aTestResult then begin
|
| 842 | Inc(FSuccesses);
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 843 | Console.WriteLine( aTestInfo+': passed');
|
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 844 | end
|
| 845 | else begin
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 846 | FErrors.Add( FTestGroup+': '+aTestInfo);
|
| 847 | Console.WriteLine( aTestInfo+': *** FAILED ***');
|
| 848 |
|
| 849 | // We have a failed test!
|
| 850 | // -> issue DebugBreak ONLY if a debugger is attached,
|
| 851 | // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise
|
| 852 | if IsDebuggerPresent then asm int 3 end;
|
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 853 | end;
|
| 854 | end;
|
| 855 |
|
| 856 |
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 857 | procedure TClientThread.ReportResults;
|
| 858 | var nTotal : Integer;
|
| 859 | sLine : string;
|
| 860 | begin
|
| 861 | // prevent us from stupid DIV/0 errors
|
| 862 | nTotal := FSuccesses + FErrors.Count;
|
| 863 | if nTotal = 0 then begin
|
| 864 | Console.WriteLine('No results logged');
|
| 865 | Exit;
|
| 866 | end;
|
| 867 |
|
| 868 | Console.WriteLine('');
|
| 869 | Console.WriteLine( StringOfChar('=',60));
|
| 870 | Console.WriteLine( IntToStr(nTotal)+' tests performed');
|
| 871 | Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)');
|
| 872 | Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)');
|
| 873 | Console.WriteLine( StringOfChar('=',60));
|
| 874 | if FErrors.Count > 0 then begin
|
| 875 | Console.WriteLine('FAILED TESTS:');
|
| 876 | for sLine in FErrors do Console.WriteLine('- '+sLine);
|
| 877 | Console.WriteLine( StringOfChar('=',60));
|
| 878 | end;
|
| 879 | Console.WriteLine('');
|
| 880 | end;
|
| 881 |
|
| 882 |
|
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 883 | constructor TClientThread.Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 884 | begin
|
| 885 | inherited Create( True );
|
| 886 | FNumIteration := ANumIteration;
|
| 887 | FTransport := ATransport;
|
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 888 | FProtocol := AProtocol;
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 889 | FConsole := TThreadConsole.Create( Self );
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 890 |
|
| 891 | // error list: keep correct order, allow for duplicates
|
| 892 | FErrors := TStringList.Create;
|
| 893 | FErrors.Sorted := FALSE;
|
| 894 | FErrors.Duplicates := dupAccept;
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 895 | end;
|
| 896 |
|
| 897 | destructor TClientThread.Destroy;
|
| 898 | begin
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 899 | FreeAndNil( FConsole);
|
| 900 | FreeAndNil( FErrors);
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 901 | inherited;
|
| 902 | end;
|
| 903 |
|
| 904 | procedure TClientThread.Execute;
|
| 905 | var
|
| 906 | i : Integer;
|
| 907 | proc : TThreadProcedure;
|
| 908 | begin
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 909 | // perform all tests
|
| 910 | try
|
| 911 | for i := 0 to FNumIteration - 1 do
|
| 912 | begin
|
| 913 | ClientTest;
|
| 914 | JSONProtocolReadWriteTest;
|
| 915 | end;
|
| 916 | except
|
| 917 | on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"');
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 918 | end;
|
| 919 |
|
Jake Farrell | e34009b | 2011-11-17 00:17:29 +0000 | [diff] [blame] | 920 | // report the outcome
|
| 921 | ReportResults;
|
| 922 |
|
| 923 | // shutdown
|
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 924 | proc := procedure
|
| 925 | begin
|
| 926 | if FTransport <> nil then
|
| 927 | begin
|
| 928 | FTransport.Close;
|
| 929 | FTransport := nil;
|
| 930 | end;
|
| 931 | end;
|
| 932 |
|
| 933 | Synchronize( proc );
|
| 934 | end;
|
| 935 |
|
| 936 | { TThreadConsole }
|
| 937 |
|
| 938 | constructor TThreadConsole.Create(AThread: TThread);
|
| 939 | begin
|
| 940 | FThread := AThread;
|
| 941 | end;
|
| 942 |
|
| 943 | procedure TThreadConsole.Write(const S: string);
|
| 944 | var
|
| 945 | proc : TThreadProcedure;
|
| 946 | begin
|
| 947 | proc := procedure
|
| 948 | begin
|
| 949 | Console.Write( S );
|
| 950 | end;
|
| 951 | TThread.Synchronize( FThread, proc);
|
| 952 | end;
|
| 953 |
|
| 954 | procedure TThreadConsole.WriteLine(const S: string);
|
| 955 | var
|
| 956 | proc : TThreadProcedure;
|
| 957 | begin
|
| 958 | proc := procedure
|
| 959 | begin
|
| 960 | Console.WriteLine( S );
|
| 961 | end;
|
| 962 | TThread.Synchronize( FThread, proc);
|
| 963 | end;
|
| 964 |
|
| 965 | initialization
|
| 966 | begin
|
| 967 | TTestClient.FNumIteration := 1;
|
| 968 | TTestClient.FNumThread := 1;
|
| 969 | end;
|
| 970 |
|
| 971 | end.
|