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:
Markus Ewald 2008-12-07 19:36:29 +00:00
parent 00caebf7e9
commit 88b51ef0fa
15 changed files with 516 additions and 5 deletions

View File

@ -156,15 +156,26 @@
<DependentUpon>PluginRepository.cs</DependentUpon>
</Compile>
<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\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.Test.cs">
<DependentUpon>OperationQueue.cs</DependentUpon>
</Compile>
<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.Test.cs">
<DependentUpon>ThreadOperation.cs</DependentUpon>
</Compile>
<Compile Include="Source\Shared.cs" />
<Compile Include="Source\Shared.Test.cs">
<DependentUpon>Shared.cs</DependentUpon>

View File

@ -145,7 +145,7 @@ namespace Nuclex.Support.Collections {
if(!(testCollection as ICollection).IsSynchronized) {
lock((testCollection as ICollection).SyncRoot) {
int count = testCollection.Count;
Assert.AreEqual(0, testCollection.Count);
}
}
}

View File

@ -180,6 +180,7 @@ namespace Nuclex.Support.Collections {
ReadOnlyDictionary<int, string> testDictionary = makeReadOnly(numbers);
string numberName = testDictionary[24];
Console.WriteLine(numberName);
}
/// <summary>
@ -423,7 +424,7 @@ namespace Nuclex.Support.Collections {
if(!(testDictionary as ICollection).IsSynchronized) {
lock((testDictionary as ICollection).SyncRoot) {
int count = testDictionary.Count;
Assert.AreEqual(numbers.Count, testDictionary.Count);
}
}
}

View File

@ -345,7 +345,7 @@ namespace Nuclex.Support.Collections {
if(!(testList as ICollection).IsSynchronized) {
lock((testList as ICollection).SyncRoot) {
int count = testList.Count;
Assert.AreEqual(0, testList.Count);
}
}
}

View File

@ -241,6 +241,7 @@ namespace Nuclex.Support.Collections {
[Test, ExpectedException(typeof(NotSupportedException))]
public void TestThrowOnRetrievePosition() {
long position = new RingMemoryStream(10).Position;
Console.WriteLine(position.ToString());
}
/// <summary>

View File

@ -439,7 +439,7 @@ namespace Nuclex.Support.Collections {
if(!(testCollection as ICollection).IsSynchronized) {
lock((testCollection as ICollection).SyncRoot) {
int count = testCollection.Count;
Assert.AreEqual(0, testCollection.Count);
}
}
}

View File

@ -109,6 +109,7 @@ namespace Nuclex.Support.Licensing {
public void TestGetByIndexerWithInvalidIndex() {
LicenseKey key = new LicenseKey();
int indexMinusOne = key[-1];
Console.WriteLine(indexMinusOne.ToString());
}
/// <summary>

View File

@ -123,6 +123,7 @@ namespace Nuclex.Support.Parsing {
[Test]
public void TestStringConstructorWithUnfinishedAssignment() {
CommandLineParser parser = new CommandLineParser("--hello= --world=");
Assert.AreEqual(0, parser.Values.Count);
}
}

View File

@ -42,6 +42,7 @@ namespace Nuclex.Support.Scheduling {
AbortedException testException = new AbortedException();
string testExceptionString = testException.ToString();
Assert.IsNotNull(testExceptionString);
}
/// <summary>

View 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

View File

@ -209,6 +209,133 @@ namespace Nuclex.Support.Scheduling {
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>
/// <param name="operation">Operation to mock an event subscriber for</param>
/// <returns>The mocked event subscriber</returns>

View 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

View 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

View File

@ -20,6 +20,7 @@ License along with this library
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
namespace Nuclex.Support.Scheduling {
@ -50,6 +51,11 @@ namespace Nuclex.Support.Scheduling {
/// <summary>Launches the background operation</summary>
public override void Start() {
Debug.Assert(
!Ended,
"Tried to Start an Operation again that has already ended",
"Operations cannot be re-run"
);
if(useThreadPool) {
ThreadPool.QueueUserWorkItem(callMethod);
} else {
@ -68,6 +74,11 @@ namespace Nuclex.Support.Scheduling {
private void callMethod(object state) {
try {
Execute();
Debug.Assert(
!Ended,
"Operation unexpectedly ended during Execute()",
"Do not call OnAsyncEnded() yourself when deriving from ThreadOperation"
);
}
catch(Exception exception) {
this.exception = exception;

View File

@ -312,6 +312,9 @@ namespace Nuclex.Support.Tracking {
using(ProgressTracker tracker = new ProgressTracker()) {
IProgressTrackerSubscriber mockedSubscriber = mockSubscriber(tracker);
Expect.Never.On(mockedSubscriber).Method("IdleStateChanged").WithAnyArguments();
Expect.Never.On(mockedSubscriber).Method("ProgressChanged").WithAnyArguments();
tracker.Track(Transaction.EndedDummy);
}