summaryrefslogtreecommitdiff
path: root/chromium/net/http/http_pipelined_host_pool_unittest.cc
blob: fa8d93facb17848cff0603d3c85d3efde28c127f (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright (c) 2012 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 "base/rand_util.h"
#include "net/http/http_pipelined_host.h"
#include "net/http/http_pipelined_host_capability.h"
#include "net/http/http_server_properties_impl.h"
#include "net/proxy/proxy_info.h"
#include "net/ssl/ssl_config_service.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(HttpPipelinedHost* host));
};

class MockHostFactory : public HttpPipelinedHost::Factory {
 public:
  MOCK_METHOD5(CreateNewHost, HttpPipelinedHost*(
      HttpPipelinedHost::Delegate* delegate,
      const HttpPipelinedHost::Key& key,
      HttpPipelinedConnection::Factory* factory,
      HttpPipelinedHostCapability capability,
      bool force_pipelining));
};

class MockHost : public HttpPipelinedHost {
 public:
  MockHost(const Key& key)
      : key_(key) {
  }

  MOCK_METHOD6(CreateStreamOnNewPipeline, HttpPipelinedStream*(
      ClientSocketHandle* connection,
      const SSLConfig& used_ssl_config,
      const ProxyInfo& used_proxy_info,
      const BoundNetLog& net_log,
      bool was_npn_negotiated,
      NextProto protocol_negotiated));
  MOCK_METHOD0(CreateStreamOnExistingPipeline, HttpPipelinedStream*());
  MOCK_CONST_METHOD0(IsExistingPipelineAvailable, bool());
  MOCK_CONST_METHOD0(PipelineInfoToValue, base::Value*());

  virtual const Key& GetKey() const OVERRIDE { return key_; }

 private:
  Key key_;
};

class HttpPipelinedHostPoolTest : public testing::Test {
 public:
  HttpPipelinedHostPoolTest()
      : key_(HostPortPair("host", 123)),
        factory_(new MockHostFactory),  // Owned by pool_.
        host_(new MockHost(key_)),  // Owned by pool_.
        http_server_properties_(new HttpServerPropertiesImpl()),
        pool_(new HttpPipelinedHostPool(
            &delegate_, factory_,
            http_server_properties_->GetWeakPtr(), false)),
        was_npn_negotiated_(false),
        protocol_negotiated_(kProtoUnknown) {
  }

  void CreateDummyStream(const HttpPipelinedHost::Key& key,
                         ClientSocketHandle* connection,
                         HttpPipelinedStream* stream,
                         MockHost* host) {
    EXPECT_CALL(*host, CreateStreamOnNewPipeline(connection,
                                                 Ref(ssl_config_),
                                                 Ref(proxy_info_),
                                                 Ref(net_log_),
                                                 was_npn_negotiated_,
                                                 protocol_negotiated_))
        .Times(1)
        .WillOnce(Return(stream));
    EXPECT_EQ(stream,
              pool_->CreateStreamOnNewPipeline(key, connection,
                                               ssl_config_, proxy_info_,
                                               net_log_, was_npn_negotiated_,
                                               protocol_negotiated_));
  }

  MockHost* CreateDummyHost(const HttpPipelinedHost::Key& key) {
    MockHost* mock_host = new MockHost(key);
    EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(key), _,
                                         PIPELINE_UNKNOWN, false))
        .Times(1)
        .WillOnce(Return(mock_host));
    ClientSocketHandle* dummy_connection =
        reinterpret_cast<ClientSocketHandle*>(base::RandUint64());
    HttpPipelinedStream* dummy_stream =
        reinterpret_cast<HttpPipelinedStream*>(base::RandUint64());
    CreateDummyStream(key, dummy_connection, dummy_stream, mock_host);
    return mock_host;
  }

  HttpPipelinedHost::Key key_;
  MockPoolDelegate delegate_;
  MockHostFactory* factory_;
  MockHost* host_;
  scoped_ptr<HttpServerPropertiesImpl> http_server_properties_;
  scoped_ptr<HttpPipelinedHostPool> pool_;

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

TEST_F(HttpPipelinedHostPoolTest, DefaultUnknown) {
  EXPECT_TRUE(pool_->IsKeyEligibleForPipelining(key_));
  EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(key_), _,
                                       PIPELINE_UNKNOWN, false))
      .Times(1)
      .WillOnce(Return(host_));

  CreateDummyStream(key_, kDummyConnection, kDummyStream, host_);
  pool_->OnHostIdle(host_);
}

