Added InUseThreads to IWorkItemsGroup

Also added a UnitTest for it.

Thanks to TheCutter.
This commit is contained in:
ggrote
2013-05-27 13:37:50 +02:00
committed by Ami Bar
parent b797c98868
commit 922cd9ebd6
5 changed files with 70 additions and 70 deletions
+43 -58
View File
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Threading;
using NUnit.Framework;
@@ -13,9 +14,6 @@ namespace WorkItemsGroupTests
[Category("TestWIGConcurrencyChanges")]
public class TestWIGConcurrencyChanges
{
/// <summary>
/// Example of waiting for idle
/// </summary>
[Test]
public void TestWIGConcurrencyChange1WIG()
{
@@ -57,9 +55,6 @@ namespace WorkItemsGroupTests
smartThreadPool.Shutdown();
}
/// <summary>
/// Example of waiting for idle
/// </summary>
[Test]
public void TestWIGConcurrencyChange2WIGs()
{
@@ -141,67 +136,57 @@ namespace WorkItemsGroupTests
return 1;
}
/*
/// <summary>
/// Example of waiting for idle
/// </summary>
[Test]
public void WaitForIdle()
{
SmartThreadPool smartThreadPool = new SmartThreadPool(10*1000, 25, 0);
public void TestWIGConcurrencyChange()
{
SmartThreadPool smartThreadPool = new SmartThreadPool(10 * 1000, 25, 0);
IWorkItemsGroup wig = smartThreadPool.CreateWorkItemsGroup(smartThreadPool.MaxThreads);
bool success = false;
for(int i = 0; i < 100; ++i)
for (int i = 0; i < 100; ++i)
{
smartThreadPool.QueueWorkItem(
new WorkItemCallback(this.DoSomeWork),
null);
wig.QueueWorkItem(new WorkItemCallback(this.DoSomeLongWork), null);
}
success = !smartThreadPool.WaitForIdle(3500);
success = success && smartThreadPool.WaitForIdle(1000);
wig.Concurrency = 1;
success = WaitForWIGThreadsInUse(wig, 1, 1 * 1000);
Assert.IsTrue(success);
wig.Concurrency = 5;
success = WaitForWIGThreadsInUse(wig, 5, 2 * 1000);
Assert.IsTrue(success);
wig.Concurrency = 25;
success = WaitForWIGThreadsInUse(wig, 25, 4 * 1000);
Assert.IsTrue(success);
wig.Concurrency = 10;
success = WaitForWIGThreadsInUse(wig, 10, 10 * 1000);
Assert.IsTrue(success);
smartThreadPool.Shutdown();
Assert.IsTrue(success);
}
[Test]
public void WaitForIdleOnWrongThread()
{
SmartThreadPool smartThreadPool = new SmartThreadPool(10*1000, 25, 0);
IWorkItemResult wir = smartThreadPool.QueueWorkItem(
new WorkItemCallback(this.DoWaitForIdle),
smartThreadPool);
Exception e;
wir.GetResult(out e);
smartThreadPool.Shutdown();
Assert.IsTrue(e is NotSupportedException);
}
private int x = 0;
private object DoSomeWork(object state)
{
Debug.WriteLine(Interlocked.Increment(ref x));
Thread.Sleep(1000);
return 1;
}
private object DoWaitForIdle(object state)
{
SmartThreadPool smartThreadPool = state as SmartThreadPool;
smartThreadPool.WaitForIdle();
return null;
}
*/
}
private bool WaitForWIGThreadsInUse(IWorkItemsGroup wig, int maxThreadsCount, int timeout)
{
DateTime end = DateTime.Now + new TimeSpan(0, 0, 0, 0, timeout);
bool success = false;
while (DateTime.Now <= end && !success)
{
success = (wig.InUseThreads == maxThreadsCount);
Thread.Sleep(10);
}
return success;
}
private object DoSomeLongWork(object state)
{
Thread.Sleep(250);
return 1;
}
}
}