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"
19 class BufferedWriteStreamSocketTest
: public testing::Test
{
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));
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_
;
50 TestCompletionCallback callback_
;
53 TEST_F(BufferedWriteStreamSocketTest
, SingleWrite
) {
54 MockWrite writes
[] = {
55 MockWrite(SYNCHRONOUS
, 0, "abc"),
57 Initialize(writes
, arraysize(writes
));
62 TEST_F(BufferedWriteStreamSocketTest
, AsyncWrite
) {
63 MockWrite writes
[] = {
64 MockWrite(ASYNC
, 0, "abc"),
66 Initialize(writes
, arraysize(writes
));
72 TEST_F(BufferedWriteStreamSocketTest
, TwoWritesIntoOne
) {
73 MockWrite writes
[] = {
74 MockWrite(SYNCHRONOUS
, 0, "abcdef"),
76 Initialize(writes
, arraysize(writes
));
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
));
90 base::MessageLoop::current()->RunUntilIdle();
98 TEST_F(BufferedWriteStreamSocketTest
, ContinuesPartialWrite
) {
99 MockWrite writes
[] = {
100 MockWrite(ASYNC
, 0, "abc"),
101 MockWrite(ASYNC
, 1, "def"),
103 Initialize(writes
, arraysize(writes
));
109 TEST_F(BufferedWriteStreamSocketTest
, TwoSeparateWrites
) {
110 MockWrite writes
[] = {
111 MockWrite(ASYNC
, 0, "abc"),
112 MockWrite(ASYNC
, 1, "def"),
114 Initialize(writes
, arraysize(writes
));
122 } // anonymous namespace