blob: 0155c9a406961de653a88b48cbea082c560ebefc [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'
Jake Farrell6fa2b302011-12-06 00:59:30 +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>;
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 Farrelle34009b2011-11-17 00:17:29 +0000279 pos : IThriftDictionary<Integer, Integer>;
280 neg : IThriftDictionary<Integer, Integer>;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000281 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 Farrell27274222011-11-10 20:32:44 +0000291 userMap : IThriftDictionary<TNumberz, Int64>;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000292 xtructs : IThriftList<IXtruct>;
293 x : IXtruct;
294 arg0 : ShortInt;
295 arg1 : Integer;
296 arg2 : Int64;
Jake Farrelle34009b2011-11-17 00:17:29 +0000297 arg3 : IThriftDictionary<SmallInt, string>;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000298 arg4 : TNumberz;
299 arg5 : Int64;
300 StartTick : Cardinal;
301 k : Integer;
302 proc : TThreadProcedure;
Jake Farrelle34009b2011-11-17 00:17:29 +0000303 hello, goodbye : IXtruct;
304 crazy : IInsanity;
305 looney : IInsanity;
306 first_map : IThriftDictionary<TNumberz, IInsanity>;
307 second_map : IThriftDictionary<TNumberz, IInsanity>;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000308
309begin
Jake Farrell27274222011-11-10 20:32:44 +0000310 client := TThriftTest.TClient.Create( FProtocol);
Jake Farrell7ae13e12011-10-18 14:35:26 +0000311 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 Farrelle34009b2011-11-17 00:17:29 +0000324 // 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 Farrell7ae13e12011-10-18 14:35:26 +0000329 try
330 client.testException('Xception');
Jake Farrelle34009b2011-11-17 00:17:29 +0000331 Expect( FALSE, 'testException(''Xception''): must trow an exception');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000332 except
Jake Farrelle34009b2011-11-17 00:17:29 +0000333 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 Farrell7ae13e12011-10-18 14:35:26 +0000337 end;
Jake Farrelle34009b2011-11-17 00:17:29 +0000338 on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
339 on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000340 end;
341
Jake Farrelle34009b2011-11-17 00:17:29 +0000342 // simple things
343 StartTestGroup( 'simple Thrift calls');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000344 client.testVoid();
Jake Farrelle34009b2011-11-17 00:17:29 +0000345 Expect( TRUE, 'testVoid()'); // success := no exception
Jake Farrell7ae13e12011-10-18 14:35:26 +0000346
Jake Farrell7ae13e12011-10-18 14:35:26 +0000347 s := client.testString('Test');
Jake Farrelle34009b2011-11-17 00:17:29 +0000348 Expect( s = 'Test', 'testString(''Test'') = "'+s+'"');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000349
Jake Farrell7ae13e12011-10-18 14:35:26 +0000350 i8 := client.testByte(1);
Jake Farrelle34009b2011-11-17 00:17:29 +0000351 Expect( i8 = 1, 'testByte(1) = ' + IntToStr( i8 ));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000352
Jake Farrell7ae13e12011-10-18 14:35:26 +0000353 i32 := client.testI32(-1);
Jake Farrelle34009b2011-11-17 00:17:29 +0000354 Expect( i32 = -1, 'testI32(-1) = ' + IntToStr(i32));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000355
Jake Farrelle34009b2011-11-17 00:17:29 +0000356 Console.WriteLine('testI64(-34359738368)');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000357 i64 := client.testI64(-34359738368);
Jake Farrelle34009b2011-11-17 00:17:29 +0000358 Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000359
Jake Farrelle34009b2011-11-17 00:17:29 +0000360 Console.WriteLine('testDouble(5.325098235)');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000361 dub := client.testDouble(5.325098235);
Jake Farrelle34009b2011-11-17 00:17:29 +0000362 Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000363
Jake Farrelle34009b2011-11-17 00:17:29 +0000364 // structs
365 StartTestGroup( 'testStruct');
366 Console.WriteLine('testStruct({''Zero'', 1, -3, -5})');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000367 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 Farrelle34009b2011-11-17 00:17:29 +0000373 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 Farrell7ae13e12011-10-18 14:35:26 +0000381
Jake Farrelle34009b2011-11-17 00:17:29 +0000382 // nested structs
383 StartTestGroup( 'testNest');
384 Console.WriteLine('testNest({1, {''Zero'', 1, -3, -5}, 5})');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000385 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 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( 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 Farrell7ae13e12011-10-18 14:35:26 +0000403
Jake Farrelle34009b2011-11-17 00:17:29 +0000404 // 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 Farrell7ae13e12011-10-18 14:35:26 +0000407 mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000408 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 Farrelle34009b2011-11-17 00:17:29 +0000416 if first
417 then first := False
418 else Console.Write( ', ' );
Jake Farrell7ae13e12011-10-18 14:35:26 +0000419 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
420 end;
Jake Farrelle34009b2011-11-17 00:17:29 +0000421 Console.WriteLine('})');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000422
423 mapin := client.testMap( mapout );
Jake Farrelle34009b2011-11-17 00:17:29 +0000424 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 Farrell7ae13e12011-10-18 14:35:26 +0000429 for key in mapin.Keys do
430 begin
Jake Farrelle34009b2011-11-17 00:17:29 +0000431 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 Farrell7ae13e12011-10-18 14:35:26 +0000433 end;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000434
Jake Farrelle34009b2011-11-17 00:17:29 +0000435
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 Farrell7ae13e12011-10-18 14:35:26 +0000440 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 Farrelle34009b2011-11-17 00:17:29 +0000449 if first
450 then first := False
451 else Console.Write(', ');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000452 Console.Write(IntToStr( j));
453 end;
Jake Farrelle34009b2011-11-17 00:17:29 +0000454 Console.WriteLine('})');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000455
Jake Farrell7ae13e12011-10-18 14:35:26 +0000456 setin := client.testSet(setout);
Jake Farrelle34009b2011-11-17 00:17:29 +0000457 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 Farrell7ae13e12011-10-18 14:35:26 +0000460 begin
Jake Farrelle34009b2011-11-17 00:17:29 +0000461 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 Farrell7ae13e12011-10-18 14:35:26 +0000476 begin
Jake Farrelle34009b2011-11-17 00:17:29 +0000477 if first
478 then first := False
479 else Console.Write(', ');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000480 Console.Write(IntToStr( j));
481 end;
Jake Farrelle34009b2011-11-17 00:17:29 +0000482 Console.WriteLine('})');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000483
Jake Farrelle34009b2011-11-17 00:17:29 +0000484 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 Farrell7ae13e12011-10-18 14:35:26 +0000494 ret := client.testEnum(TNumberz.ONE);
Jake Farrelle34009b2011-11-17 00:17:29 +0000495 Expect( ret = TNumberz.ONE, 'testEnum(ONE) = '+IntToStr(Ord(ret)));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000496
Jake Farrell7ae13e12011-10-18 14:35:26 +0000497 ret := client.testEnum(TNumberz.TWO);
Jake Farrelle34009b2011-11-17 00:17:29 +0000498 Expect( ret = TNumberz.TWO, 'testEnum(TWO) = '+IntToStr(Ord(ret)));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000499
Jake Farrell7ae13e12011-10-18 14:35:26 +0000500 ret := client.testEnum(TNumberz.THREE);
Jake Farrelle34009b2011-11-17 00:17:29 +0000501 Expect( ret = TNumberz.THREE, 'testEnum(THREE) = '+IntToStr(Ord(ret)));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000502
Jake Farrell7ae13e12011-10-18 14:35:26 +0000503 ret := client.testEnum(TNumberz.FIVE);
Jake Farrelle34009b2011-11-17 00:17:29 +0000504 Expect( ret = TNumberz.FIVE, 'testEnum(FIVE) = '+IntToStr(Ord(ret)));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000505
Jake Farrell7ae13e12011-10-18 14:35:26 +0000506 ret := client.testEnum(TNumberz.EIGHT);
Jake Farrelle34009b2011-11-17 00:17:29 +0000507 Expect( ret = TNumberz.EIGHT, 'testEnum(EIGHT) = '+IntToStr(Ord(ret)));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000508
Jake Farrelle34009b2011-11-17 00:17:29 +0000509
510 // typedef
Jake Farrell7ae13e12011-10-18 14:35:26 +0000511 uid := client.testTypedef(309858235082523);
Jake Farrelle34009b2011-11-17 00:17:29 +0000512 Expect( uid = 309858235082523, 'testTypedef(309858235082523) = '+IntToStr(uid));
Jake Farrell7ae13e12011-10-18 14:35:26 +0000513
Jake Farrelle34009b2011-11-17 00:17:29 +0000514
515 // maps of maps
516 StartTestGroup( 'testMapMap(1)');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000517 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 Farrelle34009b2011-11-17 00:17:29 +0000531 // 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 Farrell7ae13e12011-10-18 14:35:26 +0000545 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 Farrell7ae13e12011-10-18 14:35:26 +0000555 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 Farrelle34009b2011-11-17 00:17:29 +0000601 // 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 Farrell7ae13e12011-10-18 14:35:26 +0000651 arg0 := 1;
652 arg1 := 2;
653 arg2 := High(Int64);
Jake Farrelle34009b2011-11-17 00:17:29 +0000654 arg3 := TThriftDictionaryImpl<SmallInt, string>.Create;
655 arg3.AddOrSetValue( 1, 'one');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000656 arg4 := TNumberz.FIVE;
657 arg5 := 5000000;
658 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
659 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
Jake Farrelle34009b2011-11-17 00:17:29 +0000660 arg3.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
Jake Farrell7ae13e12011-10-18 14:35:26 +0000661 IntToStr( arg5) + ')');
662
Jake Farrelle34009b2011-11-17 00:17:29 +0000663 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 Farrell7ae13e12011-10-18 14:35:26 +0000721 client.testOneway(1);
Jake Farrelle34009b2011-11-17 00:17:29 +0000722 Expect( TRUE, 'Test Oneway(1)'); // success := no exception
Jake Farrell7ae13e12011-10-18 14:35:26 +0000723
Jake Farrelle34009b2011-11-17 00:17:29 +0000724 // call time
725 StartTestGroup( 'Test Calltime()');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000726 StartTick := GetTIckCount;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000727 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 Farrelle34009b2011-11-17 00:17:29 +0000733 // no more tests here
734 StartTestGroup( '');
Jake Farrell7ae13e12011-10-18 14:35:26 +0000735end;
736
Jake Farrell27274222011-11-10 20:32:44 +0000737
738procedure 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.
743var prot : IProtocol;
744 stm : TStringStream;
745 list : IList;
746 binary, binRead : TBytes;
747 i,iErr : Integer;
748const
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")
756begin
757 stm := TStringStream.Create;
758 try
Jake Farrelle34009b2011-11-17 00:17:29 +0000759 StartTestGroup( 'JsonProtocolTest'); // no more tests here
760
Jake Farrell27274222011-11-10 20:32:44 +0000761 // 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 Farrelle34009b2011-11-17 00:17:29 +0000823 StartTestGroup( ''); // no more tests here
824 end;
825end;
826
827
828procedure TClientThread.StartTestGroup( const aGroup : string);
829begin
830 FTestGroup := aGroup;
831 if FTestGroup <> '' then begin
832 Console.WriteLine('');
833 Console.WriteLine( aGroup+' tests');
834 Console.WriteLine( StringOfChar('-',60));
Jake Farrell27274222011-11-10 20:32:44 +0000835 end;
836end;
837
838
839procedure TClientThread.Expect( aTestResult : Boolean; const aTestInfo : string);
840begin
841 if aTestResult then begin
842 Inc(FSuccesses);
Jake Farrelle34009b2011-11-17 00:17:29 +0000843 Console.WriteLine( aTestInfo+': passed');
Jake Farrell27274222011-11-10 20:32:44 +0000844 end
845 else begin
Jake Farrelle34009b2011-11-17 00:17:29 +0000846 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 Farrell27274222011-11-10 20:32:44 +0000853 end;
854end;
855
856
Jake Farrelle34009b2011-11-17 00:17:29 +0000857procedure TClientThread.ReportResults;
858var nTotal : Integer;
859 sLine : string;
860begin
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('');
880end;
881
882
Roger Meier333bbf32012-01-08 21:51:08 +0000883constructor TClientThread.Create( const ATransport: ITransport; const AProtocol : IProtocol; ANumIteration: Integer);
Jake Farrell7ae13e12011-10-18 14:35:26 +0000884begin
885 inherited Create( True );
886 FNumIteration := ANumIteration;
887 FTransport := ATransport;
Jake Farrell27274222011-11-10 20:32:44 +0000888 FProtocol := AProtocol;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000889 FConsole := TThreadConsole.Create( Self );
Jake Farrelle34009b2011-11-17 00:17:29 +0000890
891 // error list: keep correct order, allow for duplicates
892 FErrors := TStringList.Create;
893 FErrors.Sorted := FALSE;
894 FErrors.Duplicates := dupAccept;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000895end;
896
897destructor TClientThread.Destroy;
898begin
Jake Farrelle34009b2011-11-17 00:17:29 +0000899 FreeAndNil( FConsole);
900 FreeAndNil( FErrors);
Jake Farrell7ae13e12011-10-18 14:35:26 +0000901 inherited;
902end;
903
904procedure TClientThread.Execute;
905var
906 i : Integer;
907 proc : TThreadProcedure;
908begin
Jake Farrelle34009b2011-11-17 00:17:29 +0000909 // 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 Farrell7ae13e12011-10-18 14:35:26 +0000918 end;
919
Jake Farrelle34009b2011-11-17 00:17:29 +0000920 // report the outcome
921 ReportResults;
922
923 // shutdown
Jake Farrell7ae13e12011-10-18 14:35:26 +0000924 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 );
934end;
935
936{ TThreadConsole }
937
938constructor TThreadConsole.Create(AThread: TThread);
939begin
940 FThread := AThread;
941end;
942
943procedure TThreadConsole.Write(const S: string);
944var
945 proc : TThreadProcedure;
946begin
947 proc := procedure
948 begin
949 Console.Write( S );
950 end;
951 TThread.Synchronize( FThread, proc);
952end;
953
954procedure TThreadConsole.WriteLine(const S: string);
955var
956 proc : TThreadProcedure;
957begin
958 proc := procedure
959 begin
960 Console.WriteLine( S );
961 end;
962 TThread.Synchronize( FThread, proc);
963end;
964
965initialization
966begin
967 TTestClient.FNumIteration := 1;
968 TTestClient.FNumThread := 1;
969end;
970
971end.