public async Task CanRun() { var countdown = new AsyncCountdownEvent(1); Func<Task<DateTime?>> callback = () => { countdown.Signal(); return null; }; using (var timer = new ScheduledTimer(callback, loggerFactory: Log)) { timer.ScheduleNext(); await countdown.WaitAsync(TimeSpan.FromMilliseconds(100)); Assert.Equal(0, countdown.CurrentCount); } }
public async Task CanRunWithMinimumInterval() { var countdown = new AsyncCountdownEvent(2); Func<Task<DateTime?>> callback = async () => { _logger.Info("Starting work."); countdown.Signal(); await SystemClock.SleepAsync(500); _logger.Info("Finished work."); return null; }; using (var timer = new ScheduledTimer(callback, minimumIntervalTime: TimeSpan.FromMilliseconds(100), loggerFactory: Log)) { for (int i = 0; i < 4; i++) { timer.ScheduleNext(); SystemClock.Sleep(1); } await countdown.WaitAsync(TimeSpan.FromMilliseconds(100)); Assert.Equal(1, countdown.CurrentCount); await countdown.WaitAsync(TimeSpan.FromSeconds(1.5)); Assert.Equal(0, countdown.CurrentCount); } }
protected void InitializeMaintenance(TimeSpan?dueTime = null, TimeSpan?intervalTime = null) { _maintenanceTimer = new ScheduledTimer(DoMaintenanceAsync, dueTime, intervalTime, _loggerFactory); }
public async Task CanRecoverFromError() { var resetEvent = new AsyncAutoResetEvent(false); int hits = 0; Func<Task<DateTime?>> callback = () => { Interlocked.Increment(ref hits); _logger.Info("Callback called for the #{time} time", hits); if (hits == 1) throw new Exception("Error in callback"); resetEvent.Set(); return Task.FromResult<DateTime?>(null); }; using (var timer = new ScheduledTimer(callback, loggerFactory: Log)) { timer.ScheduleNext(); await resetEvent.WaitAsync(new CancellationTokenSource(500).Token); Assert.Equal(2, hits); } }
protected void InitializeMaintenance(TimeSpan? dueTime = null, TimeSpan? intervalTime = null) { _maintenanceTimer = new ScheduledTimer(DoMaintenanceAsync, dueTime, intervalTime, _loggerFactory); }