Permalink
Browse files

Now testing single threaded executor cancelling using lock/monitor.en…

…ter/exit.
  • Loading branch information...
1 parent 80915dd commit 8349d28f971c862453cfe7a941b1a276dd0d55bb @magwo committed Aug 22, 2011
View
@@ -47,6 +47,8 @@
<Compile Include="test\ImmediateTester.cs" />
<Compile Include="test\SingleThreadTester.cs" />
<Compile Include="src\IThreadFactory.cs" />
+ <Compile Include="src\TaskCancelledException.cs" />
+ <Compile Include="test\TestingHelpers.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
@@ -81,7 +81,7 @@ public Future<T> Submit<T>(ICallable<T> callable)
{
lock(locker) {
if(shutdown) {
- throw new InvalidOperationException("May not submit tasks after shutting down executor.");
+ throw new InvalidOperationException("May not submit tasks after shutting down executor. Create a new executor.");
}
Future<T> future = new Future<T>();
WorkItem<T> task = new WorkItem<T>(callable, future);
@@ -0,0 +1,9 @@
+using System;
+namespace Executors
+{
+ public class TaskCancelledException : Exception
+ {
+ public TaskCancelledException(string message) : base(message) { }
+ }
+}
+
View
@@ -44,7 +44,7 @@ public void Cancel(string reason)
if(future.IsDone) {
throw new InvalidOperationException("Can not cancel a future that is done.");
}
- future.SetException(new ExecutionException(new Exception("Task was cancelled due to: " + reason)));
+ future.SetException(new ExecutionException(new TaskCancelledException("Task was cancelled due to: " + reason)));
future.SetDone();
}
}
View
@@ -14,46 +14,7 @@ public abstract class CommonTester
}
public abstract IExecutor CreateDefaultExecutor(ShutdownMode shutdownMode);
- class SummationTask : ICallable<int>
- {
- int a, b;
- public SummationTask(int a, int b)
- {
- this.a = a;
- this.b = b;
- }
- public int Call()
- {
- return a + b;
- }
- }
-
- class DummyException : Exception
- {
- }
- class ExceptionThrowingTask : ICallable<int>
- {
- public int Call()
- {
- throw new DummyException();
- }
- }
-
- class SleepingTask : ICallable<int>
- {
- int sleepDuration;
-
- public SleepingTask(int sleepDuration)
- {
- this.sleepDuration = sleepDuration;
- }
-
- public int Call()
- {
- Thread.Sleep(sleepDuration);
- return 0;
- }
- }
+
[Test]
View
@@ -1,6 +1,8 @@
using System;
using NUnit.Framework;
using Executors;
+using System.Collections.Generic;
+using System.Threading;
namespace Executors.Test
{
[TestFixture]
@@ -9,6 +11,36 @@ public class SingleThreadTester : CommonTester
public override IExecutor CreateDefaultExecutor(ShutdownMode shutdownMode) {
return new SingleThreadExecutor(shutdownMode);
}
+
+
+ [Test]
+ public void ShouldCancelTasksOnShutdown() {
+ var executor = CreateDefaultExecutor(ShutdownMode.CancelQueuedTasks);
+ var tasks = new List<WaitingTask>();
+ var futures = new List<Future<int>>();
+ for(int i=0; i<10; i++) {
+ WaitingTask task = new WaitingTask();
+ tasks.Add(task);
+ if(i == 0) {
+ Monitor.Enter(task);
+ }
+ futures.Add(executor.Submit(task));
+ }
+ executor.Shutdown();
+ Monitor.Exit(tasks[0]);
+
+ while(!executor.IsShutdown()) { }
+ Assert.AreEqual(0, futures[0].GetResult());
+ foreach(Future<int> future in futures.GetRange(1, 9)) {
+ Assert.IsTrue(future.IsDone);
+ try {
+ Assert.AreEqual(0, future.GetResult());
+ Assert.Fail(); // Should not reach this
+ } catch(ExecutionException e) {
+ Assert.IsInstanceOfType(typeof(TaskCancelledException), e.delayedException);
+ }
+ }
+ }
}
}
View
@@ -0,0 +1,59 @@
+using System;
+using System.Threading;
+namespace Executors.Test
+{
+
+ public class SummationTask : ICallable<int>
+ {
+ int a, b;
+ public SummationTask(int a, int b)
+ {
+ this.a = a;
+ this.b = b;
+ }
+ public int Call()
+ {
+ return a + b;
+ }
+ }
+
+ public class DummyException : Exception
+ {
+ }
+ public class ExceptionThrowingTask : ICallable<int>
+ {
+ public int Call()
+ {
+ throw new DummyException();
+ }
+ }
+
+ public class SleepingTask : ICallable<int>
+ {
+ int sleepDuration;
+
+ public SleepingTask(int sleepDuration)
+ {
+ this.sleepDuration = sleepDuration;
+ }
+
+ public int Call()
+ {
+ Thread.Sleep(sleepDuration);
+ return 0;
+ }
+ }
+
+
+ public class WaitingTask : ICallable<int>
+ {
+
+ public int Call() {
+ lock(this) {
+
+ }
+ return 0;
+ }
+ }
+}
+

0 comments on commit 8349d28

Please sign in to comment.