public void MethodReturnsATaskOfTAndMutatesGlobalState() { const int expected = 5; SyncMethods.MutateGlobalState(); Assert.AreEqual(expected, MutableGlobalState.State); }
public void UsingAResultLooksFineWithTaskCompletionSource() { // what is the problem with this (only relevant with certain sync contexts) const string expected = "foo"; var actual = SyncMethods .WaitASecondAndReturnAValueUsingTaskCompletionSource(expected) .Result; Assert.AreEqual(expected, actual); }
public void UsingResultLooksFineSoWhatsTheProblem() { // what is the problem with this (only relevant with certain sync contexts) const string expected = "foo"; var actual = SyncMethods .WaitASecondAndReturnAValue(expected) .Result; Assert.AreEqual(expected, actual); }
public void SyncExceptionHandlingWithoutAwait() { Exception exceptionThrown = null; try { SyncMethods.ThrowAnExceptionAfterOneSecond(1); } catch (Exception ex) { exceptionThrown = ex; } Assert.IsNotNull(exceptionThrown); }
public void AwaitedAsyncWrappedInNonAwaitedAction() { Exception exceptionThrown = null; Action willThrow = async() => await SyncMethods.ThrowAnExceptionAfterOneSecond(1); try { willThrow(); } catch (Exception ex) { exceptionThrown = ex; } Assert.IsNotNull(exceptionThrown); }
public async Task SyncExceptionHandlingWithAwait() { Exception exceptionThrown = null; try { await SyncMethods.ThrowAnExceptionAfterOneSecond(1); } catch (Exception ex) { exceptionThrown = ex; } Assert.IsNotNull(exceptionThrown); Assert.IsInstanceOf <AggregateException>(exceptionThrown); }
public async Task AwaitedAsyncWrappedInNonAwaitedFuncOfTask() { Exception exceptionThrown = null; Func <Task> willThrow = async() => await SyncMethods.ThrowAnExceptionAfterOneSecond(1); try { //awaitable because it's a func await willThrow(); } catch (Exception ex) { exceptionThrown = ex; } Assert.IsNotNull(exceptionThrown); }
public async Task FindingMyException() { Exception exceptionThrown = null; var delay = Task.Delay(TimeSpan.FromSeconds(0.5)); var errorProneTask = SyncMethods .ThrowAnExceptionAfterOneSecond(1) .ContinueWith(task => exceptionThrown = task.Exception); var firstTaskToComplete = await Task.WhenAny(delay, errorProneTask); Assert.AreEqual(delay, firstTaskToComplete); //give the error prone task a chance to fail. await Task.Delay(1); Assert.IsNotNull(exceptionThrown); }
public async Task LosingMyException() { Exception exceptionThrown = null; var delay = Task.Delay(TimeSpan.FromSeconds(0.5)); var errorProneTask = SyncMethods.ThrowAnExceptionAfterOneSecond(1); try { var firstTaskToComplete = await Task.WhenAny(delay, errorProneTask); Assert.AreEqual(delay, firstTaskToComplete); //give the error prone task a chance to fail. await Task.Delay(1); } catch (Exception ex) { exceptionThrown = ex; } Assert.IsNotNull(exceptionThrown, "Exception wont be set because my code " + "on the calling thread left the try catch block"); }