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

Side by Side Diff: net/quic/chromium/bidirectional_stream_quic_impl_unittest.cc

Issue 2227083002: Remove net::BidirectionalStream::Cancel (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix typo 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "net/quic/chromium/bidirectional_stream_quic_impl.h" 5 #include "net/quic/chromium/bidirectional_stream_quic_impl.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/callback_helpers.h" 9 #include "base/callback_helpers.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 read_buf_len, 62 read_buf_len,
63 base::WrapUnique(new base::Timer(false, false))) {} 63 base::WrapUnique(new base::Timer(false, false))) {}
64 64
65 TestDelegateBase(IOBuffer* read_buf, 65 TestDelegateBase(IOBuffer* read_buf,
66 int read_buf_len, 66 int read_buf_len,
67 std::unique_ptr<base::Timer> timer) 67 std::unique_ptr<base::Timer> timer)
68 : read_buf_(read_buf), 68 : read_buf_(read_buf),
69 read_buf_len_(read_buf_len), 69 read_buf_len_(read_buf_len),
70 timer_(std::move(timer)), 70 timer_(std::move(timer)),
71 loop_(nullptr), 71 loop_(nullptr),
72 next_proto_(kProtoUnknown),
73 received_bytes_(0),
74 sent_bytes_(0),
72 error_(OK), 75 error_(OK),
73 on_data_read_count_(0), 76 on_data_read_count_(0),
74 on_data_sent_count_(0), 77 on_data_sent_count_(0),
75 not_expect_callback_(false), 78 not_expect_callback_(false),
76 on_failed_called_(false), 79 on_failed_called_(false),
77 send_request_headers_automatically_(true), 80 send_request_headers_automatically_(true),
78 is_ready_(false) { 81 is_ready_(false) {
79 loop_.reset(new base::RunLoop); 82 loop_.reset(new base::RunLoop);
80 } 83 }
81 84
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 not_expect_callback_ = true; 176 not_expect_callback_ = true;
174 int rv = stream_->ReadData(read_buf_.get(), read_buf_len_); 177 int rv = stream_->ReadData(read_buf_.get(), read_buf_len_);
175 not_expect_callback_ = false; 178 not_expect_callback_ = false;
176 if (rv > 0) 179 if (rv > 0)
177 data_received_.append(read_buf_->data(), rv); 180 data_received_.append(read_buf_->data(), rv);
178 if (rv == ERR_IO_PENDING) 181 if (rv == ERR_IO_PENDING)
179 callback_ = callback; 182 callback_ = callback;
180 return rv; 183 return rv;
181 } 184 }
182 185
183 // Cancels |stream_|. 186 NextProto GetProtocol() const {
184 void CancelStream() { stream_->Cancel(); } 187 if (stream_)
185 188 return stream_->GetProtocol();
186 NextProto GetProtocol() const { return stream_->GetProtocol(); } 189 return next_proto_;
190 }
187 191
188 int64_t GetTotalReceivedBytes() const { 192 int64_t GetTotalReceivedBytes() const {
189 return stream_->GetTotalReceivedBytes(); 193 if (stream_)
194 return stream_->GetTotalReceivedBytes();
195 return received_bytes_;
190 } 196 }
191 197
192 int64_t GetTotalSentBytes() const { return stream_->GetTotalSentBytes(); } 198 int64_t GetTotalSentBytes() const {
199 if (stream_)
200 return stream_->GetTotalSentBytes();
201 return sent_bytes_;
202 }
193 203
194 void DoNotSendRequestHeadersAutomatically() { 204 void DoNotSendRequestHeadersAutomatically() {
195 send_request_headers_automatically_ = false; 205 send_request_headers_automatically_ = false;
196 } 206 }
197 207
208 // Deletes |stream_|.
209 void DeleteStream() {
210 next_proto_ = stream_->GetProtocol();
211 received_bytes_ = stream_->GetTotalReceivedBytes();
212 sent_bytes_ = stream_->GetTotalSentBytes();
213 stream_.reset();
214 }
215
198 // Const getters for internal states. 216 // Const getters for internal states.
199 const std::string& data_received() const { return data_received_; } 217 const std::string& data_received() const { return data_received_; }
200 int error() const { return error_; } 218 int error() const { return error_; }
201 const SpdyHeaderBlock& response_headers() const { return response_headers_; } 219 const SpdyHeaderBlock& response_headers() const { return response_headers_; }
202 const SpdyHeaderBlock& trailers() const { return trailers_; } 220 const SpdyHeaderBlock& trailers() const { return trailers_; }
203 int on_data_read_count() const { return on_data_read_count_; } 221 int on_data_read_count() const { return on_data_read_count_; }
204 int on_data_sent_count() const { return on_data_sent_count_; } 222 int on_data_sent_count() const { return on_data_sent_count_; }
205 bool on_failed_called() const { return on_failed_called_; } 223 bool on_failed_called() const { return on_failed_called_; }
206 bool is_ready() const { return is_ready_; } 224 bool is_ready() const { return is_ready_; }
207 225
208 protected: 226 protected:
209 // Quits |loop_|. 227 // Quits |loop_|.
210 void QuitLoop() { loop_->Quit(); } 228 void QuitLoop() { loop_->Quit(); }
211 229
212 // Deletes |stream_|.
213 void DeleteStream() { stream_.reset(); }
214
215 private: 230 private:
216 std::unique_ptr<BidirectionalStreamQuicImpl> stream_; 231 std::unique_ptr<BidirectionalStreamQuicImpl> stream_;
217 scoped_refptr<IOBuffer> read_buf_; 232 scoped_refptr<IOBuffer> read_buf_;
218 int read_buf_len_; 233 int read_buf_len_;
219 std::unique_ptr<base::Timer> timer_; 234 std::unique_ptr<base::Timer> timer_;
220 std::string data_received_; 235 std::string data_received_;
221 std::unique_ptr<base::RunLoop> loop_; 236 std::unique_ptr<base::RunLoop> loop_;
222 SpdyHeaderBlock response_headers_; 237 SpdyHeaderBlock response_headers_;
223 SpdyHeaderBlock trailers_; 238 SpdyHeaderBlock trailers_;
239 NextProto next_proto_;
240 int64_t received_bytes_;
241 int64_t sent_bytes_;
224 int error_; 242 int error_;
225 int on_data_read_count_; 243 int on_data_read_count_;
226 int on_data_sent_count_; 244 int on_data_sent_count_;
227 // This is to ensure that delegate callback is not invoked synchronously when 245 // This is to ensure that delegate callback is not invoked synchronously when
228 // calling into |stream_|. 246 // calling into |stream_|.
229 bool not_expect_callback_; 247 bool not_expect_callback_;
230 bool on_failed_called_; 248 bool on_failed_called_;
231 CompletionCallback callback_; 249 CompletionCallback callback_;
232 bool send_request_headers_automatically_; 250 bool send_request_headers_automatically_;
233 bool is_ready_; 251 bool is_ready_;
(...skipping 1102 matching lines...) Expand 10 before | Expand all | Expand 10 after
1336 IsError(ERR_QUIC_PROTOCOL_ERROR)); 1354 IsError(ERR_QUIC_PROTOCOL_ERROR));
1337 EXPECT_THAT(delegate->error(), IsError(ERR_QUIC_PROTOCOL_ERROR)); 1355 EXPECT_THAT(delegate->error(), IsError(ERR_QUIC_PROTOCOL_ERROR));
1338 EXPECT_EQ(0, delegate->on_data_read_count()); 1356 EXPECT_EQ(0, delegate->on_data_read_count());
1339 EXPECT_EQ(0, delegate->on_data_sent_count()); 1357 EXPECT_EQ(0, delegate->on_data_sent_count());
1340 EXPECT_EQ(static_cast<int64_t>(spdy_request_headers_frame_length), 1358 EXPECT_EQ(static_cast<int64_t>(spdy_request_headers_frame_length),
1341 delegate->GetTotalSentBytes()); 1359 delegate->GetTotalSentBytes());
1342 EXPECT_EQ(static_cast<int64_t>(spdy_response_headers_frame_length), 1360 EXPECT_EQ(static_cast<int64_t>(spdy_response_headers_frame_length),
1343 delegate->GetTotalReceivedBytes()); 1361 delegate->GetTotalReceivedBytes());
1344 } 1362 }
1345 1363
1346 TEST_P(BidirectionalStreamQuicImplTest, CancelStreamAfterSendData) {
1347 SetRequest("POST", "/", DEFAULT_PRIORITY);
1348 size_t spdy_request_headers_frame_length;
1349 AddWrite(ConstructRequestHeadersPacket(1, !kFin, DEFAULT_PRIORITY,
1350 &spdy_request_headers_frame_length));
1351 AddWrite(ConstructAckAndDataPacket(2, !kIncludeVersion, 2, 1, !kFin, 0,
1352 kUploadData, &client_maker_));
1353 AddWrite(ConstructRstStreamCancelledPacket(3, strlen(kUploadData),
1354 &client_maker_));
1355
1356 Initialize();
1357
1358 BidirectionalStreamRequestInfo request;
1359 request.method = "POST";
1360 request.url = GURL("http://www.google.com/");
1361 request.end_stream_on_headers = false;
1362 request.priority = DEFAULT_PRIORITY;
1363
1364 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(kReadBufferSize));
1365 std::unique_ptr<TestDelegateBase> delegate(
1366 new TestDelegateBase(read_buffer.get(), kReadBufferSize));
1367 delegate->Start(&request, net_log().bound(), session()->GetWeakPtr());
1368 ConfirmHandshake();
1369 delegate->WaitUntilNextCallback(); // OnStreamReady
1370
1371 // Server acks the request.
1372 ProcessPacket(ConstructServerAckPacket(1, 0, 0));
1373
1374 // Server sends the response headers.
1375 SpdyHeaderBlock response_headers = ConstructResponseHeaders("200");
1376 size_t spdy_response_headers_frame_length;
1377 ProcessPacket(
1378 ConstructResponseHeadersPacket(2, !kFin, std::move(response_headers),
1379 &spdy_response_headers_frame_length, 0));
1380
1381 delegate->WaitUntilNextCallback(); // OnHeadersReceived
1382 EXPECT_EQ("200", delegate->response_headers().find(":status")->second);
1383
1384 // Send a DATA frame.
1385 scoped_refptr<StringIOBuffer> buf(new StringIOBuffer(kUploadData));
1386
1387 delegate->SendData(buf, buf->size(), false);
1388 delegate->WaitUntilNextCallback(); // OnDataSent
1389
1390 delegate->CancelStream();
1391 base::RunLoop().RunUntilIdle();
1392
1393 // Try to send data after Cancel(), should not get called back.
1394 delegate->SendData(buf, buf->size(), false);
1395 base::RunLoop().RunUntilIdle();
1396 EXPECT_FALSE(delegate->on_failed_called());
1397
1398 EXPECT_EQ(0, delegate->on_data_read_count());
1399 EXPECT_EQ(1, delegate->on_data_sent_count());
1400 EXPECT_EQ(kProtoQUIC1SPDY3, delegate->GetProtocol());
1401 EXPECT_EQ(static_cast<int64_t>(spdy_request_headers_frame_length +
1402 strlen(kUploadData)),
1403 delegate->GetTotalSentBytes());
1404 EXPECT_EQ(static_cast<int64_t>(spdy_response_headers_frame_length),
1405 delegate->GetTotalReceivedBytes());
1406 }
1407
1408 TEST_P(BidirectionalStreamQuicImplTest, SessionClosedBeforeReadData) { 1364 TEST_P(BidirectionalStreamQuicImplTest, SessionClosedBeforeReadData) {
1409 SetRequest("POST", "/", DEFAULT_PRIORITY); 1365 SetRequest("POST", "/", DEFAULT_PRIORITY);
1410 size_t spdy_request_headers_frame_length; 1366 size_t spdy_request_headers_frame_length;
1411 AddWrite(ConstructRequestHeadersPacket(1, !kFin, DEFAULT_PRIORITY, 1367 AddWrite(ConstructRequestHeadersPacket(1, !kFin, DEFAULT_PRIORITY,
1412 &spdy_request_headers_frame_length)); 1368 &spdy_request_headers_frame_length));
1413 Initialize(); 1369 Initialize();
1414 1370
1415 BidirectionalStreamRequestInfo request; 1371 BidirectionalStreamRequestInfo request;
1416 request.method = "POST"; 1372 request.method = "POST";
1417 request.url = GURL("http://www.google.com/"); 1373 request.url = GURL("http://www.google.com/");
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1455 EXPECT_THAT(delegate->error(), IsError(ERR_UNEXPECTED)); 1411 EXPECT_THAT(delegate->error(), IsError(ERR_UNEXPECTED));
1456 EXPECT_EQ(0, delegate->on_data_read_count()); 1412 EXPECT_EQ(0, delegate->on_data_read_count());
1457 EXPECT_EQ(0, delegate->on_data_sent_count()); 1413 EXPECT_EQ(0, delegate->on_data_sent_count());
1458 EXPECT_EQ(kProtoQUIC1SPDY3, delegate->GetProtocol()); 1414 EXPECT_EQ(kProtoQUIC1SPDY3, delegate->GetProtocol());
1459 EXPECT_EQ(static_cast<int64_t>(spdy_request_headers_frame_length), 1415 EXPECT_EQ(static_cast<int64_t>(spdy_request_headers_frame_length),
1460 delegate->GetTotalSentBytes()); 1416 delegate->GetTotalSentBytes());
1461 EXPECT_EQ(static_cast<int64_t>(spdy_response_headers_frame_length), 1417 EXPECT_EQ(static_cast<int64_t>(spdy_response_headers_frame_length),
1462 delegate->GetTotalReceivedBytes()); 1418 delegate->GetTotalReceivedBytes());
1463 } 1419 }
1464 1420
1465 TEST_P(BidirectionalStreamQuicImplTest, CancelStreamAfterReadData) { 1421 TEST_P(BidirectionalStreamQuicImplTest, DeleteStreamAfterReadData) {
1466 SetRequest("POST", "/", DEFAULT_PRIORITY); 1422 SetRequest("POST", "/", DEFAULT_PRIORITY);
1467 size_t spdy_request_headers_frame_length; 1423 size_t spdy_request_headers_frame_length;
1468 AddWrite(ConstructRequestHeadersPacket(1, !kFin, DEFAULT_PRIORITY, 1424 AddWrite(ConstructRequestHeadersPacket(1, !kFin, DEFAULT_PRIORITY,
1469 &spdy_request_headers_frame_length)); 1425 &spdy_request_headers_frame_length));
1470 AddWrite(ConstructClientAckAndRstStreamPacket(2, 2, 1, 1)); 1426 AddWrite(ConstructClientAckAndRstStreamPacket(2, 2, 1, 1));
1471 1427
1472 Initialize(); 1428 Initialize();
1473 1429
1474 BidirectionalStreamRequestInfo request; 1430 BidirectionalStreamRequestInfo request;
1475 request.method = "POST"; 1431 request.method = "POST";
(...skipping 17 matching lines...) Expand all
1493 ProcessPacket( 1449 ProcessPacket(
1494 ConstructResponseHeadersPacket(2, !kFin, std::move(response_headers), 1450 ConstructResponseHeadersPacket(2, !kFin, std::move(response_headers),
1495 &spdy_response_headers_frame_length, 0)); 1451 &spdy_response_headers_frame_length, 0));
1496 1452
1497 delegate->WaitUntilNextCallback(); // OnHeadersReceived 1453 delegate->WaitUntilNextCallback(); // OnHeadersReceived
1498 EXPECT_EQ("200", delegate->response_headers().find(":status")->second); 1454 EXPECT_EQ("200", delegate->response_headers().find(":status")->second);
1499 1455
1500 // Cancel the stream after ReadData returns ERR_IO_PENDING. 1456 // Cancel the stream after ReadData returns ERR_IO_PENDING.
1501 TestCompletionCallback cb; 1457 TestCompletionCallback cb;
1502 EXPECT_THAT(delegate->ReadData(cb.callback()), IsError(ERR_IO_PENDING)); 1458 EXPECT_THAT(delegate->ReadData(cb.callback()), IsError(ERR_IO_PENDING));
1503 delegate->CancelStream(); 1459 delegate->DeleteStream();
1504 1460
1505 base::RunLoop().RunUntilIdle(); 1461 base::RunLoop().RunUntilIdle();
1506 1462
1507 EXPECT_EQ(0, delegate->on_data_read_count()); 1463 EXPECT_EQ(0, delegate->on_data_read_count());
1508 EXPECT_EQ(0, delegate->on_data_sent_count()); 1464 EXPECT_EQ(0, delegate->on_data_sent_count());
1509 EXPECT_EQ(kProtoQUIC1SPDY3, delegate->GetProtocol()); 1465 EXPECT_EQ(kProtoQUIC1SPDY3, delegate->GetProtocol());
1510 EXPECT_EQ(static_cast<int64_t>(spdy_request_headers_frame_length), 1466 EXPECT_EQ(static_cast<int64_t>(spdy_request_headers_frame_length),
1511 delegate->GetTotalSentBytes()); 1467 delegate->GetTotalSentBytes());
1512 EXPECT_EQ(static_cast<int64_t>(spdy_response_headers_frame_length), 1468 EXPECT_EQ(static_cast<int64_t>(spdy_response_headers_frame_length),
1513 delegate->GetTotalReceivedBytes()); 1469 delegate->GetTotalReceivedBytes());
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
1672 1628
1673 base::RunLoop().RunUntilIdle(); 1629 base::RunLoop().RunUntilIdle();
1674 1630
1675 EXPECT_EQ(1, delegate->on_data_read_count()); 1631 EXPECT_EQ(1, delegate->on_data_read_count());
1676 EXPECT_EQ(0, delegate->on_data_sent_count()); 1632 EXPECT_EQ(0, delegate->on_data_sent_count());
1677 } 1633 }
1678 1634
1679 } // namespace test 1635 } // namespace test
1680 1636
1681 } // namespace net 1637 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/chromium/bidirectional_stream_quic_impl.cc ('k') | net/spdy/bidirectional_stream_spdy_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698