summaryrefslogtreecommitdiffstats
path: root/net/socket/buffered_write_stream_socket_unittest.cc
blob: 485295f33f698740189f246cfc3d1570804e4267 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/socket/buffered_write_stream_socket.h"

#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "net/base/net_errors.h"
#include "net/base/net_log.h"
#include "net/socket/socket_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

namespace {

class BufferedWriteStreamSocketTest : public testing::Test {
 public:
  void Finish() {
    base::MessageLoop::current()->RunUntilIdle();
    EXPECT_TRUE(data_->at_read_eof());
    EXPECT_TRUE(data_->at_write_eof());
  }

  void Initialize(MockWrite* writes, size_t writes_count) {
    data_.reset(new DeterministicSocketData(NULL, 0, writes, writes_count));
    data_->set_connect_data(MockConnect(SYNCHRONOUS, 0));
    if (writes_count) {
      data_->StopAfter(writes_count);
    }
    scoped_ptr<DeterministicMockTCPClientSocket> wrapped_socket(
        new DeterministicMockTCPClientSocket(net_log_.net_log(), data_.get()));
    data_->set_delegate(wrapped_socket->AsWeakPtr());
    socket_.reset(new BufferedWriteStreamSocket(
        wrapped_socket.PassAs<StreamSocket>()));
    socket_->Connect(callback_.callback());
  }

  void TestWrite(const char* text) {
    scoped_refptr<StringIOBuffer> buf(new StringIOBuffer(text));
    EXPECT_EQ(buf->size(),
              socket_->Write(buf.get(), buf->size(), callback_.callback()));
  }

  scoped_ptr<BufferedWriteStreamSocket> socket_;
  scoped_ptr<DeterministicSocketData> data_;
  BoundNetLog net_log_;
  TestCompletionCallback callback_;
};

TEST_F(BufferedWriteStreamSocketTest, SingleWrite) {
  MockWrite writes[] = {
    MockWrite(SYNCHRONOUS, 0, "abc"),
  };
  Initialize(writes, arraysize(writes));
  TestWrite("abc");
  Finish();
}

TEST_F(BufferedWriteStreamSocketTest, AsyncWrite) {
  MockWrite writes[] = {
    MockWrite(ASYNC, 0, "abc"),
  };
  Initialize(writes, arraysize(writes));
  TestWrite("abc");
  data_->Run();
  Finish();
}

TEST_F(BufferedWriteStreamSocketTest, TwoWritesIntoOne) {
  MockWrite writes[] = {
    MockWrite(SYNCHRONOUS, 0, "abcdef"),
  };
  Initialize(writes, arraysize(writes));
  TestWrite("abc");
  TestWrite("def");
  Finish();
}

TEST_F(BufferedWriteStreamSocketTest, WriteWhileBlocked) {
  MockWrite writes[] = {
    MockWrite(ASYNC, 0, "abc"),
    MockWrite(ASYNC, 1, "def"),
    MockWrite(ASYNC, 2, "ghi"),
  };
  Initialize(writes, arraysize(writes));
  TestWrite("abc");
  base::MessageLoop::current()->RunUntilIdle();
  TestWrite("def");
  data_->RunFor(1);
  TestWrite("ghi");
  data_->RunFor(1);
  Finish();
}

TEST_F(BufferedWriteStreamSocketTest, ContinuesPartialWrite) {
  MockWrite writes[] = {
    MockWrite(ASYNC, 0, "abc"),
    MockWrite(ASYNC, 1, "def"),
  };
  Initialize(writes, arraysize(writes));
  TestWrite("abcdef");
  data_->Run();
  Finish();
}

TEST_F(BufferedWriteStreamSocketTest, TwoSeparateWrites) {
  MockWrite writes[] = {
    MockWrite(ASYNC, 0, "abc"),
    MockWrite(ASYNC, 1, "def"),
  };
  Initialize(writes, arraysize(writes));
  TestWrite("abc");
  data_->RunFor(1);
  TestWrite("def");
  data_->RunFor(1);
  Finish();
}

}  // anonymous namespace

}  // namespace net