public async Task ScheduleAndCancelDoesNotRunTask(int delayMs) { TimeSpan delay = TimeSpan.FromMilliseconds(delayMs); ScheduledTask task = SchedulerService.Schedule(() => { Assert.Fail("The task executed when it shouldn't have."); }, delay); task.Cancel(); await NwTask.Delay(delay + TimeSpan.FromSeconds(1)); }
public async Task RepeatingScheduleRunsRepeatingTask(int delayMs) { TimeSpan interval = TimeSpan.FromMilliseconds(delayMs); Stopwatch stopwatch = Stopwatch.StartNew(); int executionCount = 0; ScheduledTask task = SchedulerService.ScheduleRepeating(() => { Assert.That(stopwatch.Elapsed, Is.EqualTo(interval).Within(100).Milliseconds, "Delay was not within the margin of error."); executionCount++; stopwatch.Restart(); }, interval); await NwTask.WaitUntil(() => executionCount > 5); Assert.That(executionCount, Is.GreaterThan(5)); task.Cancel(); }
public void ScheduledTask_WithCancellation_Test() { // create and start the task var task = new ScheduledTask("Task", ct => Thread.Sleep(500), 100); task.Changed += (s, e) => ReportAction(e); task.Start(); // schedule a cancellation in 1 to 5 seconds var cancelTask = Task.Factory.StartNew(() => { var random = new Random(); Thread.Sleep(random.Next(1000, 5000)); task.Cancel(); }); // allow the task time to be scheduled and run Thread.Sleep(5000); // stop the task task.Stop(); // wait for completion (only in unit tests) task.Wait(); // wait for completion of cancellation task cancelTask.Wait(); }
public void ScheduledTask_WithCancellation_Test() { // create and start the task var task = new ScheduledTask("Task", ct => Thread.Sleep(500), 100); task.Changed += (s, e) => ReportAction(e); task.Start(); // schedule a cancellation in 1 to 5 seconds var cancelTask = Task.Factory.StartNew(() => { var random = new Random(); Thread.Sleep(random.Next(1000, 5000)); task.Cancel(); }); // allow the task time to be scheduled and run Thread.Sleep(5000); // stop the task task.Stop(); // wait for completion (only in unit tests) task.Wait(); // wait for completion of cancellation task cancelTask.Wait(); }