Index: net/proxy/dhcp_proxy_script_fetcher_win_unittest.cc |
diff --git a/net/proxy/dhcp_proxy_script_fetcher_win_unittest.cc b/net/proxy/dhcp_proxy_script_fetcher_win_unittest.cc |
index e22915b0a6591baa6907b15b060e7637a23eb94c..09ffb66b13c72e80f69ddac7503c515a1d92d7ab 100644 |
--- a/net/proxy/dhcp_proxy_script_fetcher_win_unittest.cc |
+++ b/net/proxy/dhcp_proxy_script_fetcher_win_unittest.cc |
@@ -263,8 +263,37 @@ class DummyDhcpProxyScriptAdapterFetcher |
class MockDhcpProxyScriptFetcherWin : public DhcpProxyScriptFetcherWin { |
public: |
+ class MockWorkerThread : public WorkerThread { |
+ public: |
+ MockWorkerThread() : worker_finished_event_(true, false) { |
+ } |
+ |
+ virtual ~MockWorkerThread() { |
+ } |
+ |
+ void Init(const base::WeakPtr<DhcpProxyScriptFetcherWin>& owner) { |
+ WorkerThread::Init(owner); |
+ } |
+ |
+ virtual bool ImplGetCandidateAdapterNames( |
+ std::set<std::string>* adapter_names) OVERRIDE { |
+ adapter_names->insert( |
+ mock_adapter_names_.begin(), mock_adapter_names_.end()); |
+ return true; |
+ } |
+ |
+ virtual void OnThreadDone() OVERRIDE { |
+ WorkerThread::OnThreadDone(); |
+ worker_finished_event_.Signal(); |
+ } |
+ |
+ std::vector<std::string> mock_adapter_names_; |
+ base::WaitableEvent worker_finished_event_; |
+ }; |
+ |
MockDhcpProxyScriptFetcherWin() |
- : DhcpProxyScriptFetcherWin(new TestURLRequestContext()) { |
+ : DhcpProxyScriptFetcherWin(new TestURLRequestContext()), |
+ num_fetchers_created_(0) { |
ResetTestState(); |
} |
@@ -273,7 +302,7 @@ class MockDhcpProxyScriptFetcherWin : public DhcpProxyScriptFetcherWin { |
// returned by ImplGetCandidateAdapterNames. |
void PushBackAdapter(const std::string& adapter_name, |
DhcpProxyScriptAdapterFetcher* fetcher) { |
- adapter_names_.push_back(adapter_name); |
+ worker_thread_->mock_adapter_names_.push_back(adapter_name); |
adapter_fetchers_.push_back(fetcher); |
} |
@@ -289,13 +318,15 @@ class MockDhcpProxyScriptFetcherWin : public DhcpProxyScriptFetcherWin { |
} |
DhcpProxyScriptAdapterFetcher* ImplCreateAdapterFetcher() OVERRIDE { |
+ ++num_fetchers_created_; |
return adapter_fetchers_[next_adapter_fetcher_index_++]; |
} |
- bool ImplGetCandidateAdapterNames( |
- std::set<std::string>* adapter_names) OVERRIDE { |
- adapter_names->insert(adapter_names_.begin(), adapter_names_.end()); |
- return true; |
+ virtual WorkerThread* ImplCreateWorkerThread( |
+ const base::WeakPtr<DhcpProxyScriptFetcherWin>& owner) OVERRIDE { |
+ DCHECK(worker_thread_); |
+ worker_thread_->Init(owner); |
+ return worker_thread_.get(); |
} |
int ImplGetMaxWaitMs() OVERRIDE { |
@@ -304,9 +335,9 @@ class MockDhcpProxyScriptFetcherWin : public DhcpProxyScriptFetcherWin { |
void ResetTestState() { |
next_adapter_fetcher_index_ = 0; |
+ num_fetchers_created_ = 0; |
adapter_fetchers_.clear(); |
- // String pointers contained herein will have been freed during test. |
- adapter_names_.clear(); |
+ worker_thread_ = new MockWorkerThread(); |
max_wait_ms_ = TestTimeouts::tiny_timeout_ms(); |
} |
@@ -320,9 +351,10 @@ class MockDhcpProxyScriptFetcherWin : public DhcpProxyScriptFetcherWin { |
// class via ImplCreateAdapterFetcher. |
std::vector<DhcpProxyScriptAdapterFetcher*> adapter_fetchers_; |
- std::vector<std::string> adapter_names_; |
+ scoped_refptr<MockWorkerThread> worker_thread_; |
int max_wait_ms_; |
+ int num_fetchers_created_; |
}; |
class FetcherClient { |
@@ -339,11 +371,6 @@ public: |
ASSERT_EQ(ERR_IO_PENDING, result); |
} |
- void RunImmediateReturnTest() { |
- int result = fetcher_.Fetch(&pac_text_, &completion_callback_); |
- ASSERT_EQ(ERR_PAC_NOT_IN_DHCP, result); |
- } |
- |
void RunMessageLoopUntilComplete() { |
while (!finished_) { |
MessageLoop::current()->RunAllPending(); |
@@ -351,6 +378,14 @@ public: |
MessageLoop::current()->RunAllPending(); |
} |
+ void RunMessageLoopUntilWorkerDone() { |
+ DCHECK(fetcher_.worker_thread_.get()); |
+ while (!fetcher_.worker_thread_->worker_finished_event_.TimedWait( |
+ base::TimeDelta::FromMilliseconds(10))) { |
+ MessageLoop::current()->RunAllPending(); |
+ } |
+ } |
+ |
void OnCompletion(int result) { |
finished_ = true; |
result_ = result; |
@@ -477,10 +512,11 @@ TEST(DhcpProxyScriptFetcherWin, FailureCaseNoURLConfigured) { |
} |
void TestFailureCaseNoDhcpAdapters(FetcherClient* client) { |
- client->RunImmediateReturnTest(); |
- // In case there are any pending messages that get us in a bad state |
- // (there shouldn't be). |
- MessageLoop::current()->RunAllPending(); |
+ client->RunTest(); |
+ client->RunMessageLoopUntilComplete(); |
+ ASSERT_EQ(ERR_PAC_NOT_IN_DHCP, client->result_); |
+ ASSERT_EQ(L"", client->pac_text_); |
+ ASSERT_EQ(0, client->fetcher_.num_fetchers_created_); |
} |
TEST(DhcpProxyScriptFetcherWin, FailureCaseNoDhcpAdapters) { |
@@ -522,6 +558,24 @@ TEST(DhcpProxyScriptFetcherWin, ShortCircuitLessPreferredAdapters) { |
TestShortCircuitLessPreferredAdapters(&client); |
} |
+void TestImmediateCancel(FetcherClient* client) { |
+ scoped_ptr<DummyDhcpProxyScriptAdapterFetcher> adapter_fetcher( |
+ new DummyDhcpProxyScriptAdapterFetcher()); |
+ adapter_fetcher->Configure(true, OK, L"bingo", 1); |
+ client->fetcher_.PushBackAdapter("a", adapter_fetcher.release()); |
+ client->RunTest(); |
+ client->fetcher_.Cancel(); |
+ client->RunMessageLoopUntilWorkerDone(); |
+ ASSERT_EQ(0, client->fetcher_.num_fetchers_created_); |
+} |
+ |
+// Regression test to check that when we cancel immediately, no |
+// adapter fetchers get created. |
+TEST(DhcpProxyScriptFetcherWin, ImmediateCancel) { |
+ FetcherClient client; |
+ TestImmediateCancel(&client); |
+} |
+ |
TEST(DhcpProxyScriptFetcherWin, ReuseFetcher) { |
FetcherClient client; |
@@ -542,6 +596,7 @@ TEST(DhcpProxyScriptFetcherWin, ReuseFetcher) { |
test_functions.push_back(TestFailureCaseNoURLConfigured); |
test_functions.push_back(TestFailureCaseNoDhcpAdapters); |
test_functions.push_back(TestShortCircuitLessPreferredAdapters); |
+ test_functions.push_back(TestImmediateCancel); |
std::random_shuffle(test_functions.begin(), |
test_functions.end(), |