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/src/TestClient.cc b/test/cpp/src/TestClient.cc
new file mode 100644
index 0000000..6bd06b9
--- /dev/null
+++ b/test/cpp/src/TestClient.cc
@@ -0,0 +1,353 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include "protocol/TBinaryProtocol.h"
+#include "transport/TBufferedTransport.h"
+#include "transport/TSocket.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;
+ int numTests = 1;
+
+ if (argc > 1) {
+ host = argv[1];
+ }
+ if (argc > 2) {
+ port = atoi(argv[2]);
+ }
+ if (argc > 3) {
+ numTests = atoi(argv[3]);
+ }
+
+ TSocket socket(host, port);
+ TBufferedTransport bufferedSocket(&socket, 2048);
+ TBinaryProtocol 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);
+ try {
+ bufferedSocket.open();
+ } catch (TTransportException& ttx) {
+ printf("Connect failed: %s\n", ttx.getMessage().c_str());
+ continue;
+ }
+
+ uint64_t start = now();
+
+ /**
+ * VOID TEST
+ */
+ printf("testVoid()");
+ testClient.testVoid();
+ printf(" = void\n");
+
+ /**
+ * STRING TEST
+ */
+ printf("testString(\"Test\")");
+ string s = testClient.testString("Test");
+ printf(" = \"%s\"\n", s.c_str());
+
+ /**
+ * BYTE TEST
+ */
+ printf("testByte(1)");
+ uint8_t u8 = testClient.testByte(1);
+ printf(" = %d\n", (int)u8);
+
+ /**
+ * U32 TEST
+ */
+ printf("testU32(1)");
+ uint32_t u32 = testClient.testU32(1);
+ printf(" = %u\n", u32);
+
+ /**
+ * I32 TEST
+ */
+ printf("testI32(-1)");
+ int32_t i32 = testClient.testI32(-1);
+ printf(" = %d\n", i32);
+
+ /**
+ * U64 TEST
+ */
+ printf("testU64(34359738368)");
+ uint64_t u64 = testClient.testU64(34359738368);
+ printf(" = %lu\n", u64);
+
+ /**
+ * I64 TEST
+ */
+ printf("testI64(-34359738368)");
+ int64_t i64 = testClient.testI64(-34359738368);
+ printf(" = %ld\n", i64);
+
+ /**
+ * STRUCT TEST
+ */
+ printf("testStruct({\"Zero\", 1, 2, -3, 4, -5})");
+ Xtruct out;
+ out.string_thing = "Zero";
+ out.byte_thing = 1;
+ out.u32_thing = 2;
+ out.i32_thing = -3;
+ out.u64_thing = 4;
+ out.i64_thing = -5;
+ Xtruct in = testClient.testStruct(out);
+ printf(" = {\"%s\", %d, %u, %d, %lu, %ld}\n",
+ in.string_thing.c_str(),
+ (int)in.byte_thing,
+ in.u32_thing,
+ in.i32_thing,
+ in.u64_thing,
+ in.i64_thing);
+
+ /**
+ * NESTED STRUCT TEST
+ */
+ printf("testNest({1, {\"Zero\", 1, 2, -3, 4, -5}), 5}");
+ Xtruct2 out2;
+ out2.byte_thing = 1;
+ out2.struct_thing = out;
+ out2.i32_thing = 5;
+ Xtruct2 in2 = testClient.testNest(out2);
+ in = in2.struct_thing;
+ printf(" = {%d, {\"%s\", %d, %u, %d, %lu, %ld}, %d}\n",
+ in2.byte_thing,
+ in.string_thing.c_str(),
+ (int)in.byte_thing,
+ in.u32_thing,
+ in.i32_thing,
+ in.u64_thing,
+ in.i64_thing,
+ in2.i32_thing);
+
+ /**
+ * MAP TEST
+ */
+ map<int32_t,int32_t> mapout;
+ for (int32_t i = 0; i < 5; ++i) {
+ mapout.insert(make_pair(i, i-10));
+ }
+ printf("testMap({");
+ map<int32_t, int32_t>::const_iterator m_iter;
+ bool first = true;
+ for (m_iter = mapout.begin(); m_iter != mapout.end(); ++m_iter) {
+ if (first) {
+ first = false;
+ } else {
+ printf(", ");
+ }
+ printf("%d => %d", m_iter->first, m_iter->second);
+ }
+ printf("})");
+ map<int32_t,int32_t> mapin = testClient.testMap(mapout);
+ printf(" = {");
+ first = true;
+ for (m_iter = mapin.begin(); m_iter != mapin.end(); ++m_iter) {
+ if (first) {
+ first = false;
+ } else {
+ printf(", ");
+ }
+ printf("%d => %d", m_iter->first, m_iter->second);
+ }
+ printf("}\n");
+
+ /**
+ * SET TEST
+ */
+ set<int32_t> setout;
+ for (int32_t i = -2; i < 3; ++i) {
+ setout.insert(i);
+ }
+ printf("testSet({");
+ set<int32_t>::const_iterator s_iter;
+ first = true;
+ for (s_iter = setout.begin(); s_iter != setout.end(); ++s_iter) {
+ if (first) {
+ first = false;
+ } else {
+ printf(", ");
+ }
+ printf("%d", *s_iter);
+ }
+ printf("})");
+ set<int32_t> setin = testClient.testSet(setout);
+ printf(" = {");
+ first = true;
+ for (s_iter = setin.begin(); s_iter != setin.end(); ++s_iter) {
+ if (first) {
+ first = false;
+ } else {
+ printf(", ");
+ }
+ printf("%d", *s_iter);
+ }
+ printf("}\n");
+
+ /**
+ * LIST TEST
+ */
+ list<int32_t> listout;
+ for (int32_t i = -2; i < 3; ++i) {
+ listout.push_back(i);
+ }
+ printf("testList({");
+ list<int32_t>::const_iterator l_iter;
+ first = true;
+ for (l_iter = listout.begin(); l_iter != listout.end(); ++l_iter) {
+ if (first) {
+ first = false;
+ } else {
+ printf(", ");
+ }
+ printf("%d", *l_iter);
+ }
+ printf("})");
+ list<int32_t> listin = testClient.testList(listout);
+ printf(" = {");
+ first = true;
+ for (l_iter = listin.begin(); l_iter != listin.end(); ++l_iter) {
+ if (first) {
+ first = false;
+ } else {
+ printf(", ");
+ }
+ printf("%d", *l_iter);
+ }
+ printf("}\n");
+
+ /**
+ * ENUM TEST
+ */
+ printf("testEnum(ONE)");
+ Numberz ret = testClient.testEnum(ONE);
+ printf(" = %d\n", ret);
+
+ printf("testEnum(TWO)");
+ ret = testClient.testEnum(TWO);
+ printf(" = %d\n", ret);
+
+ printf("testEnum(THREE)");
+ ret = testClient.testEnum(THREE);
+ printf(" = %d\n", ret);
+
+ printf("testEnum(FIVE)");
+ ret = testClient.testEnum(FIVE);
+ printf(" = %d\n", ret);
+
+ printf("testEnum(EIGHT)");
+ ret = testClient.testEnum(EIGHT);
+ printf(" = %d\n", ret);
+
+ /**
+ * TYPEDEF TEST
+ */
+ printf("testTypedef(309858235082523)");
+ UserId uid = testClient.testTypedef(309858235082523);
+ printf(" = %lu\n", uid);
+
+ /**
+ * NESTED MAP TEST
+ */
+ printf("testMapMap(1)");
+ map<int32_t, map<int32_t, int32_t> > mm = testClient.testMapMap(1);
+ printf(" = {");
+ map<int32_t, map<int32_t, int32_t> >::const_iterator mi;
+ for (mi = mm.begin(); mi != mm.end(); ++mi) {
+ printf("%d => {", mi->first);
+ map<int32_t, int32_t>::const_iterator mi2;
+ for (mi2 = mi->second.begin(); mi2 != mi->second.end(); ++mi2) {
+ printf("%d => %d, ", mi2->first, mi2->second);
+ }
+ printf("}, ");
+ }
+ printf("}\n");
+
+ /**
+ * INSANITY TEST
+ */
+ Insanity insane;
+ insane.userMap.insert(make_pair(FIVE, 5000));
+ Xtruct truck;
+ truck.string_thing = "Truck";
+ truck.byte_thing = 8;
+ truck.u32_thing = 8;
+ truck.i32_thing = 8;
+ truck.u64_thing = 8;
+ truck.i64_thing = 8;
+ insane.xtructs.push_back(truck);
+ printf("testInsanity()");
+ map<UserId, map<Numberz,Insanity> > whoa = testClient.testInsanity(insane);
+ printf(" = {");
+ map<UserId, map<Numberz,Insanity> >::const_iterator i_iter;
+ for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) {
+ printf("%lu => {", i_iter->first);
+ map<Numberz,Insanity>::const_iterator i2_iter;
+ for (i2_iter = i_iter->second.begin();
+ i2_iter != i_iter->second.end();
+ ++i2_iter) {
+ printf("%d => {", i2_iter->first);
+ map<Numberz, UserId> userMap = i2_iter->second.userMap;
+ map<Numberz, UserId>::const_iterator um;
+ printf("{");
+ for (um = userMap.begin(); um != userMap.end(); ++um) {
+ printf("%d => %lu, ", um->first, um->second);
+ }
+ printf("}, ");
+
+ list<Xtruct> xtructs = i2_iter->second.xtructs;
+ list<Xtruct>::const_iterator x;
+ printf("{");
+ for (x = xtructs.begin(); x != xtructs.end(); ++x) {
+ printf("{\"%s\", %d, %u, %d, %lu, %ld}, ",
+ x->string_thing.c_str(),
+ (int)x->byte_thing,
+ x->u32_thing,
+ x->i32_thing,
+ x->u64_thing,
+ x->i64_thing);
+ }
+ printf("}");
+
+ printf("}, ");
+ }
+ printf("}, ");
+ }
+ printf("}\n");
+
+ 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/src/TestServer.cc b/test/cpp/src/TestServer.cc
new file mode 100644
index 0000000..206b6f5
--- /dev/null
+++ b/test/cpp/src/TestServer.cc
@@ -0,0 +1,254 @@
+#include <stdio.h>
+#include "protocol/TBinaryProtocol.h"
+#include "server/TSimpleServer.h"
+#include "transport/TServerSocket.h"
+#include "ThriftTest.h"
+using namespace std;
+
+class TestServer : public ThriftTestServerIf {
+ public:
+ TestServer(TProtocol* protocol) :
+ ThriftTestServerIf(protocol) {}
+
+ void testVoid() {
+ printf("testVoid()\n");
+ }
+
+ string testString(string thing) {
+ printf("testString(\"%s\")\n", thing.c_str());
+ return thing;
+ }
+
+ uint8_t testByte(uint8_t thing) {
+ printf("testByte(%d)\n", (int)thing);
+ return thing;
+ }
+
+ uint32_t testU32(uint32_t thing) {
+ printf("testU32(%u)\n", thing);
+ return thing;
+ }
+
+ int32_t testI32(int32_t thing) {
+ printf("testI32(%d)\n", thing);
+ return thing;
+ }
+
+ uint64_t testU64(uint64_t thing) {
+ printf("testU64(%lu)\n", thing);
+ return thing;
+ }
+
+ int64_t testI64(int64_t thing) {
+ printf("testI64(%ld)\n", thing);
+ return thing;
+ }
+
+ Xtruct testStruct(Xtruct thing) {
+ printf("testStruct({\"%s\", %d, %u, %d, %lu, %ld})\n",
+ thing.string_thing.c_str(),
+ (int)thing.byte_thing,
+ thing.u32_thing,
+ thing.i32_thing,
+ thing.u64_thing,
+ thing.i64_thing);
+ return thing;
+ }
+
+ Xtruct2 testNest(Xtruct2 nest) {
+ Xtruct thing = nest.struct_thing;
+ printf("testNest({%d, {\"%s\", %d, %u, %d, %lu, %ld}, %d})\n",
+ (int)nest.byte_thing,
+ thing.string_thing.c_str(),
+ (int)thing.byte_thing,
+ thing.u32_thing,
+ thing.i32_thing,
+ thing.u64_thing,
+ thing.i64_thing,
+ nest.i32_thing);
+ return nest;
+ }
+
+ map<int32_t, int32_t> testMap(map<int32_t, int32_t> thing) {
+ printf("testMap({");
+ map<int32_t, int32_t>::const_iterator m_iter;
+ bool first = true;
+ for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
+ if (first) {
+ first = false;
+ } else {
+ printf(", ");
+ }
+ printf("%d => %d", m_iter->first, m_iter->second);
+ }
+ printf("})\n");
+ return thing;
+ }
+
+ set<int32_t> testSet(set<int32_t> thing) {
+ printf("testSet({");
+ set<int32_t>::const_iterator s_iter;
+ bool first = true;
+ for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
+ if (first) {
+ first = false;
+ } else {
+ printf(", ");
+ }
+ printf("%d", *s_iter);
+ }
+ printf("})\n");
+ return thing;
+ }
+
+ list<int32_t> testList(list<int32_t> thing) {
+ printf("testList({");
+ list<int32_t>::const_iterator l_iter;
+ bool first = true;
+ for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
+ if (first) {
+ first = false;
+ } else {
+ printf(", ");
+ }
+ printf("%d", *l_iter);
+ }
+ printf("})\n");
+ return thing;
+ }
+
+ Numberz testEnum(Numberz thing) {
+ printf("testEnum(%d)\n", thing);
+ return thing;
+ }
+
+ UserId testTypedef(UserId thing) {
+ printf("testTypedef(%lu)\n", thing);
+ return thing;
+ }
+
+ map<int32_t, map<int32_t,int32_t> > testMapMap(int32_t hello) {
+ printf("testMapMap(%d)\n", hello);
+ map<int32_t, map<int32_t,int32_t> > mapmap;
+
+ map<int32_t,int32_t> pos;
+ map<int32_t,int32_t> neg;
+ for (int i = 1; i < 5; i++) {
+ pos.insert(make_pair(i,i));
+ neg.insert(make_pair(-i,-i));
+ }
+
+ mapmap.insert(make_pair(4, pos));
+ mapmap.insert(make_pair(-4, neg));
+
+ return mapmap;
+ }
+
+ map<UserId, map<Numberz,Insanity> > testInsanity(Insanity argument) {
+ printf("testInsanity()\n");
+
+ Xtruct hello;
+ hello.string_thing = "Hello2";
+ hello.byte_thing = 2;
+ hello.u32_thing = 2;
+ hello.i32_thing = 2;
+ hello.u64_thing = 2;
+ hello.i64_thing = 2;
+
+ Xtruct goodbye;
+ goodbye.string_thing = "Goodbye4";
+ goodbye.byte_thing = 4;
+ goodbye.u32_thing = 4;
+ goodbye.i32_thing = 4;
+ goodbye.u64_thing = 4;
+ goodbye.i64_thing = 4;
+
+ Insanity crazy;
+ crazy.userMap.insert(make_pair(EIGHT, 8));
+ crazy.xtructs.push_back(goodbye);
+
+ Insanity looney;
+ crazy.userMap.insert(make_pair(FIVE, 5));
+ crazy.xtructs.push_back(hello);
+
+ map<Numberz, Insanity> first_map;
+ map<Numberz, Insanity> second_map;
+
+ first_map.insert(make_pair(TWO, crazy));
+ first_map.insert(make_pair(THREE, crazy));
+
+ second_map.insert(make_pair(SIX, looney));
+
+ map<UserId, map<Numberz,Insanity> > insane;
+ insane.insert(make_pair(1, first_map));
+ insane.insert(make_pair(2, second_map));
+
+ printf("return");
+ printf(" = {");
+ map<UserId, map<Numberz,Insanity> >::const_iterator i_iter;
+ for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
+ printf("%lu => {", i_iter->first);
+ map<Numberz,Insanity>::const_iterator i2_iter;
+ for (i2_iter = i_iter->second.begin();
+ i2_iter != i_iter->second.end();
+ ++i2_iter) {
+ printf("%d => {", i2_iter->first);
+ map<Numberz, UserId> userMap = i2_iter->second.userMap;
+ map<Numberz, UserId>::const_iterator um;
+ printf("{");
+ for (um = userMap.begin(); um != userMap.end(); ++um) {
+ printf("%d => %lu, ", um->first, um->second);
+ }
+ printf("}, ");
+
+ list<Xtruct> xtructs = i2_iter->second.xtructs;
+ list<Xtruct>::const_iterator x;
+ printf("{");
+ for (x = xtructs.begin(); x != xtructs.end(); ++x) {
+ printf("{\"%s\", %d, %u, %d, %lu, %ld}, ",
+ x->string_thing.c_str(),
+ (int)x->byte_thing,
+ x->u32_thing,
+ x->i32_thing,
+ x->u64_thing,
+ x->i64_thing);
+ }
+ printf("}");
+
+ printf("}, ");
+ }
+ printf("}, ");
+ }
+ printf("}\n");
+
+ return insane;
+ }
+
+};
+
+int main(int argc, char **argv) {
+ int port = 9090;
+ if (argc > 1) {
+ port = atoi(argv[1]);
+ }
+
+ // Dispatcher
+ TBinaryProtocol binaryProtocol;
+ TestServer testServer(&binaryProtocol);
+
+ // Options
+ TServerOptions serverOptions;
+
+ // Transport
+ TServerSocket serverSocket(port);
+
+ // Server
+ TSimpleServer simpleServer(&testServer,
+ &serverOptions,
+ &serverSocket);
+
+ printf("Starting the server on port %d...\n", port);
+ simpleServer.run();
+ printf("done.\n");
+ return 0;
+}