Threading libraries test for Thrift C++


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665041 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/test/threads/Makefile b/test/threads/Makefile
new file mode 100644
index 0000000..79c0134
--- /dev/null
+++ b/test/threads/Makefile
@@ -0,0 +1,46 @@
+# Makefile for Thrift test project.
+# 
+# Author:
+#   Mark Slee <mcslee@facebook.com>
+
+# Default target is everything
+
+ifndef thrift_home
+thrift_home=../../
+endif #thrift_home
+
+target: all
+
+ifndef boost_home
+boost_home=/usr/local/include/boost-1_33_1
+endif #boost_home
+target: all
+
+include_paths = $(thrift_home)/lib/cpp/src \
+		$(boost_home)
+
+include_flags = $(patsubst %,-I%, $(include_paths))
+
+# Tools
+ifndef THRIFT
+THRIFT = ../../compiler/cpp/thrift
+endif # THRIFT
+
+CC     = g++
+LD     = g++
+
+# Compiler flags
+LFL   =  -L$(thrift_home)/lib/cpp/.libs -lthrift
+CCFL  = -Wall -O3 -g -I./gen-cpp $(include_flags)
+CFL   = $(CCFL) $(LFL)
+
+all: server
+
+stubs: ThreadsTest.thrift
+	$(THRIFT) -cpp -py ThreadsTest.thrift
+
+server: stubs
+	g++ -o ThreadsServer $(CFL) ThreadsServer.cpp ./gen-cpp/ThreadsTest.cpp ./gen-cpp/ThreadsTest_types.cpp
+
+clean:
+	rm -fr *.o ThreadsServer gen-cpp
diff --git a/test/threads/ThreadsServer.cpp b/test/threads/ThreadsServer.cpp
new file mode 100644
index 0000000..1e3494c
--- /dev/null
+++ b/test/threads/ThreadsServer.cpp
@@ -0,0 +1,104 @@
+// This autogenerated skeleton file illustrates how to build a server.
+// You should copy it to another filename to avoid overwriting it.
+
+#include "ThreadsTest.h"
+#include <protocol/TBinaryProtocol.h>
+#include <server/TThreadPoolServer.h>
+#include <transport/TServerSocket.h>
+#include <transport/TTransportUtils.h>
+#include <thrift/concurrency/Monitor.h>
+#include <thrift/concurrency/ThreadManager.h>
+#include <thrift/concurrency/PosixThreadFactory.h>
+
+using namespace facebook::thrift;
+using namespace facebook::thrift::protocol;
+using namespace facebook::thrift::transport;
+using namespace facebook::thrift::server;
+using namespace facebook::thrift::concurrency;
+
+
+class ThreadsTestHandler : virtual public ThreadsTestIf {
+ public:
+  ThreadsTestHandler() {
+    // Your initialization goes here
+  }
+
+  void threadOne(const int32_t sleep) {
+    // Your implementation goes here
+    printf("threadOne\n");
+    go2sleep(1, sleep);
+  }
+
+  void threadTwo(const int32_t sleep) {
+    // Your implementation goes here
+    printf("threadTwo\n");
+    go2sleep(2, sleep);
+  }
+
+  void threadThree(const int32_t sleep) {
+    // Your implementation goes here
+    printf("threadThree\n");
+    go2sleep(3, sleep);
+  }
+
+  void threadFour(const int32_t sleep) {
+    // Your implementation goes here
+    printf("threadFour\n");
+    go2sleep(4, sleep);
+  }
+
+  void stop() {
+    printf("stop\n");
+    server_->stop();
+  }
+
+  void setServer(boost::shared_ptr<TServer> server) {
+    server_ = server;
+  }
+
+protected:
+  void go2sleep(int thread, int seconds) {
+    Monitor m;
+    for (int i = 0; i < seconds; ++i) {
+      fprintf(stderr, "Thread %d: sleep %d\n", thread, i);
+      m.wait(1000);
+    }
+    fprintf(stderr, "THREAD %d DONE\n", thread);
+  }
+
+private:
+  boost::shared_ptr<TServer> server_;
+
+};
+
+int main(int argc, char **argv) {
+  int port = 9090;
+  shared_ptr<ThreadsTestHandler> handler(new ThreadsTestHandler());
+  shared_ptr<TProcessor> processor(new ThreadsTestProcessor(handler));
+  shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
+  shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
+  shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
+
+  shared_ptr<ThreadManager> threadManager = 
+    ThreadManager::newSimpleThreadManager(10);
+  shared_ptr<PosixThreadFactory> threadFactory =
+    shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
+  threadManager->threadFactory(threadFactory);
+  threadManager->start();
+  
+  shared_ptr<TServer> threadPoolServer =
+    shared_ptr<TServer>(new TThreadPoolServer(processor,
+                                              serverTransport,
+                                              transportFactory,
+                                              protocolFactory,
+                                              threadManager));
+
+  handler->setServer(threadPoolServer);
+
+  threadPoolServer->serve();
+
+  fprintf(stderr, "done.\n");
+
+  return 0;
+}
+
diff --git a/test/threads/ThreadsTest.thrift b/test/threads/ThreadsTest.thrift
new file mode 100755
index 0000000..ebf2513
--- /dev/null
+++ b/test/threads/ThreadsTest.thrift
@@ -0,0 +1,11 @@
+#!/usr/local/bin/thrift -cpp -py
+
+service ThreadsTest {
+  void threadOne(1: i32 sleep=15),
+  void threadTwo(2: i32 sleep=15),
+  void threadThree(3: i32 sleep=15),
+  void threadFour(4: i32 sleep=15)
+
+  void stop();
+
+}