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

Side by Side Diff: blimp/net/engine_connection_manager_unittest.cc

Issue 2439403003: Refactor BlimpConnection to TCPConnection (Closed)
Patch Set: Sync merge Created 4 years, 1 month 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 unified diff | Download patch
OLDNEW
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 // We explicitly do not rely on peeking at the connection's port as different
60 net::CompletionCallback connect_cb_2; 54 // implementations will have different ways of connecting to a client.
61 EXPECT_CALL(*transport1, Connect(_)) 55 // Instead, we retry for a freely available port number and use that instead.
62 .Times(2) 56 int port = 25467;
Garrett Casto 2016/10/25 18:33:50 I would think that you would want to do what the b
Kevin M 2016/10/25 18:49:23 +1, this is why we need the local listening port g
perumaal 2016/10/25 23:02:39 Again, this was to avoid relying on ports at all i
63 .WillRepeatedly(SaveArg<0>(&connect_cb_1));
64 EXPECT_CALL(*transport2, Connect(_))
65 .Times(2)
66 .WillRepeatedly(SaveArg<0>(&connect_cb_2));
67 57
68 std::unique_ptr<MessagePort> port1 = 58 do {
69 base::MakeUnique<MessagePort>(base::MakeUnique<MockPacketReader>(), 59 net::IPAddress ip_addr(127, 0, 0, 1);
Garrett Casto 2016/10/25 18:33:50 Nit: Use IPAddress::IPv4Localhost.
perumaal 2016/10/25 23:02:39 Done.
70 base::MakeUnique<MockPacketWriter>()); 60 net::IPEndPoint engine_endpoint(ip_addr, port);
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 61
76 EXPECT_CALL(*transport1, TakeMessagePortPtr()) 62 manager_->ConnectTransport(
77 .WillOnce(Return(port1.release())); 63 engine_endpoint,
78 EXPECT_CALL(*transport2, TakeMessagePortPtr()) 64 EngineConnectionManager::EngineTransportType::TCP);
79 .WillOnce(Return(port2.release()));
80 65
81 ASSERT_TRUE(connect_cb_1.is_null()); 66 net::TestCompletionCallback connect_callback;
82 manager_->AddTransport(std::move(transport1)); 67 TCPClientTransport client(engine_endpoint, nullptr);
83 ASSERT_FALSE(connect_cb_1.is_null()); 68 client.Connect(connect_callback.callback());
84 69 auto client_connect_result = connect_callback.WaitForResult();
85 ASSERT_TRUE(connect_cb_2.is_null()); 70 if (client_connect_result != net::OK) {
86 manager_->AddTransport(std::move(transport2)); 71 ++port;
87 ASSERT_FALSE(connect_cb_2.is_null()); 72 LOG(ERROR) << "Retrying with a different port : " << port;
88 73 } else {
89 base::ResetAndReturn(&connect_cb_1).Run(net::OK); 74 break;
90 base::ResetAndReturn(&connect_cb_2).Run(net::OK); 75 }
91 ASSERT_FALSE(connect_cb_1.is_null()); 76 } while (port > 0);
92 ASSERT_FALSE(connect_cb_2.is_null());
93 } 77 }
94 78
95 } // namespace blimp 79 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698