|
|
@@ -1,3 +1,5 @@
|
|
|
+#include <boost/bind.hpp>
|
|
|
+#include <boost/asio.hpp>
|
|
|
#include <gtest/gtest.h>
|
|
|
#include <gflags/gflags.h>
|
|
|
#include <glog/logging.h>
|
|
|
@@ -5,23 +7,19 @@
|
|
|
|
|
|
namespace Egametang {
|
|
|
|
|
|
-static int port = 10001;
|
|
|
+static int global_port = 10001;
|
|
|
|
|
|
class RPCServerTest: public RPCCommunicator
|
|
|
{
|
|
|
public:
|
|
|
- StringPtr recv_string_;
|
|
|
-
|
|
|
- int send_id_;
|
|
|
- RpcHandlerPtr send_handler_;
|
|
|
-
|
|
|
- boost::asio::io_service& io_service_;
|
|
|
+ std::string recv_string_;
|
|
|
boost::asio::ip::tcp::acceptor acceptor_;
|
|
|
-
|
|
|
- boost::asio::ip::tcp::socket server_socket;
|
|
|
+ boost::asio::io_service& io_service_;
|
|
|
|
|
|
public:
|
|
|
- RPCServerTest(boost::asio::io_service& io_service, int port)
|
|
|
+ RPCServerTest(boost::asio::io_service& io_service, int port):
|
|
|
+ io_service_(io_service), acceptor_(io_service_),
|
|
|
+ RPCCommunicator(io_service_)
|
|
|
{
|
|
|
boost::asio::ip::address address;
|
|
|
address.from_string("localhost");
|
|
|
@@ -41,40 +39,31 @@ public:
|
|
|
|
|
|
void Start()
|
|
|
{
|
|
|
- IntPtr size(new int);
|
|
|
- boost::asio::async_read(socket_,
|
|
|
- boost::asio::buffer(reinterpret_cast<char*>(size.get()), sizeof(int)),
|
|
|
- boost::bind(&RPCCommunicator::RecvMessage, this, size,
|
|
|
- boost::asio::placeholders::error));
|
|
|
+ RecvSize();
|
|
|
}
|
|
|
|
|
|
virtual void OnRecvMessage(StringPtr ss)
|
|
|
{
|
|
|
- recv_string_ = ss;
|
|
|
+ recv_string_ = *ss;
|
|
|
}
|
|
|
- virtual void OnSendMessage(int32 id, RpcHandlerPtr handler)
|
|
|
+ virtual void OnSendMessage()
|
|
|
{
|
|
|
- send_id_ = id;
|
|
|
- send_handler_ = handler;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
class RPCClientTest: public RPCCommunicator
|
|
|
{
|
|
|
public:
|
|
|
- StringPtr recv_string_;
|
|
|
- int send_id_;
|
|
|
- RpcHandlerPtr send_handler_;
|
|
|
-
|
|
|
+ std::string recv_string_;
|
|
|
boost::asio::io_service& io_service_;
|
|
|
|
|
|
public:
|
|
|
- RPCClientTest(boost::asio::io_service& io_service, std::string& host, int port):
|
|
|
- io_service_(io_service)
|
|
|
+ RPCClientTest(boost::asio::io_service& io_service, std::string host, int port):
|
|
|
+ io_service_(io_service), RPCCommunicator(io_service_)
|
|
|
{
|
|
|
boost::asio::ip::address address;
|
|
|
address.from_string(host);
|
|
|
- boost::asio::ip::tcp::endpoint endpoint(address, port_);
|
|
|
+ boost::asio::ip::tcp::endpoint endpoint(address, port);
|
|
|
socket_.async_connect(endpoint,
|
|
|
boost::bind(&RPCClientTest::OnAsyncConnect, this,
|
|
|
boost::asio::placeholders::error));
|
|
|
@@ -85,47 +74,56 @@ public:
|
|
|
io_service_.run();
|
|
|
}
|
|
|
|
|
|
- void OnAsyncConnect()
|
|
|
- {
|
|
|
- }
|
|
|
-
|
|
|
- void SendString()
|
|
|
+ void OnAsyncConnect(const boost::system::error_code& err)
|
|
|
{
|
|
|
-
|
|
|
- req->set_request(request->SerializeAsString());
|
|
|
- RpcHandlerPtr handler(new RpcHandler(controller, response, done));
|
|
|
- SendSize(req, handler);
|
|
|
+ if (err)
|
|
|
+ {
|
|
|
+ LOG(ERROR) << "async connect failed: " << err.message();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ std::string send_string("send test rpc communicator string");
|
|
|
+ SendSize(send_string.size(), send_string);
|
|
|
}
|
|
|
|
|
|
virtual void OnRecvMessage(StringPtr ss)
|
|
|
{
|
|
|
- recv_string_ = ss;
|
|
|
+ recv_string_ = *ss;
|
|
|
}
|
|
|
|
|
|
- virtual void OnSendMessage(int32 id, RpcHandlerPtr handler)
|
|
|
+ virtual void OnSendMessage()
|
|
|
{
|
|
|
- send_id_ = id;
|
|
|
- send_handler_ = handler;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
class RPCCommunicatorTest: public testing::Test
|
|
|
{
|
|
|
protected:
|
|
|
+ boost::asio::io_service io_service_;
|
|
|
RPCServerTest rpc_server_;
|
|
|
RPCClientTest rpc_client_;
|
|
|
+
|
|
|
+public:
|
|
|
+ RPCCommunicatorTest():
|
|
|
+ io_service_(), rpc_server_(io_service_, global_port),
|
|
|
+ rpc_client_(io_service_, "localhost", global_port)
|
|
|
+ {
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
|
|
|
-TEST_F(RPCCommunicatorTest, CallMethod)
|
|
|
+TEST_F(RPCCommunicatorTest, ClientSendString)
|
|
|
{
|
|
|
- RpcServerTest server(io_service_, port);
|
|
|
- ASSERT_EQ(0, server.size);
|
|
|
-
|
|
|
- RpcChannel channel(io_service_, "localhost", port);
|
|
|
- channel.CallMethod(NULL, NULL, request, response_, done_);
|
|
|
-
|
|
|
- ASSERT_EQ(request.ByteSize(), server.size);
|
|
|
+ int a = 2;
|
|
|
}
|
|
|
|
|
|
} // namespace Egametang
|
|
|
+
|
|
|
+
|
|
|
+int main(int argc, char* argv[])
|
|
|
+{
|
|
|
+ FLAGS_logtostderr = true;
|
|
|
+ testing::InitGoogleTest(&argc, argv);
|
|
|
+ google::ParseCommandLineFlags(&argc, &argv, true);
|
|
|
+ google::InitGoogleLogging(argv[0]);
|
|
|
+ return RUN_ALL_TESTS();
|
|
|
+}
|