Nobuaki Sukegawa | 6304a53 | 2014-12-18 01:30:58 +0900 | [diff] [blame] | 1 | #define BOOST_TEST_MODULE TQTcpServerTest |
| 2 | #include <QTest> |
Nobuaki Sukegawa | 6304a53 | 2014-12-18 01:30:58 +0900 | [diff] [blame] | 3 | #include <iostream> |
| 4 | |
| 5 | #include <QTcpServer> |
| 6 | #include <QTcpSocket> |
| 7 | #include <QHostAddress> |
Sebastian Zenker | 3506b66 | 2016-01-18 08:37:54 +0100 | [diff] [blame] | 8 | #include <QThread> |
Nobuaki Sukegawa | 6304a53 | 2014-12-18 01:30:58 +0900 | [diff] [blame] | 9 | |
Antonio Di Monaco | 796667b | 2016-01-04 23:05:19 +0100 | [diff] [blame] | 10 | #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 Sukegawa | 6304a53 | 2014-12-18 01:30:58 +0900 | [diff] [blame] | 19 | |
| 20 | using namespace apache::thrift; |
| 21 | |
| 22 | struct 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 | |
| 45 | class TQTcpServerTest : public QObject { |
Sebastian Zenker | 3506b66 | 2016-01-18 08:37:54 +0100 | [diff] [blame] | 46 | Q_OBJECT |
Nobuaki Sukegawa | 6304a53 | 2014-12-18 01:30:58 +0900 | [diff] [blame] | 47 | |
Sebastian Zenker | 3506b66 | 2016-01-18 08:37:54 +0100 | [diff] [blame] | 48 | private slots: |
| 49 | void initTestCase(); |
| 50 | void cleanupTestCase(); |
| 51 | void test_communicate(); |
Nobuaki Sukegawa | 6304a53 | 2014-12-18 01:30:58 +0900 | [diff] [blame] | 52 | |
Sebastian Zenker | 3506b66 | 2016-01-18 08:37:54 +0100 | [diff] [blame] | 53 | private: |
| 54 | boost::shared_ptr<QThread> serverThread; |
Nobuaki Sukegawa | 6304a53 | 2014-12-18 01:30:58 +0900 | [diff] [blame] | 55 | boost::shared_ptr<async::TQTcpServer> server; |
Nobuaki Sukegawa | 6304a53 | 2014-12-18 01:30:58 +0900 | [diff] [blame] | 56 | boost::shared_ptr<test::ParentServiceClient> client; |
| 57 | }; |
| 58 | |
Sebastian Zenker | 3506b66 | 2016-01-18 08:37:54 +0100 | [diff] [blame] | 59 | void 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 | |
| 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 | |
| 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 | |
Nobuaki Sukegawa | 6304a53 | 2014-12-18 01:30:58 +0900 | [diff] [blame] | 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" |