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