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> |
| 8 | |
Antonio Di Monaco | 796667b | 2016-01-04 23:05:19 +0100 | [diff] [blame^] | 9 | #ifndef Q_MOC_RUN |
| 10 | #include <boost/smart_ptr.hpp> |
| 11 | #include "thrift/protocol/TBinaryProtocol.h" |
| 12 | #include "thrift/async/TAsyncProcessor.h" |
| 13 | #include "thrift/qt/TQTcpServer.h" |
| 14 | #include "thrift/qt/TQIODeviceTransport.h" |
| 15 | |
| 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; |
| 23 | virtual void addString(tcxx::function<void()> cob, const std::string& s) { |
| 24 | strings.push_back(s); |
| 25 | cob(); |
| 26 | } |
| 27 | virtual void getStrings(tcxx::function<void(std::vector<std::string> const& _return)> cob) { |
| 28 | cob(strings); |
| 29 | } |
| 30 | |
| 31 | // Overrides not used in this test |
| 32 | virtual void incrementGeneration(tcxx::function<void(int32_t const& _return)> cob) {} |
| 33 | virtual void getGeneration(tcxx::function<void(int32_t const& _return)> cob) {} |
| 34 | virtual void getDataWait(tcxx::function<void(std::string const& _return)> cob, |
| 35 | const int32_t length) {} |
| 36 | virtual void onewayWait(tcxx::function<void()> cob) {} |
| 37 | virtual void exceptionWait( |
| 38 | tcxx::function<void()> cob, |
| 39 | tcxx::function<void(::apache::thrift::TDelayedException* _throw)> /* exn_cob */, |
| 40 | const std::string& message) {} |
| 41 | virtual void unexpectedExceptionWait(tcxx::function<void()> cob, const std::string& message) {} |
| 42 | }; |
| 43 | |
| 44 | class TQTcpServerTest : public QObject { |
| 45 | void init() { |
| 46 | // setup server |
| 47 | serverSocket.reset(new QTcpServer); |
| 48 | server.reset(new async::TQTcpServer(serverSocket, |
| 49 | boost::make_shared<test::ParentServiceAsyncProcessor>( |
| 50 | boost::make_shared<AsyncHandler>()), |
| 51 | boost::make_shared<protocol::TBinaryProtocolFactory>())); |
| 52 | QVERIFY(serverSocket->listen(QHostAddress::LocalHost)); |
| 53 | int port = serverSocket->serverPort(); |
| 54 | QVERIFY(port > 0); |
| 55 | |
| 56 | // setup client |
| 57 | socket.reset(new QTcpSocket); |
| 58 | client.reset(new test::ParentServiceClient(boost::make_shared<protocol::TBinaryProtocol>( |
| 59 | boost::make_shared<transport::TQIODeviceTransport>(socket)))); |
| 60 | socket->connectToHost(QHostAddress::LocalHost, port); |
| 61 | QVERIFY(socket->waitForConnected()); |
| 62 | } |
| 63 | |
| 64 | void cleanup() { |
| 65 | socket->close(); |
| 66 | serverSocket->close(); |
| 67 | } |
| 68 | |
| 69 | void test_communicate() { |
| 70 | client->addString("foo"); |
| 71 | client->addString("bar"); |
| 72 | |
| 73 | std::vector<std::string> reply; |
| 74 | client->getStrings(reply); |
| 75 | QCOMPARE(reply[0], "foo"); |
| 76 | QCOMPARE(reply[1], "foo"); |
| 77 | } |
| 78 | |
| 79 | boost::shared_ptr<QTcpServer> serverSocket; |
| 80 | boost::shared_ptr<async::TQTcpServer> server; |
| 81 | boost::shared_ptr<QTcpSocket> socket; |
| 82 | boost::shared_ptr<test::ParentServiceClient> client; |
| 83 | }; |
| 84 | |
| 85 | #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) |
| 86 | QTEST_GUILESS_MAIN(TQTcpServerTest); |
| 87 | #else |
| 88 | #undef QT_GUI_LIB |
| 89 | QTEST_MAIN(TQTcpServerTest); |
| 90 | #endif |
| 91 | #include "TQTcpServerTest.moc" |