Fixed TestCancel tests

This commit is contained in:
Ami Bar
2009-12-25 08:39:48 +02:00
parent 3a35ce5340
commit 3672bb3673
+43 -3
View File
@@ -138,9 +138,8 @@ namespace SmartThreadPoolTests
IWorkItemResult wir = stp.QueueWorkItem( IWorkItemResult wir = stp.QueueWorkItem(
state => { state => {
waitToStart.Set(); waitToStart.Set();
Thread.Sleep(100); waitToComplete.WaitOne();
cancelled = SmartThreadPool.IsWorkItemCanceled; cancelled = SmartThreadPool.IsWorkItemCanceled;
waitToComplete.Set();
return null; return null;
} }
); );
@@ -149,10 +148,51 @@ namespace SmartThreadPoolTests
wir.Cancel(false); wir.Cancel(false);
waitToComplete.WaitOne(); waitToComplete.Set();
stp.WaitForIdle();
Assert.IsTrue(cancelled); Assert.IsTrue(cancelled);
stp.Shutdown();
}
/// <summary>
/// 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
/// </summary>
[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(); stp.Shutdown();
} }