/// <summary> /// Run update periodically /// </summary> public static RecurringWorker UpdatePeriodically(this IUpdater updater, TimeSpan dueTime, TimeSpan period) { var worker = new RecurringWorker(updater.UpdateAsync, dueTime, period); worker.Start(); return(worker); }
public void RecurringWorker_should_execute_tasks_periodically() { // Arrange var counter = 0; var delayBeforeStart = TimeSpan.FromMilliseconds(400); var delayAfterEnd = TimeSpan.FromMilliseconds(400); var due = TimeSpan.FromMilliseconds(1200); var period = TimeSpan.FromMilliseconds(300); var workingTime = TimeSpan.FromMilliseconds(3800); var expectedCount = 9; var tolerance = 3; Task testAction() { counter++; return(TaskUtilities.CompletedTask); } using (var sut = new RecurringWorker(testAction, due, period)) { sut.ExecutionOmitted += (o, e) => counter++; Delay(delayBeforeStart); sut.Start(); Assert.AreEqual(0, counter); // Act Delay(workingTime); } Delay(delayAfterEnd); // Assert #if (DNXCORE50) Assert.InRange(counter, expectedCount - tolerance, expectedCount + tolerance); #else Assert.GreaterOrEqual(counter, expectedCount - tolerance); Assert.LessOrEqual(counter, expectedCount + tolerance); #endif }