Beispiel #1
0
        public void Emitting_MsgOp_Should_continue_Emitting_When_using_delegate_without_error_handler_but_failing_observer_gets_discarded()
        {
            var msgOp  = MsgOp.CreateMsg("TestSubject", "e8fb57beeb094bbfb545056057a8f7f2", ReadOnlySpan <char> .Empty, new byte[0]);
            var countA = 0;
            var countB = 0;
            var countC = 0;

            UnitUnderTest.MsgOpsStream.Subscribe(op =>
            {
                if (countA == 0)
                {
                    countA += 1;
                    throw new Exception("Fail");
                }

                countA += 1;
            });
            UnitUnderTest.MsgOpsStream.Subscribe(op => countB += 1);
            UnitUnderTest.MsgOpsStream.Subscribe(op => countC += 1);

            UnitUnderTest.Emit(msgOp);
            UnitUnderTest.Emit(msgOp);

            countA.Should().Be(1);
            countB.Should().Be(2);
            countC.Should().Be(2);
        }
Beispiel #2
0
        public void Emitting_MsgOp_Should_continue_Emitting_When_using_observer_with_error_handler_but_failing_observer_gets_discarded()
        {
            var       msgOp     = MsgOp.CreateMsg("TestSubject", "f0dd86b9c2804632919b7b78292435e6", ReadOnlySpan <char> .Empty, new byte[0]);
            var       countA    = 0;
            var       countB    = 0;
            var       countC    = 0;
            var       exToThrow = new Exception(Guid.NewGuid().ToString());
            Exception caughtEx  = null;

            UnitUnderTest.MsgOpsStream.Subscribe(NatsObserver.Delegating <IOp>(op =>
            {
                if (countA == 0)
                {
                    countA += 1;
                    throw exToThrow;
                }

                countA += 1;
            }, ex => caughtEx = ex));
            UnitUnderTest.MsgOpsStream.Subscribe(NatsObserver.Delegating <IOp>(op => countB += 1));
            UnitUnderTest.MsgOpsStream.Subscribe(NatsObserver.Delegating <IOp>(op => countC += 1));

            UnitUnderTest.Emit(msgOp);
            UnitUnderTest.Emit(msgOp);

            caughtEx.Should().BeNull();
            countA.Should().Be(1);
            countB.Should().Be(2);
            countC.Should().Be(2);
        }
Beispiel #3
0
        public void Emitting_MsgOp_Should_continue_Emitting_When_using_observer_without_error_handler_but_failing_observer_gets_discarded()
        {
            var msgOp  = MsgOp.CreateMsg("TestSubject", "60a152d4b5804b23abe088eeac63b55e", ReadOnlySpan <char> .Empty, new byte[0]);
            var countA = 0;
            var countB = 0;
            var countC = 0;

            UnitUnderTest.MsgOpsStream.Subscribe(NatsObserver.Delegating <IOp>(op =>
            {
                if (countA == 0)
                {
                    countA += 1;
                    throw new Exception("Fail");
                }

                countA += 1;
            }));
            UnitUnderTest.MsgOpsStream.Subscribe(NatsObserver.Delegating <IOp>(op => countB += 1));
            UnitUnderTest.MsgOpsStream.Subscribe(NatsObserver.Delegating <IOp>(op => countC += 1));

            UnitUnderTest.Emit(msgOp);
            UnitUnderTest.Emit(msgOp);

            countA.Should().Be(1);
            countB.Should().Be(2);
            countC.Should().Be(2);
        }
Beispiel #4
0
        public void Emitting_MsgOp_Should_continue_Emitting_When_using_delegate_with_error_handler_but_failing_observer_gets_discarded()
        {
            var       msgOp     = MsgOp.CreateMsg("TestSubject", "01c549bed5f643e484c2841aff7a0d9d", ReadOnlySpan <char> .Empty, new byte[0]);
            var       countA    = 0;
            var       countB    = 0;
            var       countC    = 0;
            var       exToThrow = new Exception(Guid.NewGuid().ToString());
            Exception caughtEx  = null;

            UnitUnderTest.MsgOpsStream.Subscribe(op =>
            {
                if (countA == 0)
                {
                    countA += 1;
                    throw exToThrow;
                }

                countA += 1;
            }, ex => caughtEx = ex);
            UnitUnderTest.MsgOpsStream.Subscribe(op => countB += 1);
            UnitUnderTest.MsgOpsStream.Subscribe(op => countC += 1);

            UnitUnderTest.Emit(msgOp);
            UnitUnderTest.Emit(msgOp);

            caughtEx.Should().BeNull();
            countA.Should().Be(1);
            countB.Should().Be(2);
            countC.Should().Be(2);
        }
        public void Should_be_able_to_apply_predicate()
        {
            var observer1 = new Mock <IObserver <Data> >();
            var observer2 = new Mock <IObserver <Data> >();

            UnitUnderTest.Where(d => d.Value <= 2).Subscribe(observer1.Object);
            UnitUnderTest.Where(d => d.Value >= 2).Subscribe(observer2.Object);

            UnitUnderTest.Emit(new Data {
                Value = 1
            });
            UnitUnderTest.Emit(new Data {
                Value = 2
            });
            UnitUnderTest.Emit(new Data {
                Value = 3
            });

            observer1.Verify(f => f.OnNext(It.IsAny <Data>()), Times.Exactly(2));
            observer1.Verify(f => f.OnNext(It.Is <Data>(d => d.Value == 1)), Times.Once);
            observer1.Verify(f => f.OnNext(It.Is <Data>(d => d.Value == 2)), Times.Once);

            observer2.Verify(f => f.OnNext(It.IsAny <Data>()), Times.Exactly(2));
            observer2.Verify(f => f.OnNext(It.Is <Data>(d => d.Value == 2)), Times.Once);
            observer2.Verify(f => f.OnNext(It.Is <Data>(d => d.Value == 3)), Times.Once);
        }
