Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1999)

Unified Diff: blimp/net/blimp_connection_unittest.cc

Issue 2439403003: Refactor BlimpConnection to TCPConnection (Closed)
Patch Set: Added missing Engine Transport Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: blimp/net/blimp_connection_unittest.cc
diff --git a/blimp/net/blimp_connection_unittest.cc b/blimp/net/blimp_connection_unittest.cc
deleted file mode 100644
index 681ea2b77be4ab9030c39962aeb3343bcd665bcf..0000000000000000000000000000000000000000
--- a/blimp/net/blimp_connection_unittest.cc
+++ /dev/null
@@ -1,230 +0,0 @@
-// Copyright 2015 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "blimp/net/blimp_connection.h"
-
-#include <stddef.h>
-
-#include <string>
-#include <utility>
-
-#include "base/callback_helpers.h"
-#include "base/memory/ptr_util.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/net/common.h"
-#include "blimp/net/connection_error_observer.h"
-#include "blimp/net/message_port.h"
-#include "blimp/net/test_common.h"
-#include "net/base/completion_callback.h"
-#include "net/base/io_buffer.h"
-#include "net/base/net_errors.h"
-#include "net/base/test_completion_callback.h"
-#include "testing/gmock/include/gmock/gmock.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-using testing::_;
-using testing::InSequence;
-using testing::Return;
-using testing::SaveArg;
-
-namespace blimp {
-namespace {
-
-class BlimpConnectionTest : public testing::Test {
- public:
- BlimpConnectionTest() {
- std::unique_ptr<MockPacketReader> mock_reader(new MockPacketReader);
- std::unique_ptr<MockPacketWriter> mock_writer(new MockPacketWriter);
- mock_reader_ = mock_reader.get();
- mock_writer_ = mock_writer.get();
- connection_ =
- base::MakeUnique<BlimpConnection>(base::MakeUnique<MessagePort>(
- std::move(mock_reader), std::move(mock_writer)));
-
- connection_->AddConnectionErrorObserver(&error_observer1_);
- connection_->AddConnectionErrorObserver(&error_observer2_);
- connection_->AddConnectionErrorObserver(&error_observer3_);
- connection_->RemoveConnectionErrorObserver(&error_observer3_);
- }
-
- ~BlimpConnectionTest() override {}
-
- void DropConnection() { connection_.reset(); }
-
- protected:
- std::unique_ptr<BlimpMessage> CreateInputMessage() {
- InputMessage* input;
- return CreateBlimpMessage(&input);
- }
-
- std::unique_ptr<BlimpMessage> CreateControlMessage() {
- TabControlMessage* control;
- return CreateBlimpMessage(&control);
- }
-
- base::MessageLoop message_loop_;
- MockPacketReader* mock_reader_;
- MockPacketWriter* mock_writer_;
- testing::StrictMock<MockConnectionErrorObserver> error_observer1_;
- testing::StrictMock<MockConnectionErrorObserver> error_observer2_;
-
- // This error observer is Removed() immediately after it's added;
- // it should never be called.
- testing::StrictMock<MockConnectionErrorObserver> error_observer3_;
-
- testing::StrictMock<MockBlimpMessageProcessor> receiver_;
- std::unique_ptr<BlimpConnection> connection_;
-};
-
-// Write completes writing two packets asynchronously.
-TEST_F(BlimpConnectionTest, AsyncTwoPacketsWrite) {
- net::CompletionCallback write_packet_cb;
-
- InSequence s;
- EXPECT_CALL(*mock_writer_,
- WritePacket(BufferEqualsProto(*CreateInputMessage()), _))
- .WillOnce(SaveArg<1>(&write_packet_cb))
- .RetiresOnSaturation();
- EXPECT_CALL(*mock_writer_,
- WritePacket(BufferEqualsProto(*CreateControlMessage()), _))
- .WillOnce(SaveArg<1>(&write_packet_cb))
- .RetiresOnSaturation();
- EXPECT_CALL(error_observer1_, OnConnectionError(_)).Times(0);
- EXPECT_CALL(error_observer2_, OnConnectionError(_)).Times(0);
- EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0);
-
- BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor();
- net::TestCompletionCallback complete_cb_1;
- ASSERT_TRUE(write_packet_cb.is_null());
- sender->ProcessMessage(CreateInputMessage(),
- complete_cb_1.callback());
- ASSERT_FALSE(write_packet_cb.is_null());
- base::ResetAndReturn(&write_packet_cb).Run(net::OK);
- EXPECT_EQ(net::OK, complete_cb_1.WaitForResult());
-
- net::TestCompletionCallback complete_cb_2;
- ASSERT_TRUE(write_packet_cb.is_null());
- sender->ProcessMessage(CreateControlMessage(),
- complete_cb_2.callback());
- ASSERT_FALSE(write_packet_cb.is_null());
- base::ResetAndReturn(&write_packet_cb).Run(net::OK);
- EXPECT_EQ(net::OK, complete_cb_2.WaitForResult());
-}
-
-// Writer completes writing two packets asynchronously.
-// First write succeeds, second fails.
-TEST_F(BlimpConnectionTest, AsyncTwoPacketsWriteWithError) {
- net::CompletionCallback write_packet_cb;
-
- InSequence s;
- EXPECT_CALL(*mock_writer_,
- WritePacket(BufferEqualsProto(*CreateInputMessage()), _))
- .WillOnce(SaveArg<1>(&write_packet_cb))
- .RetiresOnSaturation();
- EXPECT_CALL(*mock_writer_,
- WritePacket(BufferEqualsProto(*CreateControlMessage()), _))
- .WillOnce(SaveArg<1>(&write_packet_cb))
- .RetiresOnSaturation();
- EXPECT_CALL(error_observer1_, OnConnectionError(net::ERR_FAILED));
- EXPECT_CALL(error_observer2_, OnConnectionError(net::ERR_FAILED));
- EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0);
-
- BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor();
- net::TestCompletionCallback complete_cb_1;
- sender->ProcessMessage(CreateInputMessage(),
- complete_cb_1.callback());
- base::ResetAndReturn(&write_packet_cb).Run(net::OK);
- EXPECT_EQ(net::OK, complete_cb_1.WaitForResult());
-
- net::TestCompletionCallback complete_cb_2;
- sender->ProcessMessage(CreateControlMessage(),
- complete_cb_2.callback());
- base::ResetAndReturn(&write_packet_cb).Run(net::ERR_FAILED);
- EXPECT_EQ(net::ERR_FAILED, complete_cb_2.WaitForResult());
-}
-
-TEST_F(BlimpConnectionTest, DeleteHappyObserversAreOK) {
- net::CompletionCallback write_packet_cb;
-
- InSequence s;
- EXPECT_CALL(*mock_writer_,
- WritePacket(BufferEqualsProto(*CreateInputMessage()), _))
- .WillOnce(SaveArg<1>(&write_packet_cb))
- .RetiresOnSaturation();
- EXPECT_CALL(error_observer1_, OnConnectionError(net::ERR_FAILED))
- .WillOnce(testing::InvokeWithoutArgs(
- this, &BlimpConnectionTest::DropConnection));
-
- BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor();
- net::TestCompletionCallback complete_cb_1;
- sender->ProcessMessage(CreateInputMessage(), complete_cb_1.callback());
- base::ResetAndReturn(&write_packet_cb).Run(net::ERR_FAILED);
- EXPECT_EQ(net::ERR_FAILED, complete_cb_1.WaitForResult());
-}
-
-// Verifies that a ReadPacket error causes ErrorObservers to be notified.
-TEST_F(BlimpConnectionTest, ReadPacketErrorInvokesErrorObservers) {
- scoped_refptr<net::GrowableIOBuffer> read_packet_buffer;
- net::CompletionCallback read_packet_cb;
-
- EXPECT_CALL(*mock_reader_, ReadPacket(_, _))
- .WillOnce(
- DoAll(SaveArg<0>(&read_packet_buffer), SaveArg<1>(&read_packet_cb)))
- .RetiresOnSaturation();
-
- EXPECT_CALL(error_observer1_, OnConnectionError(net::ERR_FAILED));
- EXPECT_CALL(error_observer2_, OnConnectionError(net::ERR_FAILED));
- EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0);
-
- EXPECT_CALL(receiver_, MockableProcessMessage(_, _)).Times(0);
-
- // Trigger the first ReadPacket() call by setting the MessageProcessor.
- connection_->SetIncomingMessageProcessor(&receiver_);
- EXPECT_TRUE(read_packet_buffer);
- EXPECT_FALSE(read_packet_cb.is_null());
-
- // Signal an error back from the ReadPacket operation.
- base::ResetAndReturn(&read_packet_cb).Run(net::ERR_FAILED);
-}
-
-// Verifies that EndConnection messages received from the peer are
-// routed through to registered ConnectionErrorObservers as errors.
-TEST_F(BlimpConnectionTest, EndConnectionInvokesErrorObservers) {
- scoped_refptr<net::GrowableIOBuffer> read_packet_buffer;
- net::CompletionCallback read_packet_cb;
-
- EXPECT_CALL(*mock_reader_, ReadPacket(_, _))
- .WillOnce(
- DoAll(SaveArg<0>(&read_packet_buffer), SaveArg<1>(&read_packet_cb)))
- .WillOnce(Return())
- .RetiresOnSaturation();
-
- EXPECT_CALL(error_observer1_,
- OnConnectionError(EndConnectionMessage::PROTOCOL_MISMATCH));
- EXPECT_CALL(error_observer2_,
- OnConnectionError(EndConnectionMessage::PROTOCOL_MISMATCH));
- EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0);
-
- EXPECT_CALL(receiver_, MockableProcessMessage(_, _)).Times(0);
-
- // Trigger the first ReadPacket() call by setting the MessageProcessor.
- connection_->SetIncomingMessageProcessor(&receiver_);
- EXPECT_TRUE(read_packet_buffer);
- EXPECT_FALSE(read_packet_cb.is_null());
-
- // Create an EndConnection message to return from ReadPacket.
- std::unique_ptr<BlimpMessage> message =
- CreateEndConnectionMessage(EndConnectionMessage::PROTOCOL_MISMATCH);
-
- // Put the EndConnection message in the buffer and invoke the read callback.
- read_packet_buffer->SetCapacity(message->ByteSize());
- ASSERT_TRUE(message->SerializeToArray(read_packet_buffer->data(),
- message->GetCachedSize()));
- base::ResetAndReturn(&read_packet_cb).Run(message->ByteSize());
-}
-
-} // namespace
-} // namespace blimp

Powered by Google App Engine
This is Rietveld 408576698