You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by ro...@apache.org on 2012/05/05 01:35:45 UTC

svn commit: r1334250 - in /thrift/trunk/lib/delphi/test: TestClient.pas TestServer.pas client.dpr

Author: roger
Date: Fri May  4 23:35:45 2012
New Revision: 1334250

URL: http://svn.apache.org/viewvc?rev=1334250&view=rev
Log:
THRIFT-1596 Delphi: Test clients should have a return codes that reflect whether they succeeded or not
Patch: Jens Geyer

Modified:
    thrift/trunk/lib/delphi/test/TestClient.pas
    thrift/trunk/lib/delphi/test/TestServer.pas
    thrift/trunk/lib/delphi/test/client.dpr

Modified: thrift/trunk/lib/delphi/test/TestClient.pas
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/delphi/test/TestClient.pas?rev=1334250&r1=1334249&r2=1334250&view=diff
==============================================================================
--- thrift/trunk/lib/delphi/test/TestClient.pas (original)
+++ thrift/trunk/lib/delphi/test/TestClient.pas Fri May  4 23:35:45 2012
@@ -85,14 +85,14 @@ function BoolToString( b : Boolean) : st
 begin
   if b
   then result := 'true'
-  else result := 'false';
-end;
-
-// not available in all versions, so make sure we have this one imported
-function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent';
-
-{ TTestClient }
-
+  else result := 'false';
+end;
+
+// not available in all versions, so make sure we have this one imported
+function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent';
+
+{ TTestClient }
+
 class procedure TTestClient.Execute(const args: array of string);
 var
   i : Integer;
@@ -266,9 +266,12 @@ var
   i2 : IXtruct2;
   mapout : IThriftDictionary<Integer,Integer>;
   mapin : IThriftDictionary<Integer,Integer>;
+  strmapout : IThriftDictionary<string,string>;
+  strmapin : IThriftDictionary<string,string>;
   j : Integer;
   first : Boolean;
   key : Integer;
+  strkey : string;
   listout : IThriftList<Integer>;
   listin : IThriftList<Integer>;
   setout : IHashSet<Integer>;
@@ -308,37 +311,52 @@ var
 
 begin
   client := TThriftTest.TClient.Create( FProtocol);
-  try
-    if not FTransport.IsOpen then
-    begin
-      FTransport.Open;
-    end;
-  except
-    on E: Exception do
-    begin
-      Console.WriteLine( E.Message );
-      Exit;
-    end;
-  end;
+  FTransport.Open;
 
   // in-depth exception test
   // (1) do we get an exception at all?
   // (2) do we get the right exception?
   // (3) does the exception contain the expected data?
   StartTestGroup( 'testException');
+  // case 1: exception type declared in IDL at the function call
   try
     client.testException('Xception');
     Expect( FALSE, 'testException(''Xception''): must trow an exception');
   except
     on e:TXception do begin
-      Expect( e.ErrorCode = 1001,                  'error code');
-      Expect( e.Message_  = 'This is an Xception', 'error message');
+      Expect( e.ErrorCode = 1001,       'error code');
+      Expect( e.Message_  = 'Xception', 'error message');
       Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ );
     end;
     on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
     on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
   end;
 
+  // case 2: exception type NOT declared in IDL at the function call
+  // this will close the connection
+  try
+    client.testException('TException');
+    Expect( FALSE, 'testException(''TException''): must trow an exception');
+  except
+    on e:TTransportException do begin
+      Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get
+      if FTransport.IsOpen then FTransport.Close;
+      FTransport.Open;   // re-open connection, server has already closed
+    end;
+    on e:TException do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
+    on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"');
+  end;
+
+  // case 3: no exception
+  try
+    client.testException('something');
+    Expect( TRUE, 'testException(''something''): must not trow an exception');
+  except
+    on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"');
+    on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"');
+  end;
+
+
   // simple things
   StartTestGroup( 'simple Thrift calls');
   client.testVoid();
@@ -433,6 +451,40 @@ begin
   end;
 
 
+  // map<type1,type2>: A map of strictly unique keys to values.
+  // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.
+  StartTestGroup( 'testStringMap');
+  strmapout := TThriftDictionaryImpl<string,string>.Create;
+  for j := 0 to 4 do
+  begin
+    strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10));
+  end;
+  Console.Write('testStringMap({');
+  first := True;
+  for strkey in strmapout.Keys do
+  begin
+    if first
+    then first := False
+    else Console.Write( ', ' );
+    Console.Write( strkey + ' => ' + strmapout[strkey]);
+  end;
+  Console.WriteLine('})');
+
+  strmapin := client.testStringMap( strmapout );
+  Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count');
+  for j := 0 to 4 do
+  begin
+    Expect( strmapout.ContainsKey(IntToStr(j)),
+            'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = '
+            + BoolToString(strmapout.ContainsKey(IntToStr(j))));
+  end;
+  for strkey in strmapin.Keys do
+  begin
+    Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]);
+    Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]);
+  end;
+
+
   // set<type>: An unordered set of unique elements.
   // Translates to an STL set, Java HashSet, set in Python, etc.
   // Note: PHP does not support sets, so it is treated similar to a List
