Thrift test code

Summary: Did I promise you this or what?! Interoperable test servers and clients in both C++ and Java that you can use to check that they all work, all perform well, and that they all actually talk to each other!

Problem: How we gon' test this Thrift bizniss?

Solution: Write some test scenarios in each language.

Reviewed By: aditya

Test Plan: This IS the test plan.

Notes: These tools are actually pretty easy to use, so long as you remember to type 'ant' in the java directory instead of 'make'.





git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664716 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/test/cpp/Makefile b/test/cpp/Makefile
index 6afb30b..21c09ec 100644
--- a/test/cpp/Makefile
+++ b/test/cpp/Makefile
@@ -7,28 +7,29 @@
 target: all
 
 # Tools
-THRIFT = /usr/local/bin/thrift
+THRIFT = thrift
 CC     = g++
 LD     = g++
 
 # 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 -g -I../../lib/cpp $(LIBS)
-CFL   = -Wall -g -lthrift -I../../lib/cpp
+CFL   = -Wall -O3 -Igen-cpp -I../../lib/cpp $(LIBS)
+CFL   = -Wall -O3 -Igen-cpp -I../../lib/cpp -lthrift
 
 all: server client
 
 stubs: ../ThriftTest.thrift
-	$(THRIFT) ../ThriftTest.thrift
+	$(THRIFT) -cpp ../ThriftTest.thrift
 
 server: stubs
-	g++ -o TestServer $(CFL) TestServer.cc gen-cpp/ThriftTest.cc
+	g++ -o TestServer $(CFL) src/TestServer.cc gen-cpp/ThriftTest.cc
 
 client: stubs
-	g++ -o TestClient $(CFL) TestClient.cc gen-cpp/ThriftTest.cc
+	g++ -o TestClient $(CFL) src/TestClient.cc gen-cpp/ThriftTest.cc
 
 clean:
 	rm -fr TestServer TestClient gen-cpp
diff --git a/test/cpp/TestClient.cc b/test/cpp/src/TestClient.cc
similarity index 90%
rename from test/cpp/TestClient.cc
rename to test/cpp/src/TestClient.cc
index 6d2a68e..6bd06b9 100644
--- a/test/cpp/TestClient.cc
+++ b/test/cpp/src/TestClient.cc
@@ -1,11 +1,26 @@
 #include <stdio.h>
 #include <unistd.h>
+#include <sys/time.h>
 #include "protocol/TBinaryProtocol.h"
-#include "client/TSimpleClient.h"
+#include "transport/TBufferedTransport.h"
 #include "transport/TSocket.h"
-#include "gen-cpp/ThriftTest.h"
+#include "ThriftTest.h"
 using namespace std;
 
+extern uint32_t g_socket_syscalls;
+
+// Current time, microseconds since the epoch
+uint64_t now()
+{
+  long long ret;
+  struct timeval tv;
+  
+  gettimeofday(&tv, NULL);
+  ret = tv.tv_sec;
+  ret = ret*1000*1000 + tv.tv_usec;
+  return ret;
+}
+
 int main(int argc, char** argv) {
   string host = "localhost";
   int port = 9090;
@@ -22,18 +37,25 @@
   }
 
   TSocket socket(host, port);
-  TSimpleClient simpleClient(&socket);
+  TBufferedTransport bufferedSocket(&socket, 2048);
   TBinaryProtocol binaryProtocol;
-  ThriftTestClient testClient(&simpleClient, &binaryProtocol);
+  ThriftTestClient testClient(&bufferedSocket, &binaryProtocol);
   
   int test = 0;
   for (test = 0; test < numTests; ++test) {
+
+    /**
+     * CONNECT TEST
+     */
     printf("Test #%d, connect %s:%d\n", test+1, host.c_str(), port);
-    bool success = simpleClient.open();
-    if (!success) {
-      printf("Connect failed, server down?\n");
+    try {
+      bufferedSocket.open();
+    } catch (TTransportException& ttx) {
+      printf("Connect failed: %s\n", ttx.getMessage().c_str());
       continue;
     }
+
+    uint64_t start = now();
     
     /**
      * VOID TEST
@@ -319,9 +341,13 @@
     }
     printf("}\n");
 
-    simpleClient.close();
+    uint64_t stop = now();
+    printf("Total time: %lu us\n", stop-start);
+
+    bufferedSocket.close();
   }
 
+  printf("\nSocket syscalls: %u", g_socket_syscalls);
   printf("\nAll tests done.\n");
   return 0;
 }
diff --git a/test/cpp/TestServer.cc b/test/cpp/src/TestServer.cc
similarity index 97%
rename from test/cpp/TestServer.cc
rename to test/cpp/src/TestServer.cc
index 8138166..206b6f5 100644
--- a/test/cpp/TestServer.cc
+++ b/test/cpp/src/TestServer.cc
@@ -2,7 +2,7 @@
 #include "protocol/TBinaryProtocol.h"
 #include "server/TSimpleServer.h"
 #include "transport/TServerSocket.h"
-#include "gen-cpp/ThriftTest.h"
+#include "ThriftTest.h"
 using namespace std;
 
 class TestServer : public ThriftTestServerIf {
@@ -117,12 +117,12 @@
     return thing;
   }
 
-  Numberz testEnum(Numberz thing = 0) {
+  Numberz testEnum(Numberz thing) {
     printf("testEnum(%d)\n", thing);
     return thing;
   }
 
-  UserId testTypedef(UserId thing = 0) {
+  UserId testTypedef(UserId thing) {
     printf("testTypedef(%lu)\n", thing);
     return thing;
   }