public void CancelAll_ShouldStopAllTasks() { // Arrange var watch = System.Diagnostics.Stopwatch.StartNew(); var task = new TaskWorker(this); var workDone = false; task.QueueWorkerWithDelay(this.workerQueue, null, (x, e) => { // some custom do work logic. workDone = true; throw new Exception("Thread not cancelled. Expected to never reach here."); }, (x, e) => { }, (e) => { }, (x, e) => { }, 5000); // Act task.CancelAll(workerQueue); watch.Stop(); // Assert Assert.InRange(watch.ElapsedMilliseconds, 0, 500); //should be more than enough if (workDone) { throw new Exception("Task not cancelled on time."); } }
public void AllTasks_ShouldNotDoWork() { // Arrange var watch = System.Diagnostics.Stopwatch.StartNew(); var task = new TaskWorker(this); var manualResetEvent = new ManualResetEvent(false); var workDone1 = false; var workDone2 = false; var workDone3 = false; var workDone4 = false; var workDone5 = false; var workDone6 = false; var completed1 = false; var completed2 = false; var completed3 = false; var completed4 = false; var completed5 = false; var completed6 = false; // Act task.QueueWorkerWithDelay(this.workerQueue, null, (x, e) => { System.Threading.Thread.Sleep(100); // used to ensure the below code is executed workDone1 = true; }, (x, e) => { completed1 = true; manualResetEvent.Set(); }, (e) => { }, (x, e) => { }, 5000); task.QueueWorker(this.workerQueue, null, (x, e) => { workDone2 = true; }, (x, e) => { completed2 = true; }, (e) => { }, (x, e) => { }); task.QueueWorker(this.workerQueue, null, (x, e) => { workDone3 = true; }, (x, e) => { completed3 = true; }, (e) => { }, (x, e) => { }); task.QueueWorker(this.workerQueue, null, (x, e) => { workDone4 = true; }, (x, e) => { completed4 = true; }, (e) => { }, (x, e) => { }); task.QueueWorkerWithDelay(this.workerQueue, null, (x, e) => { workDone5 = true; }, (x, e) => { completed5 = true; }, (e) => { }, (x, e) => { }, 4000); task.QueueWorker(this.workerQueue, null, (x, e) => { workDone6 = true; }, (x, e) => { completed6 = true; }, (e) => { }, (x, e) => { }); task.CancelAll(workerQueue); watch.Stop(); manualResetEvent.WaitOne(1000); // Assert Assert.InRange(watch.ElapsedMilliseconds, 0, 500); //should be more than enough if (workDone1) { throw new Exception("Task 1 has done work."); } if (workDone2) { throw new Exception("Task 2 has done work."); } if (workDone3) { throw new Exception("Task 3 has done work."); } if (workDone4) { throw new Exception("Task 4 has done work."); } if (workDone5) { throw new Exception("Task 5 has done work."); } if (workDone6) { throw new Exception("Task 6 has done work."); } if (!completed1) { throw new Exception("Task 1 should be completed."); } //if (!completed2) throw new Exception("Task 2 should be completed."); //if (!completed3) throw new Exception("Task 3 should be completed."); //if (!completed4) throw new Exception("Task 4 should be completed."); //if (!completed5) throw new Exception("Task 5 should be completed."); //if (!completed6) throw new Exception("Task 6 should be completed."); }