Skip to content

Commit f81ad60

Browse files
authored
Merge pull request #585 from rivaldi8/bugfix-583-scheduled-exports-always-daily
Bugfix 583: Scheduled exports are always executed twice a day - fixes #583
2 parents dae1caf + 54cfecd commit f81ad60

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

app/src/main/java/org/gnucash/android/service/ScheduledActionService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ private static int executeBackup(ScheduledAction scheduledAction, SQLiteDatabase
164164
if (endTime > 0 && endTime < now)
165165
return executionCount;
166166

167+
if (scheduledAction.computeNextScheduledExecutionTime() > now)
168+
return 0;
169+
167170
ExportParams params = ExportParams.parseCsv(scheduledAction.getTag());
168171
try {
169172
//wait for async task to finish before we proceed (we are holding a wake lock)

app/src/test/java/org/gnucash/android/test/unit/service/ScheduledActionServiceTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.gnucash.android.test.unit.testutil.ShadowCrashlytics;
4545
import org.gnucash.android.test.unit.testutil.ShadowUserVoice;
4646
import org.joda.time.DateTime;
47+
import org.joda.time.LocalDateTime;
4748
import org.joda.time.Weeks;
4849
import org.junit.After;
4950
import org.junit.Before;
@@ -304,6 +305,36 @@ public void scheduledBackups_shouldRunOnlyOnce(){
304305
assertThat(backupFiles[0]).exists().hasExtension("gnca");
305306
}
306307

308+
/**
309+
* Tests that a scheduled backup isn't executed before the next scheduled
310+
* execution according to its recurrence.
311+
*
312+
* <p>Tests for bug https://github.com/codinguser/gnucash-android/issues/583</p>
313+
*/
314+
@Test
315+
public void scheduledBackups_shouldNotRunBeforeNextScheduledExecution(){
316+
ScheduledAction scheduledBackup = new ScheduledAction(ScheduledAction.ActionType.BACKUP);
317+
scheduledBackup.setStartTime(LocalDateTime.now().minusDays(2).toDate().getTime());
318+
scheduledBackup.setExecutionCount(1);
319+
scheduledBackup.setRecurrence(PeriodType.WEEK, 1);
320+
321+
ExportParams backupParams = new ExportParams(ExportFormat.XML);
322+
backupParams.setExportTarget(ExportParams.ExportTarget.SD_CARD);
323+
scheduledBackup.setTag(backupParams.toCsv());
324+
325+
File backupFolder = new File(
326+
Exporter.getExportFolderPath(BooksDbAdapter.getInstance().getActiveBookUID()));
327+
assertThat(backupFolder).exists();
328+
assertThat(backupFolder.listFiles()).isEmpty();
329+
330+
List<ScheduledAction> actions = new ArrayList<>();
331+
actions.add(scheduledBackup);
332+
ScheduledActionService.processScheduledActions(actions, mDb);
333+
334+
assertThat(scheduledBackup.getExecutionCount()).isEqualTo(1);
335+
assertThat(backupFolder.listFiles()).hasSize(0);
336+
}
337+
307338
@After
308339
public void tearDown(){
309340
TransactionsDbAdapter.getInstance().deleteAllRecords();

0 commit comments

Comments
 (0)