@@ -611,7 +663,7 @@ begin
   Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap));
   Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs));
   //
-  for ret in [TNumberz.SIX, TNumberz.THREE] do begin
+  for ret in [TNumberz.TWO, TNumberz.THREE] do begin
     crazy := first_map[ret];
     Console.WriteLine('first_map['+intToStr(Ord(ret))+']');
 
@@ -635,7 +687,7 @@ begin
     Expect( goodbye.__isset_I32_thing, 'goodbye.__isset_I32_thing = '+BoolToString(goodbye.__isset_I32_thing));
     Expect( goodbye.__isset_I64_thing, 'goodbye.__isset_I64_thing = '+BoolToString(goodbye.__isset_I64_thing));
 
-    Expect( hello.String_thing = 'hello', 'hello.String_thing = "'+hello.String_thing+'"');
+    Expect( hello.String_thing = 'Hello2', 'hello.String_thing = "'+hello.String_thing+'"');
     Expect( hello.Byte_thing = 2, 'hello.Byte_thing = '+IntToStr(hello.Byte_thing));
     Expect( hello.I32_thing = 2, 'hello.I32_thing = '+IntToStr(hello.I32_thing));
     Expect( hello.I64_thing = 2, 'hello.I64_thing = '+IntToStr(hello.I64_thing));
@@ -875,6 +927,7 @@ begin
     Console.WriteLine('FAILED TESTS:');
     for sLine in FErrors do Console.WriteLine('- '+sLine);
     Console.WriteLine( StringOfChar('=',60));
+    InterlockedIncrement( ExitCode);  // return <> 0 on errors
   end;
   Console.WriteLine('');
 end;

Modified: thrift/trunk/lib/delphi/test/TestServer.pas
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/delphi/test/TestServer.pas?rev=1334250&r1=1334249&r2=1334250&view=diff
==============================================================================
--- thrift/trunk/lib/delphi/test/TestServer.pas (original)
+++ thrift/trunk/lib/delphi/test/TestServer.pas Fri May  4 23:35:45 2012
@@ -110,8 +110,15 @@ begin
   Console.WriteLine('testException(' + arg + ')');
   if ( arg = 'Xception') then
   begin
-    raise TXception.Create( 1001, 'This is an Xception');
+    raise TXception.Create( 1001, arg);
   end;
+
+  if (arg = 'TException') then
+  begin
+    raise TException.Create('');
+  end;
+
+  // else do not throw anything
 end;
 
 function TTestServer.TTestHandlerImpl.testI32(thing: Integer): Integer;
@@ -140,7 +147,7 @@ begin
 
   Console.WriteLine('testInsanity()');
   hello := TXtructImpl.Create;
-  hello.String_thing := 'hello';
+  hello.String_thing := 'Hello2';
   hello.Byte_thing := 2;
   hello.I32_thing := 2;
   hello.I64_thing := 2;
@@ -164,7 +171,7 @@ begin
   first_map := TThriftDictionaryImpl<TNumberz, IInsanity>.Create;
   second_map := TThriftDictionaryImpl<TNumberz, IInsanity>.Create;
 
-  first_map.AddOrSetValue( TNumberz.SIX, crazy);
+  first_map.AddOrSetValue( TNumberz.TWO, crazy);
   first_map.AddOrSetValue( TNumberz.THREE, crazy);
 
   second_map.AddOrSetValue( TNumberz.SIX, looney);
@@ -348,8 +355,25 @@ end;
 
 function TTestServer.TTestHandlerImpl.testStringMap(
   const thing: IThriftDictionary<string, string>): IThriftDictionary<string, string>;
+var
+  first : Boolean;
+  key : string;
 begin
-
+  Console.Write('testStringMap({');
+  first := True;
+  for key in thing.Keys do
+  begin
+    if (first) then
+    begin
+      first := false;
+    end else
+    begin
+      Console.Write(', ');
+    end;
+    Console.Write(key + ' => ' + thing[key]);
+  end;
+  Console.WriteLine('})');
+  Result := thing;
 end;
 
 function TTestServer.TTestHandlerImpl.testTypedef( const thing: Int64): Int64;

Modified: thrift/trunk/lib/delphi/test/client.dpr
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/delphi/test/client.dpr?rev=1334250&r1=1334249&r2=1334250&view=diff
==============================================================================
--- thrift/trunk/lib/delphi/test/client.dpr (original)
+++ thrift/trunk/lib/delphi/test/client.dpr Fri May  4 23:35:45 2012
@@ -56,8 +56,10 @@ begin
     TTestClient.Execute( args );
     Readln;
   except
-    on E: Exception do
+    on E: Exception do begin
       Writeln(E.ClassName, ': ', E.Message);
+      ExitCode := $FFFF;
+    end;
   end;
 end.