Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Side by Side Diff: net/socket/client_socket_pool_base_unittest.cc

Issue 1696005: Add net log entries that summarize transmit and receive byte counts. (Closed)
Patch Set: More tests and address comments Created 10 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/socket/client_socket_pool_base.cc ('k') | net/socket/socket_test_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/socket/client_socket_pool_base.h" 5 #include "net/socket/client_socket_pool_base.h"
6 6
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/platform_thread.h" 10 #include "base/platform_thread.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 43
44 virtual int Write( 44 virtual int Write(
45 IOBuffer* /* buf */, int /* len */, CompletionCallback* /* callback */) { 45 IOBuffer* /* buf */, int /* len */, CompletionCallback* /* callback */) {
46 return ERR_UNEXPECTED; 46 return ERR_UNEXPECTED;
47 } 47 }
48 virtual bool SetReceiveBufferSize(int32 size) { return true; }; 48 virtual bool SetReceiveBufferSize(int32 size) { return true; };
49 virtual bool SetSendBufferSize(int32 size) { return true; }; 49 virtual bool SetSendBufferSize(int32 size) { return true; };
50 50
51 // ClientSocket methods: 51 // ClientSocket methods:
52 52
53 virtual int Connect(CompletionCallback* callback, const BoundNetLog& net_log) { 53 virtual int Connect(CompletionCallback* callback) {
54 connected_ = true; 54 connected_ = true;
55 return OK; 55 return OK;
56 } 56 }
57 57
58 virtual void Disconnect() { connected_ = false; } 58 virtual void Disconnect() { connected_ = false; }
59 virtual bool IsConnected() const { return connected_; } 59 virtual bool IsConnected() const { return connected_; }
60 virtual bool IsConnectedAndIdle() const { return connected_; } 60 virtual bool IsConnectedAndIdle() const { return connected_; }
61 61
62 virtual int GetPeerAddress(AddressList* /* address */) const { 62 virtual int GetPeerAddress(AddressList* /* address */) const {
63 return ERR_UNEXPECTED; 63 return ERR_UNEXPECTED;
64 } 64 }
65 65
66 virtual const BoundNetLog& NetLog() const {
67 return net_log_;
68 }
69
66 private: 70 private:
67 bool connected_; 71 bool connected_;
72 BoundNetLog net_log_;
68 73
69 DISALLOW_COPY_AND_ASSIGN(MockClientSocket); 74 DISALLOW_COPY_AND_ASSIGN(MockClientSocket);
70 }; 75 };
71 76
72 class TestConnectJob; 77 class TestConnectJob;
73 78
74 class MockClientSocketFactory : public ClientSocketFactory { 79 class MockClientSocketFactory : public ClientSocketFactory {
75 public: 80 public:
76 MockClientSocketFactory() : allocation_count_(0) {} 81 MockClientSocketFactory() : allocation_count_(0) {}
77 82
78 virtual ClientSocket* CreateTCPClientSocket(const AddressList& addresses) { 83 virtual ClientSocket* CreateTCPClientSocket(const AddressList& addresses,
84 NetLog* /* net_log */) {
79 allocation_count_++; 85 allocation_count_++;
80 return NULL; 86 return NULL;
81 } 87 }
82 88
83 virtual SSLClientSocket* CreateSSLClientSocket( 89 virtual SSLClientSocket* CreateSSLClientSocket(
84 ClientSocket* transport_socket, 90 ClientSocket* transport_socket,
85 const std::string& hostname, 91 const std::string& hostname,
86 const SSLConfig& ssl_config) { 92 const SSLConfig& ssl_config) {
87 NOTIMPLEMENTED(); 93 NOTIMPLEMENTED();
88 return NULL; 94 return NULL;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 DoConnect(waiting_success_, true /* async */); 132 DoConnect(waiting_success_, true /* async */);
127 } 133 }
128 134
129 virtual LoadState GetLoadState() const { return load_state_; } 135 virtual LoadState GetLoadState() const { return load_state_; }
130 136
131 private: 137 private:
132 // ConnectJob methods: 138 // ConnectJob methods:
133 139
134 virtual int ConnectInternal() { 140 virtual int ConnectInternal() {
135 AddressList ignored; 141 AddressList ignored;
136 client_socket_factory_->CreateTCPClientSocket(ignored); 142 client_socket_factory_->CreateTCPClientSocket(ignored, NULL);
137 set_socket(new MockClientSocket()); 143 set_socket(new MockClientSocket());
138 switch (job_type_) { 144 switch (job_type_) {
139 case kMockJob: 145 case kMockJob:
140 return DoConnect(true /* successful */, false /* sync */); 146 return DoConnect(true /* successful */, false /* sync */);
141 case kMockFailingJob: 147 case kMockFailingJob:
142 return DoConnect(false /* error */, false /* sync */); 148 return DoConnect(false /* error */, false /* sync */);
143 case kMockPendingJob: 149 case kMockPendingJob:
144 set_load_state(LOAD_STATE_CONNECTING); 150 set_load_state(LOAD_STATE_CONNECTING);
145 151
146 // Depending on execution timings, posting a delayed task can result 152 // Depending on execution timings, posting a delayed task can result
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 return ERR_FAILED; 195 return ERR_FAILED;
190 } 196 }
191 } 197 }
192 198
193 void set_load_state(LoadState load_state) { load_state_ = load_state; } 199 void set_load_state(LoadState load_state) { load_state_ = load_state; }
194 200
195 int DoConnect(bool succeed, bool was_async) { 201 int DoConnect(bool succeed, bool was_async) {
196 int result = ERR_CONNECTION_FAILED; 202 int result = ERR_CONNECTION_FAILED;
197 if (succeed) { 203 if (succeed) {
198 result = OK; 204 result = OK;
199 socket()->Connect(NULL, NULL); 205 socket()->Connect(NULL);
200 } else { 206 } else {
201 set_socket(NULL); 207 set_socket(NULL);
202 } 208 }
203 209
204 if (was_async) 210 if (was_async)
205 NotifyDelegateOfCompletion(result); 211 NotifyDelegateOfCompletion(result);
206 return result; 212 return result;
207 } 213 }
208 214
209 void AdvanceLoadState(LoadState state) { 215 void AdvanceLoadState(LoadState state) {
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 TestCompletionCallback callback; 519 TestCompletionCallback callback;
514 ClientSocketHandle handle; 520 ClientSocketHandle handle;
515 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); 521 CapturingBoundNetLog log(CapturingNetLog::kUnbounded);
516 522
517 EXPECT_EQ(OK, InitHandle(&handle, "a", kDefaultPriority, &callback, pool_, 523 EXPECT_EQ(OK, InitHandle(&handle, "a", kDefaultPriority, &callback, pool_,
518 log.bound())); 524 log.bound()));
519 EXPECT_TRUE(handle.is_initialized()); 525 EXPECT_TRUE(handle.is_initialized());
520 EXPECT_TRUE(handle.socket()); 526 EXPECT_TRUE(handle.socket());
521 handle.Reset(); 527 handle.Reset();
522 528
523 EXPECT_EQ(5u, log.entries().size()); 529 EXPECT_EQ(7u, log.entries().size());
524 EXPECT_TRUE(LogContainsBeginEvent( 530 EXPECT_TRUE(LogContainsBeginEvent(
525 log.entries(), 0, NetLog::TYPE_SOCKET_POOL)); 531 log.entries(), 0, NetLog::TYPE_SOCKET_POOL));
526 EXPECT_TRUE(LogContainsBeginEvent( 532 EXPECT_TRUE(LogContainsBeginEvent(
527 log.entries(), 1, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB)); 533 log.entries(), 1, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_ID));
534 EXPECT_TRUE(LogContainsBeginEvent(
535 log.entries(), 2, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB));
528 EXPECT_TRUE(LogContainsEndEvent( 536 EXPECT_TRUE(LogContainsEndEvent(
529 log.entries(), 2, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB)); 537 log.entries(), 3, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB));
538 EXPECT_TRUE(LogContainsEndEvent(
539 log.entries(), 4, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_ID));
530 EXPECT_TRUE(LogContainsEvent( 540 EXPECT_TRUE(LogContainsEvent(
531 log.entries(), 3, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_ID, 541 log.entries(), 5, NetLog::TYPE_SOCKET_POOL_SOCKET_ID,
532 NetLog::PHASE_NONE)); 542 NetLog::PHASE_NONE));
533 EXPECT_TRUE(LogContainsEndEvent( 543 EXPECT_TRUE(LogContainsEndEvent(
534 log.entries(), 4, NetLog::TYPE_SOCKET_POOL)); 544 log.entries(), 6, NetLog::TYPE_SOCKET_POOL));
535 } 545 }
536 546
537 TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) { 547 TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) {
538 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); 548 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
539 549
540 connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); 550 connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob);
541 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); 551 CapturingBoundNetLog log(CapturingNetLog::kUnbounded);
542 552
543 TestSocketRequest req(&request_order_, &completion_count_); 553 TestSocketRequest req(&request_order_, &completion_count_);
544 EXPECT_EQ(ERR_CONNECTION_FAILED, 554 EXPECT_EQ(ERR_CONNECTION_FAILED,
545 InitHandle(req.handle(), "a", kDefaultPriority, &req, pool_, 555 InitHandle(req.handle(), "a", kDefaultPriority, &req, pool_,
546 log.bound())); 556 log.bound()));
547 557
548 EXPECT_EQ(5u, log.entries().size()); 558 EXPECT_EQ(6u, log.entries().size());
549 EXPECT_TRUE(LogContainsBeginEvent(log.entries(), 0, NetLog::TYPE_SOCKET_POOL)) ; 559 EXPECT_TRUE(LogContainsBeginEvent(log.entries(), 0, NetLog::TYPE_SOCKET_POOL)) ;
550 EXPECT_TRUE(LogContainsBeginEvent( 560 EXPECT_TRUE(LogContainsBeginEvent(
551 log.entries(), 1, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB)); 561 log.entries(), 2, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB));
552 EXPECT_TRUE(LogContainsEndEvent( 562 EXPECT_TRUE(LogContainsEndEvent(
553 log.entries(), 2, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB)); 563 log.entries(), 3, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB));
554 EXPECT_TRUE(LogContainsEndEvent(log.entries(), 4, NetLog::TYPE_SOCKET_POOL)); 564 EXPECT_TRUE(LogContainsEndEvent(log.entries(), 5, NetLog::TYPE_SOCKET_POOL));
555 } 565 }
556 566
557 TEST_F(ClientSocketPoolBaseTest, TotalLimit) { 567 TEST_F(ClientSocketPoolBaseTest, TotalLimit) {
558 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); 568 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
559 569
560 // TODO(eroman): Check that the NetLog contains this event. 570 // TODO(eroman): Check that the NetLog contains this event.
561 571
562 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); 572 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
563 EXPECT_EQ(OK, StartRequest("b", kDefaultPriority)); 573 EXPECT_EQ(OK, StartRequest("b", kDefaultPriority));
564 EXPECT_EQ(OK, StartRequest("c", kDefaultPriority)); 574 EXPECT_EQ(OK, StartRequest("c", kDefaultPriority));
(...skipping 586 matching lines...) Expand 10 before | Expand all | Expand 10 after
1151 TestSocketRequest req(&request_order_, &completion_count_); 1161 TestSocketRequest req(&request_order_, &completion_count_);
1152 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); 1162 CapturingBoundNetLog log(CapturingNetLog::kUnbounded);
1153 int rv = InitHandle(req.handle(), "a", LOWEST, &req, pool_, log.bound()); 1163 int rv = InitHandle(req.handle(), "a", LOWEST, &req, pool_, log.bound());
1154 EXPECT_EQ(ERR_IO_PENDING, rv); 1164 EXPECT_EQ(ERR_IO_PENDING, rv);
1155 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", req.handle())); 1165 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", req.handle()));
1156 EXPECT_EQ(OK, req.WaitForResult()); 1166 EXPECT_EQ(OK, req.WaitForResult());
1157 EXPECT_TRUE(req.handle()->is_initialized()); 1167 EXPECT_TRUE(req.handle()->is_initialized());
1158 EXPECT_TRUE(req.handle()->socket()); 1168 EXPECT_TRUE(req.handle()->socket());
1159 req.handle()->Reset(); 1169 req.handle()->Reset();
1160 1170
1161 EXPECT_EQ(5u, log.entries().size()); 1171 EXPECT_EQ(7u, log.entries().size());
1162 EXPECT_TRUE(LogContainsBeginEvent( 1172 EXPECT_TRUE(LogContainsBeginEvent(
1163 log.entries(), 0, NetLog::TYPE_SOCKET_POOL)); 1173 log.entries(), 0, NetLog::TYPE_SOCKET_POOL));
1164 EXPECT_TRUE(LogContainsBeginEvent( 1174 EXPECT_TRUE(LogContainsBeginEvent(
1165 log.entries(), 1, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB));
1166 EXPECT_TRUE(LogContainsEndEvent(
1167 log.entries(), 2, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB)); 1175 log.entries(), 2, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB));
1168 EXPECT_TRUE(LogContainsEndEvent( 1176 EXPECT_TRUE(LogContainsEndEvent(
1169 log.entries(), 4, NetLog::TYPE_SOCKET_POOL)); 1177 log.entries(), 3, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB));
1178 EXPECT_TRUE(LogContainsEndEvent(
1179 log.entries(), 5, NetLog::TYPE_SOCKET_POOL));
1170 } 1180 }
1171 1181
1172 TEST_F(ClientSocketPoolBaseTest, 1182 TEST_F(ClientSocketPoolBaseTest,
1173 InitConnectionAsynchronousFailure) { 1183 InitConnectionAsynchronousFailure) {
1174 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); 1184 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1175 1185
1176 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); 1186 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob);
1177 TestSocketRequest req(&request_order_, &completion_count_); 1187 TestSocketRequest req(&request_order_, &completion_count_);
1178 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); 1188 CapturingBoundNetLog log(CapturingNetLog::kUnbounded);
1179 EXPECT_EQ(ERR_IO_PENDING, 1189 EXPECT_EQ(ERR_IO_PENDING,
1180 InitHandle(req.handle(), "a", kDefaultPriority, &req, pool_, 1190 InitHandle(req.handle(), "a", kDefaultPriority, &req, pool_,
1181 log.bound())); 1191 log.bound()));
1182 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", req.handle())); 1192 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", req.handle()));
1183 EXPECT_EQ(ERR_CONNECTION_FAILED, req.WaitForResult()); 1193 EXPECT_EQ(ERR_CONNECTION_FAILED, req.WaitForResult());
1184 1194
1185 EXPECT_EQ(5u, log.entries().size()); 1195 EXPECT_EQ(6u, log.entries().size());
1186 EXPECT_TRUE(LogContainsBeginEvent( 1196 EXPECT_TRUE(LogContainsBeginEvent(
1187 log.entries(), 0, NetLog::TYPE_SOCKET_POOL)); 1197 log.entries(), 0, NetLog::TYPE_SOCKET_POOL));
1188 EXPECT_TRUE(LogContainsBeginEvent( 1198 EXPECT_TRUE(LogContainsBeginEvent(
1189 log.entries(), 1, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB));
1190 EXPECT_TRUE(LogContainsEndEvent(
1191 log.entries(), 2, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB)); 1199 log.entries(), 2, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB));
1192 EXPECT_TRUE(LogContainsEndEvent( 1200 EXPECT_TRUE(LogContainsEndEvent(
1193 log.entries(), 4, NetLog::TYPE_SOCKET_POOL)); 1201 log.entries(), 3, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB));
1202 EXPECT_TRUE(LogContainsEndEvent(
1203 log.entries(), 5, NetLog::TYPE_SOCKET_POOL));
1194 } 1204 }
1195 1205
1196 TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) { 1206 TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) {
1197 // TODO(eroman): Add back the log expectations! Removed them because the 1207 // TODO(eroman): Add back the log expectations! Removed them because the
1198 // ordering is difficult, and some may fire during destructor. 1208 // ordering is difficult, and some may fire during destructor.
1199 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); 1209 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1200 1210
1201 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); 1211 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1202 TestSocketRequest req(&request_order_, &completion_count_); 1212 TestSocketRequest req(&request_order_, &completion_count_);
1203 TestSocketRequest req2(&request_order_, &completion_count_); 1213 TestSocketRequest req2(&request_order_, &completion_count_);
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
1509 TestReleasingSocketRequest request(pool_.get()); 1519 TestReleasingSocketRequest request(pool_.get());
1510 EXPECT_EQ(ERR_IO_PENDING, InitHandle(request.handle(), "a", kDefaultPriority, 1520 EXPECT_EQ(ERR_IO_PENDING, InitHandle(request.handle(), "a", kDefaultPriority,
1511 &request, pool_, NULL)); 1521 &request, pool_, NULL));
1512 1522
1513 EXPECT_EQ(OK, request.WaitForResult()); 1523 EXPECT_EQ(OK, request.WaitForResult());
1514 } 1524 }
1515 1525
1516 } // namespace 1526 } // namespace
1517 1527
1518 } // namespace net 1528 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/client_socket_pool_base.cc ('k') | net/socket/socket_test_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698