[Android] Lint pylib/host_driven.
[chromium-blink-merge.git] / net / socket / buffered_write_stream_socket_unittest.cc
blob485295f33f698740189f246cfc3d1570804e4267
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/buffered_write_stream_socket.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "net/base/net_errors.h"
11 #include "net/base/net_log.h"
12 #include "net/socket/socket_test_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace net {
17 namespace {
19 class BufferedWriteStreamSocketTest : public testing::Test {
20 public:
21 void Finish() {
22 base::MessageLoop::current()->RunUntilIdle();
23 EXPECT_TRUE(data_->at_read_eof());
24 EXPECT_TRUE(data_->at_write_eof());
27 void Initialize(MockWrite* writes, size_t writes_count) {
28 data_.reset(new DeterministicSocketData(NULL, 0, writes, writes_count));
29 data_->set_connect_data(MockConnect(SYNCHRONOUS, 0));
30 if (writes_count) {
31 data_->StopAfter(writes_count);
33 scoped_ptr<DeterministicMockTCPClientSocket> wrapped_socket(
34 new DeterministicMockTCPClientSocket(net_log_.net_log(), data_.get()));
35 data_->set_delegate(wrapped_socket->AsWeakPtr());
36 socket_.reset(new BufferedWriteStreamSocket(
37 wrapped_socket.PassAs<StreamSocket>()));
38 socket_->Connect(callback_.callback());
41 void TestWrite(const char* text) {
42 scoped_refptr<StringIOBuffer> buf(new StringIOBuffer(text));
43 EXPECT_EQ(buf->size(),
44 socket_->Write(buf.get(), buf->size(), callback_.callback()));
47 scoped_ptr<BufferedWriteStreamSocket> socket_;
48 scoped_ptr<DeterministicSocketData> data_;
49 BoundNetLog net_log_;
50 TestCompletionCallback callback_;
53 TEST_F(BufferedWriteStreamSocketTest, SingleWrite) {
54 MockWrite writes[] = {
55 MockWrite(SYNCHRONOUS, 0, "abc"),
57 Initialize(writes, arraysize(writes));
58 TestWrite("abc");
59 Finish();
62 TEST_F(BufferedWriteStreamSocketTest, AsyncWrite) {
63 MockWrite writes[] = {
64 MockWrite(ASYNC, 0, "abc"),
66 Initialize(writes, arraysize(writes));
67 TestWrite("abc");
68 data_->Run();
69 Finish();
72 TEST_F(BufferedWriteStreamSocketTest, TwoWritesIntoOne) {
73 MockWrite writes[] = {
74 MockWrite(SYNCHRONOUS, 0, "abcdef"),
76 Initialize(writes, arraysize(writes));
77 TestWrite("abc");
78 TestWrite("def");
79 Finish();
82 TEST_F(BufferedWriteStreamSocketTest, WriteWhileBlocked) {
83 MockWrite writes[] = {
84 MockWrite(ASYNC, 0, "abc"),
85 MockWrite(ASYNC, 1, "def"),
86 MockWrite(ASYNC, 2, "ghi"),
88 Initialize(writes, arraysize(writes));
89 TestWrite("abc");
90 base::MessageLoop::current()->RunUntilIdle();
91 TestWrite("def");
92 data_->RunFor(1);
93 TestWrite("ghi");
94 data_->RunFor(1);
95 Finish();
98 TEST_F(BufferedWriteStreamSocketTest, ContinuesPartialWrite) {
99 MockWrite writes[] = {
100 MockWrite(ASYNC, 0, "abc"),
101 MockWrite(ASYNC, 1, "def"),
103 Initialize(writes, arraysize(writes));
104 TestWrite("abcdef");
105 data_->Run();
106 Finish();
109 TEST_F(BufferedWriteStreamSocketTest, TwoSeparateWrites) {
110 MockWrite writes[] = {
111 MockWrite(ASYNC, 0, "abc"),
112 MockWrite(ASYNC, 1, "def"),
114 Initialize(writes, arraysize(writes));
115 TestWrite("abc");
116 data_->RunFor(1);
117 TestWrite("def");
118 data_->RunFor(1);
119 Finish();
122 } // anonymous namespace
124 } // namespace net