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

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: 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
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 <memory> 5 #include <memory>
6 #include <string> 6 #include <string>
7 7
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "blimp/common/create_blimp_message.h" 9 #include "blimp/common/create_blimp_message.h"
10 #include "blimp/common/proto/blimp_message.pb.h" 10 #include "blimp/common/proto/blimp_message.pb.h"
(...skipping 10 matching lines...) Expand all
21 #include "net/base/test_completion_callback.h" 21 #include "net/base/test_completion_callback.h"
22 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
23 23
24 using testing::_; 24 using testing::_;
25 using testing::SaveArg; 25 using testing::SaveArg;
26 26
27 namespace blimp { 27 namespace blimp {
28 28
29 namespace { 29 namespace {
30 30
31 // The length of |kPayload1| and |kPayload2| must be identical.
CJ 2016/08/12 23:59:15 Perhaps add why they must be identical? It seems t
Kevin M 2016/08/15 17:35:09 You're right, it does seem arbitrary. I removed th
32 const char kPayload1[] = "foo";
33 const char kPayload2[] = "bar";
34
31 // Integration test for TCPEngineTransport and TCPClientTransport. 35 // Integration test for TCPEngineTransport and TCPClientTransport.
32 class TCPTransportTest : public testing::Test { 36 class TCPTransportTest : public testing::Test {
33 protected: 37 protected:
34 TCPTransportTest() { 38 TCPTransportTest()
35 net::IPEndPoint local_address(net::IPAddress(127, 0, 0, 1), 0); 39 : local_address_(net::IPAddress(127, 0, 0, 1), 0),
36 engine_.reset(new TCPEngineTransport(local_address, nullptr)); 40 engine_(local_address_, nullptr),
41 write_buffer_(
42 new net::DrainableIOBuffer(new net::IOBuffer(arraysize(kPayload1)),
43 arraysize(kPayload1))),
44 read_buffer_(new net::GrowableIOBuffer) {
45 read_buffer_->SetCapacity(arraysize(kPayload1));
37 } 46 }
38 47
39 net::IPEndPoint GetLocalEndpoint() const { 48 net::IPEndPoint GetLocalEndpoint() const {
40 net::IPEndPoint local_address; 49 net::IPEndPoint local_address;
41 CHECK_EQ(net::OK, engine_->GetLocalAddress(&local_address)); 50 CHECK_EQ(net::OK, engine_.GetLocalAddress(&local_address));
42 return local_address; 51 return local_address;
43 } 52 }
44 53
45 base::MessageLoopForIO message_loop_; 54 base::MessageLoopForIO message_loop_;
46 std::unique_ptr<TCPEngineTransport> engine_; 55 net::IPEndPoint local_address_;
56 TCPEngineTransport engine_;
57 scoped_refptr<net::DrainableIOBuffer> write_buffer_;
58 scoped_refptr<net::GrowableIOBuffer> read_buffer_;
47 }; 59 };
48 60
49 TEST_F(TCPTransportTest, Connect) { 61 TEST_F(TCPTransportTest, Connect) {
50 net::TestCompletionCallback accept_callback; 62 net::TestCompletionCallback accept_callback;
51 engine_->Connect(accept_callback.callback()); 63 engine_.Connect(accept_callback.callback());
52 64
53 net::TestCompletionCallback connect_callback; 65 net::TestCompletionCallback connect_callback;
54 TCPClientTransport client(GetLocalEndpoint(), nullptr); 66 TCPClientTransport client(GetLocalEndpoint(), nullptr);
55 client.Connect(connect_callback.callback()); 67 client.Connect(connect_callback.callback());
56 68
57 EXPECT_EQ(net::OK, connect_callback.WaitForResult()); 69 EXPECT_EQ(net::OK, connect_callback.WaitForResult());
58 EXPECT_EQ(net::OK, accept_callback.WaitForResult()); 70 EXPECT_EQ(net::OK, accept_callback.WaitForResult());
59 EXPECT_TRUE(engine_->TakeConnection() != nullptr); 71 EXPECT_NE(nullptr, client.TakeMessagePort());
60 } 72 }
61 73
62 TEST_F(TCPTransportTest, TwoClientConnections) { 74 TEST_F(TCPTransportTest, TwoClientConnections) {
63 net::TestCompletionCallback accept_callback1; 75 net::TestCompletionCallback accept_callback1;
64 engine_->Connect(accept_callback1.callback()); 76 engine_.Connect(accept_callback1.callback());
65 77
66 net::TestCompletionCallback connect_callback1; 78 net::TestCompletionCallback connect_callback1;
67 TCPClientTransport client1(GetLocalEndpoint(), nullptr); 79 TCPClientTransport client(GetLocalEndpoint(), nullptr);
68 client1.Connect(connect_callback1.callback()); 80 client.Connect(connect_callback1.callback());
81 EXPECT_EQ(net::OK, connect_callback1.WaitForResult());
82 EXPECT_EQ(net::OK, accept_callback1.WaitForResult());
83 EXPECT_NE(nullptr, engine_.TakeMessagePort());
84
85 net::TestCompletionCallback accept_callback2;
86 engine_.Connect(accept_callback2.callback());
69 87
70 net::TestCompletionCallback connect_callback2; 88 net::TestCompletionCallback connect_callback2;
71 TCPClientTransport client2(GetLocalEndpoint(), nullptr); 89 TCPClientTransport client2(GetLocalEndpoint(), nullptr);
72 client2.Connect(connect_callback2.callback()); 90 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()); 91 EXPECT_EQ(net::OK, connect_callback2.WaitForResult());
81 EXPECT_EQ(net::OK, accept_callback2.WaitForResult()); 92 EXPECT_EQ(net::OK, accept_callback2.WaitForResult());
82 EXPECT_TRUE(engine_->TakeConnection() != nullptr); 93 EXPECT_NE(nullptr, engine_.TakeMessagePort());
83 } 94 }
84 95
85 TEST_F(TCPTransportTest, ExchangeMessages) { 96 TEST_F(TCPTransportTest, ExchangeMessages) {
86 // Start the Engine transport and connect a client to it. 97 // Start the Engine transport and connect a client to it.
87 net::TestCompletionCallback accept_callback; 98 net::TestCompletionCallback accept_callback;
88 engine_->Connect(accept_callback.callback()); 99 engine_.Connect(accept_callback.callback());
89 net::TestCompletionCallback client_connect_callback; 100 net::TestCompletionCallback clientconnect_callback;
CJ 2016/08/12 23:59:15 Is there a reason to remove the underscore?
Kevin M 2016/08/15 17:35:09 Looks like I typo'd. :\ Thanks, fixed!
90 TCPClientTransport client(GetLocalEndpoint(), nullptr); 101 TCPClientTransport client(GetLocalEndpoint(), nullptr);
91 client.Connect(client_connect_callback.callback()); 102 client.Connect(clientconnect_callback.callback());
92 EXPECT_EQ(net::OK, client_connect_callback.WaitForResult());
93 EXPECT_EQ(net::OK, accept_callback.WaitForResult()); 103 EXPECT_EQ(net::OK, accept_callback.WaitForResult());
104 EXPECT_EQ(net::OK, clientconnect_callback.WaitForResult());
94 105
95 // Expect the engine to get two messages from the client, and the client to 106 std::unique_ptr<MessagePort> engine_message_port = engine_.TakeMessagePort();
96 // get one from the engine. 107 std::unique_ptr<MessagePort> clientmessage_port = client.TakeMessagePort();
CJ 2016/08/12 23:59:15 why are engine variables following the underscore
Kevin M 2016/08/15 17:35:09 "engine_" is a class member, which have trailing u
CJ 2016/08/15 18:17:42 I was more meaning the typo issue, since it was re
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 108
115 // Attach the ends of the connection to our mock message-processors. 109 // Engine sends kPayload1 to client.
116 std::unique_ptr<BlimpConnection> engine_connnection = 110 net::TestCompletionCallback read_cb1;
117 engine_->TakeConnection(); 111 net::TestCompletionCallback write_cb1;
118 std::unique_ptr<BlimpConnection> client_connnection = client.TakeConnection(); 112 memcpy(write_buffer_->data(), kPayload1, arraysize(kPayload1));
119 engine_connnection->SetIncomingMessageProcessor(&engine_incoming_processor); 113 engine_message_port->writer()->WritePacket(write_buffer_,
120 client_connnection->SetIncomingMessageProcessor(&client_incoming_processor); 114 write_cb1.callback());
115 clientmessage_port->reader()->ReadPacket(read_buffer_, read_cb1.callback());
116 EXPECT_EQ(arraysize(kPayload1),
117 static_cast<size_t>(read_cb1.WaitForResult()));
118 EXPECT_EQ(net::OK, write_cb1.WaitForResult());
119 EXPECT_TRUE(
120 BufferStartsWith(read_buffer_.get(), arraysize(kPayload1), kPayload1));
121 121
122 // Client sends the first message. 122 // Client sends kPayload2 to engine.
123 net::TestCompletionCallback client_send_callback1; 123 net::TestCompletionCallback read_cb2;
124 client_connnection->GetOutgoingMessageProcessor()->ProcessMessage( 124 net::TestCompletionCallback write_cb2;
125 std::move(client_message1), client_send_callback1.callback()); 125 memcpy(write_buffer_->data(), kPayload2, arraysize(kPayload2));
126 EXPECT_EQ(net::OK, client_send_callback1.WaitForResult()); 126 clientmessage_port->writer()->WritePacket(write_buffer_,
127 127 write_cb2.callback());
128 // Engine finishes processing the client message. 128 engine_message_port->reader()->ReadPacket(read_buffer_, read_cb2.callback());
129 EXPECT_FALSE(engine_process_message_cb.is_null()); 129 EXPECT_EQ(arraysize(kPayload2),
130 engine_process_message_cb.Run(client_message1_size); 130 static_cast<size_t>(read_cb2.WaitForResult()));
131 131 EXPECT_EQ(net::OK, write_cb2.WaitForResult());
132 // Engine sends one message. 132 EXPECT_TRUE(
133 net::TestCompletionCallback engine_send_callback; 133 BufferStartsWith(read_buffer_.get(), arraysize(kPayload2), kPayload2));
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 } 134 }
144 135
145 } // namespace 136 } // namespace
146 137
147 } // namespace blimp 138 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698