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

Side by Side Diff: net/socket/socket_test_util.cc

Issue 1119803006: Revert of Add AllReadDataConsumed and AllWriteDataConsumed methods to SocketDataProvider (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 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 | « net/socket/socket_test_util.h ('k') | net/spdy/spdy_http_stream_unittest.cc » ('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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/socket/socket_test_util.h" 5 #include "net/socket/socket_test_util.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 read_count_(reads_count), 149 read_count_(reads_count),
150 writes_(writes), 150 writes_(writes),
151 write_index_(0), 151 write_index_(0),
152 write_count_(writes_count) { 152 write_count_(writes_count) {
153 } 153 }
154 154
155 StaticSocketDataHelper::~StaticSocketDataHelper() { 155 StaticSocketDataHelper::~StaticSocketDataHelper() {
156 } 156 }
157 157
158 const MockRead& StaticSocketDataHelper::PeekRead() const { 158 const MockRead& StaticSocketDataHelper::PeekRead() const {
159 CHECK(!AllReadDataConsumed()); 159 CHECK(!at_read_eof());
160 return reads_[read_index_]; 160 return reads_[read_index_];
161 } 161 }
162 162
163 const MockWrite& StaticSocketDataHelper::PeekWrite() const { 163 const MockWrite& StaticSocketDataHelper::PeekWrite() const {
164 CHECK(!AllWriteDataConsumed()); 164 CHECK(!at_write_eof());
165 return writes_[write_index_]; 165 return writes_[write_index_];
166 } 166 }
167 167
168 const MockRead& StaticSocketDataHelper::AdvanceRead() { 168 const MockRead& StaticSocketDataHelper::AdvanceRead() {
169 CHECK(!AllReadDataConsumed()); 169 CHECK(!at_read_eof());
170 return reads_[read_index_++]; 170 return reads_[read_index_++];
171 } 171 }
172 172
173 const MockWrite& StaticSocketDataHelper::AdvanceWrite() { 173 const MockWrite& StaticSocketDataHelper::AdvanceWrite() {
174 CHECK(!AllWriteDataConsumed()); 174 CHECK(!at_write_eof());
175 return writes_[write_index_++]; 175 return writes_[write_index_++];
176 } 176 }
177 177
178 bool StaticSocketDataHelper::VerifyWriteData(const std::string& data) { 178 bool StaticSocketDataHelper::VerifyWriteData(const std::string& data) {
179 CHECK(!AllWriteDataConsumed()); 179 CHECK(!at_write_eof());
180 // Check that what the actual data matches the expectations. 180 // Check that what the actual data matches the expectations.
181 const MockWrite& next_write = PeekWrite(); 181 const MockWrite& next_write = PeekWrite();
182 if (!next_write.data) 182 if (!next_write.data)
183 return true; 183 return true;
184 184
185 // Note: Partial writes are supported here. If the expected data 185 // Note: Partial writes are supported here. If the expected data
186 // is a match, but shorter than the write actually written, that is legal. 186 // is a match, but shorter than the write actually written, that is legal.
187 // Example: 187 // Example:
188 // Application writes "foobarbaz" (9 bytes) 188 // Application writes "foobarbaz" (9 bytes)
189 // Expected write was "foo" (3 bytes) 189 // Expected write was "foo" (3 bytes)
190 // This is a success, and the function returns true. 190 // This is a success, and the function returns true.
191 std::string expected_data(next_write.data, next_write.data_len); 191 std::string expected_data(next_write.data, next_write.data_len);
192 std::string actual_data(data.substr(0, next_write.data_len)); 192 std::string actual_data(data.substr(0, next_write.data_len));
193 EXPECT_GE(data.length(), expected_data.length()); 193 EXPECT_GE(data.length(), expected_data.length());
194 EXPECT_EQ(expected_data, actual_data); 194 EXPECT_EQ(expected_data, actual_data);
195 return expected_data == actual_data; 195 return expected_data == actual_data;
196 } 196 }
197 197
198 bool StaticSocketDataHelper::AllReadDataConsumed() const {
199 return read_index_ >= read_count_;
200 }
201
202 bool StaticSocketDataHelper::AllWriteDataConsumed() const {
203 return write_index_ >= write_count_;
204 }
205
206 void StaticSocketDataHelper::Reset() { 198 void StaticSocketDataHelper::Reset() {
207 read_index_ = 0; 199 read_index_ = 0;
208 write_index_ = 0; 200 write_index_ = 0;
209 } 201 }
210 202
211 StaticSocketDataProvider::StaticSocketDataProvider() 203 StaticSocketDataProvider::StaticSocketDataProvider()
212 : StaticSocketDataProvider(nullptr, 0, nullptr, 0) { 204 : StaticSocketDataProvider(nullptr, 0, nullptr, 0) {
213 } 205 }
214 206
215 StaticSocketDataProvider::StaticSocketDataProvider(MockRead* reads, 207 StaticSocketDataProvider::StaticSocketDataProvider(MockRead* reads,
216 size_t reads_count, 208 size_t reads_count,
217 MockWrite* writes, 209 MockWrite* writes,
218 size_t writes_count) 210 size_t writes_count)
219 : helper_(reads, reads_count, writes, writes_count) { 211 : helper_(reads, reads_count, writes, writes_count) {
220 } 212 }
221 213
222 StaticSocketDataProvider::~StaticSocketDataProvider() { 214 StaticSocketDataProvider::~StaticSocketDataProvider() {
223 } 215 }
224 216
225 MockRead StaticSocketDataProvider::OnRead() { 217 MockRead StaticSocketDataProvider::OnRead() {
226 CHECK(!helper_.AllReadDataConsumed()); 218 CHECK(!helper_.at_read_eof());
227 return helper_.AdvanceRead(); 219 return helper_.AdvanceRead();
228 } 220 }
229 221
230 MockWriteResult StaticSocketDataProvider::OnWrite(const std::string& data) { 222 MockWriteResult StaticSocketDataProvider::OnWrite(const std::string& data) {
231 if (helper_.write_count() == 0) { 223 if (helper_.write_count() == 0) {
232 // Not using mock writes; succeed synchronously. 224 // Not using mock writes; succeed synchronously.
233 return MockWriteResult(SYNCHRONOUS, data.length()); 225 return MockWriteResult(SYNCHRONOUS, data.length());
234 } 226 }
235 EXPECT_FALSE(helper_.AllWriteDataConsumed()); 227 EXPECT_FALSE(helper_.at_write_eof());
236 if (helper_.AllWriteDataConsumed()) { 228 if (helper_.at_write_eof()) {
237 // Show what the extra write actually consists of. 229 // Show what the extra write actually consists of.
238 EXPECT_EQ("<unexpected write>", data); 230 EXPECT_EQ("<unexpected write>", data);
239 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED); 231 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED);
240 } 232 }
241 233
242 // Check that what we are writing matches the expectation. 234 // Check that what we are writing matches the expectation.
243 // Then give the mocked return value. 235 // Then give the mocked return value.
244 if (!helper_.VerifyWriteData(data)) 236 if (!helper_.VerifyWriteData(data))
245 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED); 237 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED);
246 238
247 const MockWrite& next_write = helper_.AdvanceWrite(); 239 const MockWrite& next_write = helper_.AdvanceWrite();
248 // In the case that the write was successful, return the number of bytes 240 // In the case that the write was successful, return the number of bytes
249 // written. Otherwise return the error code. 241 // written. Otherwise return the error code.
250 int result = 242 int result =
251 next_write.result == OK ? next_write.data_len : next_write.result; 243 next_write.result == OK ? next_write.data_len : next_write.result;
252 return MockWriteResult(next_write.mode, result); 244 return MockWriteResult(next_write.mode, result);
253 } 245 }
254 246
255 void StaticSocketDataProvider::Reset() { 247 void StaticSocketDataProvider::Reset() {
256 helper_.Reset(); 248 helper_.Reset();
257 } 249 }
258 250
259 bool StaticSocketDataProvider::AllReadDataConsumed() const {
260 return helper_.AllReadDataConsumed();
261 }
262
263 bool StaticSocketDataProvider::AllWriteDataConsumed() const {
264 return helper_.AllWriteDataConsumed();
265 }
266
267 DynamicSocketDataProvider::DynamicSocketDataProvider() 251 DynamicSocketDataProvider::DynamicSocketDataProvider()
268 : short_read_limit_(0), 252 : short_read_limit_(0),
269 allow_unconsumed_reads_(false) { 253 allow_unconsumed_reads_(false) {
270 } 254 }
271 255
272 DynamicSocketDataProvider::~DynamicSocketDataProvider() {} 256 DynamicSocketDataProvider::~DynamicSocketDataProvider() {}
273 257
274 MockRead DynamicSocketDataProvider::OnRead() { 258 MockRead DynamicSocketDataProvider::OnRead() {
275 if (reads_.empty()) 259 if (reads_.empty())
276 return MockRead(SYNCHRONOUS, ERR_UNEXPECTED); 260 return MockRead(SYNCHRONOUS, ERR_UNEXPECTED);
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
517 MockRead* reads, 501 MockRead* reads,
518 size_t reads_count, 502 size_t reads_count,
519 MockWrite* writes, 503 MockWrite* writes,
520 size_t writes_count) 504 size_t writes_count)
521 : SequencedSocketData(reads, reads_count, writes, writes_count) { 505 : SequencedSocketData(reads, reads_count, writes, writes_count) {
522 set_connect_data(connect); 506 set_connect_data(connect);
523 } 507 }
524 508
525 MockRead SequencedSocketData::OnRead() { 509 MockRead SequencedSocketData::OnRead() {
526 CHECK_EQ(IDLE, read_state_); 510 CHECK_EQ(IDLE, read_state_);
527 CHECK(!helper_.AllReadDataConsumed()); 511 CHECK(!helper_.at_read_eof());
528 512
529 NET_TRACE(1, " *** ") << "sequence_number: " << sequence_number_; 513 NET_TRACE(1, " *** ") << "sequence_number: " << sequence_number_;
530 const MockRead& next_read = helper_.PeekRead(); 514 const MockRead& next_read = helper_.PeekRead();
531 NET_TRACE(1, " *** ") << "next_read: " << next_read.sequence_number; 515 NET_TRACE(1, " *** ") << "next_read: " << next_read.sequence_number;
532 CHECK_GE(next_read.sequence_number, sequence_number_); 516 CHECK_GE(next_read.sequence_number, sequence_number_);
533 517
534 // Special case handling for hanging reads. 518 // Special case handling for hanging reads.
535 if (next_read.mode == ASYNC && next_read.result == ERR_IO_PENDING) { 519 if (next_read.mode == ASYNC && next_read.result == ERR_IO_PENDING) {
536 NET_TRACE(1, " *** ") << "Hanging read"; 520 NET_TRACE(1, " *** ") << "Hanging read";
537 helper_.AdvanceRead(); 521 helper_.AdvanceRead();
538 ++sequence_number_; 522 ++sequence_number_;
539 CHECK(helper_.AllReadDataConsumed()); 523 CHECK(helper_.at_read_eof());
540 return MockRead(SYNCHRONOUS, ERR_IO_PENDING); 524 return MockRead(SYNCHRONOUS, ERR_IO_PENDING);
541 } 525 }
542 526
543 if (next_read.sequence_number <= sequence_number_) { 527 if (next_read.sequence_number <= sequence_number_) {
544 if (next_read.mode == SYNCHRONOUS) { 528 if (next_read.mode == SYNCHRONOUS) {
545 NET_TRACE(1, " *** ") << "Returning synchronously"; 529 NET_TRACE(1, " *** ") << "Returning synchronously";
546 DumpMockReadWrite(next_read); 530 DumpMockReadWrite(next_read);
547 helper_.AdvanceRead(); 531 helper_.AdvanceRead();
548 ++sequence_number_; 532 ++sequence_number_;
549 MaybePostWriteCompleteTask(); 533 MaybePostWriteCompleteTask();
(...skipping 11 matching lines...) Expand all
561 } else { 545 } else {
562 NET_TRACE(1, " *** ") << "Waiting for write to trigger read"; 546 NET_TRACE(1, " *** ") << "Waiting for write to trigger read";
563 read_state_ = PENDING; 547 read_state_ = PENDING;
564 } 548 }
565 549
566 return MockRead(SYNCHRONOUS, ERR_IO_PENDING); 550 return MockRead(SYNCHRONOUS, ERR_IO_PENDING);
567 } 551 }
568 552
569 MockWriteResult SequencedSocketData::OnWrite(const std::string& data) { 553 MockWriteResult SequencedSocketData::OnWrite(const std::string& data) {
570 CHECK_EQ(IDLE, write_state_); 554 CHECK_EQ(IDLE, write_state_);
571 CHECK(!helper_.AllWriteDataConsumed()); 555 CHECK(!helper_.at_write_eof());
572 556
573 NET_TRACE(1, " *** ") << "sequence_number: " << sequence_number_; 557 NET_TRACE(1, " *** ") << "sequence_number: " << sequence_number_;
574 const MockWrite& next_write = helper_.PeekWrite(); 558 const MockWrite& next_write = helper_.PeekWrite();
575 NET_TRACE(1, " *** ") << "next_write: " << next_write.sequence_number; 559 NET_TRACE(1, " *** ") << "next_write: " << next_write.sequence_number;
576 CHECK_GE(next_write.sequence_number, sequence_number_); 560 CHECK_GE(next_write.sequence_number, sequence_number_);
577 561
578 if (!helper_.VerifyWriteData(data)) 562 if (!helper_.VerifyWriteData(data))
579 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED); 563 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED);
580 564
581 if (next_write.sequence_number <= sequence_number_) { 565 if (next_write.sequence_number <= sequence_number_) {
(...skipping 27 matching lines...) Expand all
609 } 593 }
610 594
611 void SequencedSocketData::Reset() { 595 void SequencedSocketData::Reset() {
612 helper_.Reset(); 596 helper_.Reset();
613 sequence_number_ = 0; 597 sequence_number_ = 0;
614 read_state_ = IDLE; 598 read_state_ = IDLE;
615 write_state_ = IDLE; 599 write_state_ = IDLE;
616 weak_factory_.InvalidateWeakPtrs(); 600 weak_factory_.InvalidateWeakPtrs();
617 } 601 }
618 602
619 bool SequencedSocketData::AllReadDataConsumed() const { 603 bool SequencedSocketData::at_read_eof() const {
620 return helper_.AllReadDataConsumed(); 604 return helper_.at_read_eof();
621 } 605 }
622 606
623 bool SequencedSocketData::AllWriteDataConsumed() const { 607 bool SequencedSocketData::at_write_eof() const {
624 return helper_.AllWriteDataConsumed(); 608 return helper_.at_read_eof();
625 } 609 }
626 610
627 void SequencedSocketData::MaybePostReadCompleteTask() { 611 void SequencedSocketData::MaybePostReadCompleteTask() {
628 NET_TRACE(1, " ****** ") << " current: " << sequence_number_; 612 NET_TRACE(1, " ****** ") << " current: " << sequence_number_;
629 // Only trigger the next read to complete if there is already a read pending 613 // Only trigger the next read to complete if there is already a read pending
630 // which should complete at the current sequence number. 614 // which should complete at the current sequence number.
631 if (read_state_ != PENDING || 615 if (read_state_ != PENDING ||
632 helper_.PeekRead().sequence_number != sequence_number_) { 616 helper_.PeekRead().sequence_number != sequence_number_) {
633 return; 617 return;
634 } 618 }
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
734 DCHECK(!is_running_); 718 DCHECK(!is_running_);
735 is_running_ = true; 719 is_running_ = true;
736 720
737 SetStopped(false); 721 SetStopped(false);
738 int counter = 0; 722 int counter = 0;
739 // Continue to consume data until all data has run out, or the stopped_ flag 723 // Continue to consume data until all data has run out, or the stopped_ flag
740 // has been set. Consuming data requires two separate operations -- running 724 // has been set. Consuming data requires two separate operations -- running
741 // the tasks in the message loop, and explicitly invoking the read/write 725 // the tasks in the message loop, and explicitly invoking the read/write
742 // callbacks (simulating network I/O). We check our conditions between each, 726 // callbacks (simulating network I/O). We check our conditions between each,
743 // since they can change in either. 727 // since they can change in either.
744 while ((!AllWriteDataConsumed() || !AllReadDataConsumed()) && !stopped()) { 728 while ((!at_write_eof() || !at_read_eof()) && !stopped()) {
745 if (counter % 2 == 0) 729 if (counter % 2 == 0)
746 base::RunLoop().RunUntilIdle(); 730 base::RunLoop().RunUntilIdle();
747 if (counter % 2 == 1) { 731 if (counter % 2 == 1) {
748 InvokeCallbacks(); 732 InvokeCallbacks();
749 } 733 }
750 counter++; 734 counter++;
751 } 735 }
752 // We're done consuming new data, but it is possible there are still some 736 // We're done consuming new data, but it is possible there are still some
753 // pending callbacks which we expect to complete before returning. 737 // pending callbacks which we expect to complete before returning.
754 while (delegate_.get() && 738 while (delegate_.get() &&
(...skipping 1440 matching lines...) Expand 10 before | Expand all | Expand 10 after
2195 2179
2196 const char kSOCKS5OkRequest[] = 2180 const char kSOCKS5OkRequest[] =
2197 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; 2181 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 };
2198 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); 2182 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest);
2199 2183
2200 const char kSOCKS5OkResponse[] = 2184 const char kSOCKS5OkResponse[] =
2201 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; 2185 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 };
2202 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); 2186 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse);
2203 2187
2204 } // namespace net 2188 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/socket_test_util.h ('k') | net/spdy/spdy_http_stream_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698