Exemplo n.º 1
0
        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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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);
        }
Exemplo n.º 11
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);
        }
Exemplo n.º 12
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);
        }
Exemplo n.º 13
0
        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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 15
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);
        }
Exemplo n.º 16
0
        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);
        }
Exemplo n.º 17
0
        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);
        }
Exemplo n.º 18
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);
        }
Exemplo n.º 19
0
        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);
        }