Skip to content

Commit

Permalink
Canceled task will throw OperationCanceledException instead of Invali…
Browse files Browse the repository at this point in the history
…dOperationException (Saltarelle#398)
  • Loading branch information
erik-kallen authored and n9 committed Aug 10, 2015
1 parent e74d35b commit e53717e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Runtime/CoreLib.Script/Task.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ ss.initClass(ss_Task, ss, {
case 5:
return this._result;
case 6:
throw new ss_InvalidOperationException('Task was cancelled.');
var ex = new ss_TaskCanceledException(null, this);
throw await ? ex : new ss_AggregateException(null, [ex]);
case 7:
throw await ? this.exception.innerExceptions[0] : this.exception;
default:
Expand Down
44 changes: 44 additions & 0 deletions Runtime/CoreLib.TestScript/Threading/Tasks/TaskTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,50 @@ public void TaskCompletionSourceWorksWhenCancelling() {
}, 100);
}

#if !NO_ASYNC
[Test(IsAsync = true)]
public void CancelledTaskThrowsTaskCanceledExceptionWhenAwaited() {
var tcs = new TaskCompletionSource<int>();
tcs.SetCanceled();

TaskCanceledException caughtException = null;

Action someMethod = async () => {
try {
await tcs.Task;
Assert.Fail("Await should throw");
}
catch (TaskCanceledException ex) {
caughtException = ex;
}
};
someMethod();

Globals.SetTimeout(() => {
Assert.IsNotNull(caughtException, "Should catch");
Assert.IsTrue(ReferenceEquals(tcs.Task, caughtException.Task));
Engine.Start();
}, 300);
}
#endif

[Test]
public void CancelledTaskThrowsAggregateExceptionWithTaskCanceledExceptionWhenResultIsAccessed() {
var tcs = new TaskCompletionSource<int>();
tcs.SetCanceled();

try {
int r = tcs.Task.Result;
Assert.Fail("Should throw");
}
catch (AggregateException ex) {
Assert.AreEqual(ex.InnerExceptions.Count, 1, "InnerExceptions.Count");
var tce = ex.InnerExceptions[0] as TaskCanceledException;
Assert.IsNotNull(tce, "is TaskCanceledException");
Assert.IsTrue(ReferenceEquals(tcs.Task, tce.Task), "Task");
}
}

[Test]
public void SetResultFailsWhenTheTaskIsCompleted() {
var tcs = new TaskCompletionSource<int>();
Expand Down
1 change: 1 addition & 0 deletions history.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
BREAKING CHANGE: Canceled task will throw OperationCanceledException instead of InvalidOperationException (#398).
Added support for CancellationTokens
Fixed a bug causing incorrect state machines to sometimes be generated for async methods with try blocks (#429).
Add 'string with index' overloads for Char.IsDigit and Char.IsWhiteSpace.
Expand Down

0 comments on commit e53717e

Please sign in to comment.