mirror of
https://github.com/farcasclaudiu/SmartThreadPool.git
synced 2026-06-22 11:01:28 +03:00
v2.0
SmartThreadPool v2.0
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
using Amib.Threading;
|
||||
|
||||
namespace SmartThreadPoolTests
|
||||
{
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
[TestFixture]
|
||||
[Category("TestThreadsCreate")]
|
||||
public class TestThreadsCreate
|
||||
{
|
||||
private bool _initSuccess;
|
||||
private bool _workItemSuccess;
|
||||
private bool _termSuccess;
|
||||
|
||||
private void ClearResults()
|
||||
{
|
||||
_initSuccess = false;
|
||||
_workItemSuccess = false;
|
||||
_termSuccess = false;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestThreadsEvents()
|
||||
{
|
||||
ClearResults();
|
||||
|
||||
SmartThreadPool stp = new SmartThreadPool();
|
||||
|
||||
stp.OnThreadInitialization += new ThreadInitializationHandler(OnInitialization);
|
||||
stp.OnThreadTermination += new ThreadTerminationHandler(OnTermination);
|
||||
|
||||
stp.QueueWorkItem(new WorkItemCallback(DoSomeWork), null);
|
||||
|
||||
stp.WaitForIdle();
|
||||
stp.Shutdown();
|
||||
|
||||
Assert.IsTrue(_initSuccess);
|
||||
Assert.IsTrue(_workItemSuccess);
|
||||
Assert.IsTrue(_termSuccess);
|
||||
}
|
||||
|
||||
public void OnInitialization()
|
||||
{
|
||||
ThreadContextState.Current.Counter = 1234;
|
||||
_initSuccess = true;
|
||||
}
|
||||
|
||||
private object DoSomeWork(object state)
|
||||
{
|
||||
int counter = ThreadContextState.Current.Counter;
|
||||
_workItemSuccess = (1234 == counter);
|
||||
|
||||
ThreadContextState.Current.Counter = 1111;
|
||||
return 1;
|
||||
}
|
||||
|
||||
public void OnTermination()
|
||||
{
|
||||
int counter = ThreadContextState.Current.Counter;
|
||||
_termSuccess = (1111 == counter);
|
||||
}
|
||||
}
|
||||
|
||||
internal class ThreadContextState
|
||||
{
|
||||
// Each thread will have its own ThreadContextState object
|
||||
[ThreadStatic]
|
||||
private static ThreadContextState _threadContextState;
|
||||
|
||||
private int _counter = 0;
|
||||
|
||||
public int Counter
|
||||
{
|
||||
get { return _counter; }
|
||||
set { _counter = value; }
|
||||
}
|
||||
|
||||
// Static member so it can be used anywhere in code of the work item method
|
||||
public static ThreadContextState Current
|
||||
{
|
||||
get
|
||||
{
|
||||
// If the _threadContextState is null then it was not yet initialized
|
||||
// for this thread.
|
||||
if (null == _threadContextState)
|
||||
{
|
||||
// Create a ThreadContextState object
|
||||
_threadContextState = new ThreadContextState();
|
||||
}
|
||||
return _threadContextState;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user