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
|
// Copyright (c) 2010 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 "chrome/browser/net/connection_tester.h"
#include "net/base/mock_host_resolver.h"
#include "net/url_request/url_request_unittest.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
namespace {
// This is a testing delegate which simply counts how many times each of
// the delegate's methods were invoked.
class ConnectionTesterDelegate : public ConnectionTester::Delegate {
public:
ConnectionTesterDelegate()
: start_connection_test_suite_count_(0),
start_connection_test_experiment_count_(0),
completed_connection_test_experiment_count_(0),
completed_connection_test_suite_count_(0) {
}
virtual void OnStartConnectionTestSuite() {
start_connection_test_suite_count_++;
}
virtual void OnStartConnectionTestExperiment(
const ConnectionTester::Experiment& experiment) {
start_connection_test_experiment_count_++;
}
virtual void OnCompletedConnectionTestExperiment(
const ConnectionTester::Experiment& experiment,
int result) {
completed_connection_test_experiment_count_++;
}
virtual void OnCompletedConnectionTestSuite() {
completed_connection_test_suite_count_++;
MessageLoop::current()->Quit();
}
int start_connection_test_suite_count() const {
return start_connection_test_suite_count_;
}
int start_connection_test_experiment_count() const {
return start_connection_test_experiment_count_;
}
int completed_connection_test_experiment_count() const {
return completed_connection_test_experiment_count_;
}
int completed_connection_test_suite_count() const {
return completed_connection_test_suite_count_;
}
private:
int start_connection_test_suite_count_;
int start_connection_test_experiment_count_;
int completed_connection_test_experiment_count_;
int completed_connection_test_suite_count_;
};
// The test fixture is responsible for:
// - Making sure each test has an IO loop running
// - Catching any host resolve requests and mapping them to localhost
// (so the test doesn't use any external network dependencies).
class ConnectionTesterTest : public PlatformTest {
public:
ConnectionTesterTest() : message_loop_(MessageLoop::TYPE_IO) {
scoped_refptr<net::RuleBasedHostResolverProc> catchall_resolver =
new net::RuleBasedHostResolverProc(NULL);
catchall_resolver->AddRule("*", "127.0.0.1");
scoped_host_resolver_proc_.Init(catchall_resolver);
}
protected:
net::ScopedDefaultHostResolverProc scoped_host_resolver_proc_;
ConnectionTesterDelegate test_delegate_;
MessageLoop message_loop_;
};
TEST_F(ConnectionTesterTest, RunAllTests) {
scoped_refptr<HTTPTestServer> server =
HTTPTestServer::CreateForkingServer(L"net/data/url_request_unittest/");
ConnectionTester tester(&test_delegate_);
// Start the test suite on URL "echoall".
// TODO(eroman): Is this URL right?
GURL url = server->TestServerPage("echoall");
tester.RunAllTests(url);
// Wait for all the tests to complete.
MessageLoop::current()->Run();
const int kNumExperiments =
ConnectionTester::PROXY_EXPERIMENT_COUNT *
ConnectionTester::HOST_RESOLVER_EXPERIMENT_COUNT;
EXPECT_EQ(1, test_delegate_.start_connection_test_suite_count());
EXPECT_EQ(kNumExperiments,
test_delegate_.start_connection_test_experiment_count());
EXPECT_EQ(kNumExperiments,
test_delegate_.completed_connection_test_experiment_count());
EXPECT_EQ(1, test_delegate_.completed_connection_test_suite_count());
}
TEST_F(ConnectionTesterTest, DeleteWhileInProgress) {
scoped_refptr<HTTPTestServer> server =
HTTPTestServer::CreateForkingServer(L"net/data/url_request_unittest/");
scoped_ptr<ConnectionTester> tester(new ConnectionTester(&test_delegate_));
// Start the test suite on URL "echoall".
// TODO(eroman): Is this URL right?
GURL url = server->TestServerPage("echoall");
tester->RunAllTests(url);
MessageLoop::current()->RunAllPending();
EXPECT_EQ(1, test_delegate_.start_connection_test_suite_count());
EXPECT_EQ(1, test_delegate_.start_connection_test_experiment_count());
EXPECT_EQ(0, test_delegate_.completed_connection_test_experiment_count());
EXPECT_EQ(0, test_delegate_.completed_connection_test_suite_count());
// Delete the ConnectionTester while it is in progress.
tester.reset();
// Drain the tasks on the message loop.
//
// Note that we cannot simply stop the message loop, since that will delete
// any pending tasks instead of running them. This causes a problem with
// net::ClientSocketPoolBaseHelper, since the "Group" holds a pointer
// |backup_task| that it will try to deref during the destructor, but
// depending on the order that pending tasks were deleted in, it might
// already be invalid! See http://crbug.com/43291.
MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
MessageLoop::current()->Run();
}
} // namespace
|