Commit 65aa8f65 authored by zhongyi's avatar zhongyi Committed by Commit bot

Change the HttpServerPropertiesManager::net_task_runner_ to use a...

Change the HttpServerPropertiesManager::net_task_runner_ to use a TestMockTimeTaskRunner in unittests so that
we are able to verify tasks posted with delay in the future.

Now all the tests that were ExpectCacheUpdate or ExpectScheduleUpdatePrefsOnNetworkThread
will need to call net_test_task_runner_>FastForwardUntilNoTasksRemain() so that all
the tasks complete running.

BUG=670519

Review-Url: https://codereview.chromium.org/2563273002
Cr-Commit-Position: refs/heads/master@{#437980}
parent 6741acb0
...@@ -100,7 +100,7 @@ void HttpServerPropertiesManager::InitializeOnNetworkThread() { ...@@ -100,7 +100,7 @@ void HttpServerPropertiesManager::InitializeOnNetworkThread() {
http_server_properties_impl_.reset(new HttpServerPropertiesImpl()); http_server_properties_impl_.reset(new HttpServerPropertiesImpl());
network_prefs_update_timer_.reset(new base::OneShotTimer); network_prefs_update_timer_.reset(new base::OneShotTimer);
network_prefs_update_timer_->SetTaskRunner(network_task_runner_);
pref_task_runner_->PostTask( pref_task_runner_->PostTask(
FROM_HERE, FROM_HERE,
base::Bind(&HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread, base::Bind(&HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread,
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/test/test_mock_time_task_runner.h"
#include "base/test/test_simple_task_runner.h" #include "base/test/test_simple_task_runner.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "base/values.h" #include "base/values.h"
...@@ -29,6 +30,7 @@ namespace net { ...@@ -29,6 +30,7 @@ namespace net {
namespace { namespace {
using base::StringPrintf; using base::StringPrintf;
using base::TestMockTimeTaskRunner;
using ::testing::_; using ::testing::_;
using ::testing::Invoke; using ::testing::Invoke;
using ::testing::Mock; using ::testing::Mock;
...@@ -149,14 +151,16 @@ static const int kHttpServerPropertiesVersions[] = {3, 4, 5}; ...@@ -149,14 +151,16 @@ static const int kHttpServerPropertiesVersions[] = {3, 4, 5};
class HttpServerPropertiesManagerTest : public testing::TestWithParam<int> { class HttpServerPropertiesManagerTest : public testing::TestWithParam<int> {
protected: protected:
HttpServerPropertiesManagerTest() {} HttpServerPropertiesManagerTest()
: net_test_task_runner_(new TestMockTimeTaskRunner()) {}
void SetUp() override { void SetUp() override {
one_day_from_now_ = base::Time::Now() + base::TimeDelta::FromDays(1); one_day_from_now_ = base::Time::Now() + base::TimeDelta::FromDays(1);
pref_delegate_ = new MockPrefDelegate; pref_delegate_ = new MockPrefDelegate;
http_server_props_manager_.reset( http_server_props_manager_.reset(
new StrictMock<TestingHttpServerPropertiesManager>( new StrictMock<TestingHttpServerPropertiesManager>(
pref_delegate_, base::ThreadTaskRunnerHandle::Get())); pref_delegate_, net_test_task_runner_));
ExpectCacheUpdate(); ExpectCacheUpdate();
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
} }
...@@ -218,6 +222,7 @@ class HttpServerPropertiesManagerTest : public testing::TestWithParam<int> { ...@@ -218,6 +222,7 @@ class HttpServerPropertiesManagerTest : public testing::TestWithParam<int> {
std::unique_ptr<TestingHttpServerPropertiesManager> std::unique_ptr<TestingHttpServerPropertiesManager>
http_server_props_manager_; http_server_props_manager_;
base::Time one_day_from_now_; base::Time one_day_from_now_;
scoped_refptr<TestMockTimeTaskRunner> net_test_task_runner_;
private: private:
DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManagerTest); DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManagerTest);
...@@ -357,6 +362,8 @@ TEST_P(HttpServerPropertiesManagerTest, ...@@ -357,6 +362,8 @@ TEST_P(HttpServerPropertiesManagerTest,
pref_delegate_->SetPrefs(http_server_properties_dict); pref_delegate_->SetPrefs(http_server_properties_dict);
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
net_test_task_runner_->FastForwardUntilNoTasksRemain();
Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
// Verify SupportsSpdy. // Verify SupportsSpdy.
...@@ -447,7 +454,7 @@ TEST_P(HttpServerPropertiesManagerTest, ...@@ -447,7 +454,7 @@ TEST_P(HttpServerPropertiesManagerTest,
TEST_P(HttpServerPropertiesManagerTest, BadCachedHostPortPair) { TEST_P(HttpServerPropertiesManagerTest, BadCachedHostPortPair) {
ExpectCacheUpdate(); ExpectCacheUpdate();
// The prefs are automaticalls updated in the case corruption is detected. // The prefs are automatically updated in the case corruption is detected.
ExpectPrefsUpdate(); ExpectPrefsUpdate();
ExpectScheduleUpdatePrefsOnNetworkThread(); ExpectScheduleUpdatePrefsOnNetworkThread();
...@@ -509,6 +516,8 @@ TEST_P(HttpServerPropertiesManagerTest, BadCachedHostPortPair) { ...@@ -509,6 +516,8 @@ TEST_P(HttpServerPropertiesManagerTest, BadCachedHostPortPair) {
pref_delegate_->SetPrefs(http_server_properties_dict); pref_delegate_->SetPrefs(http_server_properties_dict);
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
net_test_task_runner_->FastForwardUntilNoTasksRemain();
Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
// Verify that nothing is set. // Verify that nothing is set.
...@@ -528,7 +537,7 @@ TEST_P(HttpServerPropertiesManagerTest, BadCachedHostPortPair) { ...@@ -528,7 +537,7 @@ TEST_P(HttpServerPropertiesManagerTest, BadCachedHostPortPair) {
TEST_P(HttpServerPropertiesManagerTest, BadCachedAltProtocolPort) { TEST_P(HttpServerPropertiesManagerTest, BadCachedAltProtocolPort) {
ExpectCacheUpdate(); ExpectCacheUpdate();
// The prefs are automaticalls updated in the case corruption is detected. // The prefs are automatically updated in the case corruption is detected.
ExpectPrefsUpdate(); ExpectPrefsUpdate();
ExpectScheduleUpdatePrefsOnNetworkThread(); ExpectScheduleUpdatePrefsOnNetworkThread();
...@@ -573,6 +582,8 @@ TEST_P(HttpServerPropertiesManagerTest, BadCachedAltProtocolPort) { ...@@ -573,6 +582,8 @@ TEST_P(HttpServerPropertiesManagerTest, BadCachedAltProtocolPort) {
pref_delegate_->SetPrefs(http_server_properties_dict); pref_delegate_->SetPrefs(http_server_properties_dict);
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
net_test_task_runner_->FastForwardUntilNoTasksRemain();
Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
// Verify alternative service is not set. // Verify alternative service is not set.
...@@ -597,6 +608,7 @@ TEST_P(HttpServerPropertiesManagerTest, SupportsSpdy) { ...@@ -597,6 +608,7 @@ TEST_P(HttpServerPropertiesManagerTest, SupportsSpdy) {
// Run the task. // Run the task.
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
net_test_task_runner_->FastForwardUntilNoTasksRemain();
EXPECT_TRUE(http_server_props_manager_->SupportsRequestPriority(spdy_server)); EXPECT_TRUE(http_server_props_manager_->SupportsRequestPriority(spdy_server));
Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
...@@ -618,6 +630,8 @@ TEST_P(HttpServerPropertiesManagerTest, GetAlternativeServices) { ...@@ -618,6 +630,8 @@ TEST_P(HttpServerPropertiesManagerTest, GetAlternativeServices) {
// Run the task. // Run the task.
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
net_test_task_runner_->FastForwardUntilNoTasksRemain();
Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
AlternativeServiceVector alternative_service_vector = AlternativeServiceVector alternative_service_vector =
...@@ -649,6 +663,8 @@ TEST_P(HttpServerPropertiesManagerTest, SetAlternativeServices) { ...@@ -649,6 +663,8 @@ TEST_P(HttpServerPropertiesManagerTest, SetAlternativeServices) {
// Run the task. // Run the task.
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
net_test_task_runner_->FastForwardUntilNoTasksRemain();
Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
AlternativeServiceVector alternative_service_vector = AlternativeServiceVector alternative_service_vector =
...@@ -712,6 +728,8 @@ TEST_P(HttpServerPropertiesManagerTest, ConfirmAlternativeService) { ...@@ -712,6 +728,8 @@ TEST_P(HttpServerPropertiesManagerTest, ConfirmAlternativeService) {
// Run the task. // Run the task.
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
net_test_task_runner_->FastForwardUntilNoTasksRemain();
Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
EXPECT_FALSE(http_server_props_manager_->IsAlternativeServiceBroken( EXPECT_FALSE(http_server_props_manager_->IsAlternativeServiceBroken(
...@@ -734,6 +752,8 @@ TEST_P(HttpServerPropertiesManagerTest, SupportsQuic) { ...@@ -734,6 +752,8 @@ TEST_P(HttpServerPropertiesManagerTest, SupportsQuic) {
// Run the task. // Run the task.
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
net_test_task_runner_->FastForwardUntilNoTasksRemain();
Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&address)); EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&address));
...@@ -756,6 +776,8 @@ TEST_P(HttpServerPropertiesManagerTest, ServerNetworkStats) { ...@@ -756,6 +776,8 @@ TEST_P(HttpServerPropertiesManagerTest, ServerNetworkStats) {
// Run the task. // Run the task.
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
net_test_task_runner_->FastForwardUntilNoTasksRemain();
Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
const ServerNetworkStats* stats2 = const ServerNetworkStats* stats2 =
...@@ -779,6 +801,8 @@ TEST_P(HttpServerPropertiesManagerTest, QuicServerInfo) { ...@@ -779,6 +801,8 @@ TEST_P(HttpServerPropertiesManagerTest, QuicServerInfo) {
// Run the task. // Run the task.
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
net_test_task_runner_->FastForwardUntilNoTasksRemain();
Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
EXPECT_EQ(quic_server_info1, *http_server_props_manager_->GetQuicServerInfo( EXPECT_EQ(quic_server_info1, *http_server_props_manager_->GetQuicServerInfo(
...@@ -807,6 +831,7 @@ TEST_P(HttpServerPropertiesManagerTest, Clear) { ...@@ -807,6 +831,7 @@ TEST_P(HttpServerPropertiesManagerTest, Clear) {
// Run the task. // Run the task.
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
net_test_task_runner_->FastForwardUntilNoTasksRemain();
EXPECT_TRUE(http_server_props_manager_->SupportsRequestPriority(spdy_server)); EXPECT_TRUE(http_server_props_manager_->SupportsRequestPriority(spdy_server));
EXPECT_TRUE(HasAlternativeService(spdy_server)); EXPECT_TRUE(HasAlternativeService(spdy_server));
...@@ -912,6 +937,8 @@ TEST_P(HttpServerPropertiesManagerTest, BadSupportsQuic) { ...@@ -912,6 +937,8 @@ TEST_P(HttpServerPropertiesManagerTest, BadSupportsQuic) {
pref_delegate_->SetPrefs(http_server_properties_dict); pref_delegate_->SetPrefs(http_server_properties_dict);
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
net_test_task_runner_->FastForwardUntilNoTasksRemain();
Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
// Verify alternative service. // Verify alternative service.
...@@ -983,6 +1010,8 @@ TEST_P(HttpServerPropertiesManagerTest, UpdateCacheWithPrefs) { ...@@ -983,6 +1010,8 @@ TEST_P(HttpServerPropertiesManagerTest, UpdateCacheWithPrefs) {
ExpectPrefsUpdate(); ExpectPrefsUpdate();
ExpectCacheUpdate(); ExpectCacheUpdate();
http_server_props_manager_->ScheduleUpdateCacheOnPrefThread(); http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
net_test_task_runner_->FastForwardUntilNoTasksRemain();
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
// Verify preferences. // Verify preferences.
...@@ -1114,6 +1143,8 @@ TEST_P(HttpServerPropertiesManagerTest, ...@@ -1114,6 +1143,8 @@ TEST_P(HttpServerPropertiesManagerTest,
ExpectPrefsUpdate(); ExpectPrefsUpdate();
ExpectCacheUpdate(); ExpectCacheUpdate();
http_server_props_manager_->ScheduleUpdateCacheOnPrefThread(); http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
net_test_task_runner_->FastForwardUntilNoTasksRemain();
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
const base::DictionaryValue& pref_dict = const base::DictionaryValue& pref_dict =
...@@ -1241,7 +1272,9 @@ TEST_P(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs1) { ...@@ -1241,7 +1272,9 @@ TEST_P(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs1) {
// Shutdown comes before the task is executed. // Shutdown comes before the task is executed.
http_server_props_manager_->ShutdownOnPrefThread(); http_server_props_manager_->ShutdownOnPrefThread();
// Run the task after shutdown, but before deletion. // Run the task after shutdown, but before deletion.
net_test_task_runner_->FastForwardUntilNoTasksRemain();
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
http_server_props_manager_.reset(); http_server_props_manager_.reset();
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment