1 #define BOOST_TEST_MODULE TQTcpServerTest
2 #include <QTest>
3 #include <iostream>
4 
5 #include <QTcpServer>
6 #include <QTcpSocket>
7 #include <QHostAddress>
8 #include <QThread>
9 
10 #ifndef Q_MOC_RUN
11   #include "thrift/stdcxx.h"
12   #include "thrift/protocol/TBinaryProtocol.h"
13   #include "thrift/async/TAsyncProcessor.h"
14   #include "thrift/qt/TQTcpServer.h"
15   #include "thrift/qt/TQIODeviceTransport.h"
16 
17   #include "gen-cpp/ParentService.h"
18 #endif
19 
20 using namespace apache::thrift;
21 
22 struct AsyncHandler : public test::ParentServiceCobSvIf {
23   std::vector<std::string> strings;
addStringAsyncHandler24   virtual void addString(stdcxx::function<void()> cob, const std::string& s) {
25     strings.push_back(s);
26     cob();
27   }
getStringsAsyncHandler28   virtual void getStrings(stdcxx::function<void(std::vector<std::string> const& _return)> cob) {
29     cob(strings);
30   }
31 
32   // Overrides not used in this test
incrementGenerationAsyncHandler33   virtual void incrementGeneration(stdcxx::function<void(int32_t const& _return)> cob) {}
getGenerationAsyncHandler34   virtual void getGeneration(stdcxx::function<void(int32_t const& _return)> cob) {}
getDataWaitAsyncHandler35   virtual void getDataWait(stdcxx::function<void(std::string const& _return)> cob,
36                            const int32_t length) {}
onewayWaitAsyncHandler37   virtual void onewayWait(stdcxx::function<void()> cob) {}
exceptionWaitAsyncHandler38   virtual void exceptionWait(
39       stdcxx::function<void()> cob,
40       stdcxx::function<void(::apache::thrift::TDelayedException* _throw)> /* exn_cob */,
41       const std::string& message) {}
unexpectedExceptionWaitAsyncHandler42   virtual void unexpectedExceptionWait(stdcxx::function<void()> cob, const std::string& message) {}
43 };
44 
45 class TQTcpServerTest : public QObject {
46   Q_OBJECT
47 
48 private slots:
49   void initTestCase();
50   void cleanupTestCase();
51   void test_communicate();
52 
53 private:
54   stdcxx::shared_ptr<QThread> serverThread;
55   stdcxx::shared_ptr<async::TQTcpServer> server;
56   stdcxx::shared_ptr<test::ParentServiceClient> client;
57 };
58 
initTestCase()59 void TQTcpServerTest::initTestCase() {
60   // setup server
61   stdcxx::shared_ptr<QTcpServer> serverSocket = stdcxx::make_shared<QTcpServer>();
62   server.reset(new async::TQTcpServer(serverSocket,
63                                       stdcxx::make_shared<test::ParentServiceAsyncProcessor>(
64                                       stdcxx::make_shared<AsyncHandler>()),
65                                       stdcxx::make_shared<protocol::TBinaryProtocolFactory>()));
66   QVERIFY(serverSocket->listen(QHostAddress::LocalHost));
67   int port = serverSocket->serverPort();
68   QVERIFY(port > 0);
69 
70   //setup server thread and move server to it
71   serverThread.reset(new QThread());
72   serverSocket->moveToThread(serverThread.get());
73   server->moveToThread(serverThread.get());
74   serverThread->start();
75 
76   // setup client
77   stdcxx::shared_ptr<QTcpSocket> socket = stdcxx::make_shared<QTcpSocket>();
78   client.reset(new test::ParentServiceClient(stdcxx::make_shared<protocol::TBinaryProtocol>(
79       stdcxx::make_shared<transport::TQIODeviceTransport>(socket))));
80   socket->connectToHost(QHostAddress::LocalHost, port);
81   QVERIFY(socket->waitForConnected());
82 }
83 
cleanupTestCase()84 void TQTcpServerTest::cleanupTestCase() {
85   //first, stop the thread which holds the server
86   serverThread->quit();
87   serverThread->wait();
88   // now, it is safe to delete the server
89   server.reset();
90   // delete thread now
91   serverThread.reset();
92 
93   // cleanup client
94   client.reset();
95 }
96 
test_communicate()97 void TQTcpServerTest::test_communicate() {
98   client->addString("foo");
99   client->addString("bar");
100 
101   std::vector<std::string> reply;
102   client->getStrings(reply);
103   QCOMPARE(QString::fromStdString(reply[0]), QString("foo"));
104   QCOMPARE(QString::fromStdString(reply[1]), QString("bar"));
105 }
106 
107 
108 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
109 QTEST_GUILESS_MAIN(TQTcpServerTest);
110 #else
111 #undef QT_GUI_LIB
112 QTEST_MAIN(TQTcpServerTest);
113 #endif
114 #include "TQTcpServerTest.moc"
115