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
|
// Copyright (c) 2006-2008 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 "base/message_loop.h"
#include "net/base/client_socket.h"
#include "net/base/client_socket_handle.h"
#include "net/base/client_socket_pool.h"
#include "net/base/net_errors.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
typedef testing::Test ClientSocketPoolTest;
const int kMaxSocketsPerGroup = 6;
class MockClientSocket : public net::ClientSocket {
public:
MockClientSocket() : connected_(false) {
allocation_count++;
}
// ClientSocket methods:
virtual int Connect(net::CompletionCallback* callback) {
connected_ = true;
return net::OK;
}
virtual int ReconnectIgnoringLastError(net::CompletionCallback* callback) {
return net::ERR_FAILED;
}
virtual void Disconnect() {
connected_ = false;
}
virtual bool IsConnected() const {
return connected_;
}
virtual bool IsConnectedAndIdle() const {
return connected_;
}
// Socket methods:
virtual int Read(char* buf, int buf_len,
net::CompletionCallback* callback) {
return net::ERR_FAILED;
}
virtual int Write(const char* buf, int buf_len,
net::CompletionCallback* callback) {
return net::ERR_FAILED;
}
static int allocation_count;
private:
bool connected_;
};
int MockClientSocket::allocation_count = 0;
class TestSocketRequest : public CallbackRunner< Tuple1<int> > {
public:
explicit TestSocketRequest(net::ClientSocketPool* pool) : handle(pool) {}
net::ClientSocketHandle handle;
void EnsureSocket() {
DCHECK(handle.is_initialized());
if (!handle.socket()) {
handle.set_socket(new MockClientSocket());
handle.socket()->Connect(NULL);
}
}
virtual void RunWithParams(const Tuple1<int>& params) {
DCHECK(params.a == net::OK);
completion_count++;
EnsureSocket();
}
static int completion_count;
};
int TestSocketRequest::completion_count = 0;
} // namespace
TEST(ClientSocketPoolTest, Basic) {
scoped_refptr<net::ClientSocketPool> pool =
new net::ClientSocketPool(kMaxSocketsPerGroup);
TestSocketRequest r(pool);
int rv;
rv = r.handle.Init("a", &r);
EXPECT_EQ(net::OK, rv);
EXPECT_TRUE(r.handle.is_initialized());
r.handle.Reset();
// The handle's Reset method may have posted a task.
MessageLoop::current()->RunAllPending();
}
TEST(ClientSocketPoolTest, WithIdleConnection) {
scoped_refptr<net::ClientSocketPool> pool =
new net::ClientSocketPool(kMaxSocketsPerGroup);
TestSocketRequest r(pool);
int rv;
rv = r.handle.Init("a", &r);
EXPECT_EQ(net::OK, rv);
EXPECT_TRUE(r.handle.is_initialized());
// Create a socket.
r.EnsureSocket();
// Release the socket. It should find its way into the idle list. We're
// testing that this does not trigger a crash.
r.handle.Reset();
// The handle's Reset method may have posted a task.
MessageLoop::current()->RunAllPending();
}
TEST(ClientSocketPoolTest, PendingRequests) {
scoped_refptr<net::ClientSocketPool> pool =
new net::ClientSocketPool(kMaxSocketsPerGroup);
int rv;
scoped_ptr<TestSocketRequest> reqs[kMaxSocketsPerGroup + 10];
for (size_t i = 0; i < arraysize(reqs); ++i)
reqs[i].reset(new TestSocketRequest(pool));
// Reset
MockClientSocket::allocation_count = 0;
TestSocketRequest::completion_count = 0;
// Create connections or queue up requests.
for (size_t i = 0; i < arraysize(reqs); ++i) {
rv = reqs[i]->handle.Init("a", reqs[i].get());
if (rv != net::ERR_IO_PENDING) {
EXPECT_EQ(net::OK, rv);
reqs[i]->EnsureSocket();
}
}
// Release any connections until we have no connections.
bool released_one;
do {
released_one = false;
for (size_t i = 0; i < arraysize(reqs); ++i) {
if (reqs[i]->handle.is_initialized()) {
reqs[i]->handle.Reset();
MessageLoop::current()->RunAllPending();
released_one = true;
}
}
} while (released_one);
EXPECT_EQ(kMaxSocketsPerGroup, MockClientSocket::allocation_count);
EXPECT_EQ(10, TestSocketRequest::completion_count);
}
TEST(ClientSocketPoolTest, PendingRequests_NoKeepAlive) {
scoped_refptr<net::ClientSocketPool> pool =
new net::ClientSocketPool(kMaxSocketsPerGroup);
int rv;
scoped_ptr<TestSocketRequest> reqs[kMaxSocketsPerGroup + 10];
for (size_t i = 0; i < arraysize(reqs); ++i)
reqs[i].reset(new TestSocketRequest(pool));
// Reset
MockClientSocket::allocation_count = 0;
TestSocketRequest::completion_count = 0;
// Create connections or queue up requests.
for (size_t i = 0; i < arraysize(reqs); ++i) {
rv = reqs[i]->handle.Init("a", reqs[i].get());
if (rv != net::ERR_IO_PENDING) {
EXPECT_EQ(net::OK, rv);
reqs[i]->EnsureSocket();
}
}
// Release any connections until we have no connections.
bool released_one;
do {
released_one = false;
for (size_t i = 0; i < arraysize(reqs); ++i) {
if (reqs[i]->handle.is_initialized()) {
reqs[i]->handle.socket()->Disconnect();
reqs[i]->handle.Reset();
MessageLoop::current()->RunAllPending();
released_one = true;
}
}
} while (released_one);
EXPECT_EQ(kMaxSocketsPerGroup + 10, MockClientSocket::allocation_count);
EXPECT_EQ(10, TestSocketRequest::completion_count);
}
TEST(ClientSocketPoolTest, CancelRequest) {
scoped_refptr<net::ClientSocketPool> pool =
new net::ClientSocketPool(kMaxSocketsPerGroup);
int rv;
scoped_ptr<TestSocketRequest> reqs[kMaxSocketsPerGroup + 10];
for (size_t i = 0; i < arraysize(reqs); ++i)
reqs[i].reset(new TestSocketRequest(pool));
// Reset
MockClientSocket::allocation_count = 0;
TestSocketRequest::completion_count = 0;
// Create connections or queue up requests.
for (size_t i = 0; i < arraysize(reqs); ++i) {
rv = reqs[i]->handle.Init("a", reqs[i].get());
if (rv != net::ERR_IO_PENDING) {
EXPECT_EQ(net::OK, rv);
reqs[i]->EnsureSocket();
}
}
// Cancel a request.
size_t index_to_cancel = kMaxSocketsPerGroup + 2;
EXPECT_TRUE(!reqs[index_to_cancel]->handle.is_initialized());
reqs[index_to_cancel]->handle.Reset();
// Release any connections until we have no connections.
bool released_one;
do {
released_one = false;
for (size_t i = 0; i < arraysize(reqs); ++i) {
if (reqs[i]->handle.is_initialized()) {
reqs[i]->handle.Reset();
MessageLoop::current()->RunAllPending();
released_one = true;
}
}
} while (released_one);
EXPECT_EQ(kMaxSocketsPerGroup, MockClientSocket::allocation_count);
EXPECT_EQ(9, TestSocketRequest::completion_count);
}
|