async def test_max_sync_interval_next_sync_attempt(coord: Coordinator, model, source: HelperTestSource, dest: HelperTestSource, backup, time: FakeTime, simple_config: Config): """ Next backup is after max sync interval is reached """ simple_config.override(Setting.DAYS_BETWEEN_BACKUPS, 1) simple_config.override(Setting.MAX_SYNC_INTERVAL_SECONDS, 60 * 60) time.setTimeZone("Europe/Stockholm") simple_config.override(Setting.BACKUP_TIME_OF_DAY, "03:23") simple_config.override(Setting.DAYS_BETWEEN_BACKUPS, 1) source.setMax(10) source.insert("Fri", time.toUtc(time.local(2020, 3, 16, 3, 33))) time.setNow(time.local(2020, 3, 17, 1, 29)) model.reinitialize() coord.reset() await coord.sync() assert coord.nextSyncAttempt() == time.local(2020, 3, 17, 2, 29) assert coord.nextBackupTime() > coord.nextSyncAttempt()
async def test_schedule_snapshot_next_sync_attempt(coord: Coordinator, model, source: HelperTestSource, dest: HelperTestSource, snapshot, time: FakeTime, simple_config: Config): """ Next snapshot is before max sync interval is reached """ simple_config.override(Setting.DAYS_BETWEEN_SNAPSHOTS, 1) simple_config.override(Setting.MAX_SYNC_INTERVAL_SECONDS, 60 * 60) time.setTimeZone("Europe/Stockholm") simple_config.override(Setting.SNAPSHOT_TIME_OF_DAY, "03:23") simple_config.override(Setting.DAYS_BETWEEN_SNAPSHOTS, 1) source.setMax(10) source.insert("Fri", time.toUtc(time.local(2020, 3, 16, 3, 33))) time.setNow(time.local(2020, 3, 17, 2, 29)) model.reinitialize() coord.reset() await coord.sync() assert coord.nextSnapshotTime() == time.local(2020, 3, 17, 3, 23) assert coord.nextSnapshotTime() == coord.nextSyncAttempt()
async def test_backoff(coord: Coordinator, model, source: HelperTestSource, dest: HelperTestSource, snapshot, time: FakeTime, simple_config: Config): assert coord.check() simple_config.override(Setting.DAYS_BETWEEN_SNAPSHOTS, 1) simple_config.override(Setting.MAX_SYNC_INTERVAL_SECONDS, 60 * 60 * 6) assert coord.nextSyncAttempt() == time.now() + timedelta(hours=6) assert not coord.check() error = Exception("BOOM") old_sync = model.sync model.sync = lambda s: doRaise(error) await coord.sync() # first backoff should be 0 seconds assert coord.nextSyncAttempt() == time.now() assert coord.check() # backoff maxes out at 1 hr = 3600 seconds for seconds in [ 10, 20, 40, 80, 160, 320, 640, 1280, 2560, 3600, 3600, 3600 ]: await coord.sync() assert coord.nextSyncAttempt() == time.now() + timedelta( seconds=seconds) assert not coord.check() assert not coord.check() assert not coord.check() # a good sync resets it back to 6 hours from now model.sync = old_sync await coord.sync() assert coord.nextSyncAttempt() == time.now() + timedelta(hours=6) assert not coord.check() # if the next snapshot is less that 6 hours from the last one, that that shoudl be when we sync simple_config.override(Setting.DAYS_BETWEEN_SNAPSHOTS, 1.0 / 24.0) assert coord.nextSyncAttempt() == time.now() + timedelta(hours=1) assert not coord.check() time.advance(hours=2) assert coord.nextSyncAttempt() == time.now() - timedelta(hours=1) assert coord.check()