public void TestOutcomeBeforeTimeout() { var andThenValue = 0; var completes = new BasicCompletes <int>(new Scheduler()); completes .AndThen(TimeSpan.FromMilliseconds(1000), value => value * 2) .AndThen(x => andThenValue = x); completes.With(5); var completed = completes.Await(TimeSpan.FromMilliseconds(10)); Assert.Equal(10, andThenValue); Assert.Equal(10, completed); }
public void TestTimeoutBeforeOutcome() { int andThenValue = 0; var completes = new BasicCompletes <int>(new Scheduler()); completes .After(() => completes.Outcome * 2, 1, 0) .AndThen(x => andThenValue = x); Thread.Sleep(1000); completes.With(5); Assert.NotEqual(10, andThenValue); Assert.Equal(0, andThenValue); }
public void TestThatNestedRecoverFromWithNoExceptionSetsOutput() { var failureValue = -1; var completes = new BasicCompletes <int>(_testScheduler); completes .AndThenTo(42, value => Completes.WithSuccess(value * 2).RecoverFrom(e => 0)) .RecoverFrom(e => failureValue = int.Parse(e.Message)); completes.With(2); completes.Await(); Assert.False(completes.HasFailed); Assert.Equal(-1, failureValue); Assert.Equal(4, completes.Outcome); }
public void TestThatExceptionOutcomeFails() { int failureValue = -1; var completes = new BasicCompletes <int>(new Scheduler()); completes .AndThen(42, value => value * 2) .AndThen <int>(value => throw new ApplicationException((2 * value).ToString())) .RecoverFrom(e => failureValue = int.Parse(e.Message)); completes.With(2); completes.Await <int>(); Assert.True(completes.HasFailed); Assert.Equal(8, failureValue); }
public void TestThatAwaitCompletes() { var completes = new BasicCompletes <int>(new Scheduler()); var thread = new Thread(new ThreadStart(() => { Thread.Sleep(100); completes.With(5); })); thread.Start(); var completed = completes.Await <int>(); Assert.Equal(5, completed); }
public void TestCompletesAfterAndThen() { var andThenValue = 0; var completes = new BasicCompletes <int>(0); completes .AndThen(value => value * 2) .AndThen(x => andThenValue = x); completes.With(5); var completed = completes.Await <int>(); Assert.Equal(10, andThenValue); Assert.Equal(10, completed); }
public void TestThatExceptionOtherwiseFails() { var failureValue = -1; var completes = new BasicCompletes <int>(_testScheduler); completes .AndThen(42, value => value * 2) .AndThen <int>(value => throw new ApplicationException((2 * value).ToString())) .Otherwise <int>(v => throw new ApplicationException(v.ToString())) .RecoverFrom(e => failureValue = int.Parse(e.Message)); completes.With(42); completes.Await(); Assert.True(completes.HasFailed); Assert.Equal(42, failureValue); }
public void TestThatFailureOutcomeFails() { int andThenValue = -1, failureValue = 0; var completes = new BasicCompletes <int>(new Scheduler()); completes .AndThen(-100, value => 2 * value) .AndThen(x => andThenValue = x) .Otherwise(x => failureValue = 1000); completes.With(-100); completes.Await <int>(); Assert.True(completes.HasFailed); Assert.Equal(-1, andThenValue); Assert.Equal(1000, failureValue); }
public void TestThatExceptionOutcomeFailsIfNotRecoveredExpectingWrongCompletesType() { var service = new BasicCompletes <int?>(_testScheduler); var client = service .AndThen(value => value * 2) .AndThen <string>(value => { throw new InvalidOperationException($"{value * 2}"); }) .RecoverFrom(e => { throw new InvalidOperationException("Not recovered."); }); service.With(2); var outcome = client.Await <string>(); // notice that here should await int? not string Assert.Null(outcome); Assert.True(client.HasFailed); }
public void TestThatExceptionOutcomeFailsIfNotRecovered() { var service = new BasicCompletes <int?>(_testScheduler); var client = service .AndThen(value => value * 2) .AndThen <int?>(value => { throw new InvalidOperationException($"{value * 2}"); }) .RecoverFrom(e => { throw new InvalidOperationException("Not recovered."); }); service.With(2); var outcome = client.Await <int?>(); Assert.Null(outcome); Assert.True(client.HasFailed); }
public void TestThatFailureOutcomeFailsInMiddle() { int andThenValue = -1, failureValue = -1; var completes = new BasicCompletes <int>(_testScheduler); completes .AndThen(value => value * value) .AndThen(100, x => andThenValue = 200) .Otherwise <int>(x => failureValue = 1000); completes.With(10); completes.Await(); Assert.True(completes.HasFailed); Assert.Equal(-1, andThenValue); Assert.Equal(1000, failureValue); }
public void TestThatFailureOutcomeFailsInMiddleWithChangedType() { var andThenValue = string.Empty; var failureValue = string.Empty; var completes = new BasicCompletes <int>(_testScheduler); completes .AndThen(value => (value * value).ToString()) .AndThen("100", x => andThenValue = "200") .Otherwise <string>(x => failureValue = "1000"); completes.With(10); completes.Await(); Assert.True(completes.HasFailed); Assert.Equal(string.Empty, andThenValue); Assert.Equal("1000", failureValue); }
public void TestThatItRecoversFromConsumerException() { var service = new BasicCompletes <int>(_testScheduler); var client = service .AndThen(value => value * 2) .AndThenTo(value => Completes.WithSuccess(value * 2)) .AndThenConsume(value => { throw new InvalidOperationException($"{value * 2}"); }) .RecoverFrom(e => int.Parse(e.Message)); service.With(5); var outcome = client.Await(); Assert.True(client.HasFailed); Assert.Equal(40, outcome); }
public void TestThatExceptionHandlerDelayRecovers() { var failureValue = -1; var completes = new BasicCompletes <int>(new Scheduler()); completes .AndThen(0, value => value * 2) .AndThen <int>(value => throw new Exception($"{value * 2}")); completes.With(10); completes.RecoverFrom(e => failureValue = int.Parse(e.Message)); completes.Await <int>(); Assert.True(completes.HasFailed); Assert.Equal(40, failureValue); }
public void TestThatFluentTimeoutWithNonNullFailureTimesout() { var completes = new BasicCompletes <int>(_testScheduler); completes .UseFailedOutcomeOf(-100) .TimeoutWithin(TimeSpan.FromMilliseconds(1)) .AndThen(value => 2 * value) .Otherwise <int>(failedValue => failedValue - 100); Thread.Sleep(100); completes.With(5); var failureOutcome = completes.Await(); Assert.True(completes.HasFailed); Assert.Equal(-200, failureOutcome); }
public void TestTimeoutBeforeOutcome() { int andThenValue = 0; var completes = new BasicCompletes <int>(new Scheduler()); completes .AndThen(TimeSpan.FromMilliseconds(1), -10, value => value * 2) .AndThen(x => andThenValue = x); var thread = new Thread(new ThreadStart(() => { Thread.Sleep(100); completes.With(5); })); thread.Start(); completes.Await <int>(); Assert.True(completes.HasFailed); Assert.NotEqual(10, andThenValue); Assert.Equal(0, andThenValue); }
public void TestOutcomeIsConsumedOncePipelineIsCompleted() { var service = new BasicCompletes <int>(_testScheduler); var nested = new BasicCompletes <int>(_testScheduler); var andThenValue = 0; var client = service .AndThen(value => value * 2) .AndThenTo(value => nested.AndThen(v => v * value)) .AndThenTo(value => Completes.WithSuccess(value * 2)) .AndThenConsume(o => andThenValue = o); service.With(5); Thread.Sleep(100); nested.With(2); var outcome = client.Await(); Assert.False(client.HasFailed); Assert.Equal(40, andThenValue); Assert.Equal(40, outcome); }
public void TestThatAlreadyFailedWithExceptionExecutesRecover() { Exception failureValue = null; var completes = new BasicCompletes <int>(_testScheduler) .AndThen <int>(x => throw new Exception("Small exception")); completes.With(5); completes.Await(); completes .AndThen(value => value * value) .RecoverFrom(x => { failureValue = x; return(100); }); completes.Await(); Assert.True(completes.HasFailed); Assert.Equal("Small exception", failureValue.Message); }
public void TestThatFailureOutcomeFailsWhenScheduledTimesOutWithOneAndThen() { var andThenValue = 0; var failedValue = -1; var completes = new BasicCompletes <int>(_testScheduler); completes .AndThen(TimeSpan.FromMilliseconds(1), -10, value => value * 2) .Otherwise <int>(failedOutcome => failedValue = failedOutcome); var thread = new Thread(() => { Thread.Sleep(100); completes.With(5); }); thread.Start(); completes.Await(); Assert.True(completes.HasFailed); Assert.Equal(0, andThenValue); Assert.Equal(-10, failedValue); }