diff options
author | haibinlu <haibinlu@chromium.org> | 2015-12-18 11:52:47 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-12-18 19:53:28 +0000 |
commit | ebd55851dec2eacfc94c16ca6610ea70c3ead003 (patch) | |
tree | f58600e763a25574e9fd73d9d7cf6c73846bbfb4 /blimp | |
parent | 56b13fe22b8d25294504c14d08ab306a7cc2a28f (diff) | |
download | chromium_src-ebd55851dec2eacfc94c16ca6610ea70c3ead003.zip chromium_src-ebd55851dec2eacfc94c16ca6610ea70c3ead003.tar.gz chromium_src-ebd55851dec2eacfc94c16ca6610ea70c3ead003.tar.bz2 |
This is an integration test that involves underlying TCP connection and BlimpConnection.
Also fixes a bug in BlimpConnection that results in wrong buffer's remaining bytes.
Review URL: https://codereview.chromium.org/1522163003
Cr-Commit-Position: refs/heads/master@{#366153}
Diffstat (limited to 'blimp')
-rw-r--r-- | blimp/net/blimp_connection.cc | 15 | ||||
-rw-r--r-- | blimp/net/tcp_transport_unittest.cc | 83 |
2 files changed, 79 insertions, 19 deletions
diff --git a/blimp/net/blimp_connection.cc b/blimp/net/blimp_connection.cc index dd40021..dee90c1 100644 --- a/blimp/net/blimp_connection.cc +++ b/blimp/net/blimp_connection.cc @@ -39,17 +39,14 @@ class BlimpMessageSender : public BlimpMessageProcessor { PacketWriter* writer_; ConnectionErrorObserver* error_observer_; - scoped_refptr<net::DrainableIOBuffer> buffer_; + scoped_refptr<net::IOBuffer> buffer_; net::CompletionCallback pending_process_msg_callback_; DISALLOW_COPY_AND_ASSIGN(BlimpMessageSender); }; BlimpMessageSender::BlimpMessageSender(PacketWriter* writer) - : writer_(writer), - buffer_(new net::DrainableIOBuffer( - new net::IOBuffer(kMaxPacketPayloadSizeBytes), - kMaxPacketPayloadSizeBytes)) { + : writer_(writer), buffer_(new net::IOBuffer(kMaxPacketPayloadSizeBytes)) { DCHECK(writer_); } @@ -64,7 +61,6 @@ void BlimpMessageSender::ProcessMessage( return; } - buffer_->SetOffset(0); if (!message->SerializeToArray(buffer_->data(), message->ByteSize())) { DLOG(ERROR) << "Failed to serialize message."; callback.Run(net::ERR_INVALID_ARGUMENT); @@ -72,9 +68,10 @@ void BlimpMessageSender::ProcessMessage( } pending_process_msg_callback_ = callback; - writer_->WritePacket(buffer_, - base::Bind(&BlimpMessageSender::OnWritePacketComplete, - base::Unretained(this))); + writer_->WritePacket( + new net::DrainableIOBuffer(buffer_.get(), message->ByteSize()), + base::Bind(&BlimpMessageSender::OnWritePacketComplete, + base::Unretained(this))); } void BlimpMessageSender::OnWritePacketComplete(int result) { diff --git a/blimp/net/tcp_transport_unittest.cc b/blimp/net/tcp_transport_unittest.cc index 2df4328..13d620a 100644 --- a/blimp/net/tcp_transport_unittest.cc +++ b/blimp/net/tcp_transport_unittest.cc @@ -6,15 +6,22 @@ #include "base/memory/scoped_ptr.h" #include "base/message_loop/message_loop.h" +#include "blimp/common/create_blimp_message.h" +#include "blimp/common/proto/blimp_message.pb.h" +#include "blimp/common/proto/protocol_control.pb.h" #include "blimp/net/blimp_connection.h" #include "blimp/net/tcp_client_transport.h" #include "blimp/net/tcp_engine_transport.h" +#include "blimp/net/test_common.h" #include "net/base/address_list.h" #include "net/base/ip_endpoint.h" #include "net/base/net_errors.h" #include "net/base/test_completion_callback.h" #include "testing/gtest/include/gtest/gtest.h" +using testing::_; +using testing::SaveArg; + namespace blimp { namespace { @@ -25,12 +32,12 @@ class TCPTransportTest : public testing::Test { TCPTransportTest() { net::IPEndPoint local_address; ParseAddress("127.0.0.1", 0, &local_address); - server_.reset(new TCPEngineTransport(local_address, nullptr)); + engine_.reset(new TCPEngineTransport(local_address, nullptr)); } net::AddressList GetLocalAddressList() const { net::IPEndPoint local_address; - server_->GetLocalAddressForTesting(&local_address); + engine_->GetLocalAddressForTesting(&local_address); return net::AddressList(local_address); } @@ -45,12 +52,12 @@ class TCPTransportTest : public testing::Test { } base::MessageLoopForIO message_loop_; - scoped_ptr<TCPEngineTransport> server_; + scoped_ptr<TCPEngineTransport> engine_; }; TEST_F(TCPTransportTest, Connect) { net::TestCompletionCallback accept_callback; - server_->Connect(accept_callback.callback()); + engine_->Connect(accept_callback.callback()); net::TestCompletionCallback connect_callback; TCPClientTransport client(GetLocalAddressList(), nullptr); @@ -58,12 +65,12 @@ TEST_F(TCPTransportTest, Connect) { EXPECT_EQ(net::OK, connect_callback.WaitForResult()); EXPECT_EQ(net::OK, accept_callback.WaitForResult()); - EXPECT_TRUE(server_->TakeConnection() != nullptr); + EXPECT_TRUE(engine_->TakeConnection() != nullptr); } TEST_F(TCPTransportTest, TwoClientConnections) { net::TestCompletionCallback accept_callback1; - server_->Connect(accept_callback1.callback()); + engine_->Connect(accept_callback1.callback()); net::TestCompletionCallback connect_callback1; TCPClientTransport client1(GetLocalAddressList(), nullptr); @@ -75,16 +82,72 @@ TEST_F(TCPTransportTest, TwoClientConnections) { EXPECT_EQ(net::OK, connect_callback1.WaitForResult()); EXPECT_EQ(net::OK, accept_callback1.WaitForResult()); - EXPECT_TRUE(server_->TakeConnection() != nullptr); + EXPECT_TRUE(engine_->TakeConnection() != nullptr); net::TestCompletionCallback accept_callback2; - server_->Connect(accept_callback2.callback()); + engine_->Connect(accept_callback2.callback()); EXPECT_EQ(net::OK, connect_callback2.WaitForResult()); EXPECT_EQ(net::OK, accept_callback2.WaitForResult()); - EXPECT_TRUE(server_->TakeConnection() != nullptr); + EXPECT_TRUE(engine_->TakeConnection() != nullptr); } -// TODO(haibinlu): add a test case for message exchange. +TEST_F(TCPTransportTest, ExchangeMessages) { + // Start the Engine transport and connect a client to it. + net::TestCompletionCallback accept_callback; + engine_->Connect(accept_callback.callback()); + net::TestCompletionCallback client_connect_callback; + TCPClientTransport client(GetLocalAddressList(), nullptr /* NetLog */); + client.Connect(client_connect_callback.callback()); + EXPECT_EQ(net::OK, client_connect_callback.WaitForResult()); + EXPECT_EQ(net::OK, accept_callback.WaitForResult()); + + // Expect the engine to get two messages from the client, and the client to + // get one from the engine. + MockBlimpMessageProcessor engine_incoming_processor; + MockBlimpMessageProcessor client_incoming_processor; + net::CompletionCallback engine_process_message_cb; + scoped_ptr<BlimpMessage> client_message1 = + CreateStartConnectionMessage("", 0); + scoped_ptr<BlimpMessage> client_message2 = CreateCheckpointAckMessage(5); + scoped_ptr<BlimpMessage> engine_message = CreateCheckpointAckMessage(10); + EXPECT_CALL(engine_incoming_processor, + MockableProcessMessage(EqualsProto(*client_message1), _)) + .WillOnce(SaveArg<1>(&engine_process_message_cb)); + EXPECT_CALL(engine_incoming_processor, + MockableProcessMessage(EqualsProto(*client_message2), _)) + .Times(1); + EXPECT_CALL(client_incoming_processor, + MockableProcessMessage(EqualsProto(*engine_message), _)) + .Times(1); + + // Attach the ends of the connection to our mock message-processors. + scoped_ptr<BlimpConnection> engine_connnection = engine_->TakeConnection(); + scoped_ptr<BlimpConnection> client_connnection = client.TakeConnection(); + engine_connnection->SetIncomingMessageProcessor(&engine_incoming_processor); + client_connnection->SetIncomingMessageProcessor(&client_incoming_processor); + + // Client sends the first message. + net::TestCompletionCallback client_send_callback1; + client_connnection->GetOutgoingMessageProcessor()->ProcessMessage( + std::move(client_message1), client_send_callback1.callback()); + EXPECT_EQ(net::OK, client_send_callback1.WaitForResult()); + + // Engine finishes processing the client message. + EXPECT_FALSE(engine_process_message_cb.is_null()); + engine_process_message_cb.Run(net::OK); + + // Engine sends one message. + net::TestCompletionCallback engine_send_callback; + engine_connnection->GetOutgoingMessageProcessor()->ProcessMessage( + std::move(engine_message), engine_send_callback.callback()); + EXPECT_EQ(net::OK, engine_send_callback.WaitForResult()); + + // Client sends the second message. + net::TestCompletionCallback client_send_callback2; + client_connnection->GetOutgoingMessageProcessor()->ProcessMessage( + std::move(client_message2), client_send_callback2.callback()); + EXPECT_EQ(net::OK, client_send_callback2.WaitForResult()); +} } // namespace |