Beispiel #6
0
        public void Emitting_non_MsgOp_Should_continue_Emitting_When_using_delegate_with_error_handler_but_failing_observer_gets_discarded()
        {
            var       countA    = 0;
            var       countB    = 0;
            var       countC    = 0;
            var       exToThrow = new Exception(Guid.NewGuid().ToString());
            Exception caughtEx  = null;

            UnitUnderTest.AllOpsStream.Subscribe(op =>
            {
                if (countA == 0)
                {
                    countA += 1;
                    throw exToThrow;
                }

                countA += 1;
            }, ex => caughtEx = ex);
            UnitUnderTest.AllOpsStream.Subscribe(op => countB += 1);
            UnitUnderTest.AllOpsStream.Subscribe(op => countC += 1);

            UnitUnderTest.Emit(PingOp.Instance);
            UnitUnderTest.Emit(PingOp.Instance);

            caughtEx.Should().BeNull();
            countA.Should().Be(1);
            countB.Should().Be(2);
            countC.Should().Be(2);
        }
Beispiel #7
0
        public void Emitting_non_MsgOp_Should_continue_Emitting_When_using_delegate_without_error_handler_but_failing_observer_gets_discarded()
        {
            var countA = 0;
            var countB = 0;
            var countC = 0;

            UnitUnderTest.AllOpsStream.Subscribe(op =>
            {
                if (countA == 0)
                {
                    countA += 1;
                    throw new Exception("Fail");
                }

                countA += 1;
            });
            UnitUnderTest.AllOpsStream.Subscribe(op => countB += 1);
            UnitUnderTest.AllOpsStream.Subscribe(op => countC += 1);

            UnitUnderTest.Emit(PingOp.Instance);
            UnitUnderTest.Emit(PingOp.Instance);

            countA.Should().Be(1);
            countB.Should().Be(2);
            countC.Should().Be(2);
        }
        public void Emitting_Should_invoke_logger_for_error_When_exception_is_thrown()
        {
            var thrown = new Exception("I FAILED!");

            UnitUnderTest.Subscribe(msg => throw thrown);

            UnitUnderTest.Emit(Mock.Of <Data>());

            FakeLogger.Verify(f => f.Error(It.Is <string>(m => m.Contains("Error in observer while emitting value.")), thrown), Times.Once);
        }
        public void Emitting_Should_dispatch_to_all_observers()
        {
            var callCount = 0;

            UnitUnderTest.Subscribe(ev => Interlocked.Increment(ref callCount));
            UnitUnderTest.Subscribe(ev => Interlocked.Increment(ref callCount));

            UnitUnderTest.Emit(Mock.Of <Data>());

            callCount.Should().Be(2);
        }
Beispiel #10
0
        public void Emitting_non_MsgOp_Should_not_dispatch_to_MsgOpsStream_but_AllOpsStream()
        {
            var opStreamRec    = false;
            var msgOpStreamRec = false;

            UnitUnderTest.AllOpsStream.Subscribe(NatsObserver.Delegating <IOp>(op => opStreamRec      = true));
            UnitUnderTest.MsgOpsStream.Subscribe(NatsObserver.Delegating <MsgOp>(op => msgOpStreamRec = true));

            UnitUnderTest.Emit(PingOp.Instance);

            opStreamRec.Should().BeTrue();
            msgOpStreamRec.Should().BeFalse();
        }