TEST_F(HttpPipelinedHostPoolTest, RemembersIncapable) {
  EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(key_), _,
                                       PIPELINE_UNKNOWN, false))
      .Times(1)
      .WillOnce(Return(host_));

  CreateDummyStream(key_, kDummyConnection, kDummyStream, host_);
  pool_->OnHostDeterminedCapability(host_, PIPELINE_INCAPABLE);
  pool_->OnHostIdle(host_);
  EXPECT_FALSE(pool_->IsKeyEligibleForPipelining(key_));
  EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(key_), _,
                                       PIPELINE_INCAPABLE, false))
      .Times(0);
  EXPECT_EQ(NULL,
            pool_->CreateStreamOnNewPipeline(key_, kDummyConnection,
                                             ssl_config_, proxy_info_, net_log_,
                                             was_npn_negotiated_,
                                             protocol_negotiated_));
}

TEST_F(HttpPipelinedHostPoolTest, RemembersCapable) {
  EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(key_), _,
                                       PIPELINE_UNKNOWN, false))
      .Times(1)
      .WillOnce(Return(host_));

  CreateDummyStream(key_, kDummyConnection, kDummyStream, host_);
  pool_->OnHostDeterminedCapability(host_, PIPELINE_CAPABLE);
  pool_->OnHostIdle(host_);
  EXPECT_TRUE(pool_->IsKeyEligibleForPipelining(key_));

  host_ = new MockHost(key_);
  EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(key_), _,
                                       PIPELINE_CAPABLE, false))
      .Times(1)
      .WillOnce(Return(host_));
  CreateDummyStream(key_, kDummyConnection, kDummyStream, host_);
  pool_->OnHostIdle(host_);
}

TEST_F(HttpPipelinedHostPoolTest, IncapableIsSticky) {
  EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(key_), _,
                                       PIPELINE_UNKNOWN, false))
      .Times(1)
      .WillOnce(Return(host_));

  CreateDummyStream(key_, kDummyConnection, kDummyStream, host_);
  pool_->OnHostDeterminedCapability(host_, PIPELINE_CAPABLE);
  pool_->OnHostDeterminedCapability(host_, PIPELINE_INCAPABLE);
  pool_->OnHostDeterminedCapability(host_, PIPELINE_CAPABLE);
  pool_->OnHostIdle(host_);
  EXPECT_FALSE(pool_->IsKeyEligibleForPipelining(key_));
}

TEST_F(HttpPipelinedHostPoolTest, RemainsUnknownWithoutFeedback) {
  EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(key_), _,
                                       PIPELINE_UNKNOWN, false))
      .Times(1)
      .WillOnce(Return(host_));

  CreateDummyStream(key_, kDummyConnection, kDummyStream, host_);
  pool_->OnHostIdle(host_);
  EXPECT_TRUE(pool_->IsKeyEligibleForPipelining(key_));

  host_ = new MockHost(key_);
  EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(key_), _,
                                       PIPELINE_UNKNOWN, false))
      .Times(1)
      .WillOnce(Return(host_));

  CreateDummyStream(key_, kDummyConnection, kDummyStream, host_);
  pool_->OnHostIdle(host_);
}

TEST_F(HttpPipelinedHostPoolTest, PopulatesServerProperties) {
  EXPECT_EQ(PIPELINE_UNKNOWN,
            http_server_properties_->GetPipelineCapability(
                host_->GetKey().origin()));
  pool_->OnHostDeterminedCapability(host_, PIPELINE_CAPABLE);
  EXPECT_EQ(PIPELINE_CAPABLE,
            http_server_properties_->GetPipelineCapability(
                host_->GetKey().origin()));
  delete host_;  // Must manually delete, because it's never added to |pool_|.
}

TEST_F(HttpPipelinedHostPoolTest, MultipleKeys) {
  HttpPipelinedHost::Key key1(HostPortPair("host", 123));
  HttpPipelinedHost::Key key2(HostPortPair("host", 456));
  HttpPipelinedHost::Key key3(HostPortPair("other", 456));
  HttpPipelinedHost::Key key4(HostPortPair("other", 789));
  MockHost* host1 = CreateDummyHost(key1);
  MockHost* host2 = CreateDummyHost(key2);
  MockHost* host3 = CreateDummyHost(key3);

  EXPECT_CALL(*host1, IsExistingPipelineAvailable())
      .Times(1)
      .WillOnce(Return(true));
  EXPECT_TRUE(pool_->IsExistingPipelineAvailableForKey(key1));

  EXPECT_CALL(*host2, IsExistingPipelineAvailable())
      .Times(1)
      .WillOnce(Return(false));
  EXPECT_FALSE(pool_->IsExistingPipelineAvailableForKey(key2));

  EXPECT_CALL(*host3, IsExistingPipelineAvailable())
      .Times(1)
      .WillOnce(Return(true));
  EXPECT_TRUE(pool_->IsExistingPipelineAvailableForKey(key3));

  EXPECT_FALSE(pool_->IsExistingPipelineAvailableForKey(key4));

  pool_->OnHostIdle(host1);
  pool_->OnHostIdle(host2);
  pool_->OnHostIdle(host3);

  delete host_;  // Must manually delete, because it's never added to |pool_|.
}

}  // anonymous namespace

}  // namespace net