Commit 702e92ed authored by fgorski's avatar fgorski Committed by Commit bot

Fixing the super-long delay when GCMAccountTracker posts a task

* In order to sort out dependencies between initialization of components, I am doing work only when I have a valid GCM connection going (this means all the infrastructure is up and running).
* I am also making sure that time to next token reporting is always between 0 and 12 hours.

BUG=453018

Review URL: https://codereview.chromium.org/885753002

Cr-Commit-Position: refs/heads/master@{#313754}
parent ddbee033
...@@ -80,6 +80,11 @@ void GCMAccountTracker::Start() { ...@@ -80,6 +80,11 @@ void GCMAccountTracker::Start() {
} }
void GCMAccountTracker::ScheduleReportTokens() { void GCMAccountTracker::ScheduleReportTokens() {
// Shortcutting here, in case GCM Driver is not yet connected. In that case
// reporting will be scheduled/started when the connection is made.
if (!driver_->IsConnected())
return;
DVLOG(1) << "Deferring the token reporting for: " DVLOG(1) << "Deferring the token reporting for: "
<< GetTimeToNextTokenReporting().InSeconds() << " seconds."; << GetTimeToNextTokenReporting().InSeconds() << " seconds.";
...@@ -164,8 +169,12 @@ void GCMAccountTracker::OnGetTokenFailure( ...@@ -164,8 +169,12 @@ void GCMAccountTracker::OnGetTokenFailure(
} }
void GCMAccountTracker::OnConnected(const net::IPEndPoint& ip_endpoint) { void GCMAccountTracker::OnConnected(const net::IPEndPoint& ip_endpoint) {
// We are sure here, that GCM is running and connected. We can start reporting
// tokens if reporting is due now, or schedule reporting for later.
if (IsTokenReportingRequired()) if (IsTokenReportingRequired())
ReportTokens(); ReportTokens();
else
ScheduleReportTokens();
} }
void GCMAccountTracker::OnDisconnected() { void GCMAccountTracker::OnDisconnected() {
...@@ -276,8 +285,20 @@ base::TimeDelta GCMAccountTracker::GetTimeToNextTokenReporting() const { ...@@ -276,8 +285,20 @@ base::TimeDelta GCMAccountTracker::GetTimeToNextTokenReporting() const {
driver_->GetLastTokenFetchTime() + driver_->GetLastTokenFetchTime() +
base::TimeDelta::FromMilliseconds(kTokenReportingIntervalMs) - base::TimeDelta::FromMilliseconds(kTokenReportingIntervalMs) -
base::Time::Now(); base::Time::Now();
return time_till_next_reporting < base::TimeDelta() ?
base::TimeDelta() : time_till_next_reporting; // Case when token fetching is overdue.
if (time_till_next_reporting < base::TimeDelta())
return base::TimeDelta();
// Case when calculated period is larger than expected, including the
// situation when the method is called before GCM driver is completely
// initialized.
if (time_till_next_reporting >
base::TimeDelta::FromMilliseconds(kTokenReportingIntervalMs)) {
return base::TimeDelta::FromMilliseconds(kTokenReportingIntervalMs);
}
return time_till_next_reporting;
} }
void GCMAccountTracker::DeleteTokenRequest( void GCMAccountTracker::DeleteTokenRequest(
......
...@@ -476,9 +476,22 @@ TEST_F(GCMAccountTrackerTest, GetTimeToNextTokenReporting) { ...@@ -476,9 +476,22 @@ TEST_F(GCMAccountTrackerTest, GetTimeToNextTokenReporting) {
// At this point the last token fetch time is never. // At this point the last token fetch time is never.
EXPECT_EQ(base::TimeDelta(), GetTimeToNextTokenReporting()); EXPECT_EQ(base::TimeDelta(), GetTimeToNextTokenReporting());
// Regular case. The tokens have been just reported.
driver()->SetLastTokenFetchTime(base::Time::Now()); driver()->SetLastTokenFetchTime(base::Time::Now());
EXPECT_TRUE(GetTimeToNextTokenReporting() <= EXPECT_TRUE(GetTimeToNextTokenReporting() <=
base::TimeDelta::FromSeconds(12 * 60 * 60)); base::TimeDelta::FromSeconds(12 * 60 * 60));
// A case when gcm driver is not yet initialized.
driver()->SetLastTokenFetchTime(base::Time::Max());
EXPECT_EQ(base::TimeDelta::FromSeconds(12 * 60 * 60),
GetTimeToNextTokenReporting());
// A case when token reporting calculation is expected to result in more than
// 12 hours, in which case we expect exactly 12 hours.
driver()->SetLastTokenFetchTime(base::Time::Now() +
base::TimeDelta::FromDays(2));
EXPECT_EQ(base::TimeDelta::FromSeconds(12 * 60 * 60),
GetTimeToNextTokenReporting());
} }
// Tests conditions when token reporting is required. // Tests conditions when token reporting is required.
......
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