Fix broken thrift test code for new model


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664839 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/test/ThriftTest.thrift b/test/ThriftTest.thrift
index 414dcea..f5642ac 100644
--- a/test/ThriftTest.thrift
+++ b/test/ThriftTest.thrift
@@ -1,4 +1,5 @@
-namespace thrift.test
+java_package thrift.test
+cpp_namespace thrift.test
 
 enum Numberz
 {
@@ -14,61 +15,61 @@
 
 struct Xtruct
 {
-  string string_thing = 1,
-  byte   byte_thing = 4,
-  i32    i32_thing = 9,
-  i64    i64_thing = 11
+  1:  string string_thing,
+  4:  byte   byte_thing,
+  9:  i32    i32_thing,
+  11: i64    i64_thing
 }
 
 struct Xtruct2
 {
-  byte   byte_thing,
-  Xtruct struct_thing,
-  i32    i32_thing
+  1: byte   byte_thing,
+  2: Xtruct struct_thing,
+  3: i32    i32_thing
 }
 
 struct Insanity
 {
-  map<Numberz, UserId> userMap = 0,
-  list<Xtruct> xtructs = 1
+  1: map<Numberz, UserId> userMap,
+  2: list<Xtruct> xtructs
 }
 
 exception Xception {
-    i32 errorCode,
-    string message
+  1: i32 errorCode,
+  2: string message
 }
 
 exception Xception2 {
-    i32 errorCode,
-    Xtruct struct_thing
+  1: i32 errorCode,
+  2: Xtruct struct_thing
 }
  
 struct EmptyStruct {}
 
 struct OneField {
-   EmptyStruct field
+  1: EmptyStruct field
 }
 
 service ThriftTest
 {
   void         testVoid(),
-  string       testString(string thing = 1),
-  byte         testByte(byte thing = 1),
-  i32          testI32(i32 thing = 1),
-  i64          testI64(i64 thing = 1),
-  double       testDouble(double 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),
+  string       testString(1: string thing),
+  byte         testByte(1: byte thing),
+  i32          testI32(1: i32 thing),
+  i64          testI64(1: i64 thing),
+  double       testDouble(1: double thing),
+  Xtruct       testStruct(1: Xtruct thing),
+  Xtruct2      testNest(1: Xtruct2 thing),
+  map<i32,i32> testMap(1: map<i32,i32> thing),
+  set<i32>     testSet(1: set<i32> thing),
+  list<i32>    testList(1: list<i32> thing),
+  Numberz      testEnum(1: Numberz thing),
+  UserId       testTypedef(1: UserId thing),
 
-  map<i32,map<i32,i32>> testMapMap(i32 hello = 1),
+  map<i32,map<i32,i32>> testMapMap(1: i32 hello),
 
   /* So you think you've got this all worked, out eh? */
-  map<UserId, map<Numberz,Insanity>> testInsanity(Insanity argument = 1),
+  map<UserId, map<Numberz,Insanity>> testInsanity(1: Insanity argument),
 
   /* Multiple parameters */
   
@@ -80,7 +81,7 @@
 
   /* Multiple exceptions specifier */
 
-  Xtruct testMultiException(string arg0, string arg1) throws(Xception err1=1, Xception2 err2)
+  Xtruct testMultiException(string arg0, string arg1) throws(Xception err1, Xception2 err2)
 }
 
 service SecondService
diff --git a/test/cpp/src/TestClient.cpp b/test/cpp/src/TestClient.cpp
index 83e8f79..e508b0f 100644
--- a/test/cpp/src/TestClient.cpp
+++ b/test/cpp/src/TestClient.cpp
@@ -2,7 +2,7 @@
 #include <unistd.h>
 #include <sys/time.h>
 #include <protocol/TBinaryProtocol.h>
-#include <transport/TBufferedTransport.h>
+#include <transport/TTransportUtils.h>
 #include <transport/TSocket.h>
 
 #include <boost/shared_ptr.hpp>
@@ -33,21 +33,50 @@
   string host = "localhost";
   int port = 9090;
   int numTests = 1;
+  bool framed = false;
+  bool frameInput = true;
 
-  if (argc > 1) {
-    host = argv[1];
+  for (int i = 0; i < argc; ++i) {
+    if (strcmp(argv[i], "-h") == 0) {
+      char* pch = strtok(argv[++i], ":");
+      if (pch != NULL) {
+        host = string(pch);
+      }
+      pch = strtok(NULL, ":");
+      if (pch != NULL) {
+        port = atoi(pch);
+      }
+    } else if (strcmp(argv[i], "-n") == 0) {
+      numTests = atoi(argv[++i]);
+    } else if (strcmp(argv[i], "-f") == 0) {
+      framed = true;
+    } else if (strcmp(argv[i], "-fo") == 0) {
+      framed = true;
+      frameInput = false;
+    }
   }
-  if (argc > 2) {
-    port = atoi(argv[2]);
-  }
-  if (argc > 3) {
-    numTests = atoi(argv[3]);
-  }
+
+
+  shared_ptr<TTransport> transport;
 
   shared_ptr<TSocket> socket(new TSocket(host, port));
-  shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket));
-  shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol());
-  ThriftTestClient testClient(bufferedSocket, binaryProtocol);
+  
+  if (framed) {
+    shared_ptr<TFramedTransport> framedSocket(new TFramedTransport(socket));
+    framedSocket->setRead(frameInput);
+    transport = framedSocket;
+    if (frameInput) {
+      printf("Using bi-directional framed transport mode\n");
+    } else {
+      printf("Using framed output only mode\n");
+    }
+  } else {
+    shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket));
+    transport = bufferedSocket;
+  }
+
+  shared_ptr<TBinaryProtocol> protocol(new TBinaryProtocol(transport, transport));
+  ThriftTestClient testClient(protocol);
 
   uint64_t time_min = 0;
   uint64_t time_max = 0;
