-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSchedulerComputeNextRunTests.cs
More file actions
119 lines (102 loc) · 3.93 KB
/
Copy pathSchedulerComputeNextRunTests.cs
File metadata and controls
119 lines (102 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using BorgMate.Models;
using BorgMate.Services;
namespace BorgMate.Tests;
public class SchedulerComputeNextRunTests
{
private static BorgRepository MakeRepo(ScheduleFrequency frequency, DateTime? lastBackup = null,
int hour = 2, int minute = 0, DayOfWeek dayOfWeek = DayOfWeek.Monday,
int dayOfMonth = 1, int intervalHours = 6, bool runMissed = true)
{
return new BorgRepository
{
Name = "test",
Path = "/test",
Mode = BackupMode.Scheduled,
LastBackupAt = lastBackup,
Schedule = new BackupSchedule
{
Frequency = frequency,
Hour = hour,
Minute = minute,
DayOfWeek = dayOfWeek,
DayOfMonth = dayOfMonth,
IntervalHours = intervalHours,
RunMissed = runMissed
}
};
}
[Fact]
public void Manual_ReturnsNull()
{
var repo = new BorgRepository { Name = "test", Path = "/test", Mode = BackupMode.Manual };
Assert.Null(SchedulerService.ComputeNextRun(repo));
}
[Fact]
public void EveryNHours_NeverRun_ReturnsNow()
{
var repo = MakeRepo(ScheduleFrequency.EveryNHours, intervalHours: 6);
var next = SchedulerService.ComputeNextRun(repo);
Assert.NotNull(next);
Assert.True((DateTime.Now - next.Value).Duration() < TimeSpan.FromSeconds(5));
}
[Fact]
public void EveryNHours_LastRunRecent_ReturnsLastPlusInterval()
{
var lastRun = DateTime.Now.AddHours(-2);
var repo = MakeRepo(ScheduleFrequency.EveryNHours, lastRun, intervalHours: 6);
var next = SchedulerService.ComputeNextRun(repo);
Assert.NotNull(next);
Assert.True((next.Value - lastRun.AddHours(6)).Duration() < TimeSpan.FromSeconds(1));
}
[Fact]
public void Daily_ReturnsToday()
{
var repo = MakeRepo(ScheduleFrequency.Daily, DateTime.Now.AddDays(-2), hour: 2, minute: 30);
var next = SchedulerService.ComputeNextRun(repo);
Assert.NotNull(next);
Assert.Equal(2, next.Value.Hour);
Assert.Equal(30, next.Value.Minute);
}
[Fact]
public void Weekly_ReturnsCorrectDay()
{
var repo = MakeRepo(ScheduleFrequency.Weekly, DateTime.Now.AddDays(-14),
dayOfWeek: DayOfWeek.Sunday, hour: 3);
var next = SchedulerService.ComputeNextRun(repo);
Assert.NotNull(next);
Assert.Equal(DayOfWeek.Sunday, next.Value.DayOfWeek);
Assert.Equal(3, next.Value.Hour);
}
[Fact]
public void Monthly_ReturnsCorrectDayOfMonth()
{
var repo = MakeRepo(ScheduleFrequency.Monthly, DateTime.Now.AddMonths(-2),
dayOfMonth: 15, hour: 4);
var next = SchedulerService.ComputeNextRun(repo);
Assert.NotNull(next);
Assert.Equal(4, next.Value.Hour);
// Day should be 15 or clamped to month's max
Assert.True(next.Value.Day <= 15);
}
[Fact]
public void RunMissed_False_AdvancesToFuture()
{
// Last run was 3 days ago, daily at 2am, RunMissed=false
var repo = MakeRepo(ScheduleFrequency.Daily, DateTime.Now.AddDays(-3),
hour: 2, runMissed: false);
var next = SchedulerService.ComputeNextRun(repo);
Assert.NotNull(next);
Assert.True(next.Value > DateTime.Now, "Should be in the future when RunMissed=false");
}
[Fact]
public void RunMissed_True_ReturnsPastDue()
{
// Last run was 3 days ago, daily at midnight, RunMissed=true (default).
// Use hour=0 so today's occurrence is always in the past regardless of current time.
var repo = MakeRepo(ScheduleFrequency.Daily, DateTime.Now.AddDays(-3), hour: 0, minute: 0);
var next = SchedulerService.ComputeNextRun(repo);
Assert.NotNull(next);
// Past-due time should NOT be advanced, allowing immediate run
Assert.True(next.Value < DateTime.Now);
}
}