public void Should_throw_an_exception_without_compensation() { Task task = ComposerFactory.Compose( composer => { composer.Execute(() => { throw new InvalidOperationException("This is expected"); }); }); var exception = Assert.Throws <InvalidOperationException>(async() => await task); }
public void Should_throw_the_specified_exception_if_unhandled() { bool compensated = false; var p = new Payload { Name = "Chris" }; Task <Payload> task = ComposerFactory.Compose(p, composer => { composer.Execute(x => { x.Name = "Joe"; throw new InvalidOperationException("This is expected"); }); composer.Compensate(compensation => { compensated = true; return(compensation.Throw(new NotImplementedException("This is also expected"))); }); }); Assert.Throws <NotImplementedException>(async() => { Payload payload = await task; }); Assert.IsTrue(compensated); }
public async void Should_execute_the_method_immediately() { bool called = false; Task task = ComposerFactory.Compose(composer => composer.Execute(() => called = true, ExecuteOptions.RunSynchronously)); await task; Assert.IsTrue(called); }
public async void Should_execute_the_task() { bool called = false; Task task = ComposerFactory.Compose(composer => composer.Execute(() => called = true)); await task; Assert.IsTrue(called); }
public async void Should_support_cool_stuff() { SideCarrier <B> factorySideCarrier = new FactorySideCarrier <B>(); var carrier = new AircraftCarrier(); Payload <A> payload = new PayloadImpl <A>(new A()); Task <Payload <A> > task = ComposerFactory.Compose(payload, composer => { factorySideCarrier.Compose(composer, carrier); }); await task; }
public void Should_call_the_method_only_once() { var tracker = new Tracker(3); IRetryPolicy retryPolicy = Retry.Filter <InvalidOperationException>(x => false).Immediate(5); Task task = ComposerFactory.Compose(composer => composer.Retry(retryPolicy, x => x.Execute(tracker.FaultingMethod))); Assert.Throws <InvalidOperationException>(async() => await task); Assert.AreEqual(1, tracker.CallCount); }
public void Should_call_the_method_three_times() { var tracker = new Tracker(3); IRetryPolicy retryPolicy = Retry.Immediate(5); Task task = ComposerFactory.Compose(composer => composer.Retry(retryPolicy, x => x.Execute(tracker.FaultingMethod))); task.Wait(); Assert.AreEqual(4, tracker.CallCount); }
public async void Should_include_the_handled_exception() { var tracker = new Tracker(3); IRetryPolicy retryPolicy = Retry.Selected <InvalidOperationException>().Immediate(5); Task task = ComposerFactory.Compose(composer => composer.Retry(retryPolicy, x => x.Execute(tracker.FaultingMethod))); await task; Assert.AreEqual(4, tracker.CallCount); }
public void Should_call_an_async_method_nicely() { var task = ComposerFactory.Compose(new Payload(), composer => { composer.ExecuteAsync(async(payload, token) => { var result = await SomeAsyncMethod(payload.Name); payload.Result = result; }); }); }
public void Should_not_include_the_excluded() { var tracker = new Tracker(3); IRetryPolicy retryPolicy = Retry.Except <InvalidOperationException>().Immediate(5); Task task = ComposerFactory.Compose(composer => composer.Retry(retryPolicy, x => x.Execute(tracker.FaultingMethod))); Assert.Throws <InvalidOperationException>(async() => await task); Assert.AreEqual(1, tracker.CallCount); }
public async void Should_delay_then_execute() { bool called = false; Task task = ComposerFactory.Compose(composer => { composer.Delay(100); composer.Execute(() => called = true); }); await task; Assert.IsTrue(called); }
public void Should_always_be_called() { bool called = false; Task task = ComposerFactory.Compose(composer => { composer.Execute(() => { throw new InvalidOperationException("This is expected"); }); composer.Finally(status => { called = true; }); }); Assert.Throws <InvalidOperationException>(async() => await task); Assert.IsTrue(called); }
public void Should_fault_if_not_compensated() { var tracker = new Tracker(3); Stopwatch timer = Stopwatch.StartNew(); Task task = ComposerFactory.Compose(x => x.Repeat(3, composer => { composer.Execute(tracker.RepeatedMethod); }, tracker.Token)); Assert.Throws <AggregateException>(() => task.Wait()); timer.Stop(); Assert.AreEqual(1, tracker.CallCount); }
public void Should_call_the_method_for_each_timespan() { var tracker = new Tracker(3); IRetryPolicy retryPolicy = Retry.Intervals(10, 50, 500, 1000); Task task = ComposerFactory.Compose(composer => composer.Retry(retryPolicy, x => x.Execute(tracker.FaultingMethod))); Stopwatch timer = Stopwatch.StartNew(); task.Wait(); timer.Stop(); Console.WriteLine("Timespan: {0}", timer.Elapsed); Assert.AreEqual(4, tracker.CallCount); }
public void Should_repeat_until_canceled() { var tracker = new Tracker(3); Stopwatch timer = Stopwatch.StartNew(); Task task = ComposerFactory.Compose(x => x.Repeat(3, composer => { composer.Execute(tracker.RepeatedMethod); composer.Compensate(comp => comp.Handled()); }, tracker.Token)); task.Wait(); timer.Stop(); Assert.AreEqual(3, tracker.CallCount); }
public void Should_repeat_until_canceled() { var tracker = new Tracker(10); Stopwatch timer = Stopwatch.StartNew(); Task task = ComposerFactory.Compose(x => x.Repeat(10, composer => { Console.WriteLine("Timespan: {0}", timer.Elapsed); composer.Execute(tracker.RepeatedMethod); }, tracker.Token)); task.Wait(); timer.Stop(); Assert.AreEqual(10, tracker.CallCount); }
public async void Should_compensate_on_exception() { bool called = false; Task task = ComposerFactory.Compose(composer => { composer.Execute(() => { throw new InvalidOperationException("This is expected"); }); composer.Compensate(compensation => { called = true; return(compensation.Handled()); }); }); await task; Assert.IsTrue(called); }
public void Should_throw_the_same_exception_if_not_handled() { bool called = false; Task task = ComposerFactory.Compose(composer => { composer.Execute(() => { throw new InvalidOperationException("This is expected"); }); composer.Compensate(compensation => { called = true; return(compensation.Throw()); }); }); var exception = Assert.Throws <InvalidOperationException>(async() => await task); Assert.IsTrue(called); }
public async void Should_use_async_processing() { int threadId = Thread.CurrentThread.ManagedThreadId; int asyncThreadId = threadId; Task <Payload> task = ComposerFactory.Compose(new Payload { Name = "Chris" }, composer => { composer.Execute(x => { x.Name = "Joe"; asyncThreadId = Thread.CurrentThread.ManagedThreadId; }); }); Payload payload = await task; Assert.AreEqual("Joe", payload.Name); Assert.AreNotEqual(threadId, asyncThreadId); }
public async void Should_compose_a_nested_delay() { int sequenceId = 0; int delayed = 0; int executed = 0; Task task = ComposerFactory.Compose(composer => { composer.ComposeTask(x => { x.ExecuteAsync(token => Task.Delay(100, token)); x.Execute(() => delayed = ++sequenceId); }); composer.Execute(() => executed = ++sequenceId); }); await task; Assert.AreEqual(1, delayed); Assert.AreEqual(2, executed); }
public void Should_use_async_processing_and_capture_exceptions_synchronously() { int threadId = Thread.CurrentThread.ManagedThreadId; int asyncThreadId = threadId; Task <Payload> task = ComposerFactory.Compose(new Payload { Name = "Chris" }, composer => { composer.Execute(x => { x.Name = "Joe"; asyncThreadId = Thread.CurrentThread.ManagedThreadId; throw new InvalidOperationException("This is expected"); }, ExecuteOptions.RunSynchronously); }); Assert.Throws <InvalidOperationException>(async() => { Payload payload = await task; }); Assert.AreEqual(threadId, asyncThreadId, "Should have been on the same thread"); }
public void Should_use_async_processing_and_capture_exceptions() { int threadId = Thread.CurrentThread.ManagedThreadId; int asyncThreadId = threadId; Task <Payload> task = ComposerFactory.Compose(new Payload { Name = "Chris" }, composer => { composer.ExecuteAsync((x, token) => { x.Name = "Joe"; asyncThreadId = Thread.CurrentThread.ManagedThreadId; throw new InvalidOperationException("This is expected"); }); }); Assert.Throws <InvalidOperationException>(async() => { Payload payload = await task; }); Assert.AreNotEqual(threadId, asyncThreadId); }
public void Should_throw_task_canceled_exception() { var cancel = new CancellationTokenSource(); var payload = new Payload { Name = "Chris" }; Task <Payload> task = ComposerFactory.Compose(payload, composer => { composer.Execute(x => { x.Name = "Joe"; cancel.Cancel(); }); composer.Execute(x => { x.Name = "Mark"; }); }, cancel.Token); Assert.Throws <TaskCanceledException>(async() => { Payload p = await task; }); Assert.AreEqual("Joe", payload.Name); }
public async void Should_use_async_processing() { var p = new Payload { Name = "Chris" }; Task <Payload> task = ComposerFactory.Compose(p, composer => { composer.Execute(x => { x.Name = "Joe"; throw new InvalidOperationException(); }); composer.Compensate(x => { x.Payload.Name = "Mark"; return(x.Handled()); }); }); Payload payload = await task; Assert.AreEqual("Mark", payload.Name); }
public async void Should_call_even_when_cancelled_synchronously() { bool firstCalled = false; bool secondCalled = false; bool finallyCalled = false; var cancel = new CancellationTokenSource(); Task task = ComposerFactory.Compose(composer => { composer.Execute(() => firstCalled = true, ExecuteOptions.RunSynchronously); composer.Execute(cancel.Cancel, ExecuteOptions.RunSynchronously); composer.Execute(() => secondCalled = true, ExecuteOptions.RunSynchronously); composer.Finally(() => finallyCalled = true, ExecuteOptions.RunSynchronously); }, cancel.Token); Assert.Throws <TaskCanceledException>(async() => await task); Assert.IsTrue(firstCalled); Assert.IsFalse(secondCalled); Assert.IsTrue(finallyCalled); Assert.IsTrue(cancel.Token.IsCancellationRequested); }
async Task <int> SomeAsyncMethod(string value) { await ComposerFactory.Compose(composer => { }); return(27); }