Beispiel #1
0
        public void TestThatFailureOutcomeFailsWhenScheduledInMiddle()
        {
            var andThenValue = 0;
            var failedValue  = -1;
            var completes    = new BasicCompletes <int>(_testScheduler);

            completes
            .AndThen(x => x * x)
            .AndThen(TimeSpan.FromMilliseconds(200), 100, value => andThenValue = value * 2)
            .Otherwise <int>(failedOutcome => failedValue = failedOutcome);

            var thread = new Thread(() =>
            {
                Thread.Sleep(100);
                completes.With(10);
            });

            thread.Start();

            completes.Await();

            Assert.True(completes.HasFailed);
            Assert.Equal(0, andThenValue);
            Assert.Equal(100, failedValue);
        }
Beispiel #2
0
        public void TestTimeoutBeforeOutcome()
        {
            var andThenValue = 0;
            var completes    = new BasicCompletes <int>(_testScheduler);

            completes
            .AndThen(TimeSpan.FromMilliseconds(1), -10, value => value * 2)
            .AndThen(x => andThenValue = x);

            var thread = new Thread(() =>
            {
                Thread.Sleep(100);
                completes.With(5);
            });

            thread.Start();

            var completed = completes.Await();

            Assert.True(completes.HasFailed);
            Assert.True(completes.HasOutcome);
            Assert.NotEqual(10, andThenValue);
            Assert.Equal(0, andThenValue);
            Assert.Equal(-10, completed);
        }
Beispiel #3
0
        public void TestThatAwaitTimesOut()
        {
            var completes = new BasicCompletes <int>(_testScheduler);

            var completed = completes.Await(TimeSpan.FromMilliseconds(10));

            completes.With(5);

            Assert.NotEqual(5, completed);
            Assert.Equal(default, completed);
Beispiel #4
0
        public void TestCompletesAfterConsumer()
        {
            var andThenValue = 0;
            var completes    = new BasicCompletes <int>(0);

            completes.AndThen(x => andThenValue = x);

            completes.With(5);

            var completed = completes.Await <int>();

            Assert.Equal(5, andThenValue);
            Assert.Equal(5, completed);
        }
Beispiel #5
0
        public void TestOutcomeBeforeTimeout()
        {
            int andThenValue = 0;
            var completes    = new BasicCompletes <int>(new Scheduler());

            completes
            .AndThen(TimeSpan.FromMilliseconds(1000), value => value * 2)
            .AndThen(x => andThenValue = x);

            completes.With(5);
            completes.Await <int>(TimeSpan.FromMilliseconds(10));

            Assert.Equal(10, andThenValue);
        }
Beispiel #6
0
        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);
        }
Beispiel #7
0
        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);
        }
Beispiel #8
0
        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);
        }
Beispiel #9
0
        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);
        }
Beispiel #10
0
        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);
        }
Beispiel #11
0
        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);
        }
Beispiel #12
0
        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);
        }
Beispiel #13
0
        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);
        }
Beispiel #14
0
        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);
        }
Beispiel #15
0
        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);
        }
Beispiel #16
0
        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);
        }
Beispiel #17
0
        public void TestThatFailureOutcomeFailsWhenScheduledTimesOutWithOneAndThen()
        {
            var andThenValue = 0;
            var failedValue  = -1;
            var completes    = new BasicCompletes <int>(new Scheduler());

            completes
            .AndThen(TimeSpan.FromMilliseconds(10), -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);
        }