public void CanRunAfterError() { var times = 0; var errors = 0; ScheduledTask s = null; s = new ScheduledTask(() => { times++; if (times == 2) { throw new Exception("error"); } if (times == 4) { s.Stop(); } }) { StartInterval = 10, AfterSuccessInterval = 10, AfterErrorInterval = 10, ProceedOnError = true }; s.OnError += _ => errors++; s.Start(); Thread.Sleep(1000); Assert.AreEqual(4, times); Assert.AreEqual(1, errors); }
public void ScheduledTask_Test() { // create and start the task var task = new ScheduledTask("Task", ct => Thread.Sleep(500), 100); task.Changed += (s, e) => ReportAction(e); task.Start(); // 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(); }
public void CanRunRegular() { var times = 0; ScheduledTask s = null; s = new ScheduledTask(() => { Console.WriteLine("CanRunRegular " + times); times++; if (times == 3) { s.Stop(); } }) { StartInterval = 10, AfterSuccessInterval = 10 }; s.Start(); Thread.Sleep(1000); Assert.AreEqual(3, times); }
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(); }