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

Side by Side Diff: blimp/net/tcp_connection_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/blimp_connection.h" 5 #include "blimp/net/tcp_connection.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 "blimp/common/create_blimp_message.h" 15 #include "blimp/common/create_blimp_message.h"
16 #include "blimp/common/proto/blimp_message.pb.h" 16 #include "blimp/common/proto/blimp_message.pb.h"
17 #include "blimp/net/common.h" 17 #include "blimp/net/common.h"
18 #include "blimp/net/connection_error_observer.h" 18 #include "blimp/net/connection_error_observer.h"
19 #include "blimp/net/message_port.h" 19 #include "blimp/net/message_port.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"
23 #include "net/base/net_errors.h" 24 #include "net/base/net_errors.h"
24 #include "net/base/test_completion_callback.h" 25 #include "net/base/test_completion_callback.h"
25 #include "testing/gmock/include/gmock/gmock.h" 26 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
27 28
28 using testing::_; 29 using testing::_;
29 using testing::InSequence; 30 using testing::InSequence;
30 using testing::Return; 31 using testing::Return;
31 using testing::SaveArg; 32 using testing::SaveArg;
32 33
33 namespace blimp { 34 namespace blimp {
34 namespace { 35 namespace {
35 36
37 // NOTE: TCPConnection and BlimpConnection will soon be replaced with a
38 // pure-gRPC implementation. So not adding more unit-tests here. Instead,
39 // we are reusing TCPConnection as the implementation under test here.
Garrett Casto 2016/10/25 18:33:50 Nit: Not sure if you need this disclaimer since yo
perumaal 2016/10/25 23:02:40 I avoided renaming BlimpConnectionTest but you are
36 class BlimpConnectionTest : public testing::Test { 40 class BlimpConnectionTest : public testing::Test {
37 public: 41 public:
38 BlimpConnectionTest() { 42 BlimpConnectionTest() {
39 std::unique_ptr<MockPacketReader> mock_reader(new MockPacketReader); 43 std::unique_ptr<MockPacketReader> mock_reader(new MockPacketReader);
40 std::unique_ptr<MockPacketWriter> mock_writer(new MockPacketWriter); 44 std::unique_ptr<MockPacketWriter> mock_writer(new MockPacketWriter);
41 mock_reader_ = mock_reader.get(); 45 mock_reader_ = mock_reader.get();
42 mock_writer_ = mock_writer.get(); 46 mock_writer_ = mock_writer.get();
43 connection_ = 47 connection_ = base::MakeUnique<TCPConnection>(base::MakeUnique<MessagePort>(
44 base::MakeUnique<BlimpConnection>(base::MakeUnique<MessagePort>( 48 std::move(mock_reader), std::move(mock_writer)));
45 std::move(mock_reader), std::move(mock_writer)));
46 49
47 connection_->AddConnectionErrorObserver(&error_observer1_); 50 connection_->AddConnectionErrorObserver(&error_observer1_);
48 connection_->AddConnectionErrorObserver(&error_observer2_); 51 connection_->AddConnectionErrorObserver(&error_observer2_);
49 connection_->AddConnectionErrorObserver(&error_observer3_); 52 connection_->AddConnectionErrorObserver(&error_observer3_);
50 connection_->RemoveConnectionErrorObserver(&error_observer3_); 53 connection_->RemoveConnectionErrorObserver(&error_observer3_);
51 } 54 }
52 55
53 ~BlimpConnectionTest() override {} 56 ~BlimpConnectionTest() override {}
54 57
55 void DropConnection() { connection_.reset(); } 58 void DropConnection() { connection_.reset(); }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 WritePacket(BufferEqualsProto(*CreateControlMessage()), _)) 95 WritePacket(BufferEqualsProto(*CreateControlMessage()), _))
93 .WillOnce(SaveArg<1>(&write_packet_cb)) 96 .WillOnce(SaveArg<1>(&write_packet_cb))
94 .RetiresOnSaturation(); 97 .RetiresOnSaturation();
95 EXPECT_CALL(error_observer1_, OnConnectionError(_)).Times(0); 98 EXPECT_CALL(error_observer1_, OnConnectionError(_)).Times(0);
96 EXPECT_CALL(error_observer2_, OnConnectionError(_)).Times(0); 99 EXPECT_CALL(error_observer2_, OnConnectionError(_)).Times(0);
97 EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0); 100 EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0);
98 101
99 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor(); 102 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor();
100 net::TestCompletionCallback complete_cb_1; 103 net::TestCompletionCallback complete_cb_1;
101 ASSERT_TRUE(write_packet_cb.is_null()); 104 ASSERT_TRUE(write_packet_cb.is_null());
102 sender->ProcessMessage(CreateInputMessage(), 105 sender->ProcessMessage(CreateInputMessage(), complete_cb_1.callback());
103 complete_cb_1.callback());
104 ASSERT_FALSE(write_packet_cb.is_null()); 106 ASSERT_FALSE(write_packet_cb.is_null());
105 base::ResetAndReturn(&write_packet_cb).Run(net::OK); 107 base::ResetAndReturn(&write_packet_cb).Run(net::OK);
106 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult()); 108 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult());
107 109
108 net::TestCompletionCallback complete_cb_2; 110 net::TestCompletionCallback complete_cb_2;
109 ASSERT_TRUE(write_packet_cb.is_null()); 111 ASSERT_TRUE(write_packet_cb.is_null());
110 sender->ProcessMessage(CreateControlMessage(), 112 sender->ProcessMessage(CreateControlMessage(), complete_cb_2.callback());
111 complete_cb_2.callback());
112 ASSERT_FALSE(write_packet_cb.is_null()); 113 ASSERT_FALSE(write_packet_cb.is_null());
113 base::ResetAndReturn(&write_packet_cb).Run(net::OK); 114 base::ResetAndReturn(&write_packet_cb).Run(net::OK);
114 EXPECT_EQ(net::OK, complete_cb_2.WaitForResult()); 115 EXPECT_EQ(net::OK, complete_cb_2.WaitForResult());
115 } 116 }
116 117
117 // Writer completes writing two packets asynchronously. 118 // Writer completes writing two packets asynchronously.
118 // First write succeeds, second fails. 119 // First write succeeds, second fails.
119 TEST_F(BlimpConnectionTest, AsyncTwoPacketsWriteWithError) { 120 TEST_F(BlimpConnectionTest, AsyncTwoPacketsWriteWithError) {
120 net::CompletionCallback write_packet_cb; 121 net::CompletionCallback write_packet_cb;
121 122
122 InSequence s; 123 InSequence s;
123 EXPECT_CALL(*mock_writer_, 124 EXPECT_CALL(*mock_writer_,
124 WritePacket(BufferEqualsProto(*CreateInputMessage()), _)) 125 WritePacket(BufferEqualsProto(*CreateInputMessage()), _))
125 .WillOnce(SaveArg<1>(&write_packet_cb)) 126 .WillOnce(SaveArg<1>(&write_packet_cb))
126 .RetiresOnSaturation(); 127 .RetiresOnSaturation();
127 EXPECT_CALL(*mock_writer_, 128 EXPECT_CALL(*mock_writer_,
128 WritePacket(BufferEqualsProto(*CreateControlMessage()), _)) 129 WritePacket(BufferEqualsProto(*CreateControlMessage()), _))
129 .WillOnce(SaveArg<1>(&write_packet_cb)) 130 .WillOnce(SaveArg<1>(&write_packet_cb))
130 .RetiresOnSaturation(); 131 .RetiresOnSaturation();
131 EXPECT_CALL(error_observer1_, OnConnectionError(net::ERR_FAILED)); 132 EXPECT_CALL(error_observer1_, OnConnectionError(net::ERR_FAILED));
132 EXPECT_CALL(error_observer2_, OnConnectionError(net::ERR_FAILED)); 133 EXPECT_CALL(error_observer2_, OnConnectionError(net::ERR_FAILED));
133 EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0); 134 EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0);
134 135
135 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor(); 136 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor();
136 net::TestCompletionCallback complete_cb_1; 137 net::TestCompletionCallback complete_cb_1;
137 sender->ProcessMessage(CreateInputMessage(), 138 sender->ProcessMessage(CreateInputMessage(), complete_cb_1.callback());
138 complete_cb_1.callback());
139 base::ResetAndReturn(&write_packet_cb).Run(net::OK); 139 base::ResetAndReturn(&write_packet_cb).Run(net::OK);
140 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult()); 140 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult());
141 141
142 net::TestCompletionCallback complete_cb_2; 142 net::TestCompletionCallback complete_cb_2;
143 sender->ProcessMessage(CreateControlMessage(), 143 sender->ProcessMessage(CreateControlMessage(), complete_cb_2.callback());
144 complete_cb_2.callback());
145 base::ResetAndReturn(&write_packet_cb).Run(net::ERR_FAILED); 144 base::ResetAndReturn(&write_packet_cb).Run(net::ERR_FAILED);
146 EXPECT_EQ(net::ERR_FAILED, complete_cb_2.WaitForResult()); 145 EXPECT_EQ(net::ERR_FAILED, complete_cb_2.WaitForResult());
147 } 146 }
148 147
149 TEST_F(BlimpConnectionTest, DeleteHappyObserversAreOK) { 148 TEST_F(BlimpConnectionTest, DeleteHappyObserversAreOK) {
150 net::CompletionCallback write_packet_cb; 149 net::CompletionCallback write_packet_cb;
151 150
152 InSequence s; 151 InSequence s;
153 EXPECT_CALL(*mock_writer_, 152 EXPECT_CALL(*mock_writer_,
154 WritePacket(BufferEqualsProto(*CreateInputMessage()), _)) 153 WritePacket(BufferEqualsProto(*CreateInputMessage()), _))
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 220
222 // Put the EndConnection message in the buffer and invoke the read callback. 221 // Put the EndConnection message in the buffer and invoke the read callback.
223 read_packet_buffer->SetCapacity(message->ByteSize()); 222 read_packet_buffer->SetCapacity(message->ByteSize());
224 ASSERT_TRUE(message->SerializeToArray(read_packet_buffer->data(), 223 ASSERT_TRUE(message->SerializeToArray(read_packet_buffer->data(),
225 message->GetCachedSize())); 224 message->GetCachedSize()));
226 base::ResetAndReturn(&read_packet_cb).Run(message->ByteSize()); 225 base::ResetAndReturn(&read_packet_cb).Run(message->ByteSize());
227 } 226 }
228 227
229 } // namespace 228 } // namespace
230 } // namespace blimp 229 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698