Beispiel #11
0
        public void Emitting_MsgOp_Should_dispatch_to_both_AllOpsStream_and_MsgOpsStream()
        {
            var msgOp          = MsgOp.CreateMsg("TestSubject", "0a3282e769e34677809db5d756dfd768", ReadOnlySpan <char> .Empty, new byte[0]);
            var opStreamRec    = false;
            var msgOpStreamRec = false;

            UnitUnderTest.AllOpsStream.Subscribe(NatsObserver.Delegating <IOp>(op => opStreamRec      = true));
            UnitUnderTest.MsgOpsStream.Subscribe(NatsObserver.Delegating <MsgOp>(op => msgOpStreamRec = true));

            UnitUnderTest.Emit(msgOp);

            opStreamRec.Should().BeTrue();
            msgOpStreamRec.Should().BeTrue();
        }
        public void Emitting_Should_not_continue_emitting_to_failing_observer_but_to_other_observers_When_an_observer_has_failed()
        {
            var failingObserver = new Mock <IObserver <Data> >();

            failingObserver.Setup(f => f.OnNext(It.IsAny <Data>())).Throws <Exception>();
            var nonFailingObserver = new Mock <IObserver <Data> >();

            UnitUnderTest.Subscribe(failingObserver.Object);
            UnitUnderTest.Subscribe(nonFailingObserver.Object);

            UnitUnderTest.Emit(Mock.Of <Data>());
            UnitUnderTest.Emit(Mock.Of <Data>());

            failingObserver.Verify(f => f.OnNext(It.IsAny <Data>()), Times.Once);
            failingObserver.Verify(f => f.OnError(It.IsAny <Exception>()), Times.Never);
            nonFailingObserver.Verify(f => f.OnNext(It.IsAny <Data>()), Times.Exactly(2));
            nonFailingObserver.Verify(f => f.OnError(It.IsAny <Exception>()), Times.Never);
        }
        public void Emitting_Should_not_invoke_onError_on_any_observer_When_exception_is_thrown_in_safe_observer()
        {
            var failingExHandlerWasInvoked    = false;
            var nonFailingExHandlerWasInvoked = false;

            UnitUnderTest.SubscribeSafe(
                ev => throw new Exception("I FAILED!"),
                ex => failingExHandlerWasInvoked = true);

            UnitUnderTest.Subscribe(
                ev => { },
                ex => nonFailingExHandlerWasInvoked = true);

            UnitUnderTest.Emit(Mock.Of <Data>());

            failingExHandlerWasInvoked.Should().BeFalse();
            nonFailingExHandlerWasInvoked.Should().BeFalse();
        }
        public void Should_be_able_filter_by_type()
        {
            var observer1 = new Mock <IObserver <Data> >();
            var observer2 = new Mock <IObserver <ExtendedData> >();

            UnitUnderTest.OfType <ExtendedData>().Subscribe(observer1.Object);
            UnitUnderTest.OfType <ExtendedData>().Subscribe(observer2.Object);

            UnitUnderTest.Emit(new ExtendedData {
                Value = 1, OtherValue = 2
            });

            observer1.Verify(f => f.OnNext(It.IsAny <ExtendedData>()), Times.Exactly(1));
            observer1.Verify(f => f.OnNext(It.Is <ExtendedData>(d => d.Value == 1 && d.OtherValue == 2)), Times.Once);

            observer2.Verify(f => f.OnNext(It.IsAny <ExtendedData>()), Times.Exactly(1));
            observer2.Verify(f => f.OnNext(It.Is <ExtendedData>(d => d.Value == 1 && d.OtherValue == 2)), Times.Once);
        }
        public void Emitting_Should_not_dispatch_to_a_disposed_observer()
        {
            var s1CallCount = 0;
            var s2CallCount = 0;

            var s1 = UnitUnderTest.Subscribe(ev =>
            {
                Interlocked.Increment(ref s1CallCount);
            });
            var _ = UnitUnderTest.Subscribe(ev =>
            {
                Interlocked.Increment(ref s2CallCount);
            });

            UnitUnderTest.Emit(Mock.Of <Data>());
            s1.Dispose();
            UnitUnderTest.Emit(Mock.Of <Data>());

            s1CallCount.Should().Be(1);
            s2CallCount.Should().Be(2);
        }
        public void Emitting_Should_invoke_handler_of_failing_observer_When_using_catchAny_and_a_more_specific_exception_is_thrown()
        {
            var failingCatchWasInvoked        = false;
            var failingExHandlerWasInvoked    = false;
            var nonFailingCatchWasInvoked     = false;
            var nonFailingExHandlerWasInvoked = false;

            UnitUnderTest.CatchAny(ex => failingCatchWasInvoked = true).Subscribe(
                ev => throw new NotSupportedException("I FAILED!"),
                ex => failingExHandlerWasInvoked = true);

            UnitUnderTest.CatchAny(ex => nonFailingCatchWasInvoked = true).Subscribe(
                ev => { },
                ex => nonFailingExHandlerWasInvoked = true);

            UnitUnderTest.Emit(Mock.Of <Data>());

            failingCatchWasInvoked.Should().BeTrue();
            failingExHandlerWasInvoked.Should().BeFalse();
            nonFailingCatchWasInvoked.Should().BeFalse();
            nonFailingExHandlerWasInvoked.Should().BeFalse();
        }
        public void Emitting_Should_not_fail_When_no_observers_exists()
        {
            Action a = () => UnitUnderTest.Emit(Mock.Of <Data>());

            a.Should().NotThrow();
        }