blob: 8a327aa24ca5e4efc5c51c024fe304ced0adb126 [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
James E. King, III82ae9572017-08-05 12:23:54 -040011 #include "thrift/stdcxx.h"
Antonio Di Monaco796667b2016-01-04 23:05:19 +010012 #include "thrift/protocol/TBinaryProtocol.h"
13 #include "thrift/async/TAsyncProcessor.h"
14 #include "thrift/qt/TQTcpServer.h"
15 #include "thrift/qt/TQIODeviceTransport.h"
James E. King, III82ae9572017-08-05 12:23:54 -040016
Antonio Di Monaco796667b2016-01-04 23:05:19 +010017 #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;
James E. King, III82ae9572017-08-05 12:23:54 -040024 virtual void addString(stdcxx::function<void()> cob, const std::string& s) {
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +090025 strings.push_back(s);
26 cob();
27 }
James E. King, III82ae9572017-08-05 12:23:54 -040028 virtual void getStrings(stdcxx::function<void(std::vector<std::string> const& _return)> cob) {
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +090029 cob(strings);
30 }
31
32 // Overrides not used in this test
James E. King, III82ae9572017-08-05 12:23:54 -040033 virtual void incrementGeneration(stdcxx::function<void(int32_t const& _return)> cob) {}
34 virtual void getGeneration(stdcxx::function<void(int32_t const& _return)> cob) {}
35 virtual void getDataWait(stdcxx::function<void(std::string const& _return)> cob,
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +090036 const int32_t length) {}
James E. King, III82ae9572017-08-05 12:23:54 -040037 virtual void onewayWait(stdcxx::function<void()> cob) {}
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +090038 virtual void exceptionWait(
James E. King, III82ae9572017-08-05 12:23:54 -040039 stdcxx::function<void()> cob,
40 stdcxx::function<void(::apache::thrift::TDelayedException* _throw)> /* exn_cob */,
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +090041 const std::string& message) {}
James E. King, III82ae9572017-08-05 12:23:54 -040042 virtual void unexpectedExceptionWait(stdcxx::function<void()> cob, const std::string& message) {}
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +090043};
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:
James E. King, III82ae9572017-08-05 12:23:54 -040054 stdcxx::shared_ptr<QThread> serverThread;
55 stdcxx::shared_ptr<async::TQTcpServer> server;
56 stdcxx::shared_ptr<test::ParentServiceClient> client;
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +090057};
58
Sebastian Zenker3506b662016-01-18 08:37:54 +010059void TQTcpServerTest::initTestCase() {
60 // setup server
James E. King, III82ae9572017-08-05 12:23:54 -040061 stdcxx::shared_ptr<QTcpServer> serverSocket = stdcxx::make_shared<QTcpServer>();
Sebastian Zenker3506b662016-01-18 08:37:54 +010062 server.reset(new async::TQTcpServer(serverSocket,
James E. King, III82ae9572017-08-05 12:23:54 -040063 stdcxx::make_shared<test::ParentServiceAsyncProcessor>(
64 stdcxx::make_shared<AsyncHandler>()),
65 stdcxx::make_shared<protocol::TBinaryProtocolFactory>()));
Sebastian Zenker3506b662016-01-18 08:37:54 +010066 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
James E. King, III82ae9572017-08-05 12:23:54 -040077 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))));
Sebastian Zenker3506b662016-01-18 08:37:54 +010080 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"