Thrift test improvements, tests for both inline and normal PHP code gen

Summary: So you can A/B test and see 6ms vs. 4ms response time :)


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664773 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/test/StressTest.thrift b/test/StressTest.thrift
index a1f909c..3cce4eb 100644
--- a/test/StressTest.thrift
+++ b/test/StressTest.thrift
@@ -4,10 +4,10 @@
 
   void echoVoid(),
   byte echoByte(byte arg),
-  u16 echoU16(u16 arg),
-  u32 echoU32(u32 arg),
-  u64 echoU64(u64 arg),
-  i16 echoI16(i16 arg),
+//  u16 echoU16(u16 arg),
+//  u32 echoU32(u32 arg),
+//  u64 echoU64(u64 arg),
+//  i16 echoI16(i16 arg),
   i32 echoI32(i32 arg),
   i64 echoI64(i64 arg),
   string echoString(string arg),
diff --git a/test/ThriftTest.thrift b/test/ThriftTest.thrift
index 50021ba..c90a507 100644
--- a/test/ThriftTest.thrift
+++ b/test/ThriftTest.thrift
@@ -14,10 +14,10 @@
 
 struct Xtruct
 {
-  string string_thing = 0,
-  byte   byte_thing = 1,
-  i32    i32_thing = 3,
-  i64    i64_thing = 5
+  string string_thing = 1,
+  byte   byte_thing = 4,
+  i32    i32_thing = 9,
+  i64    i64_thing = 11
 }
 
 struct Xtruct2
@@ -34,43 +34,44 @@
 }
 
 exception Xception {
-    u32 errorCode,
+    i32 errorCode,
     string message
 }
 
 exception Xception2 {
-    u32 errorCode,
+    i32 errorCode,
     Xtruct struct_thing
 }
  
