OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "net/socket/buffered_write_stream_socket.h" |
| 6 |
| 7 #include "base/message_loop.h" |
| 8 #include "base/memory/ref_counted.h" |
| 9 #include "net/base/net_errors.h" |
| 10 #include "net/base/net_log.h" |
| 11 #include "net/socket/socket_test_util.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace net { |
| 15 |
| 16 namespace { |
| 17 |
| 18 class BufferedWriteStreamSocketTest : public testing::Test { |
| 19 public: |
| 20 void Finish() { |
| 21 MessageLoop::current()->RunAllPending(); |
| 22 EXPECT_TRUE(data_->at_read_eof()); |
| 23 EXPECT_TRUE(data_->at_write_eof()); |
| 24 } |
| 25 |
| 26 void Initialize(MockWrite* writes, size_t writes_count) { |
| 27 data_ = new DeterministicSocketData(NULL, 0, writes, writes_count); |
| 28 data_->set_connect_data(MockConnect(SYNCHRONOUS, 0)); |
| 29 if (writes_count) { |
| 30 data_->StopAfter(writes_count); |
| 31 } |
| 32 DeterministicMockTCPClientSocket* wrapped_socket = |
| 33 new DeterministicMockTCPClientSocket(net_log_.net_log(), data_.get()); |
| 34 data_->set_socket(wrapped_socket->AsWeakPtr()); |
| 35 socket_.reset(new BufferedWriteStreamSocket(wrapped_socket)); |
| 36 socket_->Connect(callback_.callback()); |
| 37 } |
| 38 |
| 39 void TestWrite(const char* text) { |
| 40 scoped_refptr<StringIOBuffer> buf(new StringIOBuffer(text)); |
| 41 EXPECT_EQ(buf->size(), |
| 42 socket_->Write(buf.get(), buf->size(), callback_.callback())); |
| 43 } |
| 44 |
| 45 scoped_ptr<BufferedWriteStreamSocket> socket_; |
| 46 scoped_refptr<DeterministicSocketData> data_; |
| 47 BoundNetLog net_log_; |
| 48 TestCompletionCallback callback_; |
| 49 }; |
| 50 |
| 51 TEST_F(BufferedWriteStreamSocketTest, SingleWrite) { |
| 52 MockWrite writes[] = { |
| 53 MockWrite(false, 0, "abc"), |
| 54 }; |
| 55 Initialize(writes, arraysize(writes)); |
| 56 TestWrite("abc"); |
| 57 Finish(); |
| 58 } |
| 59 |
| 60 TEST_F(BufferedWriteStreamSocketTest, AsyncWrite) { |
| 61 MockWrite writes[] = { |
| 62 MockWrite(true, 0, "abc"), |
| 63 }; |
| 64 Initialize(writes, arraysize(writes)); |
| 65 TestWrite("abc"); |
| 66 data_->Run(); |
| 67 Finish(); |
| 68 } |
| 69 |
| 70 TEST_F(BufferedWriteStreamSocketTest, TwoWritesIntoOne) { |
| 71 MockWrite writes[] = { |
| 72 MockWrite(false, 0, "abcdef"), |
| 73 }; |
| 74 Initialize(writes, arraysize(writes)); |
| 75 TestWrite("abc"); |
| 76 TestWrite("def"); |
| 77 Finish(); |
| 78 } |
| 79 |
| 80 TEST_F(BufferedWriteStreamSocketTest, WriteWhileBlocked) { |
| 81 MockWrite writes[] = { |
| 82 MockWrite(true, 0, "abc"), |
| 83 MockWrite(true, 1, "def"), |
| 84 }; |
| 85 Initialize(writes, arraysize(writes)); |
| 86 TestWrite("abc"); |
| 87 MessageLoop::current()->RunAllPending(); |
| 88 TestWrite("def"); |
| 89 data_->Run(); |
| 90 Finish(); |
| 91 } |
| 92 |
| 93 TEST_F(BufferedWriteStreamSocketTest, ContinuesPartialWrite) { |
| 94 MockWrite writes[] = { |
| 95 MockWrite(true, 0, "abc"), |
| 96 MockWrite(true, 1, "def"), |
| 97 }; |
| 98 Initialize(writes, arraysize(writes)); |
| 99 TestWrite("abcdef"); |
| 100 data_->Run(); |
| 101 Finish(); |
| 102 } |
| 103 |
| 104 TEST_F(BufferedWriteStreamSocketTest, TwoSeparateWrites) { |
| 105 MockWrite writes[] = { |
| 106 MockWrite(true, 0, "abc"), |
| 107 MockWrite(true, 1, "def"), |
| 108 }; |
| 109 Initialize(writes, arraysize(writes)); |
| 110 TestWrite("abc"); |
| 111 data_->RunFor(1); |
| 112 TestWrite("def"); |
| 113 data_->RunFor(1); |
| 114 Finish(); |
| 115 } |
| 116 |
| 117 } // anonymous namespace |
| 118 |
| 119 } // namespace net |
OLD | NEW |