summaryrefslogtreecommitdiffstats
path: root/net/socket/client_socket_pool_base_unittest.cc
diff options
context:
space:
mode:
Diffstat (limited to 'net/socket/client_socket_pool_base_unittest.cc')
-rw-r--r--net/socket/client_socket_pool_base_unittest.cc114
1 files changed, 107 insertions, 7 deletions
diff --git a/net/socket/client_socket_pool_base_unittest.cc b/net/socket/client_socket_pool_base_unittest.cc
index dcae067b..6ccc2e4 100644
--- a/net/socket/client_socket_pool_base_unittest.cc
+++ b/net/socket/client_socket_pool_base_unittest.cc
@@ -6,6 +6,7 @@
#include "base/compiler_specific.h"
#include "base/message_loop.h"
+#include "base/platform_thread.h"
#include "base/scoped_vector.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
@@ -107,16 +108,22 @@ class TestConnectJob : public ConnectJob {
TestConnectJob(JobType job_type,
const std::string& group_name,
const ClientSocketPoolBase::Request& request,
+ base::TimeDelta timeout_duration,
ConnectJob::Delegate* delegate,
MockClientSocketFactory* client_socket_factory)
- : ConnectJob(group_name, request.handle, delegate),
+ : ConnectJob(group_name, request.handle, timeout_duration, delegate),
job_type_(job_type),
client_socket_factory_(client_socket_factory),
method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {}
+ void Signal() {
+ DoConnect(waiting_success_, true /* async */);
+ }
+
+ private:
// ConnectJob methods:
- virtual int Connect() {
+ virtual int ConnectInternal() {
AddressList ignored;
client_socket_factory_->CreateTCPClientSocket(ignored);
switch (job_type_) {
@@ -158,11 +165,6 @@ class TestConnectJob : public ConnectJob {
}
}
- void Signal() {
- DoConnect(waiting_success_, true /* async */);
- }
-
- private:
int DoConnect(bool succeed, bool was_async) {
int result = ERR_CONNECTION_FAILED;
if (succeed) {
@@ -207,6 +209,10 @@ class TestConnectJobFactory : public ClientSocketPoolBase::ConnectJobFactory {
void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; }
+ void set_timeout_duration(base::TimeDelta timeout_duration) {
+ timeout_duration_ = timeout_duration;
+ }
+
// ConnectJobFactory methods:
virtual ConnectJob* NewConnectJob(
@@ -216,12 +222,14 @@ class TestConnectJobFactory : public ClientSocketPoolBase::ConnectJobFactory {
return new TestConnectJob(job_type_,
group_name,
request,
+ timeout_duration_,
delegate,
client_socket_factory_);
}
private:
TestConnectJob::JobType job_type_;
+ base::TimeDelta timeout_duration_;
MockClientSocketFactory* const client_socket_factory_;
DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory);
@@ -275,6 +283,10 @@ class TestClientSocketPool : public ClientSocketPool {
const ClientSocketPoolBase* base() const { return base_.get(); }
+ int NumConnectJobsInGroup(const std::string& group_name) const {
+ return base_->NumConnectJobsInGroup(group_name);
+ }
+
private:
const scoped_refptr<ClientSocketPoolBase> base_;
@@ -289,6 +301,37 @@ void MockClientSocketFactory::SignalJobs() {
waiting_jobs_.clear();
}
+class TestConnectJobDelegate : public ConnectJob::Delegate {
+ public:
+ TestConnectJobDelegate()
+ : have_result_(false), waiting_for_result_(false), result_(OK) {}
+ virtual ~TestConnectJobDelegate() {}
+
+ virtual void OnConnectJobComplete(int result, ConnectJob* job) {
+ result_ = result;
+ delete job;
+ have_result_ = true;
+ if (waiting_for_result_)
+ MessageLoop::current()->Quit();
+ }
+
+ int WaitForResult() {
+ DCHECK(!waiting_for_result_);
+ while (!have_result_) {
+ waiting_for_result_ = true;
+ MessageLoop::current()->Run();
+ waiting_for_result_ = false;
+ }
+ have_result_ = false; // auto-reset for next callback
+ return result_;
+ }
+
+ private:
+ bool have_result_;
+ bool waiting_for_result_;
+ int result_;
+};
+
class ClientSocketPoolBaseTest : public ClientSocketPoolTest {
protected:
ClientSocketPoolBaseTest()
@@ -324,6 +367,41 @@ class ClientSocketPoolBaseTest : public ClientSocketPoolTest {
scoped_refptr<TestClientSocketPool> pool_;
};
+// Even though a timeout is specified, it doesn't time out on a synchronous
+// completion.
+TEST_F(ClientSocketPoolBaseTest, ConnectJob_NoTimeoutOnSynchronousCompletion) {
+ TestConnectJobDelegate delegate;
+ ClientSocketPoolBase::Request request;
+ ClientSocketHandle ignored(pool_.get());
+ request.handle = &ignored;
+ scoped_ptr<TestConnectJob> job(
+ new TestConnectJob(TestConnectJob::kMockJob,
+ "a",
+ request,
+ base::TimeDelta::FromMicroseconds(1),
+ &delegate,
+ &client_socket_factory_));
+ EXPECT_EQ(OK, job->Connect());
+}
+
+TEST_F(ClientSocketPoolBaseTest, ConnectJob_TimedOut) {
+ TestConnectJobDelegate delegate;
+ ClientSocketPoolBase::Request request;
+ ClientSocketHandle ignored(pool_.get());
+ request.handle = &ignored;
+ // Deleted by TestConnectJobDelegate.
+ TestConnectJob* job =
+ new TestConnectJob(TestConnectJob::kMockPendingJob,
+ "a",
+ request,
+ base::TimeDelta::FromMicroseconds(1),
+ &delegate,
+ &client_socket_factory_);
+ ASSERT_EQ(ERR_IO_PENDING, job->Connect());
+ PlatformThread::Sleep(1);
+ EXPECT_EQ(ERR_TIMED_OUT, delegate.WaitForResult());
+}
+
TEST_F(ClientSocketPoolBaseTest, BasicSynchronous) {
CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
@@ -1210,6 +1288,28 @@ TEST_F(ClientSocketPoolBaseTest_LateBinding, CancelRequest) {
EXPECT_EQ(kIndexOutOfBounds, GetOrderOfRequest(8));
}
+TEST_F(ClientSocketPoolBaseTest_LateBinding, CancelRequestLimitsJobs) {
+ connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
+
+ CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
+
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", 1));
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", 2));
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", 3));
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", 4));
+
+ EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->NumConnectJobsInGroup("a"));
+ requests_[2]->handle()->Reset();
+ requests_[3]->handle()->Reset();
+ EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->NumConnectJobsInGroup("a"));
+
+ requests_[1]->handle()->Reset();
+ EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->NumConnectJobsInGroup("a"));
+
+ requests_[0]->handle()->Reset();
+ EXPECT_EQ(kDefaultMaxSocketsPerGroup - 1, pool_->NumConnectJobsInGroup("a"));
+}
+
TEST_F(ClientSocketPoolBaseTest_LateBinding, RequestPendingJobTwice) {
CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);