]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | #define BOOST_TEST_MODULE TQTcpServerTest |
2 | #include <QTest> | |
3 | #include <iostream> | |
4 | ||
5 | #include <QTcpServer> | |
6 | #include <QTcpSocket> | |
7 | #include <QHostAddress> | |
8 | #include <QThread> | |
9 | ||
10 | #ifndef Q_MOC_RUN | |
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 | |
18 | ||
19 | using namespace apache::thrift; | |
20 | ||
21 | struct AsyncHandler : public test::ParentServiceCobSvIf { | |
22 | std::vector<std::string> strings; | |
23 | void addString(std::function<void()> cob, const std::string& s) override { | |
24 | strings.push_back(s); | |
25 | cob(); | |
26 | } | |
27 | void getStrings(std::function<void(std::vector<std::string> const& _return)> cob) override { | |
28 | cob(strings); | |
29 | } | |
30 | ||
31 | // Overrides not used in this test | |
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( | |
38 | std::function<void()> cob, | |
39 | std::function<void(::apache::thrift::TDelayedException* _throw)> /* exn_cob */, | |
40 | const std::string& message) override {} | |
41 | void unexpectedExceptionWait(std::function<void()> cob, const std::string& message) override {} | |
42 | }; | |
43 | ||
44 | class TQTcpServerTest : public QObject { | |
45 | Q_OBJECT | |
46 | ||
47 | private slots: | |
48 | void initTestCase(); | |
49 | void cleanupTestCase(); | |
50 | void test_communicate(); | |
51 | ||
52 | private: | |
53 | std::shared_ptr<QThread> serverThread; | |
54 | std::shared_ptr<async::TQTcpServer> server; | |
55 | std::shared_ptr<test::ParentServiceClient> client; | |
56 | }; | |
57 | ||
58 | void TQTcpServerTest::initTestCase() { | |
59 | // setup server | |
60 | std::shared_ptr<QTcpServer> serverSocket = std::make_shared<QTcpServer>(); | |
61 | server.reset(new async::TQTcpServer(serverSocket, | |
62 | std::make_shared<test::ParentServiceAsyncProcessor>( | |
63 | std::make_shared<AsyncHandler>()), | |
64 | std::make_shared<protocol::TBinaryProtocolFactory>())); | |
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 | |
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)))); | |
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 | ||
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" |