Show Pages in chrome://md-settings
[chromium-blink-merge.git] / net / socket / deterministic_socket_data_unittest.cc
blobbdeba2bef55d6e5cf282a08008546541842503b9
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 void TearDown() override;
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(
75 endpoint_,
76 false,
77 false,
78 OnHostResolutionCallback(),
79 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)),
80 histograms_(std::string()),
81 socket_pool_(10, 10, &histograms_, &socket_factory_) {}
83 void DeterministicSocketDataTest::TearDown() {
84 // Empty the current queue.
85 base::MessageLoop::current()->RunUntilIdle();
86 PlatformTest::TearDown();
89 void DeterministicSocketDataTest::Initialize(MockRead* reads,
90 size_t reads_count,
91 MockWrite* writes,
92 size_t writes_count) {
93 data_.reset(new DeterministicSocketData(reads, reads_count,
94 writes, writes_count));
95 data_->set_connect_data(connect_data_);
96 socket_factory_.AddSocketDataProvider(data_.get());
98 // Perform the TCP connect
99 EXPECT_EQ(OK,
100 connection_.Init(endpoint_.ToString(),
101 tcp_params_,
102 LOWEST,
103 CompletionCallback(),
104 reinterpret_cast<TransportClientSocketPool*>(&socket_pool_),
105 BoundNetLog()));
106 sock_ = connection_.socket();
109 void DeterministicSocketDataTest::AssertSyncReadEquals(const char* data,
110 int len) {
111 // Issue the read, which will complete immediately
112 AssertReadReturns(data, len, len);
113 AssertReadBufferEquals(data, len);
116 void DeterministicSocketDataTest::AssertAsyncReadEquals(const char* data,
117 int len) {
118 // Issue the read, which will be completed asynchronously
119 AssertReadReturns(data, len, ERR_IO_PENDING);
121 EXPECT_FALSE(read_callback_.have_result());
122 EXPECT_TRUE(sock_->IsConnected());
123 data_->RunFor(1); // Runs 1 step, to cause the callbacks to be invoked
125 // Now the read should complete
126 ASSERT_EQ(len, read_callback_.WaitForResult());
127 AssertReadBufferEquals(data, len);
130 void DeterministicSocketDataTest::AssertReadReturns(const char* data,
131 int len, int rv) {
132 read_buf_ = new IOBuffer(len);
133 ASSERT_EQ(rv, sock_->Read(read_buf_.get(), len, read_callback_.callback()));
136 void DeterministicSocketDataTest::AssertReadBufferEquals(const char* data,
137 int len) {
138 ASSERT_EQ(std::string(data, len), std::string(read_buf_->data(), len));
141 void DeterministicSocketDataTest::AssertSyncWriteEquals(const char* data,
142 int len) {
143 scoped_refptr<IOBuffer> buf(new IOBuffer(len));
144 memcpy(buf->data(), data, len);
146 // Issue the write, which will complete immediately
147 ASSERT_EQ(len, sock_->Write(buf.get(), len, write_callback_.callback()));
150 void DeterministicSocketDataTest::AssertAsyncWriteEquals(const char* data,
151 int len) {
152 // Issue the read, which will be completed asynchronously
153 AssertWriteReturns(data, len, ERR_IO_PENDING);
155 EXPECT_FALSE(read_callback_.have_result());
156 EXPECT_TRUE(sock_->IsConnected());
157 data_->RunFor(1); // Runs 1 step, to cause the callbacks to be invoked
159 ASSERT_EQ(len, write_callback_.WaitForResult());
162 void DeterministicSocketDataTest::AssertWriteReturns(const char* data,
163 int len, int rv) {
164 scoped_refptr<IOBuffer> buf(new IOBuffer(len));
165 memcpy(buf->data(), data, len);
167 // Issue the read, which will complete asynchronously
168 ASSERT_EQ(rv, sock_->Write(buf.get(), len, write_callback_.callback()));
171 void DeterministicSocketDataTest::ReentrantReadCallback(int len, int rv) {
172 scoped_refptr<IOBuffer> read_buf(new IOBuffer(len));
173 EXPECT_EQ(len,
174 sock_->Read(
175 read_buf.get(),
176 len,
177 base::Bind(&DeterministicSocketDataTest::ReentrantReadCallback,
178 base::Unretained(this),
179 len)));
182 void DeterministicSocketDataTest::ReentrantWriteCallback(
183 const char* data, int len, int rv) {
184 scoped_refptr<IOBuffer> write_buf(new IOBuffer(len));
185 memcpy(write_buf->data(), data, len);
186 EXPECT_EQ(len,
187 sock_->Write(
188 write_buf.get(),
189 len,
190 base::Bind(&DeterministicSocketDataTest::ReentrantWriteCallback,
191 base::Unretained(this),
192 data,
193 len)));
196 // ----------- Read
198 TEST_F(DeterministicSocketDataTest, SingleSyncReadWhileStopped) {
199 MockRead reads[] = {
200 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
201 MockRead(SYNCHRONOUS, 0, 1), // EOF
204 Initialize(reads, arraysize(reads), NULL, 0);
206 data_->SetStopped(true);
207 AssertReadReturns(kMsg1, kLen1, ERR_UNEXPECTED);
210 TEST_F(DeterministicSocketDataTest, SingleSyncReadTooEarly) {
211 MockRead reads[] = {
212 MockRead(SYNCHRONOUS, kMsg1, kLen1, 1), // Sync Read
213 MockRead(SYNCHRONOUS, 0, 2), // EOF
216 MockWrite writes[] = {
217 MockWrite(SYNCHRONOUS, 0, 0)
220 Initialize(reads, arraysize(reads), writes, arraysize(writes));
222 data_->StopAfter(2);
223 ASSERT_FALSE(data_->stopped());
224 AssertReadReturns(kMsg1, kLen1, ERR_UNEXPECTED);
227 TEST_F(DeterministicSocketDataTest, SingleSyncRead) {
228 MockRead reads[] = {
229 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
230 MockRead(SYNCHRONOUS, 0, 1), // EOF
233 Initialize(reads, arraysize(reads), NULL, 0);
234 // Make sure we don't stop before we've read all the data
235 data_->StopAfter(1);
236 AssertSyncReadEquals(kMsg1, kLen1);
239 TEST_F(DeterministicSocketDataTest, MultipleSyncReads) {
240 MockRead reads[] = {
241 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
242 MockRead(SYNCHRONOUS, kMsg2, kLen2, 1), // Sync Read
243 MockRead(SYNCHRONOUS, kMsg3, kLen3, 2), // Sync Read
244 MockRead(SYNCHRONOUS, kMsg3, kLen3, 3), // Sync Read
245 MockRead(SYNCHRONOUS, kMsg2, kLen2, 4), // Sync Read
246 MockRead(SYNCHRONOUS, kMsg3, kLen3, 5), // Sync Read
247 MockRead(SYNCHRONOUS, kMsg1, kLen1, 6), // Sync Read
248 MockRead(SYNCHRONOUS, 0, 7), // EOF
251 Initialize(reads, arraysize(reads), NULL, 0);
253 // Make sure we don't stop before we've read all the data
254 data_->StopAfter(10);
255 AssertSyncReadEquals(kMsg1, kLen1);
256 AssertSyncReadEquals(kMsg2, kLen2);
257 AssertSyncReadEquals(kMsg3, kLen3);
258 AssertSyncReadEquals(kMsg3, kLen3);
259 AssertSyncReadEquals(kMsg2, kLen2);
260 AssertSyncReadEquals(kMsg3, kLen3);
261 AssertSyncReadEquals(kMsg1, kLen1);
264 TEST_F(DeterministicSocketDataTest, SingleAsyncRead) {
265 MockRead reads[] = {
266 MockRead(ASYNC, kMsg1, kLen1, 0), // Async Read
267 MockRead(SYNCHRONOUS, 0, 1), // EOF
270 Initialize(reads, arraysize(reads), NULL, 0);
272 AssertAsyncReadEquals(kMsg1, kLen1);
275 TEST_F(DeterministicSocketDataTest, MultipleAsyncReads) {
276 MockRead reads[] = {
277 MockRead(ASYNC, kMsg1, kLen1, 0), // Async Read
278 MockRead(ASYNC, kMsg2, kLen2, 1), // Async Read
279 MockRead(ASYNC, kMsg3, kLen3, 2), // Async Read
280 MockRead(ASYNC, kMsg3, kLen3, 3), // Async Read
281 MockRead(ASYNC, kMsg2, kLen2, 4), // Async Read
282 MockRead(ASYNC, kMsg3, kLen3, 5), // Async Read
283 MockRead(ASYNC, kMsg1, kLen1, 6), // Async Read
284 MockRead(SYNCHRONOUS, 0, 7), // EOF
287 Initialize(reads, arraysize(reads), NULL, 0);
289 AssertAsyncReadEquals(kMsg1, kLen1);
290 AssertAsyncReadEquals(kMsg2, kLen2);
291 AssertAsyncReadEquals(kMsg3, kLen3);
292 AssertAsyncReadEquals(kMsg3, kLen3);
293 AssertAsyncReadEquals(kMsg2, kLen2);
294 AssertAsyncReadEquals(kMsg3, kLen3);
295 AssertAsyncReadEquals(kMsg1, kLen1);
298 TEST_F(DeterministicSocketDataTest, MixedReads) {
299 MockRead reads[] = {
300 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
301 MockRead(ASYNC, kMsg2, kLen2, 1), // Async Read
302 MockRead(SYNCHRONOUS, kMsg3, kLen3, 2), // Sync Read
303 MockRead(ASYNC, kMsg3, kLen3, 3), // Async Read
304 MockRead(SYNCHRONOUS, kMsg2, kLen2, 4), // Sync Read
305 MockRead(ASYNC, kMsg3, kLen3, 5), // Async Read
306 MockRead(SYNCHRONOUS, kMsg1, kLen1, 6), // Sync Read
307 MockRead(SYNCHRONOUS, 0, 7), // EOF
310 Initialize(reads, arraysize(reads), NULL, 0);
312 data_->StopAfter(1);
313 AssertSyncReadEquals(kMsg1, kLen1);
314 AssertAsyncReadEquals(kMsg2, kLen2);
315 data_->StopAfter(1);
316 AssertSyncReadEquals(kMsg3, kLen3);
317 AssertAsyncReadEquals(kMsg3, kLen3);
318 data_->StopAfter(1);
319 AssertSyncReadEquals(kMsg2, kLen2);
320 AssertAsyncReadEquals(kMsg3, kLen3);
321 data_->StopAfter(1);
322 AssertSyncReadEquals(kMsg1, kLen1);
325 TEST_F(DeterministicSocketDataTest, SyncReadFromCompletionCallback) {
326 MockRead reads[] = {
327 MockRead(ASYNC, kMsg1, kLen1, 0), // Async Read
328 MockRead(SYNCHRONOUS, kMsg2, kLen2, 1), // Sync Read
331 Initialize(reads, arraysize(reads), NULL, 0);
333 data_->StopAfter(2);
335 scoped_refptr<IOBuffer> read_buf(new IOBuffer(kLen1));
336 ASSERT_EQ(ERR_IO_PENDING,
337 sock_->Read(
338 read_buf.get(),
339 kLen1,
340 base::Bind(&DeterministicSocketDataTest::ReentrantReadCallback,
341 base::Unretained(this),
342 kLen2)));
343 data_->Run();
346 // ----------- Write
348 TEST_F(DeterministicSocketDataTest, SingleSyncWriteWhileStopped) {
349 MockWrite writes[] = {
350 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
353 Initialize(NULL, 0, writes, arraysize(writes));
355 data_->SetStopped(true);
356 AssertWriteReturns(kMsg1, kLen1, ERR_UNEXPECTED);
359 TEST_F(DeterministicSocketDataTest, SingleSyncWriteTooEarly) {
360 MockWrite writes[] = {
361 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 1), // Sync Write
364 MockRead reads[] = {
365 MockRead(SYNCHRONOUS, 0, 0)
368 Initialize(reads, arraysize(reads), writes, arraysize(writes));
370 data_->StopAfter(2);
371 ASSERT_FALSE(data_->stopped());
372 AssertWriteReturns(kMsg1, kLen1, ERR_UNEXPECTED);
375 TEST_F(DeterministicSocketDataTest, SingleSyncWrite) {
376 MockWrite writes[] = {
377 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Write
380 Initialize(NULL, 0, writes, arraysize(writes));
382 // Make sure we don't stop before we've read all the data
383 data_->StopAfter(1);
384 AssertSyncWriteEquals(kMsg1, kLen1);
387 TEST_F(DeterministicSocketDataTest, MultipleSyncWrites) {
388 MockWrite writes[] = {
389 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Write
390 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 1), // Sync Write
391 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 2), // Sync Write
392 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 3), // Sync Write
393 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 4), // Sync Write
394 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 5), // Sync Write
395 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 6), // Sync Write
398 Initialize(NULL, 0, writes, arraysize(writes));
400 // Make sure we don't stop before we've read all the data
401 data_->StopAfter(10);
402 AssertSyncWriteEquals(kMsg1, kLen1);
403 AssertSyncWriteEquals(kMsg2, kLen2);
404 AssertSyncWriteEquals(kMsg3, kLen3);
405 AssertSyncWriteEquals(kMsg3, kLen3);
406 AssertSyncWriteEquals(kMsg2, kLen2);
407 AssertSyncWriteEquals(kMsg3, kLen3);
408 AssertSyncWriteEquals(kMsg1, kLen1);
411 TEST_F(DeterministicSocketDataTest, SingleAsyncWrite) {
412 MockWrite writes[] = {
413 MockWrite(ASYNC, kMsg1, kLen1, 0), // Async Write
416 Initialize(NULL, 0, writes, arraysize(writes));
418 AssertAsyncWriteEquals(kMsg1, kLen1);
421 TEST_F(DeterministicSocketDataTest, MultipleAsyncWrites) {
422 MockWrite writes[] = {
423 MockWrite(ASYNC, kMsg1, kLen1, 0), // Async Write
424 MockWrite(ASYNC, kMsg2, kLen2, 1), // Async Write
425 MockWrite(ASYNC, kMsg3, kLen3, 2), // Async Write
426 MockWrite(ASYNC, kMsg3, kLen3, 3), // Async Write
427 MockWrite(ASYNC, kMsg2, kLen2, 4), // Async Write
428 MockWrite(ASYNC, kMsg3, kLen3, 5), // Async Write
429 MockWrite(ASYNC, kMsg1, kLen1, 6), // Async Write
432 Initialize(NULL, 0, writes, arraysize(writes));
434 AssertAsyncWriteEquals(kMsg1, kLen1);
435 AssertAsyncWriteEquals(kMsg2, kLen2);
436 AssertAsyncWriteEquals(kMsg3, kLen3);
437 AssertAsyncWriteEquals(kMsg3, kLen3);
438 AssertAsyncWriteEquals(kMsg2, kLen2);
439 AssertAsyncWriteEquals(kMsg3, kLen3);
440 AssertAsyncWriteEquals(kMsg1, kLen1);
443 TEST_F(DeterministicSocketDataTest, MixedWrites) {
444 MockWrite writes[] = {
445 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Write
446 MockWrite(ASYNC, kMsg2, kLen2, 1), // Async Write
447 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 2), // Sync Write
448 MockWrite(ASYNC, kMsg3, kLen3, 3), // Async Write
449 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 4), // Sync Write
450 MockWrite(ASYNC, kMsg3, kLen3, 5), // Async Write
451 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 6), // Sync Write
454 Initialize(NULL, 0, writes, arraysize(writes));
456 data_->StopAfter(1);
457 AssertSyncWriteEquals(kMsg1, kLen1);
458 AssertAsyncWriteEquals(kMsg2, kLen2);
459 data_->StopAfter(1);
460 AssertSyncWriteEquals(kMsg3, kLen3);
461 AssertAsyncWriteEquals(kMsg3, kLen3);
462 data_->StopAfter(1);
463 AssertSyncWriteEquals(kMsg2, kLen2);
464 AssertAsyncWriteEquals(kMsg3, kLen3);
465 data_->StopAfter(1);
466 AssertSyncWriteEquals(kMsg1, kLen1);
469 TEST_F(DeterministicSocketDataTest, SyncWriteFromCompletionCallback) {
470 MockWrite writes[] = {
471 MockWrite(ASYNC, kMsg1, kLen1, 0), // Async Write
472 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 1), // Sync Write
475 Initialize(NULL, 0, writes, arraysize(writes));
477 data_->StopAfter(2);
479 scoped_refptr<IOBuffer> write_buf(new IOBuffer(kLen1));
480 memcpy(write_buf->data(), kMsg1, kLen1);
481 ASSERT_EQ(ERR_IO_PENDING,
482 sock_->Write(
483 write_buf.get(),
484 kLen1,
485 base::Bind(&DeterministicSocketDataTest::ReentrantWriteCallback,
486 base::Unretained(this),
487 kMsg2,
488 kLen2)));
489 data_->Run();
492 // ----------- Mixed Reads and Writes
494 TEST_F(DeterministicSocketDataTest, MixedSyncOperations) {
495 MockRead reads[] = {
496 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
497 MockRead(SYNCHRONOUS, kMsg2, kLen2, 3), // Sync Read
498 MockRead(SYNCHRONOUS, 0, 4), // EOF
501 MockWrite writes[] = {
502 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 1), // Sync Write
503 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 2), // Sync Write
506 Initialize(reads, arraysize(reads), writes, arraysize(writes));
508 // Make sure we don't stop before we've read/written everything
509 data_->StopAfter(10);
510 AssertSyncReadEquals(kMsg1, kLen1);
511 AssertSyncWriteEquals(kMsg2, kLen2);
512 AssertSyncWriteEquals(kMsg3, kLen3);
513 AssertSyncReadEquals(kMsg2, kLen2);
516 TEST_F(DeterministicSocketDataTest, MixedAsyncOperations) {
517 MockRead reads[] = {
518 MockRead(ASYNC, kMsg1, kLen1, 0), // Sync Read
519 MockRead(ASYNC, kMsg2, kLen2, 3), // Sync Read
520 MockRead(ASYNC, 0, 4), // EOF
523 MockWrite writes[] = {
524 MockWrite(ASYNC, kMsg2, kLen2, 1), // Sync Write
525 MockWrite(ASYNC, kMsg3, kLen3, 2), // Sync Write
528 Initialize(reads, arraysize(reads), writes, arraysize(writes));
530 AssertAsyncReadEquals(kMsg1, kLen1);
531 AssertAsyncWriteEquals(kMsg2, kLen2);
532 AssertAsyncWriteEquals(kMsg3, kLen3);
533 AssertAsyncReadEquals(kMsg2, kLen2);
536 TEST_F(DeterministicSocketDataTest, InterleavedAsyncOperations) {
537 // Order of completion is read, write, write, read
538 MockRead reads[] = {
539 MockRead(ASYNC, kMsg1, kLen1, 0), // Async Read
540 MockRead(ASYNC, kMsg2, kLen2, 3), // Async Read
541 MockRead(ASYNC, 0, 4), // EOF
544 MockWrite writes[] = {
545 MockWrite(ASYNC, kMsg2, kLen2, 1), // Async Write
546 MockWrite(ASYNC, kMsg3, kLen3, 2), // Async Write
549 Initialize(reads, arraysize(reads), writes, arraysize(writes));
551 // Issue the write, which will block until the read completes
552 AssertWriteReturns(kMsg2, kLen2, ERR_IO_PENDING);
554 // Issue the read which will return first
555 AssertReadReturns(kMsg1, kLen1, ERR_IO_PENDING);
557 data_->RunFor(1);
558 ASSERT_TRUE(read_callback_.have_result());
559 ASSERT_EQ(kLen1, read_callback_.WaitForResult());
560 AssertReadBufferEquals(kMsg1, kLen1);
562 data_->RunFor(1);
563 ASSERT_TRUE(write_callback_.have_result());
564 ASSERT_EQ(kLen2, write_callback_.WaitForResult());
566 data_->StopAfter(1);
567 // Issue the read, which will block until the write completes
568 AssertReadReturns(kMsg2, kLen2, ERR_IO_PENDING);
570 // Issue the writes which will return first
571 AssertWriteReturns(kMsg3, kLen3, ERR_IO_PENDING);
573 data_->RunFor(1);
574 ASSERT_TRUE(write_callback_.have_result());
575 ASSERT_EQ(kLen3, write_callback_.WaitForResult());
577 data_->RunFor(1);
578 ASSERT_TRUE(read_callback_.have_result());
579 ASSERT_EQ(kLen2, read_callback_.WaitForResult());
580 AssertReadBufferEquals(kMsg2, kLen2);
583 TEST_F(DeterministicSocketDataTest, InterleavedMixedOperations) {
584 // Order of completion is read, write, write, read
585 MockRead reads[] = {
586 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
587 MockRead(ASYNC, kMsg2, kLen2, 3), // Async Read
588 MockRead(SYNCHRONOUS, 0, 4), // EOF
591 MockWrite writes[] = {
592 MockWrite(ASYNC, kMsg2, kLen2, 1), // Async Write
593 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 2), // Sync Write
596 Initialize(reads, arraysize(reads), writes, arraysize(writes));
598 // Issue the write, which will block until the read completes
599 AssertWriteReturns(kMsg2, kLen2, ERR_IO_PENDING);
601 // Issue the writes which will complete immediately
602 data_->StopAfter(1);
603 AssertSyncReadEquals(kMsg1, kLen1);
605 data_->RunFor(1);
606 ASSERT_TRUE(write_callback_.have_result());
607 ASSERT_EQ(kLen2, write_callback_.WaitForResult());
609 // Issue the read, which will block until the write completes
610 AssertReadReturns(kMsg2, kLen2, ERR_IO_PENDING);
612 // Issue the writes which will complete immediately
613 data_->StopAfter(1);
614 AssertSyncWriteEquals(kMsg3, kLen3);
616 data_->RunFor(1);
617 ASSERT_TRUE(read_callback_.have_result());
618 ASSERT_EQ(kLen2, read_callback_.WaitForResult());
619 AssertReadBufferEquals(kMsg2, kLen2);
622 } // namespace net