Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / socket / deterministic_socket_data_unittest.cc
blobdc1c42cbb02d07155f93ffb6f2afc642ebf262ef
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
3 // found in the LICENSE file.
5 #include "net/socket/socket_test_util.h"
7 #include <string.h>
9 #include "base/memory/ref_counted.h"
10 #include "testing/platform_test.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 //-----------------------------------------------------------------------------
15 namespace {
17 static const char kMsg1[] = "\0hello!\xff";
18 static const int kLen1 = arraysize(kMsg1);
19 static const char kMsg2[] = "\0a2345678\0";
20 static const int kLen2 = arraysize(kMsg2);
21 static const char kMsg3[] = "bye!";
22 static const int kLen3 = arraysize(kMsg3);
24 } // anonymous namespace
26 namespace net {
28 class DeterministicSocketDataTest : public PlatformTest {
29 public:
30 DeterministicSocketDataTest();
32 virtual void TearDown();
34 void ReentrantReadCallback(int len, int rv);
35 void ReentrantWriteCallback(const char* data, int len, int rv);
37 protected:
38 void Initialize(MockRead* reads, size_t reads_count, MockWrite* writes,
39 size_t writes_count);
41 void AssertSyncReadEquals(const char* data, int len);
42 void AssertAsyncReadEquals(const char* data, int len);
43 void AssertReadReturns(const char* data, int len, int rv);
44 void AssertReadBufferEquals(const char* data, int len);
46 void AssertSyncWriteEquals(const char* data, int len);
47 void AssertAsyncWriteEquals(const char* data, int len);
48 void AssertWriteReturns(const char* data, int len, int rv);
50 TestCompletionCallback read_callback_;
51 TestCompletionCallback write_callback_;
52 StreamSocket* sock_;
53 scoped_ptr<DeterministicSocketData> data_;
55 private:
56 scoped_refptr<IOBuffer> read_buf_;
57 MockConnect connect_data_;
59 HostPortPair endpoint_;
60 scoped_refptr<TransportSocketParams> tcp_params_;
61 ClientSocketPoolHistograms histograms_;
62 DeterministicMockClientSocketFactory socket_factory_;
63 MockTransportClientSocketPool socket_pool_;
64 ClientSocketHandle connection_;
66 DISALLOW_COPY_AND_ASSIGN(DeterministicSocketDataTest);
69 DeterministicSocketDataTest::DeterministicSocketDataTest()
70 : sock_(NULL),
71 read_buf_(NULL),
72 connect_data_(SYNCHRONOUS, OK),
73 endpoint_("www.google.com", 443),
74 tcp_params_(new TransportSocketParams(endpoint_,
75 false,
76 false,
77 OnHostResolutionCallback())),
78 histograms_(std::string()),
79 socket_pool_(10, 10, &histograms_, &socket_factory_) {}
81 void DeterministicSocketDataTest::TearDown() {
82 // Empty the current queue.
83 base::MessageLoop::current()->RunUntilIdle();
84 PlatformTest::TearDown();
87 void DeterministicSocketDataTest::Initialize(MockRead* reads,
88 size_t reads_count,
89 MockWrite* writes,
90 size_t writes_count) {
91 data_.reset(new DeterministicSocketData(reads, reads_count,
92 writes, writes_count));
93 data_->set_connect_data(connect_data_);
94 socket_factory_.AddSocketDataProvider(data_.get());
96 // Perform the TCP connect
97 EXPECT_EQ(OK,
98 connection_.Init(endpoint_.ToString(),
99 tcp_params_,
100 LOWEST,
101 CompletionCallback(),
102 reinterpret_cast<TransportClientSocketPool*>(&socket_pool_),
103 BoundNetLog()));
104 sock_ = connection_.socket();
107 void DeterministicSocketDataTest::AssertSyncReadEquals(const char* data,
108 int len) {
109 // Issue the read, which will complete immediately
110 AssertReadReturns(data, len, len);
111 AssertReadBufferEquals(data, len);
114 void DeterministicSocketDataTest::AssertAsyncReadEquals(const char* data,
115 int len) {
116 // Issue the read, which will be completed asynchronously
117 AssertReadReturns(data, len, ERR_IO_PENDING);
119 EXPECT_FALSE(read_callback_.have_result());
120 EXPECT_TRUE(sock_->IsConnected());
121 data_->RunFor(1); // Runs 1 step, to cause the callbacks to be invoked
123 // Now the read should complete
124 ASSERT_EQ(len, read_callback_.WaitForResult());
125 AssertReadBufferEquals(data, len);
128 void DeterministicSocketDataTest::AssertReadReturns(const char* data,
129 int len, int rv) {
130 read_buf_ = new IOBuffer(len);
131 ASSERT_EQ(rv, sock_->Read(read_buf_.get(), len, read_callback_.callback()));
134 void DeterministicSocketDataTest::AssertReadBufferEquals(const char* data,
135 int len) {
136 ASSERT_EQ(std::string(data, len), std::string(read_buf_->data(), len));
139 void DeterministicSocketDataTest::AssertSyncWriteEquals(const char* data,
140 int len) {
141 scoped_refptr<IOBuffer> buf(new IOBuffer(len));
142 memcpy(buf->data(), data, len);
144 // Issue the write, which will complete immediately
145 ASSERT_EQ(len, sock_->Write(buf.get(), len, write_callback_.callback()));
148 void DeterministicSocketDataTest::AssertAsyncWriteEquals(const char* data,
149 int len) {
150 // Issue the read, which will be completed asynchronously
151 AssertWriteReturns(data, len, ERR_IO_PENDING);
153 EXPECT_FALSE(read_callback_.have_result());
154 EXPECT_TRUE(sock_->IsConnected());
155 data_->RunFor(1); // Runs 1 step, to cause the callbacks to be invoked
157 ASSERT_EQ(len, write_callback_.WaitForResult());
160 void DeterministicSocketDataTest::AssertWriteReturns(const char* data,
161 int len, int rv) {
162 scoped_refptr<IOBuffer> buf(new IOBuffer(len));
163 memcpy(buf->data(), data, len);
165 // Issue the read, which will complete asynchronously
166 ASSERT_EQ(rv, sock_->Write(buf.get(), len, write_callback_.callback()));
169 void DeterministicSocketDataTest::ReentrantReadCallback(int len, int rv) {
170 scoped_refptr<IOBuffer> read_buf(new IOBuffer(len));
171 EXPECT_EQ(len,
172 sock_->Read(
173 read_buf.get(),
174 len,
175 base::Bind(&DeterministicSocketDataTest::ReentrantReadCallback,
176 base::Unretained(this),
177 len)));
180 void DeterministicSocketDataTest::ReentrantWriteCallback(
181 const char* data, int len, int rv) {
182 scoped_refptr<IOBuffer> write_buf(new IOBuffer(len));
183 memcpy(write_buf->data(), data, len);
184 EXPECT_EQ(len,
185 sock_->Write(
186 write_buf.get(),
187 len,
188 base::Bind(&DeterministicSocketDataTest::ReentrantWriteCallback,
189 base::Unretained(this),
190 data,
191 len)));
194 // ----------- Read
196 TEST_F(DeterministicSocketDataTest, SingleSyncReadWhileStopped) {
197 MockRead reads[] = {
198 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
199 MockRead(SYNCHRONOUS, 0, 1), // EOF
202 Initialize(reads, arraysize(reads), NULL, 0);
204 data_->SetStopped(true);
205 AssertReadReturns(kMsg1, kLen1, ERR_UNEXPECTED);
208 TEST_F(DeterministicSocketDataTest, SingleSyncReadTooEarly) {
209 MockRead reads[] = {
210 MockRead(SYNCHRONOUS, kMsg1, kLen1, 1), // Sync Read
211 MockRead(SYNCHRONOUS, 0, 2), // EOF
214 MockWrite writes[] = {
215 MockWrite(SYNCHRONOUS, 0, 0)
218 Initialize(reads, arraysize(reads), writes, arraysize(writes));
220 data_->StopAfter(2);
221 ASSERT_FALSE(data_->stopped());
222 AssertReadReturns(kMsg1, kLen1, ERR_UNEXPECTED);
225 TEST_F(DeterministicSocketDataTest, SingleSyncRead) {
226 MockRead reads[] = {
227 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
228 MockRead(SYNCHRONOUS, 0, 1), // EOF
231 Initialize(reads, arraysize(reads), NULL, 0);
232 // Make sure we don't stop before we've read all the data
233 data_->StopAfter(1);
234 AssertSyncReadEquals(kMsg1, kLen1);
237 TEST_F(DeterministicSocketDataTest, MultipleSyncReads) {
238 MockRead reads[] = {
239 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
240 MockRead(SYNCHRONOUS, kMsg2, kLen2, 1), // Sync Read
241 MockRead(SYNCHRONOUS, kMsg3, kLen3, 2), // Sync Read
242 MockRead(SYNCHRONOUS, kMsg3, kLen3, 3), // Sync Read
243 MockRead(SYNCHRONOUS, kMsg2, kLen2, 4), // Sync Read
244 MockRead(SYNCHRONOUS, kMsg3, kLen3, 5), // Sync Read
245 MockRead(SYNCHRONOUS, kMsg1, kLen1, 6), // Sync Read
246 MockRead(SYNCHRONOUS, 0, 7), // EOF
249 Initialize(reads, arraysize(reads), NULL, 0);
251 // Make sure we don't stop before we've read all the data
252 data_->StopAfter(10);
253 AssertSyncReadEquals(kMsg1, kLen1);
254 AssertSyncReadEquals(kMsg2, kLen2);
255 AssertSyncReadEquals(kMsg3, kLen3);
256 AssertSyncReadEquals(kMsg3, kLen3);
257 AssertSyncReadEquals(kMsg2, kLen2);
258 AssertSyncReadEquals(kMsg3, kLen3);
259 AssertSyncReadEquals(kMsg1, kLen1);
262 TEST_F(DeterministicSocketDataTest, SingleAsyncRead) {
263 MockRead reads[] = {
264 MockRead(ASYNC, kMsg1, kLen1, 0), // Async Read
265 MockRead(SYNCHRONOUS, 0, 1), // EOF
268 Initialize(reads, arraysize(reads), NULL, 0);
270 AssertAsyncReadEquals(kMsg1, kLen1);
273 TEST_F(DeterministicSocketDataTest, MultipleAsyncReads) {
274 MockRead reads[] = {
275 MockRead(ASYNC, kMsg1, kLen1, 0), // Async Read
276 MockRead(ASYNC, kMsg2, kLen2, 1), // Async Read
277 MockRead(ASYNC, kMsg3, kLen3, 2), // Async Read
278 MockRead(ASYNC, kMsg3, kLen3, 3), // Async Read
279 MockRead(ASYNC, kMsg2, kLen2, 4), // Async Read
280 MockRead(ASYNC, kMsg3, kLen3, 5), // Async Read
281 MockRead(ASYNC, kMsg1, kLen1, 6), // Async Read
282 MockRead(SYNCHRONOUS, 0, 7), // EOF
285 Initialize(reads, arraysize(reads), NULL, 0);
287 AssertAsyncReadEquals(kMsg1, kLen1);
288 AssertAsyncReadEquals(kMsg2, kLen2);
289 AssertAsyncReadEquals(kMsg3, kLen3);
290 AssertAsyncReadEquals(kMsg3, kLen3);
291 AssertAsyncReadEquals(kMsg2, kLen2);
292 AssertAsyncReadEquals(kMsg3, kLen3);
293 AssertAsyncReadEquals(kMsg1, kLen1);
296 TEST_F(DeterministicSocketDataTest, MixedReads) {
297 MockRead reads[] = {
298 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
299 MockRead(ASYNC, kMsg2, kLen2, 1), // Async Read
300 MockRead(SYNCHRONOUS, kMsg3, kLen3, 2), // Sync Read
301 MockRead(ASYNC, kMsg3, kLen3, 3), // Async Read
302 MockRead(SYNCHRONOUS, kMsg2, kLen2, 4), // Sync Read
303 MockRead(ASYNC, kMsg3, kLen3, 5), // Async Read
304 MockRead(SYNCHRONOUS, kMsg1, kLen1, 6), // Sync Read
305 MockRead(SYNCHRONOUS, 0, 7), // EOF
308 Initialize(reads, arraysize(reads), NULL, 0);
310 data_->StopAfter(1);
311 AssertSyncReadEquals(kMsg1, kLen1);
312 AssertAsyncReadEquals(kMsg2, kLen2);
313 data_->StopAfter(1);
314 AssertSyncReadEquals(kMsg3, kLen3);
315 AssertAsyncReadEquals(kMsg3, kLen3);
316 data_->StopAfter(1);
317 AssertSyncReadEquals(kMsg2, kLen2);
318 AssertAsyncReadEquals(kMsg3, kLen3);
319 data_->StopAfter(1);
320 AssertSyncReadEquals(kMsg1, kLen1);
323 TEST_F(DeterministicSocketDataTest, SyncReadFromCompletionCallback) {
324 MockRead reads[] = {
325 MockRead(ASYNC, kMsg1, kLen1, 0), // Async Read
326 MockRead(SYNCHRONOUS, kMsg2, kLen2, 1), // Sync Read
329 Initialize(reads, arraysize(reads), NULL, 0);
331 data_->StopAfter(2);
333 scoped_refptr<IOBuffer> read_buf(new IOBuffer(kLen1));
334 ASSERT_EQ(ERR_IO_PENDING,
335 sock_->Read(
336 read_buf.get(),
337 kLen1,
338 base::Bind(&DeterministicSocketDataTest::ReentrantReadCallback,
339 base::Unretained(this),
340 kLen2)));
341 data_->Run();
344 // ----------- Write
346 TEST_F(DeterministicSocketDataTest, SingleSyncWriteWhileStopped) {
347 MockWrite writes[] = {
348 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
351 Initialize(NULL, 0, writes, arraysize(writes));
353 data_->SetStopped(true);
354 AssertWriteReturns(kMsg1, kLen1, ERR_UNEXPECTED);
357 TEST_F(DeterministicSocketDataTest, SingleSyncWriteTooEarly) {
358 MockWrite writes[] = {
359 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 1), // Sync Write
362 MockRead reads[] = {
363 MockRead(SYNCHRONOUS, 0, 0)
366 Initialize(reads, arraysize(reads), writes, arraysize(writes));
368 data_->StopAfter(2);
369 ASSERT_FALSE(data_->stopped());
370 AssertWriteReturns(kMsg1, kLen1, ERR_UNEXPECTED);
373 TEST_F(DeterministicSocketDataTest, SingleSyncWrite) {
374 MockWrite writes[] = {
375 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Write
378 Initialize(NULL, 0, writes, arraysize(writes));
380 // Make sure we don't stop before we've read all the data
381 data_->StopAfter(1);
382 AssertSyncWriteEquals(kMsg1, kLen1);
385 TEST_F(DeterministicSocketDataTest, MultipleSyncWrites) {
386 MockWrite writes[] = {
387 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Write
388 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 1), // Sync Write
389 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 2), // Sync Write
390 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 3), // Sync Write
391 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 4), // Sync Write
392 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 5), // Sync Write
393 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 6), // Sync Write
396 Initialize(NULL, 0, writes, arraysize(writes));
398 // Make sure we don't stop before we've read all the data
399 data_->StopAfter(10);
400 AssertSyncWriteEquals(kMsg1, kLen1);
401 AssertSyncWriteEquals(kMsg2, kLen2);
402 AssertSyncWriteEquals(kMsg3, kLen3);
403 AssertSyncWriteEquals(kMsg3, kLen3);
404 AssertSyncWriteEquals(kMsg2, kLen2);
405 AssertSyncWriteEquals(kMsg3, kLen3);
406 AssertSyncWriteEquals(kMsg1, kLen1);
409 TEST_F(DeterministicSocketDataTest, SingleAsyncWrite) {
410 MockWrite writes[] = {
411 MockWrite(ASYNC, kMsg1, kLen1, 0), // Async Write
414 Initialize(NULL, 0, writes, arraysize(writes));
416 AssertAsyncWriteEquals(kMsg1, kLen1);
419 TEST_F(DeterministicSocketDataTest, MultipleAsyncWrites) {
420 MockWrite writes[] = {
421 MockWrite(ASYNC, kMsg1, kLen1, 0), // Async Write
422 MockWrite(ASYNC, kMsg2, kLen2, 1), // Async Write
423 MockWrite(ASYNC, kMsg3, kLen3, 2), // Async Write
424 MockWrite(ASYNC, kMsg3, kLen3, 3), // Async Write
425 MockWrite(ASYNC, kMsg2, kLen2, 4), // Async Write
426 MockWrite(ASYNC, kMsg3, kLen3, 5), // Async Write
427 MockWrite(ASYNC, kMsg1, kLen1, 6), // Async Write
430 Initialize(NULL, 0, writes, arraysize(writes));
432 AssertAsyncWriteEquals(kMsg1, kLen1);
433 AssertAsyncWriteEquals(kMsg2, kLen2);
434 AssertAsyncWriteEquals(kMsg3, kLen3);
435 AssertAsyncWriteEquals(kMsg3, kLen3);
436 AssertAsyncWriteEquals(kMsg2, kLen2);
437 AssertAsyncWriteEquals(kMsg3, kLen3);
438 AssertAsyncWriteEquals(kMsg1, kLen1);
441 TEST_F(DeterministicSocketDataTest, MixedWrites) {
442 MockWrite writes[] = {
443 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Write
444 MockWrite(ASYNC, kMsg2, kLen2, 1), // Async Write
445 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 2), // Sync Write
446 MockWrite(ASYNC, kMsg3, kLen3, 3), // Async Write
447 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 4), // Sync Write
448 MockWrite(ASYNC, kMsg3, kLen3, 5), // Async Write
449 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 6), // Sync Write
452 Initialize(NULL, 0, writes, arraysize(writes));
454 data_->StopAfter(1);
455 AssertSyncWriteEquals(kMsg1, kLen1);
456 AssertAsyncWriteEquals(kMsg2, kLen2);
457 data_->StopAfter(1);
458 AssertSyncWriteEquals(kMsg3, kLen3);
459 AssertAsyncWriteEquals(kMsg3, kLen3);
460 data_->StopAfter(1);
461 AssertSyncWriteEquals(kMsg2, kLen2);
462 AssertAsyncWriteEquals(kMsg3, kLen3);
463 data_->StopAfter(1);
464 AssertSyncWriteEquals(kMsg1, kLen1);
467 TEST_F(DeterministicSocketDataTest, SyncWriteFromCompletionCallback) {
468 MockWrite writes[] = {
469 MockWrite(ASYNC, kMsg1, kLen1, 0), // Async Write
470 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 1), // Sync Write
473 Initialize(NULL, 0, writes, arraysize(writes));
475 data_->StopAfter(2);
477 scoped_refptr<IOBuffer> write_buf(new IOBuffer(kLen1));
478 memcpy(write_buf->data(), kMsg1, kLen1);
479 ASSERT_EQ(ERR_IO_PENDING,
480 sock_->Write(
481 write_buf.get(),
482 kLen1,
483 base::Bind(&DeterministicSocketDataTest::ReentrantWriteCallback,
484 base::Unretained(this),
485 kMsg2,
486 kLen2)));
487 data_->Run();
490 // ----------- Mixed Reads and Writes
492 TEST_F(DeterministicSocketDataTest, MixedSyncOperations) {
493 MockRead reads[] = {
494 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
495 MockRead(SYNCHRONOUS, kMsg2, kLen2, 3), // Sync Read
496 MockRead(SYNCHRONOUS, 0, 4), // EOF
499 MockWrite writes[] = {
500 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 1), // Sync Write
501 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 2), // Sync Write
504 Initialize(reads, arraysize(reads), writes, arraysize(writes));
506 // Make sure we don't stop before we've read/written everything
507 data_->StopAfter(10);
508 AssertSyncReadEquals(kMsg1, kLen1);
509 AssertSyncWriteEquals(kMsg2, kLen2);
510 AssertSyncWriteEquals(kMsg3, kLen3);
511 AssertSyncReadEquals(kMsg2, kLen2);
514 TEST_F(DeterministicSocketDataTest, MixedAsyncOperations) {
515 MockRead reads[] = {
516 MockRead(ASYNC, kMsg1, kLen1, 0), // Sync Read
517 MockRead(ASYNC, kMsg2, kLen2, 3), // Sync Read
518 MockRead(ASYNC, 0, 4), // EOF
521 MockWrite writes[] = {
522 MockWrite(ASYNC, kMsg2, kLen2, 1), // Sync Write
523 MockWrite(ASYNC, kMsg3, kLen3, 2), // Sync Write
526 Initialize(reads, arraysize(reads), writes, arraysize(writes));
528 AssertAsyncReadEquals(kMsg1, kLen1);
529 AssertAsyncWriteEquals(kMsg2, kLen2);
530 AssertAsyncWriteEquals(kMsg3, kLen3);
531 AssertAsyncReadEquals(kMsg2, kLen2);
534 TEST_F(DeterministicSocketDataTest, InterleavedAsyncOperations) {
535 // Order of completion is read, write, write, read
536 MockRead reads[] = {
537 MockRead(ASYNC, kMsg1, kLen1, 0), // Async Read
538 MockRead(ASYNC, kMsg2, kLen2, 3), // Async Read
539 MockRead(ASYNC, 0, 4), // EOF
542 MockWrite writes[] = {
543 MockWrite(ASYNC, kMsg2, kLen2, 1), // Async Write
544 MockWrite(ASYNC, kMsg3, kLen3, 2), // Async Write
547 Initialize(reads, arraysize(reads), writes, arraysize(writes));
549 // Issue the write, which will block until the read completes
550 AssertWriteReturns(kMsg2, kLen2, ERR_IO_PENDING);
552 // Issue the read which will return first
553 AssertReadReturns(kMsg1, kLen1, ERR_IO_PENDING);
555 data_->RunFor(1);
556 ASSERT_TRUE(read_callback_.have_result());
557 ASSERT_EQ(kLen1, read_callback_.WaitForResult());
558 AssertReadBufferEquals(kMsg1, kLen1);
560 data_->RunFor(1);
561 ASSERT_TRUE(write_callback_.have_result());
562 ASSERT_EQ(kLen2, write_callback_.WaitForResult());
564 data_->StopAfter(1);
565 // Issue the read, which will block until the write completes
566 AssertReadReturns(kMsg2, kLen2, ERR_IO_PENDING);
568 // Issue the writes which will return first
569 AssertWriteReturns(kMsg3, kLen3, ERR_IO_PENDING);
571 data_->RunFor(1);
572 ASSERT_TRUE(write_callback_.have_result());
573 ASSERT_EQ(kLen3, write_callback_.WaitForResult());
575 data_->RunFor(1);
576 ASSERT_TRUE(read_callback_.have_result());
577 ASSERT_EQ(kLen2, read_callback_.WaitForResult());
578 AssertReadBufferEquals(kMsg2, kLen2);
581 TEST_F(DeterministicSocketDataTest, InterleavedMixedOperations) {
582 // Order of completion is read, write, write, read
583 MockRead reads[] = {
584 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
585 MockRead(ASYNC, kMsg2, kLen2, 3), // Async Read
586 MockRead(SYNCHRONOUS, 0, 4), // EOF
589 MockWrite writes[] = {
590 MockWrite(ASYNC, kMsg2, kLen2, 1), // Async Write
591 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 2), // Sync Write
594 Initialize(reads, arraysize(reads), writes, arraysize(writes));
596 // Issue the write, which will block until the read completes
597 AssertWriteReturns(kMsg2, kLen2, ERR_IO_PENDING);
599 // Issue the writes which will complete immediately
600 data_->StopAfter(1);
601 AssertSyncReadEquals(kMsg1, kLen1);
603 data_->RunFor(1);
604 ASSERT_TRUE(write_callback_.have_result());
605 ASSERT_EQ(kLen2, write_callback_.WaitForResult());
607 // Issue the read, which will block until the write completes
608 AssertReadReturns(kMsg2, kLen2, ERR_IO_PENDING);
610 // Issue the writes which will complete immediately
611 data_->StopAfter(1);
612 AssertSyncWriteEquals(kMsg3, kLen3);
614 data_->RunFor(1);
615 ASSERT_TRUE(read_callback_.have_result());
616 ASSERT_EQ(kLen2, read_callback_.WaitForResult());
617 AssertReadBufferEquals(kMsg2, kLen2);
620 } // namespace net