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 DeterministicMockTCPClientSocket
* wrapped_socket
=
34 new DeterministicMockTCPClientSocket(net_log_
.net_log(), data_
.get());
35 data_
->set_delegate(wrapped_socket
->AsWeakPtr());
36 socket_
.reset(new BufferedWriteStreamSocket(wrapped_socket
));
37 socket_
->Connect(callback_
.callback());
40 void TestWrite(const char* text
) {
41 scoped_refptr
<StringIOBuffer
> buf(new StringIOBuffer(text
));
42 EXPECT_EQ(buf
->size(),
43 socket_
->Write(buf
.get(), buf
->size(), callback_
.callback()));
46 scoped_ptr
<BufferedWriteStreamSocket
> socket_
;
47 scoped_ptr
<DeterministicSocketData
> data_
;
49 TestCompletionCallback callback_
;
52 TEST_F(BufferedWriteStreamSocketTest
, SingleWrite
) {
53 MockWrite writes
[] = {
54 MockWrite(SYNCHRONOUS
, 0, "abc"),
56 Initialize(writes
, arraysize(writes
));
61 TEST_F(BufferedWriteStreamSocketTest
, AsyncWrite
) {
62 MockWrite writes
[] = {
63 MockWrite(ASYNC
, 0, "abc"),
65 Initialize(writes
, arraysize(writes
));
71 TEST_F(BufferedWriteStreamSocketTest
, TwoWritesIntoOne
) {
72 MockWrite writes
[] = {
73 MockWrite(SYNCHRONOUS
, 0, "abcdef"),
75 Initialize(writes
, arraysize(writes
));
81 TEST_F(BufferedWriteStreamSocketTest
, WriteWhileBlocked
) {
82 MockWrite writes
[] = {
83 MockWrite(ASYNC
, 0, "abc"),
84 MockWrite(ASYNC
, 1, "def"),
85 MockWrite(ASYNC
, 2, "ghi"),
87 Initialize(writes
, arraysize(writes
));
89 base::MessageLoop::current()->RunUntilIdle();
97 TEST_F(BufferedWriteStreamSocketTest
, ContinuesPartialWrite
) {
98 MockWrite writes
[] = {
99 MockWrite(ASYNC
, 0, "abc"),
100 MockWrite(ASYNC
, 1, "def"),
102 Initialize(writes
, arraysize(writes
));
108 TEST_F(BufferedWriteStreamSocketTest
, TwoSeparateWrites
) {
109 MockWrite writes
[] = {
110 MockWrite(ASYNC
, 0, "abc"),
111 MockWrite(ASYNC
, 1, "def"),
113 Initialize(writes
, arraysize(writes
));
121 } // anonymous namespace