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

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

Issue 2236093002: Decouple Blimp transport output from BlimpConnections using MessagePort. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@statistics-singleton
Patch Set: wez feedback Created 4 years, 4 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 unified diff | Download patch
« no previous file with comments | « blimp/net/tcp_engine_transport.cc ('k') | blimp/net/test_common.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <algorithm>
5 #include <memory> 6 #include <memory>
6 #include <string> 7 #include <string>
7 8
8 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
9 #include "blimp/common/create_blimp_message.h" 10 #include "blimp/common/create_blimp_message.h"
10 #include "blimp/common/proto/blimp_message.pb.h" 11 #include "blimp/common/proto/blimp_message.pb.h"
11 #include "blimp/common/proto/protocol_control.pb.h" 12 #include "blimp/common/proto/protocol_control.pb.h"
12 #include "blimp/net/blimp_connection.h" 13 #include "blimp/net/blimp_connection.h"
13 #include "blimp/net/blimp_stats.h" 14 #include "blimp/net/blimp_stats.h"
14 #include "blimp/net/tcp_client_transport.h" 15 #include "blimp/net/tcp_client_transport.h"
15 #include "blimp/net/tcp_engine_transport.h" 16 #include "blimp/net/tcp_engine_transport.h"
16 #include "blimp/net/test_common.h" 17 #include "blimp/net/test_common.h"
17 #include "net/base/address_list.h" 18 #include "net/base/address_list.h"
18 #include "net/base/ip_address.h" 19 #include "net/base/ip_address.h"
19 #include "net/base/ip_endpoint.h" 20 #include "net/base/ip_endpoint.h"
20 #include "net/base/net_errors.h" 21 #include "net/base/net_errors.h"
21 #include "net/base/test_completion_callback.h" 22 #include "net/base/test_completion_callback.h"
22 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
23 24
24 using testing::_; 25 using testing::_;
25 using testing::SaveArg; 26 using testing::SaveArg;
26 27
27 namespace blimp { 28 namespace blimp {
28 29
29 namespace { 30 namespace {
30 31
31 // Integration test for TCPEngineTransport and TCPClientTransport. 32 // Integration test for TCPEngineTransport and TCPClientTransport.
32 class TCPTransportTest : public testing::Test { 33 class TCPTransportTest : public testing::Test {
33 protected: 34 protected:
34 TCPTransportTest() { 35 TCPTransportTest()
35 net::IPEndPoint local_address(net::IPAddress(127, 0, 0, 1), 0); 36 : local_address_(net::IPAddress(127, 0, 0, 1), 0),
36 engine_.reset(new TCPEngineTransport(local_address, nullptr)); 37 engine_(local_address_, nullptr),
38 read_buffer_(new net::GrowableIOBuffer) {
39 size_t buf_size = std::max(payload_1_.size(), payload_2_.size());
40 write_buffer_ = make_scoped_refptr(
41 new net::DrainableIOBuffer(new net::IOBuffer(buf_size), buf_size));
42 read_buffer_->SetCapacity(buf_size);
37 } 43 }
38 44
39 net::IPEndPoint GetLocalEndpoint() const { 45 net::IPEndPoint GetLocalEndpoint() const {
40 net::IPEndPoint local_address; 46 net::IPEndPoint local_address;
41 CHECK_EQ(net::OK, engine_->GetLocalAddress(&local_address)); 47 CHECK_EQ(net::OK, engine_.GetLocalAddress(&local_address));
42 return local_address; 48 return local_address;
43 } 49 }
44 50
51 std::string payload_1_ = "foo";
52 std::string payload_2_ = "bar";
45 base::MessageLoopForIO message_loop_; 53 base::MessageLoopForIO message_loop_;
46 std::unique_ptr<TCPEngineTransport> engine_; 54 net::IPEndPoint local_address_;
55 TCPEngineTransport engine_;
56 scoped_refptr<net::DrainableIOBuffer> write_buffer_;
57 scoped_refptr<net::GrowableIOBuffer> read_buffer_;
47 }; 58 };
48 59
49 TEST_F(TCPTransportTest, Connect) { 60 TEST_F(TCPTransportTest, Connect) {
50 net::TestCompletionCallback accept_callback; 61 net::TestCompletionCallback accept_callback;
51 engine_->Connect(accept_callback.callback()); 62 engine_.Connect(accept_callback.callback());
52 63
53 net::TestCompletionCallback connect_callback; 64 net::TestCompletionCallback connect_callback;
54 TCPClientTransport client(GetLocalEndpoint(), nullptr); 65 TCPClientTransport client(GetLocalEndpoint(), nullptr);
55 client.Connect(connect_callback.callback()); 66 client.Connect(connect_callback.callback());
56 67
57 EXPECT_EQ(net::OK, connect_callback.WaitForResult()); 68 EXPECT_EQ(net::OK, connect_callback.WaitForResult());
58 EXPECT_EQ(net::OK, accept_callback.WaitForResult()); 69 EXPECT_EQ(net::OK, accept_callback.WaitForResult());
59 EXPECT_TRUE(engine_->TakeConnection() != nullptr); 70 EXPECT_NE(nullptr, client.TakeMessagePort());
60 } 71 }
61 72
62 TEST_F(TCPTransportTest, TwoClientConnections) { 73 TEST_F(TCPTransportTest, TwoClientConnections) {
63 net::TestCompletionCallback accept_callback1; 74 net::TestCompletionCallback accept_callback1;
64 engine_->Connect(accept_callback1.callback()); 75 engine_.Connect(accept_callback1.callback());
65 76
66 net::TestCompletionCallback connect_callback1; 77 net::TestCompletionCallback connect_callback1;
67 TCPClientTransport client1(GetLocalEndpoint(), nullptr); 78 TCPClientTransport client(GetLocalEndpoint(), nullptr);
68 client1.Connect(connect_callback1.callback()); 79 client.Connect(connect_callback1.callback());
80 EXPECT_EQ(net::OK, connect_callback1.WaitForResult());
81 EXPECT_EQ(net::OK, accept_callback1.WaitForResult());
82 EXPECT_NE(nullptr, engine_.TakeMessagePort());
83
84 net::TestCompletionCallback accept_callback2;
85 engine_.Connect(accept_callback2.callback());
69 86
70 net::TestCompletionCallback connect_callback2; 87 net::TestCompletionCallback connect_callback2;
71 TCPClientTransport client2(GetLocalEndpoint(), nullptr); 88 TCPClientTransport client2(GetLocalEndpoint(), nullptr);
72 client2.Connect(connect_callback2.callback()); 89 client2.Connect(connect_callback2.callback());
73
74 EXPECT_EQ(net::OK, connect_callback1.WaitForResult());
75 EXPECT_EQ(net::OK, accept_callback1.WaitForResult());
76 EXPECT_TRUE(engine_->TakeConnection() != nullptr);
77
78 net::TestCompletionCallback accept_callback2;
79 engine_->Connect(accept_callback2.callback());
80 EXPECT_EQ(net::OK, connect_callback2.WaitForResult()); 90 EXPECT_EQ(net::OK, connect_callback2.WaitForResult());
81 EXPECT_EQ(net::OK, accept_callback2.WaitForResult()); 91 EXPECT_EQ(net::OK, accept_callback2.WaitForResult());
82 EXPECT_TRUE(engine_->TakeConnection() != nullptr); 92 EXPECT_NE(nullptr, engine_.TakeMessagePort());
83 } 93 }
84 94
85 TEST_F(TCPTransportTest, ExchangeMessages) { 95 TEST_F(TCPTransportTest, ExchangeMessages) {
86 // Start the Engine transport and connect a client to it. 96 // Start the Engine transport and connect a client to it.
87 net::TestCompletionCallback accept_callback; 97 net::TestCompletionCallback accept_callback;
88 engine_->Connect(accept_callback.callback()); 98 engine_.Connect(accept_callback.callback());
89 net::TestCompletionCallback client_connect_callback; 99 net::TestCompletionCallback client_connect_callback;
90 TCPClientTransport client(GetLocalEndpoint(), nullptr); 100 TCPClientTransport client(GetLocalEndpoint(), nullptr);
91 client.Connect(client_connect_callback.callback()); 101 client.Connect(client_connect_callback.callback());
102 EXPECT_EQ(net::OK, accept_callback.WaitForResult());
92 EXPECT_EQ(net::OK, client_connect_callback.WaitForResult()); 103 EXPECT_EQ(net::OK, client_connect_callback.WaitForResult());
93 EXPECT_EQ(net::OK, accept_callback.WaitForResult());
94 104
95 // Expect the engine to get two messages from the client, and the client to 105 std::unique_ptr<MessagePort> engine_message_port = engine_.TakeMessagePort();
96 // get one from the engine. 106 std::unique_ptr<MessagePort> clientmessage_port = client.TakeMessagePort();
97 MockBlimpMessageProcessor engine_incoming_processor;
98 MockBlimpMessageProcessor client_incoming_processor;
99 net::CompletionCallback engine_process_message_cb;
100 std::unique_ptr<BlimpMessage> client_message1 =
101 CreateStartConnectionMessage("", 0);
102 int client_message1_size = client_message1->ByteSize();
103 std::unique_ptr<BlimpMessage> client_message2 = CreateCheckpointAckMessage(5);
104 std::unique_ptr<BlimpMessage> engine_message = CreateCheckpointAckMessage(10);
105 EXPECT_CALL(engine_incoming_processor,
106 MockableProcessMessage(EqualsProto(*client_message1), _))
107 .WillOnce(SaveArg<1>(&engine_process_message_cb));
108 EXPECT_CALL(engine_incoming_processor,
109 MockableProcessMessage(EqualsProto(*client_message2), _))
110 .Times(1);
111 EXPECT_CALL(client_incoming_processor,
112 MockableProcessMessage(EqualsProto(*engine_message), _))
113 .Times(1);
114 107
115 // Attach the ends of the connection to our mock message-processors. 108 // Engine sends payload_1_ to client.
116 std::unique_ptr<BlimpConnection> engine_connnection = 109 net::TestCompletionCallback read_cb1;
117 engine_->TakeConnection(); 110 net::TestCompletionCallback write_cb1;
118 std::unique_ptr<BlimpConnection> client_connnection = client.TakeConnection(); 111 memcpy(write_buffer_->data(), payload_1_.data(), payload_1_.size());
119 engine_connnection->SetIncomingMessageProcessor(&engine_incoming_processor); 112 engine_message_port->writer()->WritePacket(write_buffer_,
120 client_connnection->SetIncomingMessageProcessor(&client_incoming_processor); 113 write_cb1.callback());
114 clientmessage_port->reader()->ReadPacket(read_buffer_, read_cb1.callback());
115 EXPECT_EQ(payload_1_.size(), static_cast<size_t>(read_cb1.WaitForResult()));
116 EXPECT_EQ(net::OK, write_cb1.WaitForResult());
117 EXPECT_TRUE(
118 BufferStartsWith(read_buffer_.get(), payload_1_.size(), payload_1_));
121 119
122 // Client sends the first message. 120 // Client sends payload_2_ to engine.
123 net::TestCompletionCallback client_send_callback1; 121 net::TestCompletionCallback read_cb2;
124 client_connnection->GetOutgoingMessageProcessor()->ProcessMessage( 122 net::TestCompletionCallback write_cb2;
125 std::move(client_message1), client_send_callback1.callback()); 123 memcpy(write_buffer_->data(), payload_2_.data(), payload_2_.size());
126 EXPECT_EQ(net::OK, client_send_callback1.WaitForResult()); 124 clientmessage_port->writer()->WritePacket(write_buffer_,
127 125 write_cb2.callback());
128 // Engine finishes processing the client message. 126 engine_message_port->reader()->ReadPacket(read_buffer_, read_cb2.callback());
129 EXPECT_FALSE(engine_process_message_cb.is_null()); 127 EXPECT_EQ(payload_2_.size(), static_cast<size_t>(read_cb2.WaitForResult()));
130 engine_process_message_cb.Run(client_message1_size); 128 EXPECT_EQ(net::OK, write_cb2.WaitForResult());
131 129 EXPECT_TRUE(
132 // Engine sends one message. 130 BufferStartsWith(read_buffer_.get(), payload_2_.size(), payload_2_));
133 net::TestCompletionCallback engine_send_callback;
134 engine_connnection->GetOutgoingMessageProcessor()->ProcessMessage(
135 std::move(engine_message), engine_send_callback.callback());
136 EXPECT_EQ(net::OK, engine_send_callback.WaitForResult());
137
138 // Client sends the second message.
139 net::TestCompletionCallback client_send_callback2;
140 client_connnection->GetOutgoingMessageProcessor()->ProcessMessage(
141 std::move(client_message2), client_send_callback2.callback());
142 EXPECT_EQ(net::OK, client_send_callback2.WaitForResult());
143 } 131 }
144 132
145 } // namespace 133 } // namespace
146 134
147 } // namespace blimp 135 } // namespace blimp
OLDNEW
« no previous file with comments | « blimp/net/tcp_engine_transport.cc ('k') | blimp/net/test_common.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698