#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2009 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
#if UNITTEST
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using NUnit.Framework;
using NMock2;
namespace Nuclex.Support.Scheduling {
/// Unit Test for the thread operation class
[TestFixture]
public class ThreadOperationTest {
#region class TestThreadOperation
/// Dummy operation used to run the unit tests
private class TestThreadOperation : ThreadOperation {
/// Initializes a dummy operation
public TestThreadOperation() { }
/// Initializes a dummy operation
/// Whether to use a ThreadPool thread.
public TestThreadOperation(bool useThreadPool) : base(useThreadPool) { }
/// Contains the payload to be executed in the background thread
protected override void Execute() { }
}
#endregion // class TestOperation
#region class FailingThreadOperation
/// Dummy operation used to run the unit tests
private class FailingThreadOperation : ThreadOperation {
/// Initializes a dummy operation
/// Whether to use a ThreadPool thread.
public FailingThreadOperation(bool useThreadPool) : base(useThreadPool) { }
/// Contains the payload to be executed in the background thread
protected override void Execute() {
throw new AbortedException("Hello World");
}
}
#endregion // class FailingThreadOperation
/// Verifies that the default constructor for a thread operation works
[Test]
public void TestDefaultConstructor() {
TestThreadOperation test = new TestThreadOperation();
Assert.IsFalse(test.Ended);
}
///
/// Verifies that the threaded operation can execute in a thread pool thread
///
[Test]
public void TestExecutionInThreadPool() {
TestThreadOperation test = new TestThreadOperation(true);
Assert.IsFalse(test.Ended);
test.Start();
test.Join();
Assert.IsTrue(test.Ended);
}
///
/// Verifies that the threaded operation can execute in an explicit thread
///
[Test]
public void TestExecutionInExplicitThread() {
TestThreadOperation test = new TestThreadOperation(false);
Assert.IsFalse(test.Ended);
test.Start();
test.Join();
Assert.IsTrue(test.Ended);
}
///
/// Verifies that the threaded operation forwards an exception that occurred in
/// a thread pool thread.
///
[Test, ExpectedException(typeof(AbortedException))]
public void TestForwardExceptionFromThreadPool() {
FailingThreadOperation test = new FailingThreadOperation(true);
Assert.IsFalse(test.Ended);
test.Start();
test.Join();
}
///
/// Verifies that the threaded operation forwards an exception that occurred in
/// an explicit thread.
///
[Test, ExpectedException(typeof(AbortedException))]
public void TestForwardExceptionFromExplicitThread() {
FailingThreadOperation test = new FailingThreadOperation(false);
Assert.IsFalse(test.Ended);
test.Start();
test.Join();
}
}
} // namespace Nuclex.Support.Scheduling
#endif // UNITTEST