@@ -57,7 +86,7 @@
   for (test = 0; test < numTests; ++test) {
 
     try {
-      bufferedSocket->open();
+      transport->open();
     } catch (TTransportException& ttx) {
       printf("Connect failed: %s\n", ttx.getMessage().c_str());
       continue;
@@ -396,7 +425,7 @@
       time_max = tot;
     }
 
-    bufferedSocket->close();
+    transport->close();
   }
 
   //  printf("\nSocket syscalls: %u", g_socket_syscalls);
diff --git a/test/cpp/src/TestServer.cpp b/test/cpp/src/TestServer.cpp
index 34c9dfb..28a40fd 100644
--- a/test/cpp/src/TestServer.cpp
+++ b/test/cpp/src/TestServer.cpp
@@ -5,7 +5,7 @@
 #include <server/TThreadPoolServer.h>
 #include <server/TNonblockingServer.h>
 #include <transport/TServerSocket.h>
-#include <transport/TBufferedTransportFactory.h>
+#include <transport/TTransportUtils.h>
 #include "ThriftTest.h"
 
 #include <iostream>
@@ -264,13 +264,14 @@
   string serverType = "simple";
   string protocolType = "binary";
   size_t workerCount = 4;
+  bool frameOutput = true;
 
   ostringstream usage;
 
   usage <<
     argv[0] << " [--port=<port number>] [--server-type=<server-type>] [--protocol-type=<protocol-type>] [--workers=<worker-count>]" << endl <<
 
-    "\t\tserver-type\t\ttype of server, \"simple\" or \"thread-pool\".  Default is " << serverType << endl <<
+    "\t\tserver-type\t\ttype of server, \"simple\", \"thread-pool\", or \"nonblocking\".  Default is " << serverType << endl <<
 
     "\t\tprotocol-type\t\ttype of protocol, \"binary\", \"ascii\", or \"xml\".  Default is " << protocolType << endl <<
 
@@ -285,9 +286,8 @@
       if (end != string::npos) {
 	args[string(arg, 2, end - 2)] = string(arg, end + 1);
       } else {
-	args[string(arg, 2, end - 2)] = "true";
+	args[string(arg, 2)] = "true";
       }
-      ix++;
     } else {
       throw invalid_argument("Unexcepted command line token: "+arg);
     }
@@ -299,6 +299,10 @@
       port = atoi(args["port"].c_str());
     }
 
+    if (!args["noframe"].empty()) {
+      frameOutput = false;
+    }
+
     if (!args["server-type"].empty()) {
       serverType = args["server-type"];     
       if (serverType == "simple") {
@@ -330,11 +334,11 @@
   }
 
   // Dispatcher
-  shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol);
+  shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
 
   shared_ptr<TestHandler> testHandler(new TestHandler());
 
-  shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler, binaryProtocol));
+  shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
 
   // Transport
   shared_ptr<TServerSocket> serverSocket(new TServerSocket(port));
@@ -342,17 +346,13 @@
   // Factory
   shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
 
-  // Options
-  shared_ptr<TServerOptions> serverOptions(new TServerOptions());
-
   if (serverType == "simple") {
 
     // Server
     TSimpleServer simpleServer(testProcessor,
 			       serverSocket,
                                transportFactory,
-			       serverOptions
-                               );
+                               protocolFactory);
 
     printf("Starting the server on port %d...\n", port);
     simpleServer.serve();
