[Android] Add base_junit_tests to VALID_TESTS in bb_device_steps.
[chromium-blink-merge.git] / net / socket / deterministic_socket_data_unittest.cc
blob9a95e1e17b0129eeca666a8f5e55138d84946568
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 DeterministicMockClientSocketFactory socket_factory_;
62 MockTransportClientSocketPool socket_pool_;
63 ClientSocketHandle connection_;
65 DISALLOW_COPY_AND_ASSIGN(DeterministicSocketDataTest);
68 DeterministicSocketDataTest::DeterministicSocketDataTest()
69 : sock_(NULL),
70 read_buf_(NULL),
71 connect_data_(SYNCHRONOUS, OK),
72 endpoint_("www.google.com", 443),
73 tcp_params_(new TransportSocketParams(
74 endpoint_,
75 false,
76 false,
77 OnHostResolutionCallback(),
78 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)),
79 socket_pool_(10, 10, &socket_factory_) {
82 void DeterministicSocketDataTest::TearDown() {
83 // Empty the current queue.
84 base::MessageLoop::current()->RunUntilIdle();
85 PlatformTest::TearDown();
88 void DeterministicSocketDataTest::Initialize(MockRead* reads,
89 size_t reads_count,
90 MockWrite* writes,
91 size_t writes_count) {
92 data_.reset(new DeterministicSocketData(reads, reads_count,
93 writes, writes_count));
94 data_->set_connect_data(connect_data_);
95 socket_factory_.AddSocketDataProvider(data_.get());
97 // Perform the TCP connect
98 EXPECT_EQ(OK,
99 connection_.Init(endpoint_.ToString(),
100 tcp_params_,
101 LOWEST,
102 CompletionCallback(),
103 reinterpret_cast<TransportClientSocketPool*>(&socket_pool_),
104 BoundNetLog()));
105 sock_ = connection_.socket();
108 void DeterministicSocketDataTest::AssertSyncReadEquals(const char* data,
109 int len) {
110 // Issue the read, which will complete immediately
111 AssertReadReturns(data, len, len);
112 AssertReadBufferEquals(data, len);
115 void DeterministicSocketDataTest::AssertAsyncReadEquals(const char* data,
116 int len) {
117 // Issue the read, which will be completed asynchronously
118 AssertReadReturns(data, len, ERR_IO_PENDING);
120 EXPECT_FALSE(read_callback_.have_result());
121 EXPECT_TRUE(sock_->IsConnected());
122 data_->RunFor(1); // Runs 1 step, to cause the callbacks to be invoked
124 // Now the read should complete
125 ASSERT_EQ(len, read_callback_.WaitForResult());
126 AssertReadBufferEquals(data, len);
129 void DeterministicSocketDataTest::AssertReadReturns(const char* data,
130 int len, int rv) {
131 read_buf_ = new IOBuffer(len);
132 ASSERT_EQ(rv, sock_->Read(read_buf_.get(), len, read_callback_.callback()));
135 void DeterministicSocketDataTest::AssertReadBufferEquals(const char* data,
136 int len) {
137 ASSERT_EQ(std::string(data, len), std::string(read_buf_->data(), len));
140 void DeterministicSocketDataTest::AssertSyncWriteEquals(const char* data,
141 int len) {
142 scoped_refptr<IOBuffer> buf(new IOBuffer(len));
143 memcpy(buf->data(), data, len);
145 // Issue the write, which will complete immediately
146 ASSERT_EQ(len, sock_->Write(buf.get(), len, write_callback_.callback()));
149 void DeterministicSocketDataTest::AssertAsyncWriteEquals(const char* data,
150 int len) {
151 // Issue the read, which will be completed asynchronously
152 AssertWriteReturns(data, len, ERR_IO_PENDING);
154 EXPECT_FALSE(read_callback_.have_result());
155 EXPECT_TRUE(sock_->IsConnected());
156 data_->RunFor(1); // Runs 1 step, to cause the callbacks to be invoked
158 ASSERT_EQ(len, write_callback_.WaitForResult());
161 void DeterministicSocketDataTest::AssertWriteReturns(const char* data,
162 int len, int rv) {
163 scoped_refptr<IOBuffer> buf(new IOBuffer(len));
164 memcpy(buf->data(), data, len);
166 // Issue the read, which will complete asynchronously
167 ASSERT_EQ(rv, sock_->Write(buf.get(), len, write_callback_.callback()));
170 void DeterministicSocketDataTest::ReentrantReadCallback(int len, int rv) {
171 scoped_refptr<IOBuffer> read_buf(new IOBuffer(len));
172 EXPECT_EQ(len,
173 sock_->Read(
174 read_buf.get(),
175 len,
176 base::Bind(&DeterministicSocketDataTest::ReentrantReadCallback,
177 base::Unretained(this),
178 len)));
181 void DeterministicSocketDataTest::ReentrantWriteCallback(
182 const char* data, int len, int rv) {
183 scoped_refptr<IOBuffer> write_buf(new IOBuffer(len));
184 memcpy(write_buf->data(), data, len);
185 EXPECT_EQ(len,
186 sock_->Write(
187 write_buf.get(),
188 len,
189 base::Bind(&DeterministicSocketDataTest::ReentrantWriteCallback,
190 base::Unretained(this),
191 data,
192 len)));
195 // ----------- Read
197 TEST_F(DeterministicSocketDataTest, SingleSyncReadWhileStopped) {
198 MockRead reads[] = {
199 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
200 MockRead(SYNCHRONOUS, 0, 1), // EOF
203 Initialize(reads, arraysize(reads), NULL, 0);
205 data_->SetStopped(true);
206 AssertReadReturns(kMsg1, kLen1, ERR_UNEXPECTED);
209 TEST_F(DeterministicSocketDataTest, SingleSyncReadTooEarly) {
210 MockRead reads[] = {
211 MockRead(SYNCHRONOUS, kMsg1, kLen1, 1), // Sync Read
212 MockRead(SYNCHRONOUS, 0, 2), // EOF
215 MockWrite writes[] = {
216 MockWrite(SYNCHRONOUS, 0, 0)
219 Initialize(reads, arraysize(reads), writes, arraysize(writes));
221 data_->StopAfter(2);
222 ASSERT_FALSE(data_->stopped());
223 AssertReadReturns(kMsg1, kLen1, ERR_UNEXPECTED);
226 TEST_F(DeterministicSocketDataTest, SingleSyncRead) {
227 MockRead reads[] = {
228 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
229 MockRead(SYNCHRONOUS, 0, 1), // EOF
232 Initialize(reads, arraysize(reads), NULL, 0);
233 // Make sure we don't stop before we've read all the data
234 data_->StopAfter(1);
235 AssertSyncReadEquals(kMsg1, kLen1);
238 TEST_F(DeterministicSocketDataTest, MultipleSyncReads) {
239 MockRead reads[] = {
240 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
241 MockRead(SYNCHRONOUS, kMsg2, kLen2, 1), // Sync Read
242 MockRead(SYNCHRONOUS, kMsg3, kLen3, 2), // Sync Read
243 MockRead(SYNCHRONOUS, kMsg3, kLen3, 3), // Sync Read
244 MockRead(SYNCHRONOUS, kMsg2, kLen2, 4), // Sync Read
245 MockRead(SYNCHRONOUS, kMsg3, kLen3, 5), // Sync Read
246 MockRead(SYNCHRONOUS, kMsg1, kLen1, 6), // Sync Read
247 MockRead(SYNCHRONOUS, 0, 7), // EOF
250 Initialize(reads, arraysize(reads), NULL, 0);
252 // Make sure we don't stop before we've read all the data
253 data_->StopAfter(10);
254 AssertSyncReadEquals(kMsg1, kLen1);
255 AssertSyncReadEquals(kMsg2, kLen2);
256 AssertSyncReadEquals(kMsg3, kLen3);
257 AssertSyncReadEquals(kMsg3, kLen3);
258 AssertSyncReadEquals(kMsg2, kLen2);
259 AssertSyncReadEquals(kMsg3, kLen3);
260 AssertSyncReadEquals(kMsg1, kLen1);
263 TEST_F(DeterministicSocketDataTest, SingleAsyncRead) {
264 MockRead reads[] = {
265 MockRead(ASYNC, kMsg1, kLen1, 0), // Async Read
266 MockRead(SYNCHRONOUS, 0, 1), // EOF
269 Initialize(reads, arraysize(reads), NULL, 0);
271 AssertAsyncReadEquals(kMsg1, kLen1);
274 TEST_F(DeterministicSocketDataTest, MultipleAsyncReads) {
275 MockRead reads[] = {
276 MockRead(ASYNC, kMsg1, kLen1, 0), // Async Read
277 MockRead(ASYNC, kMsg2, kLen2, 1), // Async Read
278 MockRead(ASYNC, kMsg3, kLen3, 2), // Async Read
279 MockRead(ASYNC, kMsg3, kLen3, 3), // Async Read
280 MockRead(ASYNC, kMsg2, kLen2, 4), // Async Read
281 MockRead(ASYNC, kMsg3, kLen3, 5), // Async Read
282 MockRead(ASYNC, kMsg1, kLen1, 6), // Async Read
283 MockRead(SYNCHRONOUS, 0, 7), // EOF
286 Initialize(reads, arraysize(reads), NULL, 0);
288 AssertAsyncReadEquals(kMsg1, kLen1);
289 AssertAsyncReadEquals(kMsg2, kLen2);
290 AssertAsyncReadEquals(kMsg3, kLen3);
291 AssertAsyncReadEquals(kMsg3, kLen3);
292 AssertAsyncReadEquals(kMsg2, kLen2);
293 AssertAsyncReadEquals(kMsg3, kLen3);
294 AssertAsyncReadEquals(kMsg1, kLen1);
297 TEST_F(DeterministicSocketDataTest, MixedReads) {
298 MockRead reads[] = {
299 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
300 MockRead(ASYNC, kMsg2, kLen2, 1), // Async Read
301 MockRead(SYNCHRONOUS, kMsg3, kLen3, 2), // Sync Read
302 MockRead(ASYNC, kMsg3, kLen3, 3), // Async Read
303 MockRead(SYNCHRONOUS, kMsg2, kLen2, 4), // Sync Read
304 MockRead(ASYNC, kMsg3, kLen3, 5), // Async Read
305 MockRead(SYNCHRONOUS, kMsg1, kLen1, 6), // Sync Read
306 MockRead(SYNCHRONOUS, 0, 7), // EOF
309 Initialize(reads, arraysize(reads), NULL, 0);
311 data_->StopAfter(1);
312 AssertSyncReadEquals(kMsg1, kLen1);
313 AssertAsyncReadEquals(kMsg2, kLen2);
314 data_->StopAfter(1);
315 AssertSyncReadEquals(kMsg3, kLen3);
316 AssertAsyncReadEquals(kMsg3, kLen3);
317 data_->StopAfter(1);
318 AssertSyncReadEquals(kMsg2, kLen2);
319 AssertAsyncReadEquals(kMsg3, kLen3);
320 data_->StopAfter(1);
321 AssertSyncReadEquals(kMsg1, kLen1);
324 TEST_F(DeterministicSocketDataTest, SyncReadFromCompletionCallback) {
325 MockRead reads[] = {
326 MockRead(ASYNC, kMsg1, kLen1, 0), // Async Read
327 MockRead(SYNCHRONOUS, kMsg2, kLen2, 1), // Sync Read
330 Initialize(reads, arraysize(reads), NULL, 0);
332 data_->StopAfter(2);
334 scoped_refptr<IOBuffer> read_buf(new IOBuffer(kLen1));
335 ASSERT_EQ(ERR_IO_PENDING,
336 sock_->Read(
337 read_buf.get(),
338 kLen1,
339 base::Bind(&DeterministicSocketDataTest::ReentrantReadCallback,
340 base::Unretained(this),
341 kLen2)));
342 data_->Run();
345 // ----------- Write
347 TEST_F(DeterministicSocketDataTest, SingleSyncWriteWhileStopped) {
348 MockWrite writes[] = {
349 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
352 Initialize(NULL, 0, writes, arraysize(writes));
354 data_->SetStopped(true);
355 AssertWriteReturns(kMsg1, kLen1, ERR_UNEXPECTED);
358 TEST_F(DeterministicSocketDataTest, SingleSyncWriteTooEarly) {
359 MockWrite writes[] = {
360 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 1), // Sync Write
363 MockRead reads[] = {
364 MockRead(SYNCHRONOUS, 0, 0)
367 Initialize(reads, arraysize(reads), writes, arraysize(writes));
369 data_->StopAfter(2);
370 ASSERT_FALSE(data_->stopped());
371 AssertWriteReturns(kMsg1, kLen1, ERR_UNEXPECTED);
374 TEST_F(DeterministicSocketDataTest, SingleSyncWrite) {
375 MockWrite writes[] = {
376 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Write
379 Initialize(NULL, 0, writes, arraysize(writes));
381 // Make sure we don't stop before we've read all the data
382 data_->StopAfter(1);
383 AssertSyncWriteEquals(kMsg1, kLen1);
386 TEST_F(DeterministicSocketDataTest, MultipleSyncWrites) {
387 MockWrite writes[] = {
388 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Write
389 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 1), // Sync Write
390 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 2), // Sync Write
391 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 3), // Sync Write
392 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 4), // Sync Write
393 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 5), // Sync Write
394 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 6), // Sync Write
397 Initialize(NULL, 0, writes, arraysize(writes));
399 // Make sure we don't stop before we've read all the data
400 data_->StopAfter(10);
401 AssertSyncWriteEquals(kMsg1, kLen1);
402 AssertSyncWriteEquals(kMsg2, kLen2);
403 AssertSyncWriteEquals(kMsg3, kLen3);
404 AssertSyncWriteEquals(kMsg3, kLen3);
405 AssertSyncWriteEquals(kMsg2, kLen2);
406 AssertSyncWriteEquals(kMsg3, kLen3);
407 AssertSyncWriteEquals(kMsg1, kLen1);
410 TEST_F(DeterministicSocketDataTest, SingleAsyncWrite) {
411 MockWrite writes[] = {
412 MockWrite(ASYNC, kMsg1, kLen1, 0), // Async Write
415 Initialize(NULL, 0, writes, arraysize(writes));
417 AssertAsyncWriteEquals(kMsg1, kLen1);
420 TEST_F(DeterministicSocketDataTest, MultipleAsyncWrites) {
421 MockWrite writes[] = {
422 MockWrite(ASYNC, kMsg1, kLen1, 0), // Async Write
423 MockWrite(ASYNC, kMsg2, kLen2, 1), // Async Write
424 MockWrite(ASYNC, kMsg3, kLen3, 2), // Async Write
425 MockWrite(ASYNC, kMsg3, kLen3, 3), // Async Write
426 MockWrite(ASYNC, kMsg2, kLen2, 4), // Async Write
427 MockWrite(ASYNC, kMsg3, kLen3, 5), // Async Write
428 MockWrite(ASYNC, kMsg1, kLen1, 6), // Async Write
431 Initialize(NULL, 0, writes, arraysize(writes));
433 AssertAsyncWriteEquals(kMsg1, kLen1);
434 AssertAsyncWriteEquals(kMsg2, kLen2);
435 AssertAsyncWriteEquals(kMsg3, kLen3);
436 AssertAsyncWriteEquals(kMsg3, kLen3);
437 AssertAsyncWriteEquals(kMsg2, kLen2);
438 AssertAsyncWriteEquals(kMsg3, kLen3);
439 AssertAsyncWriteEquals(kMsg1, kLen1);
442 TEST_F(DeterministicSocketDataTest, MixedWrites) {
443 MockWrite writes[] = {
444 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Write
445 MockWrite(ASYNC, kMsg2, kLen2, 1), // Async Write
446 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 2), // Sync Write
447 MockWrite(ASYNC, kMsg3, kLen3, 3), // Async Write
448 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 4), // Sync Write
449 MockWrite(ASYNC, kMsg3, kLen3, 5), // Async Write
450 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 6), // Sync Write
453 Initialize(NULL, 0, writes, arraysize(writes));
455 data_->StopAfter(1);
456 AssertSyncWriteEquals(kMsg1, kLen1);
457 AssertAsyncWriteEquals(kMsg2, kLen2);
458 data_->StopAfter(1);
459 AssertSyncWriteEquals(kMsg3, kLen3);
460 AssertAsyncWriteEquals(kMsg3, kLen3);
461 data_->StopAfter(1);
462 AssertSyncWriteEquals(kMsg2, kLen2);
463 AssertAsyncWriteEquals(kMsg3, kLen3);
464 data_->StopAfter(1);
465 AssertSyncWriteEquals(kMsg1, kLen1);
468 TEST_F(DeterministicSocketDataTest, SyncWriteFromCompletionCallback) {
469 MockWrite writes[] = {
470 MockWrite(ASYNC, kMsg1, kLen1, 0), // Async Write
471 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 1), // Sync Write
474 Initialize(NULL, 0, writes, arraysize(writes));
476 data_->StopAfter(2);
478 scoped_refptr<IOBuffer> write_buf(new IOBuffer(kLen1));
479 memcpy(write_buf->data(), kMsg1, kLen1);
480 ASSERT_EQ(ERR_IO_PENDING,
481 sock_->Write(
482 write_buf.get(),
483 kLen1,
484 base::Bind(&DeterministicSocketDataTest::ReentrantWriteCallback,
485 base::Unretained(this),
486 kMsg2,
487 kLen2)));
488 data_->Run();
491 // ----------- Mixed Reads and Writes
493 TEST_F(DeterministicSocketDataTest, MixedSyncOperations) {
494 MockRead reads[] = {
495 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
496 MockRead(SYNCHRONOUS, kMsg2, kLen2, 3), // Sync Read
497 MockRead(SYNCHRONOUS, 0, 4), // EOF
500 MockWrite writes[] = {
501 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 1), // Sync Write
502 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 2), // Sync Write
505 Initialize(reads, arraysize(reads), writes, arraysize(writes));
507 // Make sure we don't stop before we've read/written everything
508 data_->StopAfter(10);
509 AssertSyncReadEquals(kMsg1, kLen1);
510 AssertSyncWriteEquals(kMsg2, kLen2);
511 AssertSyncWriteEquals(kMsg3, kLen3);
512 AssertSyncReadEquals(kMsg2, kLen2);
515 TEST_F(DeterministicSocketDataTest, MixedAsyncOperations) {
516 MockRead reads[] = {
517 MockRead(ASYNC, kMsg1, kLen1, 0), // Sync Read
518 MockRead(ASYNC, kMsg2, kLen2, 3), // Sync Read
519 MockRead(ASYNC, 0, 4), // EOF
522 MockWrite writes[] = {
523 MockWrite(ASYNC, kMsg2, kLen2, 1), // Sync Write
524 MockWrite(ASYNC, kMsg3, kLen3, 2), // Sync Write
527 Initialize(reads, arraysize(reads), writes, arraysize(writes));
529 AssertAsyncReadEquals(kMsg1, kLen1);
530 AssertAsyncWriteEquals(kMsg2, kLen2);
531 AssertAsyncWriteEquals(kMsg3, kLen3);
532 AssertAsyncReadEquals(kMsg2, kLen2);
535 TEST_F(DeterministicSocketDataTest, InterleavedAsyncOperations) {
536 // Order of completion is read, write, write, read
537 MockRead reads[] = {
538 MockRead(ASYNC, kMsg1, kLen1, 0), // Async Read
539 MockRead(ASYNC, kMsg2, kLen2, 3), // Async Read
540 MockRead(ASYNC, 0, 4), // EOF
543 MockWrite writes[] = {
544 MockWrite(ASYNC, kMsg2, kLen2, 1), // Async Write
545 MockWrite(ASYNC, kMsg3, kLen3, 2), // Async Write
548 Initialize(reads, arraysize(reads), writes, arraysize(writes));
550 // Issue the write, which will block until the read completes
551 AssertWriteReturns(kMsg2, kLen2, ERR_IO_PENDING);
553 // Issue the read which will return first
554 AssertReadReturns(kMsg1, kLen1, ERR_IO_PENDING);
556 data_->RunFor(1);
557 ASSERT_TRUE(read_callback_.have_result());
558 ASSERT_EQ(kLen1, read_callback_.WaitForResult());
559 AssertReadBufferEquals(kMsg1, kLen1);
561 data_->RunFor(1);
562 ASSERT_TRUE(write_callback_.have_result());
563 ASSERT_EQ(kLen2, write_callback_.WaitForResult());
565 data_->StopAfter(1);
566 // Issue the read, which will block until the write completes
567 AssertReadReturns(kMsg2, kLen2, ERR_IO_PENDING);
569 // Issue the writes which will return first
570 AssertWriteReturns(kMsg3, kLen3, ERR_IO_PENDING);
572 data_->RunFor(1);
573 ASSERT_TRUE(write_callback_.have_result());
574 ASSERT_EQ(kLen3, write_callback_.WaitForResult());
576 data_->RunFor(1);
577 ASSERT_TRUE(read_callback_.have_result());
578 ASSERT_EQ(kLen2, read_callback_.WaitForResult());
579 AssertReadBufferEquals(kMsg2, kLen2);
582 TEST_F(DeterministicSocketDataTest, InterleavedMixedOperations) {
583 // Order of completion is read, write, write, read
584 MockRead reads[] = {
585 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read
586 MockRead(ASYNC, kMsg2, kLen2, 3), // Async Read
587 MockRead(SYNCHRONOUS, 0, 4), // EOF
590 MockWrite writes[] = {
591 MockWrite(ASYNC, kMsg2, kLen2, 1), // Async Write
592 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 2), // Sync Write
595 Initialize(reads, arraysize(reads), writes, arraysize(writes));
597 // Issue the write, which will block until the read completes
598 AssertWriteReturns(kMsg2, kLen2, ERR_IO_PENDING);
600 // Issue the writes which will complete immediately
601 data_->StopAfter(1);
602 AssertSyncReadEquals(kMsg1, kLen1);
604 data_->RunFor(1);
605 ASSERT_TRUE(write_callback_.have_result());
606 ASSERT_EQ(kLen2, write_callback_.WaitForResult());
608 // Issue the read, which will block until the write completes
609 AssertReadReturns(kMsg2, kLen2, ERR_IO_PENDING);
611 // Issue the writes which will complete immediately
612 data_->StopAfter(1);
613 AssertSyncWriteEquals(kMsg3, kLen3);
615 data_->RunFor(1);
616 ASSERT_TRUE(read_callback_.have_result());
617 ASSERT_EQ(kLen2, read_callback_.WaitForResult());
618 AssertReadBufferEquals(kMsg2, kLen2);
621 } // namespace net