This source file includes following definitions.
- CreateCallback
- SetUp
- CreateUpdater
- RequestExternalDataFetch
- RequestExternalDataFetch
- CreateRequest
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "components/policy/core/common/cloud/external_policy_data_updater.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/test/test_pending_task.h"
#include "base/test/test_simple_task_runner.h"
#include "base/time/time.h"
#include "components/policy/core/common/cloud/external_policy_data_fetcher.h"
#include "crypto/sha2.h"
#include "net/base/net_errors.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_status.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using testing::Mock;
using testing::Return;
namespace policy {
namespace {
const char* kExternalPolicyDataKeys[] = {"external_policy_data_1",
"external_policy_data_2",
"external_policy_data_3"};
const char* kExternalPolicyDataURLs[] = {"http://example.com/data_1",
"http://example.com/data_2",
"http://example.com/data_3"};
const char* kExternalPolicyDataPayload = "External policy data";
const int64 kExternalPolicyDataMaxSize = 5 * 1024 * 1024;
class MockFetchSuccessCallbackListener {
public:
MOCK_METHOD2(OnFetchSuccess, bool(const std::string&, const std::string&));
ExternalPolicyDataUpdater::FetchSuccessCallback CreateCallback(
const std::string& key);
};
ExternalPolicyDataUpdater::FetchSuccessCallback
MockFetchSuccessCallbackListener::CreateCallback(const std::string& key) {
return base::Bind(&MockFetchSuccessCallbackListener::OnFetchSuccess,
base::Unretained(this),
key);
}
}
class ExternalPolicyDataUpdaterTest : public testing::Test {
protected:
virtual void SetUp() OVERRIDE;
void CreateUpdater(size_t max_parallel_fetches);
ExternalPolicyDataUpdater::Request CreateRequest(
const std::string& url) const;
void RequestExternalDataFetch(int key_index, int url_index);
void RequestExternalDataFetch(int index);
net::TestURLFetcherFactory fetcher_factory_;
MockFetchSuccessCallbackListener callback_listener_;
scoped_refptr<base::TestSimpleTaskRunner> backend_task_runner_;
scoped_refptr<base::TestSimpleTaskRunner> io_task_runner_;
scoped_ptr<ExternalPolicyDataFetcherBackend> fetcher_backend_;
scoped_ptr<ExternalPolicyDataUpdater> updater_;
};
void ExternalPolicyDataUpdaterTest::SetUp() {
fetcher_factory_.set_remove_fetcher_on_delete(true);
backend_task_runner_ = new base::TestSimpleTaskRunner();
io_task_runner_ = new base::TestSimpleTaskRunner();
}
void ExternalPolicyDataUpdaterTest::CreateUpdater(size_t max_parallel_fetches) {
fetcher_backend_.reset(new ExternalPolicyDataFetcherBackend(
io_task_runner_,
scoped_refptr<net::URLRequestContextGetter>()));
updater_.reset(new ExternalPolicyDataUpdater(
backend_task_runner_,
fetcher_backend_->CreateFrontend(backend_task_runner_),
max_parallel_fetches));
}
void ExternalPolicyDataUpdaterTest::RequestExternalDataFetch(int key_index,
int url_index) {
updater_->FetchExternalData(
kExternalPolicyDataKeys[key_index],
CreateRequest(kExternalPolicyDataURLs[url_index]),
callback_listener_.CreateCallback(kExternalPolicyDataKeys[key_index]));
io_task_runner_->RunUntilIdle();
backend_task_runner_->RunPendingTasks();
}
void ExternalPolicyDataUpdaterTest::RequestExternalDataFetch(int index) {
RequestExternalDataFetch(index, index);
}
ExternalPolicyDataUpdater::Request
ExternalPolicyDataUpdaterTest::CreateRequest(const std::string& url) const {
return ExternalPolicyDataUpdater::Request(
url,
crypto::SHA256HashString(kExternalPolicyDataPayload),
kExternalPolicyDataMaxSize);
}
TEST_F(ExternalPolicyDataUpdaterTest, FetchSuccess) {
CreateUpdater(1);
RequestExternalDataFetch(0);
RequestExternalDataFetch(1);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_response_code(200);
fetcher->SetResponseString(kExternalPolicyDataPayload);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_CALL(callback_listener_,
OnFetchSuccess(kExternalPolicyDataKeys[0],
kExternalPolicyDataPayload))
.Times(1)
.WillOnce(Return(true));
backend_task_runner_->RunPendingTasks();
Mock::VerifyAndClearExpectations(&callback_listener_);
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
}
TEST_F(ExternalPolicyDataUpdaterTest, PayloadSizeExceedsLimit) {
CreateUpdater(1);
RequestExternalDataFetch(0);
RequestExternalDataFetch(1);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->delegate()->OnURLFetchDownloadProgress(
fetcher,
kExternalPolicyDataMaxSize + 1,
-1);
backend_task_runner_->RunPendingTasks();
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
EXPECT_EQ(1u, backend_task_runner_->GetPendingTasks().size());
}
TEST_F(ExternalPolicyDataUpdaterTest, FetchFailure) {
CreateUpdater(1);
RequestExternalDataFetch(0);
RequestExternalDataFetch(1);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
net::ERR_NETWORK_CHANGED));
fetcher->delegate()->OnURLFetchComplete(fetcher);
backend_task_runner_->RunPendingTasks();
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
EXPECT_EQ(1u, backend_task_runner_->GetPendingTasks().size());
}
TEST_F(ExternalPolicyDataUpdaterTest, ServerFailure) {
CreateUpdater(1);
RequestExternalDataFetch(0);
RequestExternalDataFetch(1);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_response_code(500);
fetcher->delegate()->OnURLFetchComplete(fetcher);
backend_task_runner_->RunPendingTasks();
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
EXPECT_EQ(1u, backend_task_runner_->GetPendingTasks().size());
}
TEST_F(ExternalPolicyDataUpdaterTest, RetryLimit) {
CreateUpdater(1);
RequestExternalDataFetch(0);
int fetcher_id = 0;
for (int i = 0; i < 3; ++i) {
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(fetcher_id);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_response_code(400);
fetcher->delegate()->OnURLFetchComplete(fetcher);
backend_task_runner_->RunPendingTasks();
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(fetcher_id));
EXPECT_EQ(1u, backend_task_runner_->GetPendingTasks().size());
backend_task_runner_->RunPendingTasks();
io_task_runner_->RunUntilIdle();
EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
++fetcher_id;
}
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(fetcher_id);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_response_code(400);
fetcher->delegate()->OnURLFetchComplete(fetcher);
backend_task_runner_->RunPendingTasks();
io_task_runner_->RunUntilIdle();
++fetcher_id;
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(fetcher_id));
EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
}
TEST_F(ExternalPolicyDataUpdaterTest, RetryWithBackoff) {
CreateUpdater(1);
RequestExternalDataFetch(0);
base::TimeDelta expected_delay = base::TimeDelta::FromSeconds(60);
const base::TimeDelta delay_cap = base::TimeDelta::FromHours(12);
int fetcher_id = 0;
for (int i = 0; i < 20; ++i) {
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(fetcher_id);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_response_code(500);
fetcher->delegate()->OnURLFetchComplete(fetcher);
backend_task_runner_->RunPendingTasks();
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(fetcher_id));
EXPECT_EQ(1u, backend_task_runner_->GetPendingTasks().size());
const base::TestPendingTask& task =
backend_task_runner_->GetPendingTasks().front();
EXPECT_GT(task.delay,
base::TimeDelta::FromMilliseconds(
0.799 * expected_delay.InMilliseconds()));
EXPECT_LE(task.delay, expected_delay);
if (i < 10) {
EXPECT_LT(expected_delay, delay_cap);
expected_delay *= 2;
if (i == 9) {
EXPECT_GT(expected_delay, delay_cap);
expected_delay = delay_cap;
}
}
backend_task_runner_->RunPendingTasks();
io_task_runner_->RunUntilIdle();
EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
++fetcher_id;
}
}
TEST_F(ExternalPolicyDataUpdaterTest, HashInvalid) {
CreateUpdater(1);
RequestExternalDataFetch(0);
RequestExternalDataFetch(1);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_response_code(200);
fetcher->SetResponseString("Invalid external policy data");
fetcher->delegate()->OnURLFetchComplete(fetcher);
backend_task_runner_->RunPendingTasks();
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
EXPECT_EQ(1u, backend_task_runner_->GetPendingTasks().size());
}
TEST_F(ExternalPolicyDataUpdaterTest, DataRejectedByCallback) {
CreateUpdater(1);
RequestExternalDataFetch(0);
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_response_code(200);
fetcher->SetResponseString(kExternalPolicyDataPayload);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_CALL(callback_listener_,
OnFetchSuccess(kExternalPolicyDataKeys[0],
kExternalPolicyDataPayload))
.Times(1)
.WillOnce(Return(false));
backend_task_runner_->RunPendingTasks();
Mock::VerifyAndClearExpectations(&callback_listener_);
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
EXPECT_EQ(1u, backend_task_runner_->GetPendingTasks().size());
backend_task_runner_->RunPendingTasks();
io_task_runner_->RunUntilIdle();
EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_response_code(200);
fetcher->SetResponseString(kExternalPolicyDataPayload);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_CALL(callback_listener_,
OnFetchSuccess(kExternalPolicyDataKeys[0],
kExternalPolicyDataPayload))
.Times(1)
.WillOnce(Return(true));
backend_task_runner_->RunPendingTasks();
Mock::VerifyAndClearExpectations(&callback_listener_);
io_task_runner_->RunUntilIdle();
EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
}
TEST_F(ExternalPolicyDataUpdaterTest, URLChanged) {
CreateUpdater(1);
RequestExternalDataFetch(0);
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
RequestExternalDataFetch(0, 1);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
}
TEST_F(ExternalPolicyDataUpdaterTest, JobInvalidated) {
CreateUpdater(1);
RequestExternalDataFetch(0);
RequestExternalDataFetch(1);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
RequestExternalDataFetch(1, 2);
fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
fetcher->set_response_code(500);
fetcher->delegate()->OnURLFetchComplete(fetcher);
backend_task_runner_->RunPendingTasks();
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[2]), fetcher->GetOriginalURL());
}
TEST_F(ExternalPolicyDataUpdaterTest, FetchCanceled) {
CreateUpdater(1);
RequestExternalDataFetch(0);
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
updater_->CancelExternalDataFetch(kExternalPolicyDataKeys[0]);
io_task_runner_->RunUntilIdle();
backend_task_runner_->RunPendingTasks();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
}
TEST_F(ExternalPolicyDataUpdaterTest, ParallelJobs) {
CreateUpdater(2);
RequestExternalDataFetch(0);
RequestExternalDataFetch(1);
RequestExternalDataFetch(2);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(2));
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_response_code(200);
fetcher->SetResponseString(kExternalPolicyDataPayload);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_CALL(callback_listener_,
OnFetchSuccess(kExternalPolicyDataKeys[0],
kExternalPolicyDataPayload))
.Times(1)
.WillOnce(Return(true));
backend_task_runner_->RunPendingTasks();
Mock::VerifyAndClearExpectations(&callback_listener_);
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
fetcher = fetcher_factory_.GetFetcherByID(2);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[2]), fetcher->GetOriginalURL());
fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
fetcher->set_response_code(200);
fetcher->SetResponseString(kExternalPolicyDataPayload);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_CALL(callback_listener_,
OnFetchSuccess(kExternalPolicyDataKeys[1],
kExternalPolicyDataPayload))
.Times(1)
.WillOnce(Return(true));
backend_task_runner_->RunPendingTasks();
Mock::VerifyAndClearExpectations(&callback_listener_);
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
fetcher = fetcher_factory_.GetFetcherByID(2);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[2]), fetcher->GetOriginalURL());
fetcher->set_response_code(200);
fetcher->SetResponseString(kExternalPolicyDataPayload);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_CALL(callback_listener_,
OnFetchSuccess(kExternalPolicyDataKeys[2],
kExternalPolicyDataPayload))
.Times(1)
.WillOnce(Return(true));
backend_task_runner_->RunPendingTasks();
Mock::VerifyAndClearExpectations(&callback_listener_);
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(2));
EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
}
TEST_F(ExternalPolicyDataUpdaterTest, ParallelJobsFinishingOutOfOrder) {
CreateUpdater(2);
RequestExternalDataFetch(0);
RequestExternalDataFetch(1);
RequestExternalDataFetch(2);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(2));
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
fetcher->set_response_code(200);
fetcher->SetResponseString(kExternalPolicyDataPayload);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_CALL(callback_listener_,
OnFetchSuccess(kExternalPolicyDataKeys[1],
kExternalPolicyDataPayload))
.Times(1)
.WillOnce(Return(true));
backend_task_runner_->RunPendingTasks();
Mock::VerifyAndClearExpectations(&callback_listener_);
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
fetcher = fetcher_factory_.GetFetcherByID(2);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[2]), fetcher->GetOriginalURL());
fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_response_code(200);
fetcher->SetResponseString(kExternalPolicyDataPayload);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_CALL(callback_listener_,
OnFetchSuccess(kExternalPolicyDataKeys[0],
kExternalPolicyDataPayload))
.Times(1)
.WillOnce(Return(true));
backend_task_runner_->RunPendingTasks();
Mock::VerifyAndClearExpectations(&callback_listener_);
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
fetcher = fetcher_factory_.GetFetcherByID(2);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[2]), fetcher->GetOriginalURL());
fetcher->set_response_code(200);
fetcher->SetResponseString(kExternalPolicyDataPayload);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_CALL(callback_listener_,
OnFetchSuccess(kExternalPolicyDataKeys[2],
kExternalPolicyDataPayload))
.Times(1)
.WillOnce(Return(true));
backend_task_runner_->RunPendingTasks();
Mock::VerifyAndClearExpectations(&callback_listener_);
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(2));
EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
}
TEST_F(ExternalPolicyDataUpdaterTest, ParallelJobsWithRetry) {
CreateUpdater(2);
RequestExternalDataFetch(0);
RequestExternalDataFetch(1);
RequestExternalDataFetch(2);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(2));
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_response_code(400);
fetcher->delegate()->OnURLFetchComplete(fetcher);
backend_task_runner_->RunPendingTasks();
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
fetcher = fetcher_factory_.GetFetcherByID(2);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[2]), fetcher->GetOriginalURL());
EXPECT_EQ(1u, backend_task_runner_->GetPendingTasks().size());
backend_task_runner_->RunPendingTasks();
io_task_runner_->RunUntilIdle();
EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(3));
fetcher->set_response_code(200);
fetcher->SetResponseString(kExternalPolicyDataPayload);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_CALL(callback_listener_,
OnFetchSuccess(kExternalPolicyDataKeys[2],
kExternalPolicyDataPayload))
.Times(1)
.WillOnce(Return(true));
backend_task_runner_->RunPendingTasks();
Mock::VerifyAndClearExpectations(&callback_listener_);
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(2));
fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
fetcher = fetcher_factory_.GetFetcherByID(3);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
}
TEST_F(ExternalPolicyDataUpdaterTest, ParallelJobsWithCancel) {
CreateUpdater(2);
RequestExternalDataFetch(0);
RequestExternalDataFetch(1);
RequestExternalDataFetch(2);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(2));
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
updater_->CancelExternalDataFetch(kExternalPolicyDataKeys[0]);
io_task_runner_->RunUntilIdle();
backend_task_runner_->RunPendingTasks();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
fetcher = fetcher_factory_.GetFetcherByID(2);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[2]), fetcher->GetOriginalURL());
fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
fetcher->set_response_code(200);
fetcher->SetResponseString(kExternalPolicyDataPayload);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_CALL(callback_listener_,
OnFetchSuccess(kExternalPolicyDataKeys[1],
kExternalPolicyDataPayload))
.Times(1)
.WillOnce(Return(true));
backend_task_runner_->RunPendingTasks();
Mock::VerifyAndClearExpectations(&callback_listener_);
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
fetcher = fetcher_factory_.GetFetcherByID(2);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[2]), fetcher->GetOriginalURL());
fetcher->set_response_code(200);
fetcher->SetResponseString(kExternalPolicyDataPayload);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_CALL(callback_listener_,
OnFetchSuccess(kExternalPolicyDataKeys[2],
kExternalPolicyDataPayload))
.Times(1)
.WillOnce(Return(true));
backend_task_runner_->RunPendingTasks();
Mock::VerifyAndClearExpectations(&callback_listener_);
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(2));
EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
}
TEST_F(ExternalPolicyDataUpdaterTest, ParallelJobsWithInvalidatedJob) {
CreateUpdater(2);
RequestExternalDataFetch(0);
RequestExternalDataFetch(1);
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
RequestExternalDataFetch(1, 2);
fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
fetcher = fetcher_factory_.GetFetcherByID(2);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[2]), fetcher->GetOriginalURL());
}
}