#define BOOST_TEST_MODULE TQTcpServerTest #include #include #include #include #include #include #ifndef Q_MOC_RUN #include "thrift/protocol/TBinaryProtocol.h" #include "thrift/async/TAsyncProcessor.h" #include "thrift/qt/TQTcpServer.h" #include "thrift/qt/TQIODeviceTransport.h" #include "gen-cpp/ParentService.h" #endif using namespace apache::thrift; struct AsyncHandler : public test::ParentServiceCobSvIf { std::vector strings; void addString(std::function cob, const std::string& s) override { strings.push_back(s); cob(); } void getStrings(std::function const& _return)> cob) override { cob(strings); } // Overrides not used in this test void incrementGeneration(std::function cob) override {} void getGeneration(std::function cob) override {} void getDataWait(std::function cob, const int32_t length) override {} void onewayWait(std::function cob) override {} void exceptionWait( std::function cob, std::function /* exn_cob */, const std::string& message) override {} void unexpectedExceptionWait(std::function cob, const std::string& message) override {} }; class TQTcpServerTest : public QObject { Q_OBJECT private slots: void initTestCase(); void cleanupTestCase(); void test_communicate(); private: std::shared_ptr serverThread; std::shared_ptr server; std::shared_ptr client; }; void TQTcpServerTest::initTestCase() { // setup server std::shared_ptr serverSocket = std::make_shared(); server.reset(new async::TQTcpServer(serverSocket, std::make_shared( std::make_shared()), std::make_shared())); QVERIFY(serverSocket->listen(QHostAddress::LocalHost)); int port = serverSocket->serverPort(); QVERIFY(port > 0); //setup server thread and move server to it serverThread.reset(new QThread()); serverSocket->moveToThread(serverThread.get()); server->moveToThread(serverThread.get()); serverThread->start(); // setup client std::shared_ptr socket = std::make_shared(); client.reset(new test::ParentServiceClient(std::make_shared( std::make_shared(socket)))); socket->connectToHost(QHostAddress::LocalHost, port); QVERIFY(socket->waitForConnected()); } void TQTcpServerTest::cleanupTestCase() { //first, stop the thread which holds the server serverThread->quit(); serverThread->wait(); // now, it is safe to delete the server server.reset(); // delete thread now serverThread.reset(); // cleanup client client.reset(); } void TQTcpServerTest::test_communicate() { client->addString("foo"); client->addString("bar"); std::vector reply; client->getStrings(reply); QCOMPARE(QString::fromStdString(reply[0]), QString("foo")); QCOMPARE(QString::fromStdString(reply[1]), QString("bar")); } #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) QTEST_GUILESS_MAIN(TQTcpServerTest); #else #undef QT_GUI_LIB QTEST_MAIN(TQTcpServerTest); #endif #include "TQTcpServerTest.moc"