blob: 3371a9ae824c0c5c9c8668fc8627d8330f113d89 [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
Antonio Di Monaco796667b2016-01-04 23:05:19 +010011 #include "thrift/protocol/TBinaryProtocol.h"
12 #include "thrift/async/TAsyncProcessor.h"
13 #include "thrift/qt/TQTcpServer.h"
14 #include "thrift/qt/TQIODeviceTransport.h"
James E. King, III82ae9572017-08-05 12:23:54 -040015
Antonio Di Monaco796667b2016-01-04 23:05:19 +010016 #include "gen-cpp/ParentService.h"
17#endif
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +090018
19using namespace apache::thrift;
20
21struct AsyncHandler : public test::ParentServiceCobSvIf {
22 std::vector<std::string> strings;
Sebastian Zenker042580f2019-01-29 15:48:12 +010023 void addString(std::function<void()> cob, const std::string& s) override {
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +090024 strings.push_back(s);
25 cob();
26 }
Sebastian Zenker042580f2019-01-29 15:48:12 +010027 void getStrings(std::function<void(std::vector<std::string> const& _return)> cob) override {
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +090028 cob(strings);
29 }
30
31 // Overrides not used in this test
Sebastian Zenker042580f2019-01-29 15:48:12 +010032 void incrementGeneration(std::function<void(int32_t const& _return)> cob) override {}
33 void getGeneration(std::function<void(int32_t const& _return)> cob) override {}
34 void getDataWait(std::function<void(std::string const& _return)> cob,
35 const int32_t length) override {}
36 void onewayWait(std::function<void()> cob) override {}
37 void exceptionWait(
cyy316723a2019-01-05 16:35:14 +080038 std::function<void()> cob,
39 std::function<void(::apache::thrift::TDelayedException* _throw)> /* exn_cob */,
Sebastian Zenker042580f2019-01-29 15:48:12 +010040 const std::string& message) override {}
41 void unexpectedExceptionWait(std::function<void()> cob, const std::string& message) override {}
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +090042};
43
44class TQTcpServerTest : public QObject {
Sebastian Zenker3506b662016-01-18 08:37:54 +010045 Q_OBJECT
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +090046
Sebastian Zenker3506b662016-01-18 08:37:54 +010047private slots:
48 void initTestCase();
49 void cleanupTestCase();
50 void test_communicate();
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +090051
Sebastian Zenker3506b662016-01-18 08:37:54 +010052private:
cyy316723a2019-01-05 16:35:14 +080053 std::shared_ptr<QThread> serverThread;
54 std::shared_ptr<async::TQTcpServer> server;
55 std::shared_ptr<test::ParentServiceClient> client;
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +090056};
57
Sebastian Zenker3506b662016-01-18 08:37:54 +010058void TQTcpServerTest::initTestCase() {
59 // setup server
cyy316723a2019-01-05 16:35:14 +080060 std::shared_ptr<QTcpServer> serverSocket = std::make_shared<QTcpServer>();
Sebastian Zenker3506b662016-01-18 08:37:54 +010061 server.reset(new async::TQTcpServer(serverSocket,
cyy316723a2019-01-05 16:35:14 +080062 std::make_shared<test::ParentServiceAsyncProcessor>(
63 std::make_shared<AsyncHandler>()),
64 std::make_shared<protocol::TBinaryProtocolFactory>()));
Sebastian Zenker3506b662016-01-18 08:37:54 +010065 QVERIFY(serverSocket->listen(QHostAddress::LocalHost));
66 int port = serverSocket->serverPort();
67 QVERIFY(port > 0);
68
69 //setup server thread and move server to it
70 serverThread.reset(new QThread());
71 serverSocket->moveToThread(serverThread.get());
72 server->moveToThread(serverThread.get());
73 serverThread->start();
74
75 // setup client
cyy316723a2019-01-05 16:35:14 +080076 std::shared_ptr<QTcpSocket> socket = std::make_shared<QTcpSocket>();
77 client.reset(new test::ParentServiceClient(std::make_shared<protocol::TBinaryProtocol>(
78 std::make_shared<transport::TQIODeviceTransport>(socket))));
Sebastian Zenker3506b662016-01-18 08:37:54 +010079 socket->connectToHost(QHostAddress::LocalHost, port);
80 QVERIFY(socket->waitForConnected());
81}
82
83void TQTcpServerTest::cleanupTestCase() {
84 //first, stop the thread which holds the server
85 serverThread->quit();
86 serverThread->wait();
87 // now, it is safe to delete the server
88 server.reset();
89 // delete thread now
90 serverThread.reset();
91
92 // cleanup client
93 client.reset();
94}
95
96void TQTcpServerTest::test_communicate() {
97 client->addString("foo");
98 client->addString("bar");
99
100 std::vector<std::string> reply;
101 client->getStrings(reply);
102 QCOMPARE(QString::fromStdString(reply[0]), QString("foo"));
103 QCOMPARE(QString::fromStdString(reply[1]), QString("bar"));
104}
105
106
Nobuaki Sukegawa6304a532014-12-18 01:30:58 +0900107#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
108QTEST_GUILESS_MAIN(TQTcpServerTest);
109#else
110#undef QT_GUI_LIB
111QTEST_MAIN(TQTcpServerTest);
112#endif
113#include "TQTcpServerTest.moc"