January 13, 2006

Threading using the ThreadPool

Use something along the lines:
System.Threading.ThreadPool.QueueUserWorkItem(
new System.Threading.WaitCallback(MyThreadRoutine), stateObject);
where 'stateObject' is an object which is passed as a parameter to the thread routine 'MyThreadRoutine'
For a generic version look here

Using NUnit

Running NUnit on the command line

Run a text fixture using the GUI:
"C:\Program Files\NUnit 2.2\bin\nunit-gui.exe" /fixture:sometests.test.mytest /run

Run a text fixture using the Console:
"C:\Program Files\NUnit 2.2\bin\nunit-console.exe" C:\src\ssg\SrcUnitTests\bin\Debug\NUnitTests.dll /fixture :sometests.test.mytest /out:TestResult.txt /exclude:Manual /thread
This will exclude all tests or test fixtures marked as [Category("Manual")], output goes to Results.txt and /thread causes a separate thread to be created for running the tests.

Description of the NUnit test class attributes can be found here
Here is a NUnit skeleton class with the main attributes:
using NUnit.Framework;

[TestFixture]
public class SomeTester
{
 #region Setup/TearDown

 // This is invoked before each [TestFixture]
 // ie once only, before any tests are invoked
 [TestFixtureSetUp] 
 public void TestFixtureSetUp()
 {
 }

 // This is invoked after each [TestFixture]
 // ie once only, after all tests have been invoked
 [TestFixtureTearDown] 
 public void TestFixtureTearDown()
 {
 }

 // This is invoked before each [Test}
 [SetUp] 
 public void Setup()
 {
 }

 // This is invoked after each [Test}
 [TearDown] 
 public void TearDown()
 {
 }

 #endregion Setup/TearDown

 // Format of a Test method
 [Test]
 public void SomeTest()
 {
 }
}
Use these logging lines to generically log the beginning and end of a test using reflection
[Test]
public void SomeTest()
{
 log.Info("*** Beginning Test - " + System.Reflection.
MethodBase.GetCurrentMethod().ToString());
 log.Info("*** Ending Test - " + System.Reflection.
MethodBase.GetCurrentMethod().ToString());
}
alternatively use:
//Output test class and method name:
Debug.WriteLine("*** Beginning Test - " +
  System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString()
  + "." +
  System.Reflection.MethodBase.GetCurrentMethod().Name.ToString());
NUnit Exceptions Use the 'ExpectedException' attribute when the test is expected to result in an exception being thrown.
[Test]
[ExpectedException(typeof(System.Net.Sockets.SocketException))]
public void TestNoServer()
{
...
alternatively use can use the Assert.Throws() method:
Assert.Throws(typeof(System.Net.Sockets.SocketException),
  delegate
  {
     SomeMethodThatShouldThrowAnException();
  });

System.Convert Class

System.Convert Class - Useful for converting classes between the base types

Custom Configuration Handlers In .Net

Implementing 'ConfigurationSectionHandler' objects. Think I may have an error in this code but nothing to difficult to resolve In the '.config' file for the application 1. Specify the handler within the 'configsections' area 2. Add the section as a child node of the 'configuration' node
<configuration>
 <configSections>
...
  <section name="mysection"
type="mynamespace.mysection.StuffConfigurationSectionHandler, Stuff" /> </configSections> ... <mysection TransportStreamPacketSize="188"> <download MaxMessageRetries="5" MaxWaitPeriod="5000"/> <upload MaxMessageRetries="5" MaxWaitPeriod="5000"> </upload> </mysection> ... </configuration>
Then write a 'ConfigurationSectionHandler' class for it This handler must be invoked somewhere for it to do something
System.Configuration.ConfigurationSettings.GetConfig("mysection");   
using System.Configuration;

// Handles the DescriptorFactory configuration
section of the application configuration file. public class StuffConfigurationSectionHandler
: IConfigurationSectionHandler { #region Logging //static readonly Logging.ILog log = ... #endregion Logging #region Private Data private static object threadLock = new object(); private static bool configurationComplete = false; #endregion Private Data #region IConfigurationSectionHandler Members // This method is called as a result of a call to // ConfigurationSettings.GetConfig(). // It sets the configuration data in // the PsiSiComms configuration object public object Create(object parent,
object configContext, XmlNode section) { lock(threadLock) { if (!configurationComplete) { log.Info("Configuring XXX."); RetrieveConfiguration(section); configurationComplete = true; log.Info("PsiSiComms configuration complete."); } } return null; } #region Private Helper Methods private void RetrieveConfiguration(XmlNode parent) { XmlNode node; XmlNode uploadNode = parent.SelectSingleNode("./upload"); if (!ReferenceEquals(uploadNode, null)) { node = uploadNode.Attributes.GetNamedItem
("MaxMessageRetries"); if (!ReferenceEquals(node, null)) { MyXXXConfig.Upload.MaxMessageRetries
= int.Parse(node.Value); } node = uploadNode.Attributes.GetNamedItem
("MaxWaitPeriod"); if (!ReferenceEquals(node, null)) { MyXXXConfig.Upload.MaxWaitPeriod
= int.Parse(node.Value); } RetrieveUploadTablesConfiguration(uploadNode); } } #endregion Private Helper Methods #endregion }