blob: 422d771af2baa0d855b5149c6f275774e14196fb [file] [log] [blame]
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +09001#define BOOST_TEST_MODULE TQTcpServerTest
2#include <QTest>
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +09003#include <iostream>
4
5#include <QTcpServer>
6#include <QTcpSocket>
7#include <QHostAddress>
Sebastian Zenker3506b662016-01-18 08:37:54 +01008#include <QThread>
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +09009
Antonio Di Monaco796667b2016-01-04 23:05:19 +010010#ifndef Q_MOC_RUN
11 #include <boost/smart_ptr.hpp>
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
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +090019
20using namespace apache::thrift;
21
22struct AsyncHandler : public test::ParentServiceCobSvIf {
23 std::vector<std::string> strings;
24 virtual void addString(tcxx::function<void()> cob, const std::string& s) {
25 strings.push_back(s);
26 cob();
27 }
28 virtual void getStrings(tcxx::function<void(std::vector<std::string> const& _return)> cob) {
29 cob(strings);
30 }
31
32 // Overrides not used in this test
33 virtual void incrementGeneration(tcxx::function<void(int32_t const& _return)> cob) {}
34 virtual void getGeneration(tcxx::function<void(int32_t const& _return)> cob) {}
35 virtual void getDataWait(tcxx::function<void(std::string const& _return)> cob,
36 const int32_t length) {}
37 virtual void onewayWait(tcxx::function<void()> cob) {}
38 virtual void exceptionWait(
39 tcxx::function<void()> cob,
40 tcxx::function<void(::apache::thrift::TDelayedException* _throw)> /* exn_cob */,
41 const std::string& message) {}
42 virtual void unexpectedExceptionWait(tcxx::function<void()> cob, const std::string& message) {}
43};
44
45class TQTcpServerTest : public QObject {
Sebastian Zenker3506b662016-01-18 08:37:54 +010046 Q_OBJECT
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +090047
Sebastian Zenker3506b662016-01-18 08:37:54 +010048private slots:
49 void initTestCase();
50 void cleanupTestCase();
51 void test_communicate();
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +090052
Sebastian Zenker3506b662016-01-18 08:37:54 +010053private:
54 boost::shared_ptr<QThread> serverThread;
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +090055 boost::shared_ptr<async::TQTcpServer> server;
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +090056 boost::shared_ptr<test::ParentServiceClient> client;
57};
58
Sebastian Zenker3506b662016-01-18 08:37:54 +010059void TQTcpServerTest::initTestCase() {
60 // setup server
61 boost::shared_ptr<QTcpServer> serverSocket = boost::make_shared<QTcpServer>();
62 server.reset(new async::TQTcpServer(serverSocket,
63 boost::make_shared<test::ParentServiceAsyncProcessor>(
64 boost::make_shared<AsyncHandler>()),
65 boost::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 boost::shared_ptr<QTcpSocket> socket = boost::make_shared<QTcpSocket>();
78 client.reset(new test::ParentServiceClient(boost::make_shared<protocol::TBinaryProtocol>(
79 boost::make_shared<transport::TQIODeviceTransport>(socket))));
80 socket->connectToHost(QHostAddress::LocalHost, port);
81 QVERIFY(socket->waitForConnected());
82}
83
84void 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
97void 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
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +0900108#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
109QTEST_GUILESS_MAIN(TQTcpServerTest);
110#else
111#undef QT_GUI_LIB
112QTEST_MAIN(TQTcpServerTest);
113#endif
114#include "TQTcpServerTest.moc"