-struct Empty {}
+struct EmptyStruct {}
+
 struct OneField {
-   Empty field
+   EmptyStruct field
 }
 
 service ThriftTest
 {
   void         testVoid(),
-  string       testString(string thing = 0),
-  byte         testByte(byte thing = 0),
-  i32          testI32(i32 thing = 0),
-  i64          testI64(i64 thing = 0),
-  Xtruct       testStruct(Xtruct thing = 0),
-  Xtruct2      testNest(Xtruct2 thing = 0),
-  map<i32,i32> testMap(map<i32,i32> thing = 0),
-  set<i32>     testSet(set<i32> thing = 0),
-  list<i32>    testList(list<i32> thing = 0),
-  Numberz      testEnum(Numberz thing = 0),
-  UserId       testTypedef(UserId thing = 0),
+  string       testString(string thing = 1),
+  byte         testByte(byte thing = 1),
+  i32          testI32(i32 thing = 1),
+  i64          testI64(i64 thing = 1),
+  Xtruct       testStruct(Xtruct thing = 1),
+  Xtruct2      testNest(Xtruct2 thing = 1),
+  map<i32,i32> testMap(map<i32,i32> thing = 1),
+  set<i32>     testSet(set<i32> thing = 1),
+  list<i32>    testList(list<i32> thing = 1),
+  Numberz      testEnum(Numberz thing = 1),
+  UserId       testTypedef(UserId thing = 1),
 
-  map<i32,map<i32,i32>> testMapMap(i32 hello = 0),
+  map<i32,map<i32,i32>> testMapMap(i32 hello = 1),
 
   /* So you think you've got this all worked, out eh? */
-  map<UserId, map<Numberz,Insanity>> testInsanity(Insanity argument = 0),
+  map<UserId, map<Numberz,Insanity>> testInsanity(Insanity argument = 1),
 
   /* Multiple parameters */
   
-  Xtruct	testMulti(byte arg0, i32 arg1, u64 arg2, map<i16, string> arg3, Numberz arg4, UserId arg5),
+  Xtruct	testMulti(byte arg0, i32 arg1, i64 arg2, map<i16, string> arg3, Numberz arg4, UserId arg5),
 
   /* Exception specifier */
 
diff --git a/test/cpp/Makefile.thrift b/test/cpp/Makefile.thrift
index f95401c..ddd6b2e 100644
--- a/test/cpp/Makefile.thrift
+++ b/test/cpp/Makefile.thrift
@@ -6,14 +6,15 @@
 # Default target is everything
 
 ifndef thrift_home
-thrift_home=../../build
+thrift_home=/usr/local
 endif #thrift_home
 
 target: all
 
 ifndef boost_home
 boost_home=../../../../../thirdparty/boost_1_33_1
-endif #thrift_home
+boost_home=/usr/local/include/boost-1_33_1
+endif #boost_home
 target: all
 
 include_paths = $(thrift_home)/include/thrift \
@@ -30,8 +31,8 @@
 LD     = g++
 
 # Compiler flags
-DCFL  = -Wall -O3 -g -I../cpp-gen $(include_flags) -L$(thrift_home)/lib -lthrift
-CFL   = -Wall -O3 -I../cpp-gen $(include_flags) -L$(thrift_home)/lib -lthrift
+DCFL  = -Wall -O3 -g -I./gen-cpp $(include_flags) -L$(thrift_home)/lib -lthrift
+CFL   = -Wall -O3 -I./gen-cpp $(include_flags) -L$(thrift_home)/lib -lthrift
 
 all: server client
 
@@ -41,16 +42,16 @@
 	$(THRIFT) --cpp ../ThriftTest.thrift
 
 server-debug: stubs
-	g++ -o TestServer $(DCFL) src/TestServer.cc ../cpp-gen/ThriftTest.cc
+	g++ -o TestServer $(DCFL) src/TestServer.cc ./gen-cpp/ThriftTest.cc
 
 client-debug: stubs
-	g++ -o TestClient $(DCFL) src/TestClient.cc ../cpp-gen/ThriftTest.cc
+	g++ -o TestClient $(DCFL) src/TestClient.cc ./gen-cpp/ThriftTest.cc
 
 server: stubs
-	g++ -o TestServer $(CFL) src/TestServer.cc ../cpp-gen/ThriftTest.cc
+	g++ -o TestServer $(CFL) src/TestServer.cc ./gen-cpp/ThriftTest.cc
 
 client: stubs
-	g++ -o TestClient $(CFL) src/TestClient.cc ../cpp-gen/ThriftTest.cc
+	g++ -o TestClient $(CFL) src/TestClient.cc ./gen-cpp/ThriftTest.cc
 
 clean:
 	rm -fr TestServer TestClient ../cpp-gen
diff --git a/test/cpp/src/TestClient.cc b/test/cpp/src/TestClient.cc
index 2e2f61b..d340e26 100644
--- a/test/cpp/src/TestClient.cc
+++ b/test/cpp/src/TestClient.cc
@@ -98,7 +98,7 @@
      */
     printf("testI64(-34359738368)");
     int64_t i64 = testClient.testI64(-34359738368LL);
-    printf(" = %lld\n", i64);
+    printf(" = %ld\n", i64);
     
     /**
      * STRUCT TEST
@@ -110,7 +110,7 @@
     out.i32_thing = -3;
     out.i64_thing = -5;
     Xtruct in = testClient.testStruct(out);
-    printf(" = {\"%s\", %d, %d, %lld}\n",
+    printf(" = {\"%s\", %d, %d, %ld}\n",
            in.string_thing.c_str(),
            (int)in.byte_thing,
            in.i32_thing,
@@ -126,7 +126,7 @@
     out2.i32_thing = 5;
     Xtruct2 in2 = testClient.testNest(out2);
     in = in2.struct_thing;
-    printf(" = {%d, {\"%s\", %d, %d, %lld}, %d}\n",
+    printf(" = {%d, {\"%s\", %d, %d, %ld}, %d}\n",
            in2.byte_thing,
            in.string_thing.c_str(),
            (int)in.byte_thing,
@@ -258,7 +258,7 @@
      */
     printf("testTypedef(309858235082523)");
     UserId uid = testClient.testTypedef(309858235082523LL);
-    printf(" = %lld\n", uid);
+    printf(" = %ld\n", uid);
 
     /**
      * NESTED MAP TEST
@@ -293,7 +293,7 @@
     printf(" = {");
     map<UserId, map<Numberz,Insanity> >::const_iterator i_iter;
     for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) {
-      printf("%lld => {", i_iter->first);
+      printf("%ld => {", i_iter->first);
       map<Numberz,Insanity>::const_iterator i2_iter;
       for (i2_iter = i_iter->second.begin();
            i2_iter != i_iter->second.end();
@@ -303,7 +303,7 @@
         map<Numberz, UserId>::const_iterator um;
         printf("{");
         for (um = userMap.begin(); um != userMap.end(); ++um) {
-          printf("%d => %lld, ", um->first, um->second);
+          printf("%d => %ld, ", um->first, um->second);
         }
         printf("}, ");
 
@@ -311,7 +311,7 @@
         list<Xtruct>::const_iterator x;
         printf("{");
         for (x = xtructs.begin(); x != xtructs.end(); ++x) {
-          printf("{\"%s\", %d, %d, %lld}, ",
+          printf("{\"%s\", %d, %d, %ld}, ",
                  x->string_thing.c_str(),
                  (int)x->byte_thing,
                  x->i32_thing,
@@ -350,7 +350,7 @@
       printf("testClient.testMultiException(\"Xception\", \"test 1\") =>");
       Xtruct result = testClient.testMultiException("Xception", "test 1");
       printf("  result\nFAILURE\n");
-    }  catch(Xception& e) {
+    } catch(Xception& e) {
       printf("  {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
     }
 
@@ -359,7 +359,7 @@
       Xtruct result = testClient.testMultiException("Xception2", "test 2");
       printf("  result\nFAILURE\n");
       
-    }  catch(Xception2& e) {
+    } catch(Xception2& e) {
       printf("  {%u, {\"%s\"}}\n", e.errorCode, e.struct_thing.string_thing.c_str());
     }
     
@@ -372,7 +372,7 @@
     }
     
     uint64_t stop = now();
-    printf("Total time: %llu us\n", stop-start);
+    printf("Total time: %lu us\n", stop-start);
     
     bufferedSocket->close();
   }
diff --git a/test/cpp/src/TestServer.cc b/test/cpp/src/TestServer.cc
index 9e5cf91..baf2ae5 100644
--- a/test/cpp/src/TestServer.cc
+++ b/test/cpp/src/TestServer.cc
@@ -33,7 +33,7 @@
     return thing;
   }
 
-  uint8_t testByte(uint8_t thing) {
+  int8_t testByte(int8_t thing) {
     printf("testByte(%d)\n", (int)thing);
     return thing;
   }
@@ -44,12 +44,12 @@
   }
 
   int64_t testI64(int64_t thing) {
-    printf("testI64(%lld)\n", thing);
+    printf("testI64(%ld)\n", thing);
     return thing;
   }
 
   Xtruct testStruct(Xtruct thing) {
-    printf("testStruct({\"%s\", %d, %d, %lld})\n",
+    printf("testStruct({\"%s\", %d, %d, %ld})\n",
            thing.string_thing.c_str(),
            (int)thing.byte_thing,
            thing.i32_thing,
@@ -59,7 +59,7 @@
 
   Xtruct2 testNest(Xtruct2 nest) {
     Xtruct thing = nest.struct_thing;
-    printf("testNest({%d, {\"%s\", %d, %d, %lld}, %d})\n",
+    printf("testNest({%d, {\"%s\", %d, %d, %ld}, %d})\n",
            (int)nest.byte_thing,
            thing.string_thing.c_str(),
            (int)thing.byte_thing,
@@ -123,7 +123,7 @@
   }
 
   UserId testTypedef(UserId thing) {
-    printf("testTypedef(%lld)\n", thing);
+    printf("testTypedef(%ld)\n", thing);
     return thing;
   }
 
@@ -183,7 +183,7 @@
     printf(" = {");
     map<UserId, map<Numberz,Insanity> >::const_iterator i_iter;
     for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
-      printf("%lld => {", i_iter->first);
+      printf("%ld => {", i_iter->first);
       map<Numberz,Insanity>::const_iterator i2_iter;
       for (i2_iter = i_iter->second.begin();
            i2_iter != i_iter->second.end();
@@ -193,7 +193,7 @@
         map<Numberz, UserId>::const_iterator um;
         printf("{");
         for (um = userMap.begin(); um != userMap.end(); ++um) {
-          printf("%d => %lld, ", um->first, um->second);
+          printf("%d => %ld, ", um->first, um->second);
         }
         printf("}, ");
 
@@ -201,7 +201,7 @@
         list<Xtruct>::const_iterator x;
         printf("{");
         for (x = xtructs.begin(); x != xtructs.end(); ++x) {
-          printf("{\"%s\", %d, %d, %lld}, ",
+          printf("{\"%s\", %d, %d, %ld}, ",
                  x->string_thing.c_str(),
                  (int)x->byte_thing,
                  x->i32_thing,
@@ -218,7 +218,7 @@
     return insane;
   }
 
-  Xtruct testMulti(uint8_t arg0, int32_t arg1, uint64_t arg2, std::map<int16_t, std::string>  arg3, Numberz arg4, UserId arg5) {
+  Xtruct testMulti(int8_t arg0, int32_t arg1, int64_t arg2, std::map<int16_t, std::string>  arg3, Numberz arg4, UserId arg5) {
     printf("testMulti()\n");
     
     Xtruct hello;
@@ -230,9 +230,9 @@
     return hello;
   }
 
-  virtual void testException(std::string arg) throw(struct thrift::test::Xception) {
+  void testException(std::string arg) throw(Xception) {
     printf("testException(%s)\n", arg.c_str());
-    if(arg.compare("Xception") == 0) {
+    if (arg.compare("Xception") == 0) {
       Xception e;
       e.errorCode = 1001;
       e.message = "This is an Xception";
@@ -244,7 +244,7 @@
     }
   }
   
-  virtual struct Xtruct testMultiException(std::string arg0, std::string arg1) throw(struct Xception, struct Xception2) {
+  Xtruct testMultiException(std::string arg0, std::string arg1) throw(Xception, Xception2) {
 
     printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
 
@@ -357,7 +357,7 @@
   // Transport
   shared_ptr<TServerSocket> serverSocket(new TServerSocket(port));
 
-  if(serverType == "simple") {
+  if (serverType == "simple") {
 
     // Server
     TSimpleServer simpleServer(testServer,
@@ -367,7 +367,7 @@
     printf("Starting the server on port %d...\n", port);
     simpleServer.run();
 
-  } else if(serverType == "thread-pool") {
+  } else if (serverType == "thread-pool") {
 
     shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount);
 
diff --git a/test/php/Makefile b/test/php/Makefile
index 2e6bec5..12fd7c8 100644
--- a/test/php/Makefile
+++ b/test/php/Makefile
@@ -9,19 +9,17 @@
 # Tools
 THRIFT = thrift
 
-# Compiler flags
-LIBS  = ../../lib/cpp/server/TSimpleServer.cc \
-	../../lib/cpp/protocol/TBinaryProtocol.cc \
-	../../lib/cpp/transport/TBufferedTransport.cc \
-	../../lib/cpp/transport/TServerSocket.cc \
-	../../lib/cpp/transport/TSocket.cc
-CFL   = -Wall -O3 -Igen-cpp -I../../lib/cpp $(LIBS)
-CFL   = -Wall -O3 -Igen-cpp -I../../lib/cpp -lthrift
+all: normal inline
 
-all: stubs
+normal: stubs
+
+inline: stubs-inline
 
 stubs: ../ThriftTest.thrift
-	$(THRIFT) -php ../ThriftTest.thrift
+	$(THRIFT) --php ../ThriftTest.thrift
+
+stubs-inline: ../ThriftTest.thrift
+	$(THRIFT) --phpi ../ThriftTest.thrift
 
 clean:
-	rm -fr gen-php
+	rm -fr gen-php gen-phpi
diff --git a/test/php/TestClient.php b/test/php/TestClient.php
index 8f765fa..6dd40d7 100644
--- a/test/php/TestClient.php
+++ b/test/php/TestClient.php
@@ -1,19 +1,26 @@
 <?php
 
+if (!isset($GEN_DIR)) {
+  $GEN_DIR = 'gen-php';
+}
+if (!isset($MODE)) {
+  $MODE = 'normal';
+}
+
 /** Include the Thrift base */
 require_once '/home/mcslee/code/projects/thrift/lib/php/src/Thrift.php';
 
 /** Include the binary protocol */
-require_once THRIFT_ROOT.'/protocol/TBinaryProtocol.php';
+require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
 
 /** Include the socket layer */
-require_once THRIFT_ROOT.'/transport/TSocket.php';
+require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
 
 /** Include the socket layer */
-require_once THRIFT_ROOT.'/transport/TBufferedTransport.php';
+require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
 
 /** Include the generated code */
-require_once '/home/mcslee/code/projects/thrift/test/php/gen-php/ThriftTest.php';
+require_once '/home/mcslee/code/projects/thrift/test/php/'.$GEN_DIR.'/ThriftTest.php';
 
 $host = 'localhost';
 $port = 9090;
@@ -27,11 +34,18 @@
 }
 
 $socket = new TSocket($host, $port);
