Fix Telemetry media seek action flakiness.
[chromium-blink-merge.git] / net / socket / buffered_write_stream_socket_unittest.cc
blobe579a7f51d27dc79da5f201eef867bb25987a6d8
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 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_;
48 BoundNetLog net_log_;
49 TestCompletionCallback callback_;
52 TEST_F(BufferedWriteStreamSocketTest, SingleWrite) {
53 MockWrite writes[] = {
54 MockWrite(SYNCHRONOUS, 0, "abc"),
56 Initialize(writes, arraysize(writes));
57 TestWrite("abc");
58 Finish();
61 TEST_F(BufferedWriteStreamSocketTest, AsyncWrite) {
62 MockWrite writes[] = {
63 MockWrite(ASYNC, 0, "abc"),
65 Initialize(writes, arraysize(writes));
66 TestWrite("abc");
67 data_->Run();
68 Finish();
71 TEST_F(BufferedWriteStreamSocketTest, TwoWritesIntoOne) {
72 MockWrite writes[] = {
73 MockWrite(SYNCHRONOUS, 0, "abcdef"),
75 Initialize(writes, arraysize(writes));
76 TestWrite("abc");
77 TestWrite("def");
78 Finish();
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));
88 TestWrite("abc");
89 base::MessageLoop::current()->RunUntilIdle();
90 TestWrite("def");
91 data_->RunFor(1);
92 TestWrite("ghi");
93 data_->RunFor(1);
94 Finish();
97 TEST_F(BufferedWriteStreamSocketTest, ContinuesPartialWrite) {
98 MockWrite writes[] = {
99 MockWrite(ASYNC, 0, "abc"),
100 MockWrite(ASYNC, 1, "def"),
102 Initialize(writes, arraysize(writes));
103 TestWrite("abcdef");
104 data_->Run();
105 Finish();
108 TEST_F(BufferedWriteStreamSocketTest, TwoSeparateWrites) {
109 MockWrite writes[] = {
110 MockWrite(ASYNC, 0, "abc"),
111 MockWrite(ASYNC, 1, "def"),
113 Initialize(writes, arraysize(writes));
114 TestWrite("abc");
115 data_->RunFor(1);
116 TestWrite("def");
117 data_->RunFor(1);
118 Finish();
121 } // anonymous namespace
123 } // namespace net