Last active
November 29, 2016 06:45
-
-
Save danielmarbach/f50ce8946d1d12ed443755ad6df07098 to your computer and use it in GitHub Desktop.
TaskExtensions For Dennis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class TaskExtensions | |
{ | |
public static Task<TResult> WaitWithCancellation<TResult>(this Task<TResult> task, CancellationToken token = default(CancellationToken)) | |
{ | |
var tcs = new TaskCompletionSource<TResult>(); | |
var registration = token.Register(s => | |
{ | |
var source = (TaskCompletionSource<TResult>) s; | |
source.TrySetCanceled(); | |
}, tcs); | |
task.ContinueWith((t, s) => | |
{ | |
var tcsAndRegistration = (Tuple<TaskCompletionSource<TResult>, CancellationTokenRegistration>) s; | |
if (t.IsFaulted && t.Exception!= null) | |
{ | |
tcsAndRegistration.Item1.TrySetException(t.Exception.InnerException); | |
} | |
if (t.IsCanceled) | |
{ | |
tcsAndRegistration.Item1.TrySetCanceled(); | |
} | |
if (t.IsCompleted) | |
{ | |
tcsAndRegistration.Item1.TrySetResult(t.Result); | |
} | |
tcsAndRegistration.Item2.Dispose(); | |
}, Tuple.Create(tcs, registration), CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default); | |
return tcs.Task; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public async Task Completed() | |
{ | |
var cts = new CancellationTokenSource(); | |
var t = DoSomething(); | |
var result = await t.WaitWithCancellation(cts.Token); | |
Assert.AreEqual("Hello", result); | |
} | |
[Test] | |
public async Task Cancelled() | |
{ | |
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1)); | |
var t = DoSomething(); | |
Assert.ThrowsAsync<TaskCanceledException>(async () => await t.WaitWithCancellation(cts.Token)); | |
} | |
[Test] | |
public async Task Throws() | |
{ | |
var cts = new CancellationTokenSource(); | |
var t = DoSomethingWithThrows(); | |
Assert.ThrowsAsync<InvalidOperationException>(async () => await t.WaitWithCancellation(cts.Token)); | |
} | |
static async Task<string> DoSomething() | |
{ | |
await Task.Delay(5000); | |
return "Hello"; | |
} | |
static async Task<string> DoSomethingWithThrows() | |
{ | |
await Task.Delay(5000); | |
throw new InvalidOperationException(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment