Skip to content

Commit

Permalink
Merge branch 'due' into 'master'
Browse files Browse the repository at this point in the history
Correct sync wake up at unexpected hours, fixes MER#1840.

See merge request mer-core/buteo-syncfw!27
  • Loading branch information
pvuorela committed Sep 12, 2019
2 parents beba86b + 5983eb7 commit 229e373
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 2 deletions.
22 changes: 22 additions & 0 deletions libbuteosyncfw/profile/SyncSchedule.cpp
Expand Up @@ -487,6 +487,28 @@ QDateTime SyncSchedule::nextRushSwitchTime(const QDateTime &aFromTime) const
}
}

bool SyncSchedule::isSyncScheduled(const QDateTime &aDateTime) const
{
// Simple case, aDateTime is the defined sync time.
if (d_ptr->iTime.isValid() && !d_ptr->iDays.isEmpty()) {
/* Todo: this is to simple implementation for the case where
sync time is close to midnight and the day has changed
already when fired. */
if (!d_ptr->iDays.contains(aDateTime.date().dayOfWeek())) {
return false;
}
/* Keep a 10 minutes margin to ensure that delayed
syncs by more prioritary sync in progress are still
considered as valid sync times. */
return (aDateTime.time() < d_ptr->iTime.addSecs(5 * 60)
&& aDateTime.time() > d_ptr->iTime.addSecs(-5 * 60));
}
/* Sync schedule is defined by intervals and / or rush, check that
schedule or rush is enabled for the current aDateTime. */
return ((rushEnabled() && d_ptr->iRushInterval > 0 && d_ptr->isRush(aDateTime))
|| (scheduleEnabled() && d_ptr->iInterval > 0));
}


DaySet SyncSchedulePrivate::parseDays(const QString &aDays) const
{
Expand Down
7 changes: 7 additions & 0 deletions libbuteosyncfw/profile/SyncSchedule.h
Expand Up @@ -257,6 +257,13 @@ class SyncSchedule
*/
QDateTime nextRushSwitchTime(const QDateTime& aFromTime) const;

/*! \brief Returns true if aDateTime is within a scheduled period.
*
* \param aDateTime a give date time.
* \return true if at aDateTime, the synchronization is possible.
*/
bool isSyncScheduled(const QDateTime &aDateTime) const;

private:

SyncSchedulePrivate* d_ptr;
Expand Down
25 changes: 23 additions & 2 deletions msyncd/synchronizer.cpp
Expand Up @@ -316,8 +316,29 @@ bool Synchronizer::startScheduledSync(QString aProfileName)
bool accept = acceptScheduledSync(iNetworkManager->isOnline(), iNetworkManager->connectionType());
if(accept)
{
LOG_DEBUG("Scheduled sync of" << aProfileName << "accepted with current connectivity status.");
startSync(aProfileName, true);
/* Ensure that current time is compatible with sync schedule.
The Background process may have started a sync in a period
where sync is disabled due to delayed interval wake up. */
SyncProfile *profile = iProfileManager.syncProfile(aProfileName);
bool wrongTime = (profile && !profile->syncSchedule().isSyncScheduled(QDateTime::currentDateTime()));
delete profile;
if (wrongTime)
{
LOG_DEBUG("Woken up of" << aProfileName << "in a disabled period, not starting sync.");
if (iSyncScheduler)
{
// can be null if in backup/restore state.
iSyncScheduler->syncStatusChanged(aProfileName,
Sync::SYNC_NOTPOSSIBLE,
QLatin1String("Sync cancelled due to wrong wake up"),
Buteo::SyncResults::ABORTED);
}
}
else
{
LOG_DEBUG("Scheduled sync of" << aProfileName << "accepted with current connectivity status.");
startSync(aProfileName, true);
}
}
else
{
Expand Down
Expand Up @@ -215,4 +215,56 @@ void SyncScheduleTest::testNextSyncTime()
QCOMPARE(next.time(), s.rushBegin());
}

void SyncScheduleTest::testIsSyncScheduled()
{
SyncSchedule s;

// Exact time.
QTime exact(15, 0, 0, 0);
s.setTime(exact);
QVERIFY(!s.isSyncScheduled(QDateTime(QDate(2019, 8, 27), QTime(15, 0, 0, 0))));
DaySet days;
days.insert(Qt::Wednesday);
days.insert(Qt::Monday);
s.setDays(days);
// This is a Tuesday.
QVERIFY(!s.isSyncScheduled(QDateTime(QDate(2019, 8, 27), QTime(15, 0, 0, 0))));
// These are valid within 10 seconds margin
QVERIFY(s.isSyncScheduled(QDateTime(QDate(2019, 8, 28), QTime(15, 0, 0, 0))));
QVERIFY(s.isSyncScheduled(QDateTime(QDate(2019, 8, 28), QTime(15, 0, 4, 0))));
QVERIFY(s.isSyncScheduled(QDateTime(QDate(2019, 8, 28), QTime(14, 59, 56, 0))));
// These are invalid
QVERIFY(!s.isSyncScheduled(QDateTime(QDate(2019, 8, 28), QTime(15, 0, 5, 0))));
QVERIFY(!s.isSyncScheduled(QDateTime(QDate(2019, 8, 28), QTime(14, 59, 55, 0))));

// Simply enabled, no rush.
s.setTime(QTime());
s.setInterval(3600);
s.setScheduleEnabled(true);
// Any time, any day should match
QVERIFY(s.isSyncScheduled(QDateTime(QDate(2019, 8, 25), QTime(15, 0, 0, 0))));
QVERIFY(s.isSyncScheduled(QDateTime(QDate(2019, 8, 28), QTime(5, 0, 0, 0))));
QVERIFY(s.isSyncScheduled(QDateTime(QDate(2019, 8, 29), QTime(21, 0, 0, 0))));

// Rush enabled, in rush time.
DaySet rushDays;
rushDays.insert(Qt::Tuesday);
rushDays.insert(Qt::Wednesday);
s.setRushDays(rushDays);
s.setRushEnabled(true);
s.setRushTime(QTime(8, 0, 0, 0), QTime(16, 0, 0, 0));
s.setRushInterval(300);
QVERIFY(s.isSyncScheduled(QDateTime(QDate(2019, 8, 27), QTime(15, 0, 0, 0))));
QVERIFY(s.isSyncScheduled(QDateTime(QDate(2019, 8, 28), QTime(9, 0, 0, 0))));

// Rush enabled, not in rush time.
QVERIFY(s.isSyncScheduled(QDateTime(QDate(2019, 8, 27), QTime(17, 0, 0, 0))));
QVERIFY(s.isSyncScheduled(QDateTime(QDate(2019, 8, 29), QTime(9, 0, 0, 0))));

// Rush enabled, not in rush time, schedule disabled.
s.setScheduleEnabled(false);
QVERIFY(!s.isSyncScheduled(QDateTime(QDate(2019, 8, 27), QTime(17, 0, 0, 0))));
QVERIFY(!s.isSyncScheduled(QDateTime(QDate(2019, 8, 29), QTime(9, 0, 0, 0))));
}

QTEST_MAIN(Buteo::SyncScheduleTest)
Expand Up @@ -38,6 +38,8 @@ private slots:
void testProperties();

void testNextSyncTime();

void testIsSyncScheduled();
};

}
Expand Down

0 comments on commit 229e373

Please sign in to comment.