From 3672bb36730ef2221cf78ae421fdfae280eb2692 Mon Sep 17 00:00:00 2001 From: Ami Bar Date: Fri, 25 Dec 2009 08:39:48 +0200 Subject: [PATCH] Fixed TestCancel tests --- STPTests/TestCancel.cs | 46 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/STPTests/TestCancel.cs b/STPTests/TestCancel.cs index 38759a5..a782153 100644 --- a/STPTests/TestCancel.cs +++ b/STPTests/TestCancel.cs @@ -138,9 +138,8 @@ namespace SmartThreadPoolTests IWorkItemResult wir = stp.QueueWorkItem( state => { waitToStart.Set(); - Thread.Sleep(100); + waitToComplete.WaitOne(); cancelled = SmartThreadPool.IsWorkItemCanceled; - waitToComplete.Set(); return null; } ); @@ -149,10 +148,51 @@ namespace SmartThreadPoolTests wir.Cancel(false); - waitToComplete.WaitOne(); + waitToComplete.Set(); + + stp.WaitForIdle(); Assert.IsTrue(cancelled); + stp.Shutdown(); + } + + /// + /// 1. Create STP + /// 2. Queue work item that takes some time + /// 3. Wait for it to start + /// 4. Cancel the work item (soft) + /// 5. Don't call to SmartThreadPool.IsWorkItemCanceled + /// 6. Wait for the STP to get idle + /// 7. Work item's GetResult should throw WorkItemCancelException + /// + [Test] + [ExpectedException(typeof(WorkItemCancelException))] + public void CancelInProgressWorkItemSoftWithIgnoreSample() + { + ManualResetEvent waitToStart = new ManualResetEvent(false); + ManualResetEvent waitToComplete = new ManualResetEvent(false); + + SmartThreadPool stp = new SmartThreadPool(); + IWorkItemResult wir = stp.QueueWorkItem( + state => { + waitToStart.Set(); + Thread.Sleep(100); + waitToComplete.WaitOne(); + return null; + } + ); + + waitToStart.WaitOne(); + + wir.Cancel(false); + + waitToComplete.Set(); + + stp.WaitForIdle(); + + wir.GetResult(); + stp.Shutdown(); }