public async Task ScheduleEverySecondWithRealTimer(int milliseconds, int interval, int expected) { var clock = new CustomSystemClock(); var start = clock.UtcNow().AddMilliseconds(100); var schedule = new ScheduleEveryInterval(TimeSpan.FromMilliseconds(interval), start); using var timer = new SystemTimer(); using var scheduler = new Scheduler(clock, timer, Logger.None); var testService = new TestScheduledService(schedule, scheduler); await Task.Delay(TimeSpan.FromMilliseconds(milliseconds)); testService.Invokations.Should().Be(expected); }
public async Task ScheduleEvery4thSeconds() { var clock = new TestClock(new DateTimeOffset(2000, 1, 1, 0, 0, 0, TimeSpan.Zero), TimeSpan.FromSeconds(1)); var start = new DateTimeOffset(2000, 1, 1, 0, 0, 4, 0, TimeSpan.Zero); var schedule = new ScheduleEveryInterval(TimeSpan.FromSeconds(4), start); using var timer = new TestTimer(); using var scheduler = new Scheduler(clock, timer, Logger.None); var testService = new TestScheduledService(schedule, scheduler); for (int i = 0; i < 10; i++) { await Task.Delay(TimeSpan.FromMilliseconds(1)); timer.Tick(); } await Task.Delay(TimeSpan.FromMilliseconds(1)); testService.Invokations.Should().Be(2); }
public async Task CronScheduleEvery5Minutes() { var clock = new TestClock(new DateTimeOffset(2000, 1, 1, 0, 0, 0, TimeSpan.Zero), TimeSpan.FromMinutes(1)); var schedule = new CronSchedule(CronExpression.Parse("*/5 * * * *")); using var timer = new TestTimer(); using var scheduler = new Scheduler(clock, timer, Logger.None); var testService = new TestScheduledService(schedule, scheduler); for (int i = 0; i < 10; i++) { await Task.Delay(TimeSpan.FromMilliseconds(1)); timer.Tick(); } await Task.Delay(TimeSpan.FromMilliseconds(1)); testService.Invokations.Should().Be(2); }