mirror of
https://github.com/farcasclaudiu/SmartThreadPool.git
synced 2026-06-22 09:01:19 +03:00
Added Work Item passive timeout
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
|
||||
<PropertyGroup>
|
||||
<ProjectType>Local</ProjectType>
|
||||
<ProductVersion>9.0.21022</ProductVersion>
|
||||
<ProductVersion>9.0.30729</ProductVersion>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{6A3E4DBF-12AD-4636-ACB3-24B5172FAE03}</ProjectGuid>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
@@ -91,7 +91,7 @@
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="nunit.framework, Version=2.4.8.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" />
|
||||
<Reference Include="nunit.framework, Version=2.5.2.9222, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" />
|
||||
<Reference Include="System">
|
||||
<Name>System</Name>
|
||||
</Reference>
|
||||
@@ -189,6 +189,7 @@
|
||||
<Compile Include="TestWorkItemsQueue.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="TestWorkItemTimeout.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
|
||||
|
||||
@@ -155,6 +155,7 @@ namespace SmartThreadPoolTests
|
||||
|
||||
stp.Shutdown();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 1. Create STP in suspended mode
|
||||
/// 2. Queue work item into the STP
|
||||
@@ -173,7 +174,7 @@ namespace SmartThreadPoolTests
|
||||
stpStartInfo.StartSuspended = true;
|
||||
|
||||
SmartThreadPool stp = new SmartThreadPool(stpStartInfo);
|
||||
IWorkItemResult wir = stp.QueueWorkItem(state => { return null; });
|
||||
IWorkItemResult wir = stp.QueueWorkItem(state => null);
|
||||
|
||||
int counter = 0;
|
||||
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
using System.Threading;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
using Amib.Threading;
|
||||
|
||||
namespace SmartThreadPoolTests
|
||||
{
|
||||
[TestFixture]
|
||||
[Category("TestWorkItemTimeout")]
|
||||
public class TestWorkItemTimeout
|
||||
{
|
||||
/// <summary>
|
||||
/// 1. Create STP in suspended mode
|
||||
/// 2. Queue work item into the STP
|
||||
/// 3. Wait for the work item to expire
|
||||
/// 4. Work item's GetResult should throw WorkItemCancelException
|
||||
/// </summary>
|
||||
[Test]
|
||||
[ExpectedException(typeof(WorkItemCancelException))]
|
||||
public void CancelInQueueWorkItem()
|
||||
{
|
||||
STPStartInfo stpStartInfo = new STPStartInfo();
|
||||
stpStartInfo.StartSuspended = true;
|
||||
|
||||
bool hasRun = false;
|
||||
|
||||
SmartThreadPool stp = new SmartThreadPool(stpStartInfo);
|
||||
IWorkItemResult wir = stp.QueueWorkItem(
|
||||
new WorkItemInfo()
|
||||
{
|
||||
Timeout = 1000 },
|
||||
arg =>
|
||||
{
|
||||
hasRun = true;
|
||||
return null;
|
||||
}
|
||||
);
|
||||
|
||||
Assert.IsFalse(wir.IsCanceled);
|
||||
|
||||
Thread.Sleep(2000);
|
||||
|
||||
Assert.IsTrue(wir.IsCanceled);
|
||||
|
||||
stp.Start();
|
||||
stp.WaitForIdle();
|
||||
|
||||
Assert.IsFalse(hasRun);
|
||||
|
||||
try
|
||||
{
|
||||
wir.GetResult();
|
||||
}
|
||||
finally
|
||||
{
|
||||
stp.Shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 1. Create STP
|
||||
/// 2. Queue work item that takes some time
|
||||
/// 3. Wait for it to start
|
||||
/// 4. The work item timeout expires
|
||||
/// 5. Make sure, in the work item, that SmartThreadPool.IsWorkItemCanceled is true
|
||||
/// 5. Wait for the STP to get idle
|
||||
/// 6. Work item's GetResult should throw WorkItemCancelException
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TimeoutInProgressWorkItemWithSample()
|
||||
{
|
||||
bool timedout = false;
|
||||
ManualResetEvent waitToStart = new ManualResetEvent(false);
|
||||
ManualResetEvent waitToComplete = new ManualResetEvent(false);
|
||||
|
||||
SmartThreadPool stp = new SmartThreadPool();
|
||||
IWorkItemResult wir = stp.QueueWorkItem(
|
||||
new WorkItemInfo() { Timeout = 1000 },
|
||||
state =>
|
||||
{
|
||||
waitToStart.Set();
|
||||
Thread.Sleep(1000);
|
||||
timedout = SmartThreadPool.IsWorkItemCanceled;
|
||||
waitToComplete.Set();
|
||||
return null;
|
||||
});
|
||||
|
||||
waitToStart.WaitOne();
|
||||
|
||||
waitToComplete.WaitOne();
|
||||
|
||||
Assert.IsTrue(timedout);
|
||||
|
||||
stp.Shutdown();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 1. Create STP
|
||||
/// 2. Queue work item into the STP
|
||||
/// 3. Wait for the STP to get idle
|
||||
/// 4. Work item's GetResult should return value
|
||||
/// 4. The work item expires
|
||||
/// 5. Work item's GetResult should return value
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TimeoutCompletedWorkItem()
|
||||
{
|
||||
SmartThreadPool stp = new SmartThreadPool();
|
||||
IWorkItemResult wir =
|
||||
stp.QueueWorkItem(
|
||||
new WorkItemInfo() { Timeout = 1000 },
|
||||
state => 1);
|
||||
|
||||
stp.WaitForIdle();
|
||||
|
||||
Assert.AreEqual(wir.GetResult(), 1);
|
||||
|
||||
Thread.Sleep(1000);
|
||||
|
||||
Assert.AreEqual(wir.GetResult(), 1);
|
||||
|
||||
stp.Shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user