Eliminated warnings that still came up when compiling with the mono C# compiler; all classes in the Nuclex.Support.Scheduling namespace now have 100% test coverage
git-svn-id: file:///srv/devel/repo-conversion/nusu@103 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
00caebf7e9
commit
88b51ef0fa
|
@ -156,15 +156,26 @@
|
||||||
<DependentUpon>PluginRepository.cs</DependentUpon>
|
<DependentUpon>PluginRepository.cs</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="Source\Scheduling\AbortedException.cs" />
|
<Compile Include="Source\Scheduling\AbortedException.cs" />
|
||||||
<Compile Include="Source\Scheduling\AbortedException.Test.cs" />
|
<Compile Include="Source\Scheduling\AbortedException.Test.cs">
|
||||||
|
<DependentUpon>AbortedException.cs</DependentUpon>
|
||||||
|
</Compile>
|
||||||
<Compile Include="Source\Scheduling\IAbortable.cs" />
|
<Compile Include="Source\Scheduling\IAbortable.cs" />
|
||||||
<Compile Include="Source\Scheduling\Operation.cs" />
|
<Compile Include="Source\Scheduling\Operation.cs" />
|
||||||
|
<Compile Include="Source\Scheduling\Operation.Test.cs">
|
||||||
|
<DependentUpon>Operation.cs</DependentUpon>
|
||||||
|
</Compile>
|
||||||
<Compile Include="Source\Scheduling\OperationQueue.cs" />
|
<Compile Include="Source\Scheduling\OperationQueue.cs" />
|
||||||
<Compile Include="Source\Scheduling\OperationQueue.Test.cs">
|
<Compile Include="Source\Scheduling\OperationQueue.Test.cs">
|
||||||
<DependentUpon>OperationQueue.cs</DependentUpon>
|
<DependentUpon>OperationQueue.cs</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="Source\Scheduling\ThreadCallbackOperation.cs" />
|
<Compile Include="Source\Scheduling\ThreadCallbackOperation.cs" />
|
||||||
|
<Compile Include="Source\Scheduling\ThreadCallbackOperation.Test.cs">
|
||||||
|
<DependentUpon>ThreadCallbackOperation.cs</DependentUpon>
|
||||||
|
</Compile>
|
||||||
<Compile Include="Source\Scheduling\ThreadOperation.cs" />
|
<Compile Include="Source\Scheduling\ThreadOperation.cs" />
|
||||||
|
<Compile Include="Source\Scheduling\ThreadOperation.Test.cs">
|
||||||
|
<DependentUpon>ThreadOperation.cs</DependentUpon>
|
||||||
|
</Compile>
|
||||||
<Compile Include="Source\Shared.cs" />
|
<Compile Include="Source\Shared.cs" />
|
||||||
<Compile Include="Source\Shared.Test.cs">
|
<Compile Include="Source\Shared.Test.cs">
|
||||||
<DependentUpon>Shared.cs</DependentUpon>
|
<DependentUpon>Shared.cs</DependentUpon>
|
||||||
|
|
|
@ -145,7 +145,7 @@ namespace Nuclex.Support.Collections {
|
||||||
|
|
||||||
if(!(testCollection as ICollection).IsSynchronized) {
|
if(!(testCollection as ICollection).IsSynchronized) {
|
||||||
lock((testCollection as ICollection).SyncRoot) {
|
lock((testCollection as ICollection).SyncRoot) {
|
||||||
int count = testCollection.Count;
|
Assert.AreEqual(0, testCollection.Count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -180,6 +180,7 @@ namespace Nuclex.Support.Collections {
|
||||||
ReadOnlyDictionary<int, string> testDictionary = makeReadOnly(numbers);
|
ReadOnlyDictionary<int, string> testDictionary = makeReadOnly(numbers);
|
||||||
|
|
||||||
string numberName = testDictionary[24];
|
string numberName = testDictionary[24];
|
||||||
|
Console.WriteLine(numberName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -423,7 +424,7 @@ namespace Nuclex.Support.Collections {
|
||||||
|
|
||||||
if(!(testDictionary as ICollection).IsSynchronized) {
|
if(!(testDictionary as ICollection).IsSynchronized) {
|
||||||
lock((testDictionary as ICollection).SyncRoot) {
|
lock((testDictionary as ICollection).SyncRoot) {
|
||||||
int count = testDictionary.Count;
|
Assert.AreEqual(numbers.Count, testDictionary.Count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -345,7 +345,7 @@ namespace Nuclex.Support.Collections {
|
||||||
|
|
||||||
if(!(testList as ICollection).IsSynchronized) {
|
if(!(testList as ICollection).IsSynchronized) {
|
||||||
lock((testList as ICollection).SyncRoot) {
|
lock((testList as ICollection).SyncRoot) {
|
||||||
int count = testList.Count;
|
Assert.AreEqual(0, testList.Count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -241,6 +241,7 @@ namespace Nuclex.Support.Collections {
|
||||||
[Test, ExpectedException(typeof(NotSupportedException))]
|
[Test, ExpectedException(typeof(NotSupportedException))]
|
||||||
public void TestThrowOnRetrievePosition() {
|
public void TestThrowOnRetrievePosition() {
|
||||||
long position = new RingMemoryStream(10).Position;
|
long position = new RingMemoryStream(10).Position;
|
||||||
|
Console.WriteLine(position.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -439,7 +439,7 @@ namespace Nuclex.Support.Collections {
|
||||||
|
|
||||||
if(!(testCollection as ICollection).IsSynchronized) {
|
if(!(testCollection as ICollection).IsSynchronized) {
|
||||||
lock((testCollection as ICollection).SyncRoot) {
|
lock((testCollection as ICollection).SyncRoot) {
|
||||||
int count = testCollection.Count;
|
Assert.AreEqual(0, testCollection.Count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,6 +109,7 @@ namespace Nuclex.Support.Licensing {
|
||||||
public void TestGetByIndexerWithInvalidIndex() {
|
public void TestGetByIndexerWithInvalidIndex() {
|
||||||
LicenseKey key = new LicenseKey();
|
LicenseKey key = new LicenseKey();
|
||||||
int indexMinusOne = key[-1];
|
int indexMinusOne = key[-1];
|
||||||
|
Console.WriteLine(indexMinusOne.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -123,6 +123,7 @@ namespace Nuclex.Support.Parsing {
|
||||||
[Test]
|
[Test]
|
||||||
public void TestStringConstructorWithUnfinishedAssignment() {
|
public void TestStringConstructorWithUnfinishedAssignment() {
|
||||||
CommandLineParser parser = new CommandLineParser("--hello= --world=");
|
CommandLineParser parser = new CommandLineParser("--hello= --world=");
|
||||||
|
Assert.AreEqual(0, parser.Values.Count);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,7 @@ namespace Nuclex.Support.Scheduling {
|
||||||
AbortedException testException = new AbortedException();
|
AbortedException testException = new AbortedException();
|
||||||
|
|
||||||
string testExceptionString = testException.ToString();
|
string testExceptionString = testException.ToString();
|
||||||
|
Assert.IsNotNull(testExceptionString);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
69
Source/Scheduling/Operation.Test.cs
Normal file
69
Source/Scheduling/Operation.Test.cs
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
#region CPL License
|
||||||
|
/*
|
||||||
|
Nuclex Framework
|
||||||
|
Copyright (C) 2002-2008 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
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Runtime.Serialization.Formatters.Binary;
|
||||||
|
|
||||||
|
#if UNITTEST
|
||||||
|
|
||||||
|
using NUnit.Framework;
|
||||||
|
using NMock2;
|
||||||
|
|
||||||
|
namespace Nuclex.Support.Scheduling {
|
||||||
|
|
||||||
|
/// <summary>Unit Test for the operation class</summary>
|
||||||
|
[TestFixture]
|
||||||
|
public class OperationTest {
|
||||||
|
|
||||||
|
#region class TestOperation
|
||||||
|
|
||||||
|
/// <summary>Dummy operation used to run the unit tests</summary>
|
||||||
|
private class TestOperation : Operation {
|
||||||
|
|
||||||
|
/// <summary>Launches the background operation</summary>
|
||||||
|
public override void Start() {
|
||||||
|
// This could become a race condition of this code would be used in a fashion
|
||||||
|
// different than what current unit tests do with it
|
||||||
|
if(!base.Ended) {
|
||||||
|
OnAsyncEnded();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion // class TestOperation
|
||||||
|
|
||||||
|
/// <summary>Tests whether operations can be started</summary>
|
||||||
|
[Test]
|
||||||
|
public void TestOperationStarting() {
|
||||||
|
TestOperation myOperation = new TestOperation();
|
||||||
|
|
||||||
|
Assert.IsFalse(myOperation.Ended);
|
||||||
|
myOperation.Start();
|
||||||
|
Assert.IsTrue(myOperation.Ended);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Nuclex.Support.Scheduling
|
||||||
|
|
||||||
|
#endif // UNITTEST
|
|
@ -209,6 +209,133 @@ namespace Nuclex.Support.Scheduling {
|
||||||
this.mockery.VerifyAllExpectationsHaveBeenMet();
|
this.mockery.VerifyAllExpectationsHaveBeenMet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validates that the queue executes operations sequentially and honors the weights
|
||||||
|
/// assigned to the individual operations
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestWeightedSequentialExecution() {
|
||||||
|
TestOperation operation1 = new TestOperation();
|
||||||
|
TestOperation operation2 = new TestOperation();
|
||||||
|
|
||||||
|
OperationQueue<TestOperation> testQueueOperation =
|
||||||
|
new OperationQueue<TestOperation>(
|
||||||
|
new WeightedTransaction<TestOperation>[] {
|
||||||
|
new WeightedTransaction<TestOperation>(operation1, 0.5f),
|
||||||
|
new WeightedTransaction<TestOperation>(operation2, 2.0f)
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
IOperationQueueSubscriber mockedSubscriber = mockSubscriber(testQueueOperation);
|
||||||
|
|
||||||
|
testQueueOperation.Start();
|
||||||
|
|
||||||
|
Expect.Once.On(mockedSubscriber).
|
||||||
|
Method("ProgressChanged").
|
||||||
|
With(
|
||||||
|
new Matcher[] {
|
||||||
|
new NMock2.Matchers.TypeMatcher(typeof(OperationQueue<TestOperation>)),
|
||||||
|
new ProgressUpdateEventArgsMatcher(new ProgressReportEventArgs(0.1f))
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
operation1.ChangeProgress(0.5f);
|
||||||
|
|
||||||
|
Expect.Once.On(mockedSubscriber).
|
||||||
|
Method("ProgressChanged").
|
||||||
|
With(
|
||||||
|
new Matcher[] {
|
||||||
|
new NMock2.Matchers.TypeMatcher(typeof(OperationQueue<TestOperation>)),
|
||||||
|
new ProgressUpdateEventArgsMatcher(new ProgressReportEventArgs(0.2f))
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
operation1.SetEnded();
|
||||||
|
|
||||||
|
this.mockery.VerifyAllExpectationsHaveBeenMet();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validates that the operation queue propagates the ended event once all contained
|
||||||
|
/// operations have completed
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestEndPropagation() {
|
||||||
|
TestOperation operation1 = new TestOperation();
|
||||||
|
TestOperation operation2 = new TestOperation();
|
||||||
|
|
||||||
|
OperationQueue<TestOperation> testQueueOperation =
|
||||||
|
new OperationQueue<TestOperation>(
|
||||||
|
new TestOperation[] {
|
||||||
|
operation1,
|
||||||
|
operation2
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
testQueueOperation.Start();
|
||||||
|
|
||||||
|
Assert.IsFalse(testQueueOperation.Ended);
|
||||||
|
operation1.SetEnded();
|
||||||
|
Assert.IsFalse(testQueueOperation.Ended);
|
||||||
|
operation2.SetEnded();
|
||||||
|
Assert.IsTrue(testQueueOperation.Ended);
|
||||||
|
|
||||||
|
testQueueOperation.Join();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validates that the operation queue delivers an exception occuring in one of the
|
||||||
|
/// contained operations to the operation queue joiner
|
||||||
|
/// </summary>
|
||||||
|
[Test, ExpectedException(typeof(AbortedException))]
|
||||||
|
public void TestExceptionPropagation() {
|
||||||
|
TestOperation operation1 = new TestOperation();
|
||||||
|
TestOperation operation2 = new TestOperation();
|
||||||
|
|
||||||
|
OperationQueue<TestOperation> testQueueOperation =
|
||||||
|
new OperationQueue<TestOperation>(
|
||||||
|
new TestOperation[] {
|
||||||
|
operation1,
|
||||||
|
operation2
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
testQueueOperation.Start();
|
||||||
|
|
||||||
|
Assert.IsFalse(testQueueOperation.Ended);
|
||||||
|
operation1.SetEnded();
|
||||||
|
Assert.IsFalse(testQueueOperation.Ended);
|
||||||
|
operation2.SetEnded(new AbortedException("Hello World"));
|
||||||
|
|
||||||
|
testQueueOperation.Join();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ensures that the operation queue transparently wraps the child operations in
|
||||||
|
/// an observation wrapper that is not visible to an outside user
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestTransparentWrapping() {
|
||||||
|
WeightedTransaction<TestOperation> operation1 = new WeightedTransaction<TestOperation>(
|
||||||
|
new TestOperation()
|
||||||
|
);
|
||||||
|
WeightedTransaction<TestOperation> operation2 = new WeightedTransaction<TestOperation>(
|
||||||
|
new TestOperation()
|
||||||
|
);
|
||||||
|
|
||||||
|
OperationQueue<TestOperation> testQueueOperation =
|
||||||
|
new OperationQueue<TestOperation>(
|
||||||
|
new WeightedTransaction<TestOperation>[] {
|
||||||
|
operation1,
|
||||||
|
operation2
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Order is important due to sequential execution!
|
||||||
|
Assert.AreSame(operation1, testQueueOperation.Children[0]);
|
||||||
|
Assert.AreSame(operation2, testQueueOperation.Children[1]);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Mocks a subscriber for the events of an operation</summary>
|
/// <summary>Mocks a subscriber for the events of an operation</summary>
|
||||||
/// <param name="operation">Operation to mock an event subscriber for</param>
|
/// <param name="operation">Operation to mock an event subscriber for</param>
|
||||||
/// <returns>The mocked event subscriber</returns>
|
/// <returns>The mocked event subscriber</returns>
|
||||||
|
|
150
Source/Scheduling/ThreadCallbackOperation.Test.cs
Normal file
150
Source/Scheduling/ThreadCallbackOperation.Test.cs
Normal file
|
@ -0,0 +1,150 @@
|
||||||
|
#region CPL License
|
||||||
|
/*
|
||||||
|
Nuclex Framework
|
||||||
|
Copyright (C) 2002-2008 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
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Runtime.Serialization.Formatters.Binary;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
#if UNITTEST
|
||||||
|
|
||||||
|
using NUnit.Framework;
|
||||||
|
using NMock2;
|
||||||
|
|
||||||
|
namespace Nuclex.Support.Scheduling {
|
||||||
|
|
||||||
|
/// <summary>Unit Test for the thread callback operation class</summary>
|
||||||
|
[TestFixture]
|
||||||
|
public class ThreadCallbackOperationTest {
|
||||||
|
|
||||||
|
#region class TestThreadOperation
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Provides a test callback for unit testing the thread callback operation
|
||||||
|
/// </summary>
|
||||||
|
private class TestCallbackProvider {
|
||||||
|
|
||||||
|
/// <summary>Method that can be invoked as a callback</summary>
|
||||||
|
public void Callback() {
|
||||||
|
this.called = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Whether the callback has been called</summary>
|
||||||
|
public bool Called {
|
||||||
|
get { return this.called; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Set to true when the callback has been called</summary>
|
||||||
|
private bool called;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion // class TestOperation
|
||||||
|
|
||||||
|
/// <summary>Verifies that the default constructor for a thread operation works</summary>
|
||||||
|
[Test]
|
||||||
|
public void TestDefaultConstructor() {
|
||||||
|
ThreadCallbackOperation test = new ThreadCallbackOperation(
|
||||||
|
new ThreadStart(errorCallback)
|
||||||
|
);
|
||||||
|
Assert.IsFalse(test.Ended);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that the threaded operation can execute in a thread pool thread
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestExecutionInThreadPool() {
|
||||||
|
TestCallbackProvider callbackProvider = new TestCallbackProvider();
|
||||||
|
|
||||||
|
ThreadCallbackOperation test = new ThreadCallbackOperation(
|
||||||
|
new ThreadStart(callbackProvider.Callback), true
|
||||||
|
);
|
||||||
|
|
||||||
|
Assert.IsFalse(test.Ended);
|
||||||
|
Assert.IsFalse(callbackProvider.Called);
|
||||||
|
test.Start();
|
||||||
|
test.Join();
|
||||||
|
Assert.IsTrue(test.Ended);
|
||||||
|
Assert.IsTrue(callbackProvider.Called);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that the threaded operation can execute in an explicit thread
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestExecutionInExplicitThread() {
|
||||||
|
TestCallbackProvider callbackProvider = new TestCallbackProvider();
|
||||||
|
|
||||||
|
ThreadCallbackOperation test = new ThreadCallbackOperation(
|
||||||
|
new ThreadStart(callbackProvider.Callback), false
|
||||||
|
);
|
||||||
|
|
||||||
|
Assert.IsFalse(test.Ended);
|
||||||
|
Assert.IsFalse(callbackProvider.Called);
|
||||||
|
test.Start();
|
||||||
|
test.Join();
|
||||||
|
Assert.IsTrue(test.Ended);
|
||||||
|
Assert.IsTrue(callbackProvider.Called);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that the threaded operation forwards an exception that occurred in
|
||||||
|
/// a thread pool thread.
|
||||||
|
/// </summary>
|
||||||
|
[Test, ExpectedException(typeof(AbortedException))]
|
||||||
|
public void TestForwardExceptionFromThreadPool() {
|
||||||
|
ThreadCallbackOperation test = new ThreadCallbackOperation(
|
||||||
|
new ThreadStart(errorCallback), false
|
||||||
|
);
|
||||||
|
|
||||||
|
Assert.IsFalse(test.Ended);
|
||||||
|
test.Start();
|
||||||
|
test.Join();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that the threaded operation forwards an exception that occurred in
|
||||||
|
/// an explicit thread.
|
||||||
|
/// </summary>
|
||||||
|
[Test, ExpectedException(typeof(AbortedException))]
|
||||||
|
public void TestForwardExceptionFromExplicitThread() {
|
||||||
|
ThreadCallbackOperation test = new ThreadCallbackOperation(
|
||||||
|
new ThreadStart(errorCallback), false
|
||||||
|
);
|
||||||
|
|
||||||
|
Assert.IsFalse(test.Ended);
|
||||||
|
test.Start();
|
||||||
|
test.Join();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Callback which throws an exception to simulate an error during callback execution
|
||||||
|
/// </summary>
|
||||||
|
private static void errorCallback() {
|
||||||
|
throw new AbortedException("Hello World");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Nuclex.Support.Scheduling
|
||||||
|
|
||||||
|
#endif // UNITTEST
|
135
Source/Scheduling/ThreadOperation.Test.cs
Normal file
135
Source/Scheduling/ThreadOperation.Test.cs
Normal file
|
@ -0,0 +1,135 @@
|
||||||
|
#region CPL License
|
||||||
|
/*
|
||||||
|
Nuclex Framework
|
||||||
|
Copyright (C) 2002-2008 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
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Runtime.Serialization.Formatters.Binary;
|
||||||
|
|
||||||
|
#if UNITTEST
|
||||||
|
|
||||||
|
using NUnit.Framework;
|
||||||
|
using NMock2;
|
||||||
|
|
||||||
|
namespace Nuclex.Support.Scheduling {
|
||||||
|
|
||||||
|
/// <summary>Unit Test for the thread operation class</summary>
|
||||||
|
[TestFixture]
|
||||||
|
public class ThreadOperationTest {
|
||||||
|
|
||||||
|
#region class TestThreadOperation
|
||||||
|
|
||||||
|
/// <summary>Dummy operation used to run the unit tests</summary>
|
||||||
|
private class TestThreadOperation : ThreadOperation {
|
||||||
|
|
||||||
|
/// <summary>Initializes a dummy operation</summary>
|
||||||
|
public TestThreadOperation() { }
|
||||||
|
|
||||||
|
/// <summary>Initializes a dummy operation</summary>
|
||||||
|
/// <param name="useThreadPool">Whether to use a ThreadPool thread.</param>
|
||||||
|
public TestThreadOperation(bool useThreadPool) : base(useThreadPool) { }
|
||||||
|
|
||||||
|
/// <summary>Contains the payload to be executed in the background thread</summary>
|
||||||
|
protected override void Execute() { }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion // class TestOperation
|
||||||
|
|
||||||
|
#region class FailingThreadOperation
|
||||||
|
|
||||||
|
/// <summary>Dummy operation used to run the unit tests</summary>
|
||||||
|
private class FailingThreadOperation : ThreadOperation {
|
||||||
|
|
||||||
|
/// <summary>Initializes a dummy operation</summary>
|
||||||
|
/// <param name="useThreadPool">Whether to use a ThreadPool thread.</param>
|
||||||
|
public FailingThreadOperation(bool useThreadPool) : base(useThreadPool) { }
|
||||||
|
|
||||||
|
/// <summary>Contains the payload to be executed in the background thread</summary>
|
||||||
|
protected override void Execute() {
|
||||||
|
throw new AbortedException("Hello World");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion // class FailingThreadOperation
|
||||||
|
|
||||||
|
/// <summary>Verifies that the default constructor for a thread operation works</summary>
|
||||||
|
[Test]
|
||||||
|
public void TestDefaultConstructor() {
|
||||||
|
TestThreadOperation test = new TestThreadOperation();
|
||||||
|
Assert.IsFalse(test.Ended);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that the threaded operation can execute in a thread pool thread
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestExecutionInThreadPool() {
|
||||||
|
TestThreadOperation test = new TestThreadOperation(true);
|
||||||
|
Assert.IsFalse(test.Ended);
|
||||||
|
test.Start();
|
||||||
|
test.Join();
|
||||||
|
Assert.IsTrue(test.Ended);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that the threaded operation can execute in an explicit thread
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestExecutionInExplicitThread() {
|
||||||
|
TestThreadOperation test = new TestThreadOperation(false);
|
||||||
|
Assert.IsFalse(test.Ended);
|
||||||
|
test.Start();
|
||||||
|
test.Join();
|
||||||
|
Assert.IsTrue(test.Ended);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that the threaded operation forwards an exception that occurred in
|
||||||
|
/// a thread pool thread.
|
||||||
|
/// </summary>
|
||||||
|
[Test, ExpectedException(typeof(AbortedException))]
|
||||||
|
public void TestForwardExceptionFromThreadPool() {
|
||||||
|
FailingThreadOperation test = new FailingThreadOperation(true);
|
||||||
|
|
||||||
|
Assert.IsFalse(test.Ended);
|
||||||
|
test.Start();
|
||||||
|
test.Join();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that the threaded operation forwards an exception that occurred in
|
||||||
|
/// an explicit thread.
|
||||||
|
/// </summary>
|
||||||
|
[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
|
|
@ -20,6 +20,7 @@ License along with this library
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
namespace Nuclex.Support.Scheduling {
|
namespace Nuclex.Support.Scheduling {
|
||||||
|
@ -50,6 +51,11 @@ namespace Nuclex.Support.Scheduling {
|
||||||
|
|
||||||
/// <summary>Launches the background operation</summary>
|
/// <summary>Launches the background operation</summary>
|
||||||
public override void Start() {
|
public override void Start() {
|
||||||
|
Debug.Assert(
|
||||||
|
!Ended,
|
||||||
|
"Tried to Start an Operation again that has already ended",
|
||||||
|
"Operations cannot be re-run"
|
||||||
|
);
|
||||||
if(useThreadPool) {
|
if(useThreadPool) {
|
||||||
ThreadPool.QueueUserWorkItem(callMethod);
|
ThreadPool.QueueUserWorkItem(callMethod);
|
||||||
} else {
|
} else {
|
||||||
|
@ -68,6 +74,11 @@ namespace Nuclex.Support.Scheduling {
|
||||||
private void callMethod(object state) {
|
private void callMethod(object state) {
|
||||||
try {
|
try {
|
||||||
Execute();
|
Execute();
|
||||||
|
Debug.Assert(
|
||||||
|
!Ended,
|
||||||
|
"Operation unexpectedly ended during Execute()",
|
||||||
|
"Do not call OnAsyncEnded() yourself when deriving from ThreadOperation"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
catch(Exception exception) {
|
catch(Exception exception) {
|
||||||
this.exception = exception;
|
this.exception = exception;
|
||||||
|
|
|
@ -311,6 +311,9 @@ namespace Nuclex.Support.Tracking {
|
||||||
public void TestSoleEndedTransaction() {
|
public void TestSoleEndedTransaction() {
|
||||||
using(ProgressTracker tracker = new ProgressTracker()) {
|
using(ProgressTracker tracker = new ProgressTracker()) {
|
||||||
IProgressTrackerSubscriber mockedSubscriber = mockSubscriber(tracker);
|
IProgressTrackerSubscriber mockedSubscriber = mockSubscriber(tracker);
|
||||||
|
|
||||||
|
Expect.Never.On(mockedSubscriber).Method("IdleStateChanged").WithAnyArguments();
|
||||||
|
Expect.Never.On(mockedSubscriber).Method("ProgressChanged").WithAnyArguments();
|
||||||
|
|
||||||
tracker.Track(Transaction.EndedDummy);
|
tracker.Track(Transaction.EndedDummy);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user