Skip to content

Commit dfdf287

Browse files
committed
fix tests
1 parent 30c9a0c commit dfdf287

File tree

4 files changed

+554
-34
lines changed

4 files changed

+554
-34
lines changed

src/ScheduledTasks/Entity/Schedule.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -282,17 +282,25 @@ DateTimeOffset DetermineNextRunTime(ScheduleConfiguration scheduleConfig)
282282
// compute time gap between now and startat if set else with ScheduleCreatedAt
283283
TimeSpan timeSinceStart = now - startTime;
284284

285-
if (timeSinceStart <= TimeSpan.Zero && scheduleConfig.StartImmediatelyIfLate)
285+
// timeSinceStart is negative that means next run time should be in future
286+
if (timeSinceStart < TimeSpan.Zero)
286287
{
287-
return now;
288+
return startTime;
288289
}
289-
else
290-
{
291-
// Calculate number of intervals between start time and now
292-
int intervalsElapsed = (int)(timeSinceStart.Ticks / scheduleConfig.Interval.Ticks);
293290

294-
// Compute next run time based on intervals elapsed since start
295-
return startTime + TimeSpan.FromTicks(scheduleConfig.Interval.Ticks * (intervalsElapsed + 1));
291+
// timeSinceStart is >= 0, this mean current time already past start time
292+
bool isFirstRun = this.State.LastRunAt == null;
293+
294+
// check edge case: if this is first run and startimmediatelyiflate is true, run immediately
295+
if (isFirstRun && scheduleConfig.StartImmediatelyIfLate)
296+
{
297+
return now;
296298
}
299+
300+
// Calculate number of intervals between start time and now
301+
int intervalsElapsed = (int)(timeSinceStart.Ticks / scheduleConfig.Interval.Ticks);
302+
303+
// Compute next run time based on intervals elapsed since start
304+
return startTime + TimeSpan.FromTicks(scheduleConfig.Interval.Ticks * (intervalsElapsed + 1));
297305
}
298306
}

test/ScheduledTasks.Tests/Client/ScheduleClientImplTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@ public class ScheduleClientImplTests
1414
{
1515
readonly Mock<DurableTaskClient> durableTaskClient;
1616
readonly Mock<DurableEntityClient> entityClient;
17-
readonly Mock<ILogger> logger;
17+
readonly ILogger logger;
1818
readonly ScheduleClientImpl client;
1919
readonly string scheduleId = "test-schedule";
2020

2121
public ScheduleClientImplTests()
2222
{
2323
this.durableTaskClient = new Mock<DurableTaskClient>("test", MockBehavior.Strict);
2424
this.entityClient = new Mock<DurableEntityClient>("test", MockBehavior.Strict);
25-
this.logger = new Mock<ILogger>(MockBehavior.Loose);
25+
this.logger = new TestLogger();
2626
this.durableTaskClient.Setup(x => x.Entities).Returns(this.entityClient.Object);
27-
this.client = new ScheduleClientImpl(this.durableTaskClient.Object, this.scheduleId, this.logger.Object);
27+
this.client = new ScheduleClientImpl(this.durableTaskClient.Object, this.scheduleId, this.logger);
2828
}
2929

3030
[Fact]
3131
public void Constructor_WithNullClient_ThrowsArgumentNullException()
3232
{
3333
// Act & Assert
34-
var ex = Assert.Throws<ArgumentNullException>(() =>
35-
new ScheduleClientImpl(null!, this.scheduleId, this.logger.Object));
34+
ArgumentNullException ex = Assert.Throws<ArgumentNullException>(() =>
35+
new ScheduleClientImpl(null!, this.scheduleId, this.logger));
3636
Assert.Equal("client", ex.ParamName);
3737
}
3838

@@ -43,7 +43,7 @@ public void Constructor_WithInvalidScheduleId_ThrowsArgumentException(string inv
4343
{
4444
// Act & Assert
4545
var ex = Assert.Throws<ArgumentException>(() =>
46-
new ScheduleClientImpl(this.durableTaskClient.Object, invalidScheduleId, this.logger.Object));
46+
new ScheduleClientImpl(this.durableTaskClient.Object, invalidScheduleId, this.logger));
4747
Assert.Contains("scheduleId cannot be null or empty", ex.Message, StringComparison.OrdinalIgnoreCase);
4848
}
4949

0 commit comments

Comments
 (0)