blob: 90b92a229389531b51664a2b3e2b73f35563f54b [file] [log] [blame]
Jake Farrell7ae13e12011-10-18 14:35:26 +00001(*
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
20unit TestClient;
21
22interface
23
24uses
Jake Farrell27274222011-11-10 20:32:44 +000025 Windows, SysUtils, Classes,
26 DateUtils,
27 Generics.Collections,
28 TestConstants,
Jake Farrelle34009b2011-11-17 00:17:29 +000029 Thrift,
Jake Farrell27274222011-11-10 20:32:44 +000030 Thrift.Protocol.JSON,
31 Thrift.Protocol,
32 Thrift.Transport,
33 Thrift.Stream,
34 Thrift.Test,
35 Thrift.Collections,
36 Thrift.Console;
Jake Farrell7ae13e12011-10-18 14:35:26 +000037
38type
Jake Farrell7ae13e12011-10-18 14:35:26 +000039 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 Farrell27274222011-11-10 20:32:44 +000051 FProtocol : IProtocol;
Jake Farrell7ae13e12011-10-18 14:35:26 +000052 FNumIteration : Integer;
53 FConsole : TThreadConsole;
54
Jake Farrelle34009b2011-11-17 00:17:29 +000055 // 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 Farrell27274222011-11-10 20:32:44 +000060 procedure Expect( aTestResult : Boolean; const aTestInfo : string);
Jake Farrelle34009b2011-11-17 00:17:29 +000061 procedure ReportResults;
Jake Farrell27274222011-11-10 20:32:44 +000062
Jake Farrell7ae13e12011-10-18 14:35:26 +000063 procedure ClientTest;
Jake Farrell27274222011-11-10 20:32:44 +000064 procedure JSONProtocolReadWriteTest;
Jake Farrell7ae13e12011-10-18 14:35:26 +000065 protected
66 procedure Execute; override;
67 public
Roger Meier333bbf32012-01-08 21:51:08 +000068 constructor Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
Jake Farrell7ae13e12011-10-18 14:35:26 +000069 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
81implementation
82
Jake Farrelle34009b2011-11-17 00:17:29 +000083function BoolToString( b : Boolean) : string;
84// overrides global BoolToString()
85begin
86 if b
87 then result := 'true'
Roger Meierbb6de7a2012-05-04 23:35:45 +000088 else result := 'false';
89end;
90
91// not available in all versions, so make sure we have this one imported
92function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent';
93
94{ TTestClient }
95
Jake Farrell7ae13e12011-10-18 14:35:26 +000096class procedure TTestClient.Execute(const args: array of string);
97var
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 Farrell27274222011-11-10 20:32:44 +0000111 prot : IProtocol;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000112 streamtrans : IStreamTransport;
113 http : IHTTPClient;
Jake Farrell27274222011-11-10 20:32:44 +0000114 protType, p : TKnownProtocol;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000115begin
116 bBuffered := False;;
117 bFramed := False;
Jake Farrell27274222011-11-10 20:32:44 +0000118 protType := prot_Binary;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000119 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 Farrell27274222011-11-10 20:32:44 +0000166 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 Farrell7ae13e12011-10-18 14:35:26 +0000178 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 Farrell27274222011-11-10 20:32:44 +0000213
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 Farrell7ae13e12011-10-18 14:35:26 +0000224 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!');
251end;
252
253{ TClientThread }
254
255procedure TClientThread.ClientTest;
256var
Jake Farrell7ae13e12011-10-18 14:35:26 +0000257 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>;
Roger Meierbb6de7a2012-05-04 23:35:45 +0000269 strmapout : IThriftDictionary<string,string>;
270 strmapin : IThriftDictionary<string,string>;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000271 j : Integer;
272 first : Boolean;
273 key : Integer;
Roger Meierbb6de7a2012-05-04 23:35:45 +0000274 strkey : string;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000275 listout : IThriftList<Integer>;
276 listin : IThriftList<Integer>;
277 setout : IHashSet<Integer>;
278 setin : IHashSet<Integer>;
279 ret : TNumberz;
280 uid : Int64;
281 mm : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
Jake Farrelle34009b2011-11-17 00:17:29 +0000282 pos : IThriftDictionary<Integer, Integer>;
283 neg : IThriftDictionary<Integer, Integer>;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000284 m2 : IThriftDictionary<Integer, Integer>;
285 k2 : Integer;
286 insane : IInsanity;
287 truck : IXtruct;
288 whoa : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
289 key64 : Int64;
290 val : IThriftDictionary<TNumberz, IInsanity>;
291 k2_2 : TNumberz;
292 k3 : TNumberz;
293 v2 : IInsanity;
Jake Farrell27274222011-11-10 20:32:44 +0000294 userMap : IThriftDictionary<TNumberz, Int64>;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000295 xtructs : IThriftList<IXtruct>;
296 x : IXtruct;
297 arg0 : ShortInt;
298 arg1 : Integer;
299 arg2 : Int64;
Jake Farrelle34009b2011-11-17 00:17:29 +0000300 arg3 : IThriftDictionary<SmallInt, string>;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000301 arg4 : TNumberz;
302 arg5 : Int64;
303 StartTick : Cardinal;
304 k : Integer;
305 proc : TThreadProcedure;
Jake Farrelle34009b2011-11-17 00:17:29 +0000306 hello, goodbye : IXtruct;
307 crazy : IInsanity;
308 looney : IInsanity;
309 first_map : IThriftDictionary<TNumberz, IInsanity>;
310 second_map : IThriftDictionary<TNumberz, IInsanity>;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000311
312begin
Jake Farrell27274222011-11-10 20:32:44 +0000313 client := TThriftTest.TClient.Create( FProtocol);
Roger Meierbb6de7a2012-05-04 23:35:45 +0000314 FTransport.Open;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000315
Jake Farrelle34009b2011-11-17 00:17:29 +0000316 // in-depth exception test
317 // (1) do we get an exception at all?
318 // (2) do we get the right exception?
319 // (3) does the exception contain the expected data?
320 StartTestGroup( 'testException');
Roger Meierbb6de7a2012-05-04 23:35:45 +0000321 // case 1: exception type declared in IDL at the function call
Jake Farrell7ae13e12011-10-18 14:35:26 +0000322 try
323 client.testException('Xception');
Jake Farrelle34009b2011-11-17 00:17:29 +0000324 Expect( FALSE, 'testException(''Xception''): must trow an exception');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000325 except
Jake Farrelle34009b2011-11-17 00:17:29 +0000326 on e:TXception do begin
Roger Meierbb6de7a2012-05-04 23:35:45 +0000327 Expect( e.ErrorCode = 1001, 'error code');
328 Expect( e.Message_ = 'Xception', 'error message');
Jake Farrelle34009b2011-11-17 00:17:29 +0000329 Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ );
Jake Farrell7ae13e12011-10-18 14:35:26 +0000330 end;
Jake Farrelle34009b2011-11-17 00:17:29 +0000331 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
332 on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000333 end;
334
Roger Meierbb6de7a2012-05-04 23:35:45 +0000335 // case 2: exception type NOT declared in IDL at the function call
336 // this will close the connection
337 try
338 client.testException('TException');
339 Expect( FALSE, 'testException(''TException''): must trow an exception');
340 except
341 on e:TTransportException do begin
342 Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
343 if FTransport.IsOpen then FTransport.Close;
344 FTransport.Open; // re-open connection, server has already closed
345 end;
346 on e:TException do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
347 on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
348 end;
349
350 // case 3: no exception
351 try
352 client.testException('something');
353 Expect( TRUE, 'testException(''something''): must not trow an exception');
354 except
355 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
356 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
357 end;
358
359
Jake Farrelle34009b2011-11-17 00:17:29 +0000360 // simple things
361 StartTestGroup( 'simple Thrift calls');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000362 client.testVoid();
Jake Farrelle34009b2011-11-17 00:17:29 +0000363 Expect( TRUE, 'testVoid()'); // success := no exception
Jake Farrell7ae13e12011-10-18 14:35:26 +0000364
Jake Farrell7ae13e12011-10-18 14:35:26 +0000365 s := client.testString('Test');
Jake Farrelle34009b2011-11-17 00:17:29 +0000366 Expect( s = 'Test', 'testString(''Test'') = "'+s+'"');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000367
Jake Farrell7ae13e12011-10-18 14:35:26 +0000368 i8 := client.testByte(1);
Jake Farrelle34009b2011-11-17 00:17:29 +0000369 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000370
Jake Farrell7ae13e12011-10-18 14:35:26 +0000371 i32 := client.testI32(-1);
Jake Farrelle34009b2011-11-17 00:17:29 +0000372 Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000373
Jake Farrelle34009b2011-11-17 00:17:29 +0000374 Console.WriteLine('testI64(-34359738368)');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000375 i64 := client.testI64(-34359738368);
Jake Farrelle34009b2011-11-17 00:17:29 +0000376 Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000377
Jake Farrelle34009b2011-11-17 00:17:29 +0000378 Console.WriteLine('testDouble(5.325098235)');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000379 dub := client.testDouble(5.325098235);
Jake Farrelle34009b2011-11-17 00:17:29 +0000380 Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000381
Jake Farrelle34009b2011-11-17 00:17:29 +0000382 // structs
383 StartTestGroup( 'testStruct');
384 Console.WriteLine('testStruct({''Zero'', 1, -3, -5})');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000385 o := TXtructImpl.Create;
386 o.String_thing := 'Zero';
387 o.Byte_thing := 1;
388 o.I32_thing := -3;
389 o.I64_thing := -5;
390 i := client.testStruct(o);
Jake Farrelle34009b2011-11-17 00:17:29 +0000391 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( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
396 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
397 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
398 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000399
Jake Farrelle34009b2011-11-17 00:17:29 +0000400 // nested structs
401 StartTestGroup( 'testNest');
402 Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000403 o2 := TXtruct2Impl.Create;
404 o2.Byte_thing := 1;
405 o2.Struct_thing := o;
406 o2.I32_thing := 5;
407 i2 := client.testNest(o2);
408 i := i2.Struct_thing;
Jake Farrelle34009b2011-11-17 00:17:29 +0000409 Expect( i.String_thing = 'Zero', 'i.String_thing = "'+i.String_thing+'"');
410 Expect( i.Byte_thing = 1, 'i.Byte_thing = '+IntToStr(i.Byte_thing));
411 Expect( i.I32_thing = -3, 'i.I32_thing = '+IntToStr(i.I32_thing));
412 Expect( i.I64_thing = -5, 'i.I64_thing = '+IntToStr(i.I64_thing));
413 Expect( i2.Byte_thing = 1, 'i2.Byte_thing = '+IntToStr(i2.Byte_thing));
414 Expect( i2.I32_thing = 5, 'i2.I32_thing = '+IntToStr(i2.I32_thing));
415 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
416 Expect( i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
417 Expect( i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
418 Expect( i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
419 Expect( i2.__isset_Byte_thing, 'i2.__isset_Byte_thing');
420 Expect( i2.__isset_I32_thing, 'i2.__isset_I32_thing');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000421
Jake Farrelle34009b2011-11-17 00:17:29 +0000422 // map<type1,type2>: A map of strictly unique keys to values.
423 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
424 StartTestGroup( 'testMap');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000425 mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000426 for j := 0 to 4 do
427 begin
428 mapout.AddOrSetValue( j, j - 10);
429 end;
430 Console.Write('testMap({');
431 first := True;
432 for key in mapout.Keys do
433 begin
Jake Farrelle34009b2011-11-17 00:17:29 +0000434 if first
435 then first := False
436 else Console.Write( ', ' );
Jake Farrell7ae13e12011-10-18 14:35:26 +0000437 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
438 end;
Jake Farrelle34009b2011-11-17 00:17:29 +0000439 Console.WriteLine('})');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000440
441 mapin := client.testMap( mapout );
Jake Farrelle34009b2011-11-17 00:17:29 +0000442 Expect( mapin.Count = mapout.Count, 'testMap: mapin.Count = mapout.Count');
443 for j := 0 to 4 do
444 begin
445 Expect( mapout.ContainsKey(j), 'testMap: mapout.ContainsKey('+IntToStr(j)+') = '+BoolToString(mapout.ContainsKey(j)));
446 end;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000447 for key in mapin.Keys do
448 begin
Jake Farrelle34009b2011-11-17 00:17:29 +0000449 Expect( mapin[key] = mapout[key], 'testMap: '+IntToStr(key) + ' => ' + IntToStr( mapin[key]));
450 Expect( mapin[key] = key - 10, 'testMap: mapin['+IntToStr(key)+'] = '+IntToStr( mapin[key]));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000451 end;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000452
Jake Farrelle34009b2011-11-17 00:17:29 +0000453
Roger Meierbb6de7a2012-05-04 23:35:45 +0000454 // map<type1,type2>: A map of strictly unique keys to values.
455 // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
456 StartTestGroup( 'testStringMap');
457 strmapout := TThriftDictionaryImpl<string,string>.Create;
458 for j := 0 to 4 do
459 begin
460 strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10));
461 end;
462 Console.Write('testStringMap({');
463 first := True;
464 for strkey in strmapout.Keys do
465 begin
466 if first
467 then first := False
468 else Console.Write( ', ' );
469 Console.Write( strkey + ' => ' + strmapout[strkey]);
470 end;
471 Console.WriteLine('})');
472
473 strmapin := client.testStringMap( strmapout );
474 Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count');
475 for j := 0 to 4 do
476 begin
477 Expect( strmapout.ContainsKey(IntToStr(j)),
478 'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = '
479 + BoolToString(strmapout.ContainsKey(IntToStr(j))));
480 end;
481 for strkey in strmapin.Keys do
482 begin
483 Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]);
484 Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]);
485 end;
486
487
Jake Farrelle34009b2011-11-17 00:17:29 +0000488 // set<type>: An unordered set of unique elements.
489 // Translates to an STL set, Java HashSet, set in Python, etc.
490 // Note: PHP does not support sets, so it is treated similar to a List
491 StartTestGroup( 'testSet');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000492 setout := THashSetImpl<Integer>.Create;
493 for j := -2 to 2 do
494 begin
495 setout.Add( j );
496 end;
497 Console.Write('testSet({');
498 first := True;
499 for j in setout do
500 begin
Jake Farrelle34009b2011-11-17 00:17:29 +0000501 if first
502 then first := False
503 else Console.Write(', ');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000504 Console.Write(IntToStr( j));
505 end;
Jake Farrelle34009b2011-11-17 00:17:29 +0000506 Console.WriteLine('})');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000507
Jake Farrell7ae13e12011-10-18 14:35:26 +0000508 setin := client.testSet(setout);
Jake Farrelle34009b2011-11-17 00:17:29 +0000509 Expect( setin.Count = setout.Count, 'testSet: setin.Count = setout.Count');
510 Expect( setin.Count = 5, 'testSet: setin.Count = '+IntToStr(setin.Count));
511 for j := -2 to 2 do // unordered, we can't rely on the order => test for known elements only
Jake Farrell7ae13e12011-10-18 14:35:26 +0000512 begin
Jake Farrelle34009b2011-11-17 00:17:29 +0000513 Expect( setin.Contains(j), 'testSet: setin.Contains('+IntToStr(j)+') => '+BoolToString(setin.Contains(j)));
514 end;
515
516 // list<type>: An ordered list of elements.
517 // Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
518 StartTestGroup( 'testList');
519 listout := TThriftListImpl<Integer>.Create;
520 listout.Add( +1);
521 listout.Add( -2);
522 listout.Add( +3);
523 listout.Add( -4);
524 listout.Add( 0);
525 Console.Write('testList({');
526 first := True;
527 for j in listout do
Jake Farrell7ae13e12011-10-18 14:35:26 +0000528 begin
Jake Farrelle34009b2011-11-17 00:17:29 +0000529 if first
530 then first := False
531 else Console.Write(', ');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000532 Console.Write(IntToStr( j));
533 end;
Jake Farrelle34009b2011-11-17 00:17:29 +0000534 Console.WriteLine('})');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000535
Jake Farrelle34009b2011-11-17 00:17:29 +0000536 listin := client.testList(listout);
537 Expect( listin.Count = listout.Count, 'testList: listin.Count = listout.Count');
538 Expect( listin.Count = 5, 'testList: listin.Count = '+IntToStr(listin.Count));
539 Expect( listin[0] = +1, 'listin[0] = '+IntToStr( listin[0]));
540 Expect( listin[1] = -2, 'listin[1] = '+IntToStr( listin[1]));
541 Expect( listin[2] = +3, 'listin[2] = '+IntToStr( listin[2]));
542 Expect( listin[3] = -4, 'listin[3] = '+IntToStr( listin[3]));
543 Expect( listin[4] = 0, 'listin[4] = '+IntToStr( listin[4]));
544
545 // enums
Jake Farrell7ae13e12011-10-18 14:35:26 +0000546 ret := client.testEnum(TNumberz.ONE);
Jake Farrelle34009b2011-11-17 00:17:29 +0000547 Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret)));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000548
Jake Farrell7ae13e12011-10-18 14:35:26 +0000549 ret := client.testEnum(TNumberz.TWO);
Jake Farrelle34009b2011-11-17 00:17:29 +0000550 Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret)));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000551
Jake Farrell7ae13e12011-10-18 14:35:26 +0000552 ret := client.testEnum(TNumberz.THREE);
Jake Farrelle34009b2011-11-17 00:17:29 +0000553 Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret)));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000554
Jake Farrell7ae13e12011-10-18 14:35:26 +0000555 ret := client.testEnum(TNumberz.FIVE);
Jake Farrelle34009b2011-11-17 00:17:29 +0000556 Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret)));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000557
Jake Farrell7ae13e12011-10-18 14:35:26 +0000558 ret := client.testEnum(TNumberz.EIGHT);
Jake Farrelle34009b2011-11-17 00:17:29 +0000559 Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret)));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000560
Jake Farrelle34009b2011-11-17 00:17:29 +0000561
562 // typedef
Jake Farrell7ae13e12011-10-18 14:35:26 +0000563 uid := client.testTypedef(309858235082523);
Jake Farrelle34009b2011-11-17 00:17:29 +0000564 Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000565
Jake Farrelle34009b2011-11-17 00:17:29 +0000566
567 // maps of maps
568 StartTestGroup( 'testMapMap(1)');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000569 mm := client.testMapMap(1);
570 Console.Write(' = {');
571 for key in mm.Keys do
572 begin
573 Console.Write( IntToStr( key) + ' => {');
574 m2 := mm[key];
575 for k2 in m2.Keys do
576 begin
577 Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', ');
578 end;
579 Console.Write('}, ');
580 end;
581 Console.WriteLine('}');
582
Jake Farrelle34009b2011-11-17 00:17:29 +0000583 // verify result data
584 Expect( mm.Count = 2, 'mm.Count = '+IntToStr(mm.Count));
585 pos := mm[4];
586 neg := mm[-4];
587 for j := 1 to 4 do
588 begin
589 Expect( pos[j] = j, 'pos[j] = '+IntToStr(pos[j]));
590 Expect( neg[-j] = -j, 'neg[-j] = '+IntToStr(neg[-j]));
591 end;
592
593
594
595 // insanity
596 StartTestGroup( 'testInsanity');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000597 insane := TInsanityImpl.Create;
598 insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
599 insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000);
600 truck := TXtructImpl.Create;
601 truck.String_thing := 'Truck';
602 truck.Byte_thing := 8;
603 truck.I32_thing := 8;
604 truck.I64_thing := 8;
605 insane.Xtructs := TThriftListImpl<IXtruct>.Create;
606 insane.Xtructs.Add( truck );
Jake Farrell7ae13e12011-10-18 14:35:26 +0000607 whoa := client.testInsanity( insane );
608 Console.Write(' = {');
609 for key64 in whoa.Keys do
610 begin
611 val := whoa[key64];
612 Console.Write( IntToStr( key64) + ' => {');
613 for k2_2 in val.Keys do
614 begin
615 v2 := val[k2_2];
616 Console.Write( IntToStr( Integer( k2_2)) + ' => {');
617 userMap := v2.UserMap;
618 Console.Write('{');
619 if userMap <> nil then
620 begin
621 for k3 in userMap.Keys do
622 begin
623 Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', ');
624 end;
625 end else
626 begin
627 Console.Write('null');
628 end;
629 Console.Write('}, ');
630 xtructs := v2.Xtructs;
631 Console.Write('{');
632
633 if xtructs <> nil then
634 begin
635 for x in xtructs do
636 begin
637 Console.Write('{"' + x.String_thing + '", ' +
638 IntToStr( x.Byte_thing) + ', ' +
639 IntToStr( x.I32_thing) + ', ' +
640 IntToStr( x.I32_thing) + '}, ');
641 end;
642 end else
643 begin
644 Console.Write('null');
645 end;
646 Console.Write('}');
647 Console.Write('}, ');
648 end;
649 Console.Write('}, ');
650 end;
651 Console.WriteLine('}');
652
Jake Farrelle34009b2011-11-17 00:17:29 +0000653 // verify result data
654 Expect( whoa.Count = 2, 'whoa.Count = '+IntToStr(whoa.Count));
655 //
656 first_map := whoa[1];
657 second_map := whoa[2];
658 Expect( first_map.Count = 2, 'first_map.Count = '+IntToStr(first_map.Count));
659 Expect( second_map.Count = 1, 'second_map.Count = '+IntToStr(second_map.Count));
660 //
661 looney := second_map[TNumberz.SIX];
662 Expect( Assigned(looney), 'Assigned(looney) = '+BoolToString(Assigned(looney)));
663 Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
664 Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
665 //
Roger Meierbb6de7a2012-05-04 23:35:45 +0000666 for ret in [TNumberz.TWO, TNumberz.THREE] do begin
Jake Farrelle34009b2011-11-17 00:17:29 +0000667 crazy := first_map[ret];
668 Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
669
670 Expect( crazy.__isset_UserMap, 'crazy.__isset_UserMap = '+BoolToString(crazy.__isset_UserMap));
671 Expect( crazy.__isset_Xtructs, 'crazy.__isset_Xtructs = '+BoolToString(crazy.__isset_Xtructs));
672
673 Expect( crazy.UserMap.Count = 2, 'crazy.UserMap.Count = '+IntToStr(crazy.UserMap.Count));
674 Expect( crazy.UserMap[TNumberz.FIVE] = 5, 'crazy.UserMap[TNumberz.FIVE] = '+IntToStr(crazy.UserMap[TNumberz.FIVE]));
675 Expect( crazy.UserMap[TNumberz.EIGHT] = 8, 'crazy.UserMap[TNumberz.EIGHT] = '+IntToStr(crazy.UserMap[TNumberz.EIGHT]));
676
677 Expect( crazy.Xtructs.Count = 2, 'crazy.Xtructs.Count = '+IntToStr(crazy.Xtructs.Count));
678 goodbye := crazy.Xtructs[0]; // lists are ordered, so we are allowed to assume this order
679 hello := crazy.Xtructs[1];
680
681 Expect( goodbye.String_thing = 'Goodbye4', 'goodbye.String_thing = "'+goodbye.String_thing+'"');
682 Expect( goodbye.Byte_thing = 4, 'goodbye.Byte_thing = '+IntToStr(goodbye.Byte_thing));
683 Expect( goodbye.I32_thing = 4, 'goodbye.I32_thing = '+IntToStr(goodbye.I32_thing));
684 Expect( goodbye.I64_thing = 4, 'goodbye.I64_thing = '+IntToStr(goodbye.I64_thing));
685 Expect( goodbye.__isset_String_thing, 'goodbye.__isset_String_thing = '+BoolToString(goodbye.__isset_String_thing));
686 Expect( goodbye.__isset_Byte_thing, 'goodbye.__isset_Byte_thing = '+BoolToString(goodbye.__isset_Byte_thing));
687 Expect( goodbye.__isset_I32_thing, 'goodbye.__isset_I32_thing = '+BoolToString(goodbye.__isset_I32_thing));
688 Expect( goodbye.__isset_I64_thing, 'goodbye.__isset_I64_thing = '+BoolToString(goodbye.__isset_I64_thing));
689
Roger Meierbb6de7a2012-05-04 23:35:45 +0000690 Expect( hello.String_thing = 'Hello2', 'hello.String_thing = "'+hello.String_thing+'"');
Jake Farrelle34009b2011-11-17 00:17:29 +0000691 Expect( hello.Byte_thing = 2, 'hello.Byte_thing = '+IntToStr(hello.Byte_thing));
692 Expect( hello.I32_thing = 2, 'hello.I32_thing = '+IntToStr(hello.I32_thing));
693 Expect( hello.I64_thing = 2, 'hello.I64_thing = '+IntToStr(hello.I64_thing));
694 Expect( hello.__isset_String_thing, 'hello.__isset_String_thing = '+BoolToString(hello.__isset_String_thing));
695 Expect( hello.__isset_Byte_thing, 'hello.__isset_Byte_thing = '+BoolToString(hello.__isset_Byte_thing));
696 Expect( hello.__isset_I32_thing, 'hello.__isset_I32_thing = '+BoolToString(hello.__isset_I32_thing));
697 Expect( hello.__isset_I64_thing, 'hello.__isset_I64_thing = '+BoolToString(hello.__isset_I64_thing));
698 end;
699
700
701 // multi args
702 StartTestGroup( 'testMulti');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000703 arg0 := 1;
704 arg1 := 2;
705 arg2 := High(Int64);
Jake Farrelle34009b2011-11-17 00:17:29 +0000706 arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
707 arg3.AddOrSetValue( 1, 'one');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000708 arg4 := TNumberz.FIVE;
709 arg5 := 5000000;
710 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
711 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
Jake Farrelle34009b2011-11-17 00:17:29 +0000712 arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
Jake Farrell7ae13e12011-10-18 14:35:26 +0000713 IntToStr( arg5) + ')');
714
Jake Farrelle34009b2011-11-17 00:17:29 +0000715 i := client.testMulti( arg0, arg1, arg2, arg3, arg4, arg5);
716 Expect( i.String_thing = 'Hello2', 'testMulti: i.String_thing = "'+i.String_thing+'"');
717 Expect( i.Byte_thing = arg0, 'testMulti: i.Byte_thing = '+IntToStr(i.Byte_thing));
718 Expect( i.I32_thing = arg1, 'testMulti: i.I32_thing = '+IntToStr(i.I32_thing));
719 Expect( i.I64_thing = arg2, 'testMulti: i.I64_thing = '+IntToStr(i.I64_thing));
720 Expect( i.__isset_String_thing, 'testMulti: i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
721 Expect( i.__isset_Byte_thing, 'testMulti: i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
722 Expect( i.__isset_I32_thing, 'testMulti: i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
723 Expect( i.__isset_I64_thing, 'testMulti: i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
724
725 // multi exception
726 StartTestGroup( 'testMultiException(1)');
727 try
728 i := client.testMultiException( 'need more pizza', 'run out of beer');
729 Expect( i.String_thing = 'run out of beer', 'i.String_thing = "' +i.String_thing+ '"');
730 Expect( i.__isset_String_thing, 'i.__isset_String_thing = '+BoolToString(i.__isset_String_thing));
731 Expect( not i.__isset_Byte_thing, 'i.__isset_Byte_thing = '+BoolToString(i.__isset_Byte_thing));
732 Expect( not i.__isset_I32_thing, 'i.__isset_I32_thing = '+BoolToString(i.__isset_I32_thing));
733 Expect( not i.__isset_I64_thing, 'i.__isset_I64_thing = '+BoolToString(i.__isset_I64_thing));
734 except
735 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
736 end;
737
738 StartTestGroup( 'testMultiException(Xception)');
739 try
740 i := client.testMultiException( 'Xception', 'second test');
741 Expect( FALSE, 'testMultiException(''Xception''): must trow an exception');
742 except
743 on x:TXception do begin
744 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
745 Expect( x.__isset_Message_, 'x.__isset_Message_ = '+BoolToString(x.__isset_Message_));
746 Expect( x.ErrorCode = 1001, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
747 Expect( x.Message_ = 'This is an Xception', 'x.Message = "'+x.Message_+'"');
748 end;
749 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
750 end;
751
752 StartTestGroup( 'testMultiException(Xception2)');
753 try
754 i := client.testMultiException( 'Xception2', 'third test');
755 Expect( FALSE, 'testMultiException(''Xception2''): must trow an exception');
756 except
757 on x:TXception2 do begin
758 Expect( x.__isset_ErrorCode, 'x.__isset_ErrorCode = '+BoolToString(x.__isset_ErrorCode));
759 Expect( x.__isset_Struct_thing, 'x.__isset_Struct_thing = '+BoolToString(x.__isset_Struct_thing));
760 Expect( x.ErrorCode = 2002, 'x.ErrorCode = '+IntToStr(x.ErrorCode));
761 Expect( x.Struct_thing.String_thing = 'This is an Xception2', 'x.Struct_thing.String_thing = "'+x.Struct_thing.String_thing+'"');
762 Expect( x.Struct_thing.__isset_String_thing, 'x.Struct_thing.__isset_String_thing = '+BoolToString(x.Struct_thing.__isset_String_thing));
763 Expect( not x.Struct_thing.__isset_Byte_thing, 'x.Struct_thing.__isset_Byte_thing = '+BoolToString(x.Struct_thing.__isset_Byte_thing));
764 Expect( not x.Struct_thing.__isset_I32_thing, 'x.Struct_thing.__isset_I32_thing = '+BoolToString(x.Struct_thing.__isset_I32_thing));
765 Expect( not x.Struct_thing.__isset_I64_thing, 'x.Struct_thing.__isset_I64_thing = '+BoolToString(x.Struct_thing.__isset_I64_thing));
766 end;
767 on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
768 end;
769
770
771 // oneway functions
772 StartTestGroup( 'Test Oneway(1)');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000773 client.testOneway(1);
Jake Farrelle34009b2011-11-17 00:17:29 +0000774 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
Jake Farrell7ae13e12011-10-18 14:35:26 +0000775
Jake Farrelle34009b2011-11-17 00:17:29 +0000776 // call time
777 StartTestGroup( 'Test Calltime()');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000778 StartTick := GetTIckCount;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000779 for k := 0 to 1000 - 1 do
780 begin
781 client.testVoid();
782 end;
783 Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
784
Jake Farrelle34009b2011-11-17 00:17:29 +0000785 // no more tests here
786 StartTestGroup( '');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000787end;
788
Jake Farrell27274222011-11-10 20:32:44 +0000789
790procedure TClientThread.JSONProtocolReadWriteTest;
791// Tests only then read/write procedures of the JSON protocol
792// All tests succeed, if we can read what we wrote before
793// Note that passing this test does not imply, that our JSON is really compatible to what
794// other clients or servers expect as the real JSON. This is beyond the scope of this test.
795var prot : IProtocol;
796 stm : TStringStream;
797 list : IList;
798 binary, binRead : TBytes;
799 i,iErr : Integer;
800const
801 TEST_SHORT = ShortInt( $FE);
802 TEST_SMALL = SmallInt( $FEDC);
803 TEST_LONG = LongInt( $FEDCBA98);
804 TEST_I64 = Int64( $FEDCBA9876543210);
805 TEST_DOUBLE = -1.234e-56;
806 DELTA_DOUBLE = TEST_DOUBLE * 1e-14;
807 TEST_STRING = 'abc-'#$00E4#$00f6#$00fc; // german umlauts (en-us: "funny chars")
808begin
809 stm := TStringStream.Create;
810 try
Jake Farrelle34009b2011-11-17 00:17:29 +0000811 StartTestGroup( 'JsonProtocolTest'); // no more tests here
812
Jake Farrell27274222011-11-10 20:32:44 +0000813 // prepare binary data
814 SetLength( binary, $100);
815 for i := Low(binary) to High(binary) do binary[i] := i;
816
817 // output setup
818 prot := TJSONProtocolImpl.Create(
819 TStreamTransportImpl.Create(
820 nil, TThriftStreamAdapterDelphi.Create( stm, FALSE)));
821
822 // write
823 prot.WriteListBegin( TListImpl.Create( TType.String_, 9));
824 prot.WriteBool( TRUE);
825 prot.WriteBool( FALSE);
826 prot.WriteByte( TEST_SHORT);
827 prot.WriteI16( TEST_SMALL);
828 prot.WriteI32( TEST_LONG);
829 prot.WriteI64( TEST_I64);
830 prot.WriteDouble( TEST_DOUBLE);
831 prot.WriteString( TEST_STRING);
832 prot.WriteBinary( binary);
833 prot.WriteListEnd;
834
835 // input setup
836 Expect( stm.Position = stm.Size, 'Stream position/length after write');
837 stm.Position := 0;
838 prot := TJSONProtocolImpl.Create(
839 TStreamTransportImpl.Create(
840 TThriftStreamAdapterDelphi.Create( stm, FALSE), nil));
841
842 // read and compare
843 list := prot.ReadListBegin;
844 Expect( list.ElementType = TType.String_, 'list element type');
845 Expect( list.Count = 9, 'list element count');
846 Expect( prot.ReadBool, 'WriteBool/ReadBool: TRUE');
847 Expect( not prot.ReadBool, 'WriteBool/ReadBool: FALSE');
848 Expect( prot.ReadByte = TEST_SHORT, 'WriteByte/ReadByte');
849 Expect( prot.ReadI16 = TEST_SMALL, 'WriteI16/ReadI16');
850 Expect( prot.ReadI32 = TEST_LONG, 'WriteI32/ReadI32');
851 Expect( prot.ReadI64 = TEST_I64, 'WriteI64/ReadI64');
852 Expect( abs(prot.ReadDouble-TEST_DOUBLE) < abs(DELTA_DOUBLE), 'WriteDouble/ReadDouble');
853 Expect( prot.ReadString = TEST_STRING, 'WriteString/ReadString');
854 binRead := prot.ReadBinary;
855 prot.ReadListEnd;
856
857 // test binary data
858 Expect( Length(binary) = Length(binRead), 'Binary data length check');
859 iErr := -1;
860 for i := Low(binary) to High(binary) do begin
861 if binary[i] <> binRead[i] then begin
862 iErr := i;
863 Break;
864 end;
865 end;
866 if iErr < 0
867 then Expect( TRUE, 'Binary data check ('+IntToStr(Length(binary))+' Bytes)')
868 else Expect( FALSE, 'Binary data check at offset '+IntToStr(iErr));
869
870 Expect( stm.Position = stm.Size, 'Stream position after read');
871
872 finally
873 stm.Free;
874 prot := nil; //-> Release
Jake Farrelle34009b2011-11-17 00:17:29 +0000875 StartTestGroup( ''); // no more tests here
876 end;
877end;
878
879
880procedure TClientThread.StartTestGroup( const aGroup : string);
881begin
882 FTestGroup := aGroup;
883 if FTestGroup <> '' then begin
884 Console.WriteLine('');
885 Console.WriteLine( aGroup+' tests');
886 Console.WriteLine( StringOfChar('-',60));
Jake Farrell27274222011-11-10 20:32:44 +0000887 end;
888end;
889
890
891procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
892begin
893 if aTestResult then begin
894 Inc(FSuccesses);
Jake Farrelle34009b2011-11-17 00:17:29 +0000895 Console.WriteLine( aTestInfo+': passed');
Jake Farrell27274222011-11-10 20:32:44 +0000896 end
897 else begin
Jake Farrelle34009b2011-11-17 00:17:29 +0000898 FErrors.Add( FTestGroup+': '+aTestInfo);
899 Console.WriteLine( aTestInfo+': *** FAILED ***');
900
901 // We have a failed test!
902 // -> issue DebugBreak ONLY if a debugger is attached,
903 // -> unhandled DebugBreaks would cause Windows to terminate the app otherwise
904 if IsDebuggerPresent then asm int 3 end;
Jake Farrell27274222011-11-10 20:32:44 +0000905 end;
906end;
907
908
Jake Farrelle34009b2011-11-17 00:17:29 +0000909procedure TClientThread.ReportResults;
910var nTotal : Integer;
911 sLine : string;
912begin
913 // prevent us from stupid DIV/0 errors
914 nTotal := FSuccesses + FErrors.Count;
915 if nTotal = 0 then begin
916 Console.WriteLine('No results logged');
917 Exit;
918 end;
919
920 Console.WriteLine('');
921 Console.WriteLine( StringOfChar('=',60));
922 Console.WriteLine( IntToStr(nTotal)+' tests performed');
923 Console.WriteLine( IntToStr(FSuccesses)+' tests succeeded ('+IntToStr(round(100*FSuccesses/nTotal))+'%)');
924 Console.WriteLine( IntToStr(FErrors.Count)+' tests failed ('+IntToStr(round(100*FErrors.Count/nTotal))+'%)');
925 Console.WriteLine( StringOfChar('=',60));
926 if FErrors.Count > 0 then begin
927 Console.WriteLine('FAILED TESTS:');
928 for sLine in FErrors do Console.WriteLine('- '+sLine);
929 Console.WriteLine( StringOfChar('=',60));
Roger Meierbb6de7a2012-05-04 23:35:45 +0000930 InterlockedIncrement( ExitCode); // return <> 0 on errors
Jake Farrelle34009b2011-11-17 00:17:29 +0000931 end;
932 Console.WriteLine('');
933end;
934
935
Roger Meier333bbf32012-01-08 21:51:08 +0000936constructor TClientThread.Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
Jake Farrell7ae13e12011-10-18 14:35:26 +0000937begin
938 inherited Create( True );
939 FNumIteration := ANumIteration;
940 FTransport := ATransport;
Jake Farrell27274222011-11-10 20:32:44 +0000941 FProtocol := AProtocol;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000942 FConsole := TThreadConsole.Create( Self );
Jake Farrelle34009b2011-11-17 00:17:29 +0000943
944 // error list: keep correct order, allow for duplicates
945 FErrors := TStringList.Create;
946 FErrors.Sorted := FALSE;
947 FErrors.Duplicates := dupAccept;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000948end;
949
950destructor TClientThread.Destroy;
951begin
Jake Farrelle34009b2011-11-17 00:17:29 +0000952 FreeAndNil( FConsole);
953 FreeAndNil( FErrors);
Jake Farrell7ae13e12011-10-18 14:35:26 +0000954 inherited;
955end;
956
957procedure TClientThread.Execute;
958var
959 i : Integer;
960 proc : TThreadProcedure;
961begin
Jake Farrelle34009b2011-11-17 00:17:29 +0000962 // perform all tests
963 try
964 for i := 0 to FNumIteration - 1 do
965 begin
966 ClientTest;
967 JSONProtocolReadWriteTest;
968 end;
969 except
970 on e:Exception do Expect( FALSE, 'unexpected exception: "'+e.message+'"');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000971 end;
972
Jake Farrelle34009b2011-11-17 00:17:29 +0000973 // report the outcome
974 ReportResults;
975
976 // shutdown
Jake Farrell7ae13e12011-10-18 14:35:26 +0000977 proc := procedure
978 begin
979 if FTransport <> nil then
980 begin
981 FTransport.Close;
982 FTransport := nil;
983 end;
984 end;
985
986 Synchronize( proc );
987end;
988
989{ TThreadConsole }
990
991constructor TThreadConsole.Create(AThread: TThread);
992begin
993 FThread := AThread;
994end;
995
996procedure TThreadConsole.Write(const S: string);
997var
998 proc : TThreadProcedure;
999begin
1000 proc := procedure
1001 begin
1002 Console.Write( S );
1003 end;
1004 TThread.Synchronize( FThread, proc);
1005end;
1006
1007procedure TThreadConsole.WriteLine(const S: string);
1008var
1009 proc : TThreadProcedure;
1010begin
1011 proc := procedure
1012 begin
1013 Console.WriteLine( S );
1014 end;
1015 TThread.Synchronize( FThread, proc);
1016end;
1017
1018initialization
1019begin
1020 TTestClient.FNumIteration := 1;
1021 TTestClient.FNumThread := 1;
1022end;
1023
1024end.