Added tests for wok item timeout

This commit is contained in:
Ami Bar
2009-12-20 09:42:44 +02:00
parent ccfd49f237
commit bb25bd62e3
7 changed files with 156 additions and 34 deletions
+32 -10
View File
@@ -1,4 +1,10 @@
<head>
<style type="text/css">
.style1
{
font-weight: normal;
}
</style>
</head>
<P><IMG src="SmartThreadPool.jpg" width="616" height="416"></P>
<P>See the <A href="#History">history</A> section at the bottom for changes.</P>
@@ -40,6 +46,8 @@
<li>Mono i<t>s supported.</t></li>
<li>
<t>Performance Counters (Windows &amp; internal)</t><t></t></li>
<li>
Work Item timeout (passive)</li>
</UL>
<H2>Why do you need a thread pool?</H2>
<BLOCKQUOTE>
@@ -96,8 +104,11 @@
<li><A href="#Feature26">Added new methods: Join, Choice, and Pipe.</A></li>
</ol>
<p>
</p>
<h2>
New features added in December 2009</h2>
<ol>
<li value="27"><A href="#Feature27">Added work item timeout (passive).</A></li>
</ol>
<H2>What about the .NET ThreadPool?</H2>
<P>The Windows system provides one .NET ThreadPool for each process. The .NET ThreadPool can contain up to 25 (by default) threads per processor. It is also stated that the operations in .NET ThreadPool should be quick to avoid suspension of the work of others who use the .NET ThreadPool. Note that several AppDomains in the same process share the same .NET ThreadPool. If you want a thread to work for a long period of time, then the .NET ThreadPool is not a good choice for you (unless you know what you are doing). Note that each asynchronous method call from the .NET Framework that begins with "Begin…" (e.g., <code>BeginInvoke</code>, <code>BeginSend</code>, <code>BeginReceive</code>, etc.) uses the .NET ThreadPool to run its callback. Also note that the .NET ThreadPool
doesn't support calls to COM with single threaded apartment (STA), since the ThreadPool
@@ -397,7 +408,7 @@ SmartThreadPool.WaitAny(new IWaitableResult[] { wir1, wir2});</PRE>
<p>
There is no guarantee that a work item will be cancelled, it depends on the state
of the work item when the cancel is called and the cooperation of the work item.
(Note the work item's state I mention here has nothing to do with the state object
(Note that the work item's state I mention here has nothing to do with the state object
argument provided in the <code>QueueWorkItem</code>).</p>
<p>
These are the possible states of a work item: (defined in the <code>WorkItemState</code> enum)<br/>
@@ -747,6 +758,13 @@ to simplfy the initiation of a parallel task.</p>
<li>Choice - Executes several work items and returns when the first one completes. <A href="#ChoiceExample">(Choice example)</A></li>
<li>Pipe - Executes several work items sequently and wait until the last work item completes. <A href="#PipeExample">(Pipe example)</A></li>
</ul>
<p>
<li><a name=#Feature27><b>Work item timeout (passive)</b></a></li>
<p>This featue let the user specify a timeout for the work item in milliseconds. When the timeout expires the work item is cancelled
automatically. The cancel works the same as a call to Cancel with the <code>abortExecution</code> argument set to false (This is why the timeout is passive).</p>
<p>To sample the cancel use <code>SmartThreadPool.IsWorkItemCanceled</code> which is a static property, or you can use <code>SmartThreadPool.AbortOnWorkItemOnCancel</code>
which check if the current work item is cancelled and if so abort the thread (<code>Thread.CurrentThread.Abort()</code>).</p>
</p>
</OL>
<H2>When to use?</H2>
<P>The Smart Thread Pool is good when your work items don't do too much, but wait for events, IOs, sockets, etc. This means that the work items don't use CPU, but run for a long time. It is also good when you don't need to keep alive too many threads in the air all the time. If your work items do a short time work, then use the .NET ThreadPool. If you have a constant heavy load of work, then use Toub's thread pool and define the maximum number of threads accordingly.</P>
@@ -1348,16 +1366,20 @@ public class <A name=PipeExample>PipeExample</A>
<li>Added support for WinCE</li>
<li>Added support for Action&lt;T&gt; and Func&lt;T&gt;</li>
</ul>
</li><li><b>7th April 2009: Changes
</b>
</li><li>7th April 2009: Changes
<ul>
<li><strong>Added support for Silverlight and Mono.</strong></li>
<li><strong>Added Join, Choice, and Pipe to SmartThreadPool.</strong></li>
<li><strong>Added local performance counters (for Mono, Silverlight, and WindowsCE)</strong></li>
<li><strong>Changed duration measures from DateTime.Now to Stopwatch.</strong></li>
<li><strong>Queues changed from System.Collections.Queue to System.Collections.Generic.LinkedList&lt;T&gt;.</strong></li>
<li><strong class="style1">Added support for Silverlight and Mono.</strong></li>
<li><strong class="style1">Added Join, Choice, and Pipe to SmartThreadPool.</strong></li>
<li><strong class="style1">Added local performance counters (for Mono, Silverlight, and WindowsCE)</strong></li>
<li><strong class="style1">Changed duration measures from DateTime.Now to Stopwatch.</strong></li>
<li><strong class="style1">Queues changed from System.Collections.Queue to System.Collections.Generic.LinkedList&lt;T&gt;.</strong></li>
</ul>
</li>
<li>21st December 2009: Changes
<ul>
<li><strong class="style1">Added work item timeout (passive).</strong></li>
</ul>
</li>
</UL>
+16 -10
View File
@@ -23,6 +23,11 @@
<CreateTestPage>true</CreateTestPage>
<ValidateXaml>true</ValidateXaml>
<ThrowErrorsInValidation>false</ThrowErrorsInValidation>
<FileUpgradeFlags>
</FileUpgradeFlags>
<OldToolsVersion>3.5</OldToolsVersion>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@@ -75,27 +80,28 @@
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">
<Generator>MSBuild:MarkupCompilePass1</Generator>
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Page Include="Groupbox.xaml">
<Generator>MSBuild:MarkupCompilePass1</Generator>
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Page.xaml">
<Generator>MSBuild:MarkupCompilePass1</Generator>
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="SpinButton.xaml">
<SubType>Page</SubType>
<Generator>MSBuild:MarkupCompilePass1</Generator>
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="UsageControl.xaml">
<SubType>Page</SubType>
<Generator>MSBuild:MarkupCompilePass1</Generator>
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="UsageHistoryControl.xaml">
<SubType>Page</SubType>
<Generator>MSBuild:MarkupCompilePass1</Generator>
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
@@ -122,7 +128,7 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\Silverlight\v2.0\Microsoft.Silverlight.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Silverlight\v3.0\Microsoft.Silverlight.CSharp.targets" Condition="" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
+1 -1
View File
@@ -27,6 +27,7 @@
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>2.0</OldToolsVersion>
<IsWebBootstrapper>true</IsWebBootstrapper>
<PublishUrl>http://localhost/STPTests/</PublishUrl>
<Install>true</Install>
<InstallFrom>Web</InstallFrom>
@@ -39,7 +40,6 @@
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>true</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
+41 -1
View File
@@ -13,7 +13,7 @@ namespace SmartThreadPoolTests
[Category("TestCancel")]
public class TestCancel
{
/// <summary>
/// <summary>
/// 1. Create STP in suspended mode
/// 2. Queue work item into the STP
/// 3. Cancel the work item
@@ -153,6 +153,46 @@ namespace SmartThreadPoolTests
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. Call to AbortOnWorkItemOnCancel
/// 5. Wait for the STP to get idle
/// 6. Make sure nothing ran in the work item after the AbortOnWorkItemOnCancel
/// </summary>
[Test]
public void CancelInProgressWorkItemSoftWithAbortOnWorkItemOnCancel()
{
bool abortFailed = false;
ManualResetEvent waitToStart = new ManualResetEvent(false);
ManualResetEvent waitToCancel = new ManualResetEvent(false);
SmartThreadPool stp = new SmartThreadPool();
IWorkItemResult wir = stp.QueueWorkItem(
state => {
waitToStart.Set();
waitToCancel.WaitOne();
SmartThreadPool.AbortOnWorkItemOnCancel();
abortFailed = true;
return null;
});
waitToStart.WaitOne();
wir.Cancel(false);
waitToCancel.Set();
stp.WaitForIdle();
Assert.IsTrue(wir.IsCanceled);
Assert.IsFalse(abortFailed);
stp.Shutdown();
}
+45 -11
View File
@@ -27,15 +27,12 @@ namespace SmartThreadPoolTests
SmartThreadPool stp = new SmartThreadPool(stpStartInfo);
IWorkItemResult wir = stp.QueueWorkItem(
new WorkItemInfo()
{
Timeout = 1000 },
arg =>
{
hasRun = true;
return null;
}
);
new WorkItemInfo() { Timeout = 500 },
state =>
{
hasRun = true;
return null;
});
Assert.IsFalse(wir.IsCanceled);
@@ -76,7 +73,7 @@ namespace SmartThreadPoolTests
SmartThreadPool stp = new SmartThreadPool();
IWorkItemResult wir = stp.QueueWorkItem(
new WorkItemInfo() { Timeout = 1000 },
new WorkItemInfo() { Timeout = 500 },
state =>
{
waitToStart.Set();
@@ -109,7 +106,7 @@ namespace SmartThreadPoolTests
SmartThreadPool stp = new SmartThreadPool();
IWorkItemResult wir =
stp.QueueWorkItem(
new WorkItemInfo() { Timeout = 1000 },
new WorkItemInfo() { Timeout = 500 },
state => 1);
stp.WaitForIdle();
@@ -122,5 +119,42 @@ namespace SmartThreadPoolTests
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. Call to AbortOnWorkItemOnCancel
/// 5. Wait for the STP to get idle
/// 6. Make sure nothing ran in the work item after the AbortOnWorkItemOnCancel
/// </summary>
[Test]
public void TimeoutInProgressWorkItemSoftWithAbortOnWorkItemOnCancel()
{
bool abortFailed = false;
ManualResetEvent waitToStart = new ManualResetEvent(false);
ManualResetEvent waitToComplete = new ManualResetEvent(false);
SmartThreadPool stp = new SmartThreadPool();
IWorkItemResult wir = stp.QueueWorkItem(
new WorkItemInfo() { Timeout = 500 },
state =>
{
waitToStart.Set();
Thread.Sleep(1000);
SmartThreadPool.AbortOnWorkItemOnCancel();
abortFailed = true;
return null;
});
waitToStart.WaitOne();
stp.WaitForIdle();
Assert.IsTrue(wir.IsCanceled);
Assert.IsFalse(abortFailed);
stp.Shutdown();
}
}
}
+15
View File
@@ -71,6 +71,9 @@
// - Added local performance counters (for Mono, Silverlight, and WindowsCE)
// - Changed duration measures from DateTime.Now to Stopwatch.
// - Queues changed from System.Collections.Queue to System.Collections.Generic.LinkedList<T>.
//
// 21 December 2009 - Changes:
// - Added work item timeout (passive)
#endregion
@@ -1338,6 +1341,18 @@ namespace Amib.Threading
{
return CurrentThreadEntry.CurrentWorkItem.IsCanceled;
}
}
/// <summary>
/// Checks if the work item has been cancelled, and if yes then abort the thread.
/// Can be used with Cancel and timeout
/// </summary>
public static void AbortOnWorkItemOnCancel()
{
if (IsWorkItemCanceled)
{
Thread.CurrentThread.Abort();
}
}
/// <summary>
+6 -1
View File
@@ -14,6 +14,11 @@
<SilverlightApplication>false</SilverlightApplication>
<ValidateXaml>true</ValidateXaml>
<ThrowErrorsInValidation>false</ThrowErrorsInValidation>
<FileUpgradeFlags>
</FileUpgradeFlags>
<OldToolsVersion>3.5</OldToolsVersion>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@@ -68,7 +73,7 @@
<Compile Include="WorkItemsGroupBase.cs" />
<Compile Include="WorkItemsQueue.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\Silverlight\v2.0\Microsoft.Silverlight.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Silverlight\v3.0\Microsoft.Silverlight.CSharp.targets" Condition="" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">