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 |
Antonio Di Monaco | 796667b | 2016-01-04 23:05:19 +0100 | [diff] [blame] | 11 | #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, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 15 | |
Antonio Di Monaco | 796667b | 2016-01-04 23:05:19 +0100 | [diff] [blame] | 16 | #include "gen-cpp/ParentService.h" |
| 17 | #endif |
Nobuaki Sukegawa | 6304a53 | 2014-12-18 01:30:58 +0900 | [diff] [blame] | 18 | |
| 19 | using namespace apache::thrift; |
| 20 | |
| 21 | struct AsyncHandler : public test::ParentServiceCobSvIf { |
| 22 | std::vector<std::string> strings; |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 23 | void addString(std::function<void()> cob, const std::string& s) override { |
Nobuaki Sukegawa | 6304a53 | 2014-12-18 01:30:58 +0900 | [diff] [blame] | 24 | strings.push_back(s); |
| 25 | cob(); |
| 26 | } |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 27 | void getStrings(std::function<void(std::vector<std::string> const& _return)> cob) override { |
Nobuaki Sukegawa | 6304a53 | 2014-12-18 01:30:58 +0900 | [diff] [blame] | 28 | cob(strings); |
| 29 | } |
| 30 | |
| 31 | // Overrides not used in this test |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 32 | 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( |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 38 | std::function<void()> cob, |
| 39 | std::function<void(::apache::thrift::TDelayedException* _throw)> /* exn_cob */, |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 40 | const std::string& message) override {} |
| 41 | void unexpectedExceptionWait(std::function<void()> cob, const std::string& message) override {} |
Nobuaki Sukegawa | 6304a53 | 2014-12-18 01:30:58 +0900 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | class TQTcpServerTest : public QObject { |
Sebastian Zenker | 3506b66 | 2016-01-18 08:37:54 +0100 | [diff] [blame] | 45 | Q_OBJECT |
Nobuaki Sukegawa | 6304a53 | 2014-12-18 01:30:58 +0900 | [diff] [blame] | 46 | |
Sebastian Zenker | 3506b66 | 2016-01-18 08:37:54 +0100 | [diff] [blame] | 47 | private slots: |
| 48 | void initTestCase(); |
| 49 | void cleanupTestCase(); |
| 50 | void test_communicate(); |
Nobuaki Sukegawa | 6304a53 | 2014-12-18 01:30:58 +0900 | [diff] [blame] | 51 | |
Sebastian Zenker | 3506b66 | 2016-01-18 08:37:54 +0100 | [diff] [blame] | 52 | private: |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 53 | std::shared_ptr<QThread> serverThread; |
| 54 | std::shared_ptr<async::TQTcpServer> server; |
| 55 | std::shared_ptr<test::ParentServiceClient> client; |
Nobuaki Sukegawa | 6304a53 | 2014-12-18 01:30:58 +0900 | [diff] [blame] | 56 | }; |
| 57 | |
Sebastian Zenker | 3506b66 | 2016-01-18 08:37:54 +0100 | [diff] [blame] | 58 | void TQTcpServerTest::initTestCase() { |
| 59 | // setup server |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 60 | std::shared_ptr<QTcpServer> serverSocket = std::make_shared<QTcpServer>(); |
Sebastian Zenker | 3506b66 | 2016-01-18 08:37:54 +0100 | [diff] [blame] | 61 | server.reset(new async::TQTcpServer(serverSocket, |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 62 | std::make_shared<test::ParentServiceAsyncProcessor>( |
| 63 | std::make_shared<AsyncHandler>()), |
| 64 | std::make_shared<protocol::TBinaryProtocolFactory>())); |
Sebastian Zenker | 3506b66 | 2016-01-18 08:37:54 +0100 | [diff] [blame] | 65 | 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 |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 76 | 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 Zenker | 3506b66 | 2016-01-18 08:37:54 +0100 | [diff] [blame] | 79 | socket->connectToHost(QHostAddress::LocalHost, port); |
| 80 | QVERIFY(socket->waitForConnected()); |
| 81 | } |
| 82 | |
| 83 | void 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 | |
| 96 | void 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 Sukegawa | 6304a53 | 2014-12-18 01:30:58 +0900 | [diff] [blame] | 107 | #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) |
| 108 | QTEST_GUILESS_MAIN(TQTcpServerTest); |
| 109 | #else |
| 110 | #undef QT_GUI_LIB |
| 111 | QTEST_MAIN(TQTcpServerTest); |
| 112 | #endif |
| 113 | #include "TQTcpServerTest.moc" |