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 TestCompletesAfterFunction()
        {
            var completes = new BasicCompletes <int>(0);

            completes.AndThen(value => value * 2);

            completes.With(5);

            Assert.Equal(10, completes.Outcome);
        }
Beispiel #4
0
        public void TestCompletesAfterConsumer()
        {
            int andThenValue = 0;
            var completes    = new BasicCompletes <int>(0);

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

            completes.With(5);

            Assert.Equal(5, andThenValue);
        }
Beispiel #5
0
        public void TestThatExceptionOutcomeInvalidCast()
        {
            var completes = new BasicCompletes <string>(_testScheduler);

            completes
            .AndThen("-100", value => (2 * int.Parse(value)).ToString())
            .AndThen(x => int.Parse(x))
            .Otherwise <int>(x => 1000);

            Assert.Throws <InvalidCastException>(() => completes.With("-100"));
        }
Beispiel #6
0
        public void TestCompletesAfterAndThen()
        {
            int andThenValue = 0;
            var completes    = new BasicCompletes <int>(0);

            completes
            .AndThen(value => value * 2)
            .AndThen(x => andThenValue = x);

            completes.With(5);

            Assert.Equal(10, andThenValue);
        }
Beispiel #7
0
        public void TestCompletesAfterAndThenAndThen()
        {
            var andThenValue = string.Empty;
            var completes    = new BasicCompletes <int>(0);

            completes
            .AndThen(value => value * 2)
            .AndThen(value => value.ToString())
            .AndThen(x => andThenValue = x);

            completes.With(5);

            Assert.Equal("10", andThenValue);
        }
Beispiel #8
0
        public void TestCompletesAfterAndThenMessageOut()
        {
            int andThenValue = 0;
            var completes    = new BasicCompletes <int>(0);
            var sender       = new Sender(x => andThenValue = x);

            completes
            .AndThen(value => value * 2)
            .AndThen(x => { sender.Send(x); return(x); });

            completes.With(5);

            Assert.Equal(10, andThenValue);
        }
Beispiel #9
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 #10
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 #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 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);
        }
Beispiel #13
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);
        }
Beispiel #14
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 #15
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 #16
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 #17
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);
        }
Beispiel #18
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 #19
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);
        }
Beispiel #20
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 #21
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);
        }