SmartThreadPool v2.0
This commit is contained in:
Ami Bar
2009-12-19 16:32:41 +02:00
parent b30b4abdda
commit 4d6ffb5851
89 changed files with 13714 additions and 2639 deletions
+66 -12
View File
@@ -29,15 +29,17 @@ namespace WorkItemsGroupTests
IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);
bool success = false;
ManualResetEvent isRunning = new ManualResetEvent(false);
for(int i = 0; i < 100; ++i)
for (int i = 0; i < 4; ++i)
{
workItemsGroup.QueueWorkItem(
new WorkItemCallback(this.DoSomeWork),
null);
workItemsGroup.QueueWorkItem(delegate { isRunning.WaitOne(); });
}
success = !workItemsGroup.WaitForIdle(3500);
success = !workItemsGroup.WaitForIdle(1000);
isRunning.Set();
success = success && workItemsGroup.WaitForIdle(1000);
smartThreadPool.Shutdown();
@@ -61,7 +63,7 @@ namespace WorkItemsGroupTests
smartThreadPool.Shutdown();
Assert.IsNotNull(e);
}
}
[Test]
public void WaitForIdleOnSTPThreadForAnotherWorkItemsGroup()
@@ -72,11 +74,11 @@ namespace WorkItemsGroupTests
workItemsGroup1.QueueWorkItem(
new WorkItemCallback(this.DoSomeWork),
null);
1000);
workItemsGroup1.QueueWorkItem(
new WorkItemCallback(this.DoSomeWork),
null);
1000);
IWorkItemResult wir = workItemsGroup2.QueueWorkItem(
new WorkItemCallback(this.DoWaitForIdle),
@@ -91,11 +93,16 @@ namespace WorkItemsGroupTests
}
private int x = 0;
private int _x = 0;
private object DoSomeWork(object state)
{
Debug.WriteLine(Interlocked.Increment(ref x));
Thread.Sleep(1000);
{
int sleepTime = (int)state;
int newX = Interlocked.Increment(ref _x);
Console.WriteLine("{0}: Enter, newX = {1}", DateTime.Now.ToLongTimeString(), newX);
Console.WriteLine("{0}: Sleeping for {1} ms", DateTime.Now.ToLongTimeString(), sleepTime);
Thread.Sleep(sleepTime);
newX = Interlocked.Increment(ref _x);
Console.WriteLine("{0}: Leave, newX = {1}", DateTime.Now.ToLongTimeString(), newX);
return 1;
}
@@ -105,5 +112,52 @@ namespace WorkItemsGroupTests
workItemsGroup.WaitForIdle();
return null;
}
[Test]
public void WaitForIdleWithCancel()
{
SmartThreadPool smartThreadPool = new SmartThreadPool(10 * 1000, 1, 1);
IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(2);
_x = 0;
IWorkItemResult wir1 = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), 1000);
IWorkItemResult wir2 = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), 1000);
IWorkItemResult wir3 = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), 1000);
while (0 == _x)
{
Thread.Sleep(10);
}
Console.WriteLine("{0}: Cancelling WIG", DateTime.Now.ToLongTimeString());
workItemsGroup.Cancel();
// At this point:
// The first work item is running
// The second work item is cancelled, but waits in the STP queue
// The third work item is cancelled.
Assert.AreEqual(1, _x);
Assert.IsTrue(wir2.IsCanceled);
Assert.IsTrue(wir3.IsCanceled);
// Make sure the workItemsGroup is still idle
Assert.IsFalse(workItemsGroup.IsIdle);
Console.WriteLine("{0}: Waiting for 1st result", DateTime.Now.ToLongTimeString());
wir1.GetResult();
Assert.AreEqual(2, _x);
bool isIdle = workItemsGroup.WaitForIdle(100);
Assert.IsTrue(isIdle);
smartThreadPool.Shutdown();
}
}
}