-$bufferedSocket = new TBufferedTransport($socket, 1024, 1024);
-$binary = new TBinaryProtocol();
 
-$testClient = new ThriftTestClient($bufferedSocket, $binary);
-$bufferedSocket->open();
+if ($MODE == 'inline') {
+  $transport = $socket;
+  $testClient = new ThriftTestClient($transport);
+} else {
+  $bufferedSocket = new TBufferedTransport($socket, 1024, 1024);
+  $transport = $bufferedSocket;
+  $protocol = new TBinaryProtocol();
+  $testClient = new ThriftTestClient($transport, $protocol);
+}
+
+$transport->open();
 
 $start = microtime(true);
 
@@ -261,16 +275,20 @@
     print_r("$k2 => {");
     $userMap = $v2->userMap;
     print_r("{");
-    foreach ($userMap as $k3 => $v3) {
-      print_r("$k3 => $v3, ");
+    if (is_array($usermap)) {
+      foreach ($userMap as $k3 => $v3) {
+        print_r("$k3 => $v3, ");
+      }
     }
     print_r("}, ");
     
     $xtructs = $v2->xtructs;
     print_r("{");
-    foreach ($xtructs as $x) {
-      print_r("{\"".$x->string_thing."\", ".
-              $x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, ");
+    if (is_array($xtructs)) {
+      foreach ($xtructs as $x) {
+        print_r("{\"".$x->string_thing."\", ".
+                $x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, ");
+      }
     }
     print_r("}");
     
@@ -280,6 +298,17 @@
 }
 print_r("}\n");
 
+/**
+ * EXCEPTION TEST
+ */
+print_r("testException('Xception')");
+try {
+  $testClient->testException('Xception');
+  print_r("  void\nFAILURE\n");
+} catch (Xception $x) {
+  print_r(' caught xception '.$x->errorCode.': '.$x->message."\n");
+}
+
 
 /**
  * Normal tests done.
@@ -321,7 +350,7 @@
   print "Missed $num = $num2\n";
 }
 
-$bufferedSocket->close();
+$transport->close();
 return;
 
 ?>
diff --git a/test/php/TestInline.php b/test/php/TestInline.php
new file mode 100644
index 0000000..306ece6
--- /dev/null
+++ b/test/php/TestInline.php
@@ -0,0 +1,5 @@
+<?php
+$GEN_DIR = 'gen-phpi';
+$MODE = 'inline';
+include_once('TestClient.php');
+?>