summaryrefslogtreecommitdiffstats
path: root/net/http/http_pipelined_host_pool_unittest.cc
blob: 928e4e9e6074e9d16140a04181a6bfddaf11bad2 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (c) 2011 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/http/http_pipelined_host_pool.h"

#include "base/memory/scoped_ptr.h"
#include "net/base/ssl_config_service.h"
#include "net/http/http_pipelined_host.h"
#include "net/proxy/proxy_info.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;
using testing::Ref;
using testing::Return;
using testing::ReturnNull;

namespace net {

namespace {

ClientSocketHandle* kDummyConnection =
    reinterpret_cast<ClientSocketHandle*>(188);
HttpPipelinedStream* kDummyStream =
    reinterpret_cast<HttpPipelinedStream*>(99);

class MockPoolDelegate : public HttpPipelinedHostPool::Delegate {
 public:
  MOCK_METHOD1(OnHttpPipelinedHostHasAdditionalCapacity,
               void(const HostPortPair& origin));
};

class MockHostFactory : public HttpPipelinedHost::Factory {
 public:
  MOCK_METHOD4(CreateNewHost, HttpPipelinedHost*(
      HttpPipelinedHost::Delegate* delegate, const HostPortPair& origin,
      HttpPipelinedConnection::Factory* factory,
      HttpPipelinedHost::Capability capability));
};

class MockHost : public HttpPipelinedHost {
 public:
  MockHost(const HostPortPair& origin)
      : origin_(origin) {
  }

  MOCK_METHOD5(CreateStreamOnNewPipeline, HttpPipelinedStream*(
      ClientSocketHandle* connection,
      const SSLConfig& used_ssl_config,
      const ProxyInfo& used_proxy_info,
      const BoundNetLog& net_log,
      bool was_npn_negotiated));
  MOCK_METHOD0(CreateStreamOnExistingPipeline, HttpPipelinedStream*());
  MOCK_CONST_METHOD0(IsExistingPipelineAvailable, bool());

  virtual const HostPortPair& origin() const OVERRIDE { return origin_; }

 private:
  HostPortPair origin_;
};

class HttpPipelinedHostPoolTest : public testing::Test {
 public:
  HttpPipelinedHostPoolTest()
      : origin_("host", 123),
        factory_(new MockHostFactory),  // Owned by pool_.
        host_(new MockHost(origin_)),  // Owned by pool_.
        pool_(new HttpPipelinedHostPool(&delegate_, factory_)),
        was_npn_negotiated_(false) {
  }

  void CreateDummyStream() {
    EXPECT_CALL(*host_, CreateStreamOnNewPipeline(kDummyConnection,
                                                  Ref(ssl_config_),
                                                  Ref(proxy_info_),
                                                  Ref(net_log_),
                                                  was_npn_negotiated_))
        .Times(1)
        .WillOnce(Return(kDummyStream));
    EXPECT_EQ(kDummyStream,
              pool_->CreateStreamOnNewPipeline(origin_, kDummyConnection,
                                               ssl_config_, proxy_info_,
                                               net_log_, was_npn_negotiated_));
  }

  HostPortPair origin_;
  MockPoolDelegate delegate_;
  MockHostFactory* factory_;
  MockHost* host_;
  scoped_ptr<HttpPipelinedHostPool> pool_;

  const SSLConfig ssl_config_;
  const ProxyInfo proxy_info_;
  const BoundNetLog net_log_;
  bool was_npn_negotiated_;
};

TEST_F(HttpPipelinedHostPoolTest, DefaultUnknown) {
  EXPECT_TRUE(pool_->IsHostEligibleForPipelining(origin_));
  EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _,
                                       HttpPipelinedHost::UNKNOWN))
      .Times(1)
      .WillOnce(Return(host_));

  CreateDummyStream();
  pool_->OnHostIdle(host_);
}

TEST_F(HttpPipelinedHostPoolTest, RemembersIncapable) {
  EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _,
                                       HttpPipelinedHost::UNKNOWN))
      .Times(1)
      .WillOnce(Return(host_));

  CreateDummyStream();
  pool_->OnHostDeterminedCapability(host_, HttpPipelinedHost::INCAPABLE);
  pool_->OnHostIdle(host_);
  EXPECT_FALSE(pool_->IsHostEligibleForPipelining(origin_));
  EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _,
                                       HttpPipelinedHost::INCAPABLE))
      .Times(0);
  EXPECT_EQ(NULL,
            pool_->CreateStreamOnNewPipeline(origin_, kDummyConnection,
                                             ssl_config_, proxy_info_, net_log_,
                                             was_npn_negotiated_));
}

TEST_F(HttpPipelinedHostPoolTest, RemembersCapable) {
  EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _,
                                       HttpPipelinedHost::UNKNOWN))
      .Times(1)
      .WillOnce(Return(host_));

  CreateDummyStream();
  pool_->OnHostDeterminedCapability(host_, HttpPipelinedHost::CAPABLE);
  pool_->OnHostIdle(host_);
  EXPECT_TRUE(pool_->IsHostEligibleForPipelining(origin_));

  host_ = new MockHost(origin_);
  EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _,
                                       HttpPipelinedHost::CAPABLE))
      .Times(1)
      .WillOnce(Return(host_));
  CreateDummyStream();
  pool_->OnHostIdle(host_);
}

TEST_F(HttpPipelinedHostPoolTest, IncapableIsSticky) {
  EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _,
                                       HttpPipelinedHost::UNKNOWN))
      .Times(1)
      .WillOnce(Return(host_));

  CreateDummyStream();
  pool_->OnHostDeterminedCapability(host_, HttpPipelinedHost::CAPABLE);
  pool_->OnHostDeterminedCapability(host_, HttpPipelinedHost::INCAPABLE);
  pool_->OnHostDeterminedCapability(host_, HttpPipelinedHost::CAPABLE);
  pool_->OnHostIdle(host_);
  EXPECT_FALSE(pool_->IsHostEligibleForPipelining(origin_));
}

TEST_F(HttpPipelinedHostPoolTest, RemainsUnknownWithoutFeedback) {
  EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _,
                                       HttpPipelinedHost::UNKNOWN))
      .Times(1)
      .WillOnce(Return(host_));

  CreateDummyStream();
  pool_->OnHostIdle(host_);
  EXPECT_TRUE(pool_->IsHostEligibleForPipelining(origin_));

  host_ = new MockHost(origin_);
  EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _,
                                       HttpPipelinedHost::UNKNOWN))
      .Times(1)
      .WillOnce(Return(host_));

  CreateDummyStream();
  pool_->OnHostIdle(host_);
}

}  // anonymous namespace

}  // namespace net