@@ -372,17 +372,22 @@
     TThreadPoolServer threadPoolServer(testProcessor,
 				       serverSocket,
                                        transportFactory,
-				       threadManager,
-				       serverOptions);
+                                       protocolFactory,
+				       threadManager);
 
     printf("Starting the server on port %d...\n", port);
     threadPoolServer.serve();
 
   } else if (serverType == "nonblocking") {
 
-    TNonblockingServer nonblockingServer(testProcessor,
-                                         serverOptions,
-                                         port);
+    TNonblockingServer nonblockingServer(testProcessor, port);
+    nonblockingServer.setFrameResponses(frameOutput);
+    if (frameOutput) {
+      printf("Using framed output mode\n");
+    } else {
+      printf("Using non-framed output mode\n");
+    }
+
     printf("Starting the nonblocking server on port %d...\n", port);
     nonblockingServer.serve();
 
diff --git a/test/java/src/TestClient.java b/test/java/src/TestClient.java
index 46a4ca5..546265c 100644
--- a/test/java/src/TestClient.java
+++ b/test/java/src/TestClient.java
@@ -6,6 +6,7 @@
 import com.facebook.thrift.transport.TTransport;
 import com.facebook.thrift.transport.TSocket;
 import com.facebook.thrift.transport.THttpClient;
+import com.facebook.thrift.transport.TFramedTransport;
 import com.facebook.thrift.transport.TTransportException;
 import com.facebook.thrift.protocol.TBinaryProtocol;
 
@@ -27,20 +28,24 @@
       int port = 9090;
       String url = null;
       int numTests = 1;
+      boolean framed = false;
+      boolean framedInput = true;
+      boolean framedOutput = true;
 
       try {
         for (int i = 0; i < args.length; ++i) {
           if (args[i].equals("-h")) {
-            String[] hostport = (args[++i]).split(";");
+            String[] hostport = (args[++i]).split(":");
             host = hostport[0];
             port = Integer.valueOf(hostport[1]);            
-          }
-
-          if (args[i].equals("-u")) {
+          } else if (args[i].equals("-f") || args[i].equals("-framed")) {
+            framed = true;
+          } else if (args[i].equals("-fo")) {
+            framed = true;
+            framedInput = false;
+          } else if (args[i].equals("-u")) {
             url = args[++i];
-          }
-
-          if (args[i].equals("-n")) {
+          } else if (args[i].equals("-n")) {
             numTests = Integer.valueOf(args[++i]);
           }
         }
@@ -53,7 +58,14 @@
       if (url != null) {
         transport = new THttpClient(url);
       } else {
-        transport = new TSocket(host, port);
+        TSocket socket = new TSocket(host, port);
+        socket.setTimeout(1000);
+        transport = socket;
+        if (framed) {
+          transport = new TFramedTransport(transport,
+                                           framedInput,
+                                           framedOutput);
+        }
       }
 
       TBinaryProtocol binaryProtocol =
diff --git a/test/py/TestClient.py b/test/py/TestClient.py
index 3edff1c..a2fa3c7 100755
--- a/test/py/TestClient.py
+++ b/test/py/TestClient.py
@@ -9,12 +9,49 @@
 from thrift.transport import TSocket
 from thrift.protocol import TBinaryProtocol
 
+import hotshot
+from hotshot import stats
+prof = None
+
+# Uncomment if you want to profile this biznizzy
+#prof = hotshot.Profile('hotshot_thrift_stats')
+#prof.start()
+
 import timing
 
-transport = TTransport.TBufferedTransport(TSocket.TSocket('localhost', 9090))
+host = 'localhost'
+port = 9090
+framed = False
+framedInput = True
+argi = 1
+
+# Parse args
+while argi < len(sys.argv):
+  if sys.argv[argi] == '-h':
+    parts = sys.argv[argi+1].split(':') 
+    host = parts[0]
+    port = int(parts[1])
+    argi += 1
+  elif sys.argv[argi] == '-f' or sys.argv[argi] == '-framed':
+    framed = True
+  elif sys.argv[argi] == '-fo':
+    framed = True
+    framedInput = False
+  argi += 1
+
+# Make socket
+socket = TSocket.TSocket(host, port)
+
+# Frame or buffer depending upon args
+if framed:
+  transport = TTransport.TFramedTransport(socket, framedInput, True)
+else:
+  transport = TTransport.TBufferedTransport(socket)
+
 protocol = TBinaryProtocol.TBinaryProtocol()
 client = ThriftTest.Client(transport, protocol)
 
+# Connect!
 transport.open()
 
 # Start debug timing
@@ -63,4 +100,12 @@
 timing.finish()
 print "Total time: %d microsecs" % timing.micro()
 
+# Close!
 transport.close()
+
+# Profiler output
+if prof != None:
+  prof.stop()
+  prof.close()
+  s = stats.load('hotshot_thrift_stats')
+  s.sort_stats('time').print_stats()