| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "blimp/net/engine_connection_manager.h" | 5 #include "blimp/net/engine_connection_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/callback_helpers.h" | 12 #include "base/callback_helpers.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "blimp/net/blimp_connection.h" | |
| 17 #include "blimp/net/blimp_transport.h" | 16 #include "blimp/net/blimp_transport.h" |
| 18 #include "blimp/net/common.h" | 17 #include "blimp/net/common.h" |
| 19 #include "blimp/net/connection_error_observer.h" | 18 #include "blimp/net/connection_error_observer.h" |
| 19 #include "blimp/net/tcp_client_transport.h" |
| 20 #include "blimp/net/tcp_connection.h" |
| 20 #include "blimp/net/test_common.h" | 21 #include "blimp/net/test_common.h" |
| 21 #include "net/base/completion_callback.h" | 22 #include "net/base/completion_callback.h" |
| 22 #include "net/base/io_buffer.h" | 23 #include "net/base/io_buffer.h" |
| 24 #include "net/base/ip_address.h" |
| 23 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
| 24 #include "net/base/test_completion_callback.h" | 26 #include "net/base/test_completion_callback.h" |
| 25 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 29 |
| 28 using testing::_; | 30 using testing::_; |
| 29 using testing::Eq; | 31 using testing::Eq; |
| 30 using testing::Return; | 32 using testing::Return; |
| 31 using testing::SaveArg; | 33 using testing::SaveArg; |
| 32 | 34 |
| 33 namespace blimp { | 35 namespace blimp { |
| 34 | 36 |
| 35 class EngineConnectionManagerTest : public testing::Test { | 37 class EngineConnectionManagerTest : public testing::Test { |
| 36 public: | 38 public: |
| 37 EngineConnectionManagerTest() | 39 EngineConnectionManagerTest() |
| 38 : manager_(new EngineConnectionManager(&connection_handler_)) {} | 40 : manager_(new EngineConnectionManager(&connection_handler_, nullptr)) {} |
| 39 | 41 |
| 40 ~EngineConnectionManagerTest() override {} | 42 ~EngineConnectionManagerTest() override {} |
| 41 | 43 |
| 42 std::unique_ptr<BlimpConnection> CreateConnection() { | |
| 43 return base::MakeUnique<BlimpConnection>( | |
| 44 base::MakeUnique<MessagePort>(base::MakeUnique<MockPacketReader>(), | |
| 45 base::MakeUnique<MockPacketWriter>())); | |
| 46 } | |
| 47 | |
| 48 protected: | 44 protected: |
| 49 testing::StrictMock<MockConnectionHandler> connection_handler_; | 45 testing::StrictMock<MockConnectionHandler> connection_handler_; |
| 50 std::unique_ptr<EngineConnectionManager> manager_; | 46 std::unique_ptr<EngineConnectionManager> manager_; |
| 47 base::MessageLoopForIO message_loop_; |
| 51 }; | 48 }; |
| 52 | 49 |
| 53 TEST_F(EngineConnectionManagerTest, ConnectionSucceeds) { | 50 TEST_F(EngineConnectionManagerTest, ConnectionSucceeds) { |
| 54 std::unique_ptr<testing::StrictMock<MockTransport>> transport1( | 51 EXPECT_CALL(connection_handler_, HandleConnectionPtr(_)).Times(1); |
| 55 new testing::StrictMock<MockTransport>()); | |
| 56 std::unique_ptr<testing::StrictMock<MockTransport>> transport2( | |
| 57 new testing::StrictMock<MockTransport>()); | |
| 58 | 52 |
| 59 net::CompletionCallback connect_cb_1; | 53 net::IPAddress ip_addr(net::IPAddress::IPv4Localhost()); |
| 60 net::CompletionCallback connect_cb_2; | 54 net::IPEndPoint engine_endpoint(ip_addr, 0); |
| 61 EXPECT_CALL(*transport1, Connect(_)) | |
| 62 .Times(2) | |
| 63 .WillRepeatedly(SaveArg<0>(&connect_cb_1)); | |
| 64 EXPECT_CALL(*transport2, Connect(_)) | |
| 65 .Times(2) | |
| 66 .WillRepeatedly(SaveArg<0>(&connect_cb_2)); | |
| 67 | 55 |
| 68 std::unique_ptr<MessagePort> port1 = | 56 manager_->ConnectTransport(&engine_endpoint, |
| 69 base::MakeUnique<MessagePort>(base::MakeUnique<MockPacketReader>(), | 57 EngineConnectionManager::EngineTransportType::TCP); |
| 70 base::MakeUnique<MockPacketWriter>()); | |
| 71 std::unique_ptr<MessagePort> port2 = | |
| 72 base::MakeUnique<MessagePort>(base::MakeUnique<MockPacketReader>(), | |
| 73 base::MakeUnique<MockPacketWriter>()); | |
| 74 EXPECT_CALL(connection_handler_, HandleConnectionPtr(_)).Times(2); | |
| 75 | 58 |
| 76 EXPECT_CALL(*transport1, TakeMessagePortPtr()) | 59 net::TestCompletionCallback connect_callback; |
| 77 .WillOnce(Return(port1.release())); | 60 TCPClientTransport client(engine_endpoint, nullptr); |
| 78 EXPECT_CALL(*transport2, TakeMessagePortPtr()) | 61 client.Connect(connect_callback.callback()); |
| 79 .WillOnce(Return(port2.release())); | 62 EXPECT_EQ(net::OK, connect_callback.WaitForResult()); |
| 80 | |
| 81 ASSERT_TRUE(connect_cb_1.is_null()); | |
| 82 manager_->AddTransport(std::move(transport1)); | |
| 83 ASSERT_FALSE(connect_cb_1.is_null()); | |
| 84 | |
| 85 ASSERT_TRUE(connect_cb_2.is_null()); | |
| 86 manager_->AddTransport(std::move(transport2)); | |
| 87 ASSERT_FALSE(connect_cb_2.is_null()); | |
| 88 | |
| 89 base::ResetAndReturn(&connect_cb_1).Run(net::OK); | |
| 90 base::ResetAndReturn(&connect_cb_2).Run(net::OK); | |
| 91 ASSERT_FALSE(connect_cb_1.is_null()); | |
| 92 ASSERT_FALSE(connect_cb_2.is_null()); | |
| 93 } | 63 } |
| 94 | 64 |
| 95 } // namespace blimp | 65 } // namespace blimp |
| OLD | NEW |