Exemplo n.º 1
0
        public async Task Given_subscribed_When_unsubscribing_async_It_should_not_get_any_further_messages()
        {
            var subject = Context.GenerateSubject();

            _sync   = Sync.MaxOne();
            _client = await Context.ConnectClientAsync();

            var s = _client.Sub(subject, stream => stream.Subscribe(NatsObserver.Delegating <MsgOp>(msg => _sync.Release(msg))));

            await Context.DelayAsync();

            await _client.PubAsync(subject, "Test1");

            _sync.WaitForAny();
            await _client.PubAsync(subject, "Test2");

            _sync.WaitForAny();

            await _client.UnsubAsync(s.SubscriptionInfo);

            await _client.PubAsync(subject, "Test3");

            await Context.DelayAsync();

            _sync.InterceptedCount.Should().Be(2);
        }
Exemplo n.º 2
0
        public void Emitting_non_MsgOp_Should_continue_Emitting_When_using_observer_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(NatsObserver.Delegating <IOp>(op =>
            {
                if (countA == 0)
                {
                    countA += 1;
                    throw exToThrow;
                }

                countA += 1;
            }, ex => caughtEx = ex));
            UnitUnderTest.AllOpsStream.Subscribe(NatsObserver.Delegating <IOp>(op => countB += 1));
            UnitUnderTest.AllOpsStream.Subscribe(NatsObserver.Delegating <IOp>(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);
        }
Exemplo n.º 3
0
        public async Task Given_subscribed_async_using_observer_When_the_subscription_has_been_disposed_It_should_not_get_messages()
        {
            var subject = Context.GenerateSubject();

            _sync   = Sync.MaxOne();
            _client = await Context.ConnectClientAsync();

            var s = await _client.SubAsync(subject, stream => stream.Subscribe(NatsObserver.Delegating <MsgOp>(msg => _sync.Release(msg))));

            await Context.DelayAsync();

            await _client.PubAsync(subject, "Test1");

            _sync.WaitForAny();
            await _client.PubAsync(subject, "Test2");

            _sync.WaitForAny();

            s.Dispose();

            await _client.PubAsync(subject, "Test3");

            await Context.DelayAsync();

            _sync.InterceptedCount.Should().Be(2);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        public void Emitting_non_MsgOp_Should_continue_Emitting_When_using_observer_without_error_handler_but_failing_observer_gets_discarded()
        {
            var countA = 0;
            var countB = 0;
            var countC = 0;

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

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

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

            countA.Should().Be(1);
            countB.Should().Be(2);
            countC.Should().Be(2);
        }
Exemplo n.º 7
0
        public async Task Given_subscribed_When_unsubscribing_async_It_should_not_get_any_further_messages()
        {
            var subject        = GenerateSubject();
            var interceptCount = 0;

            var subscription = _client.Sub(subject, stream => stream.Subscribe(NatsObserver.Delegating <MsgOp>(msg =>
            {
                Interlocked.Increment(ref interceptCount);
                ReleaseOne();
            })));

            await _client.PubAsync(subject, "Test1");

            WaitOne();
            await _client.PubAsync(subject, "Test2");

            WaitOne();

            await _client.UnsubAsync(subscription.SubscriptionInfo);

            await _client.PubAsync(subject, "Test3");

            WaitOne();

            interceptCount.Should().Be(2);
        }
Exemplo n.º 8
0
        public async Task Given_subscribed_async_using_observer_When_the_subscription_has_been_disposed_It_should_not_get_messages()
        {
            var subject        = GenerateSubject();
            var interceptCount = 0;

            var s = await _client.SubAsync(subject, stream => stream.Subscribe(NatsObserver.Delegating <MsgOp>(msg =>
            {
                Interlocked.Increment(ref interceptCount);
                ReleaseOne();
            })));

            await _client.PubAsync(subject, "Test1");

            WaitOne();
            await _client.PubAsync(subject, "Test2");

            WaitOne();

            s.Dispose();

            await _client.PubAsync(subject, "Test3");

            WaitOne();

            interceptCount.Should().Be(2);
        }
Exemplo n.º 9
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();
        }
Exemplo n.º 10
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();
        }
Exemplo n.º 11
0
        public async Task Given_subscribed_When_disconnectiong_and_connecting_again_It_should_resubscribe_and_get_messages()
        {
            var subject        = GenerateSubject();
            var interceptCount = 0;

            _client.Sub(subject, stream => stream.Subscribe(NatsObserver.Delegating <MsgOp>(msg =>
            {
                Interlocked.Increment(ref interceptCount);
                ReleaseOne();
            })));

            await _client.PubAsync(subject, "Test1");

            WaitOne();
            _client.Disconnect();

            _client.Connect();
            await _client.PubAsync(subject, "Test2");

            WaitOne();

            interceptCount.Should().Be(2);
        }
Exemplo n.º 12
0
        public async Task Given_subscribed_with_wildcard_async_using_observer_It_should_get_messages()
        {
            const string subjectNs = "foo.tests.";

            _sync   = Sync.MaxOne();
            _client = await Context.ConnectClientAsync();

            await _client.SubAsync(subjectNs + "*", stream => stream.Subscribe(NatsObserver.Delegating <MsgOp>(msg => _sync.Release(msg))));

            await Context.DelayAsync();

            await _client.PubAsync(subjectNs + "type1", "Test1");

            _sync.WaitForAny();
            await _client.PubAsync(subjectNs + "type2", "Test2");

            _sync.WaitForAny();
            await _client.PubAsync(subjectNs + "type3", "Test3");

            _sync.WaitForAny();

            _sync.InterceptedCount.Should().Be(3);
            _sync.Intercepted.Select(i => i.Subject).Should().OnlyContain(i => i.StartsWith(subjectNs));
        }
Exemplo n.º 13
0
        public async Task Given_subscribed_When_disconnectiong_and_connecting_again_It_should_resubscribe_and_get_messages()
        {
            var subject = Context.GenerateSubject();

            _sync   = Sync.MaxOne();
            _client = await Context.ConnectClientAsync();

            _client.Sub(subject, stream => stream.Subscribe(NatsObserver.Delegating <MsgOp>(msg => _sync.Release(msg))));

            await Context.DelayAsync();

            await _client.PubAsync(subject, "Test1");

            _sync.WaitForAny();
            _client.Disconnect();

            await _client.ConnectAsync();

            await _client.PubAsync(subject, "Test2");

            _sync.WaitForAny();

            _sync.InterceptedCount.Should().Be(2);
        }
Exemplo n.º 14
0
        public async Task Given_subscribed_with_wildcard_async_using_observer_It_should_get_messages()
        {
            const string subjectNs           = "foo.tests.";
            var          interceptedSubjects = new List <string>();

            await _client.SubAsync(subjectNs + "*", stream => stream.Subscribe(NatsObserver.Delegating <MsgOp>(msg =>
            {
                interceptedSubjects.Add(msg.Subject);
                ReleaseOne();
            })));

            await _client.PubAsync(subjectNs + "type1", "Test1");

            WaitOne();
            await _client.PubAsync(subjectNs + "type2", "Test2");

            WaitOne();
            await _client.PubAsync(subjectNs + "type3", "Test3");

            WaitOne();

            interceptedSubjects.Should().HaveCount(3);
            interceptedSubjects.Should().OnlyContain(i => i.StartsWith(subjectNs));
        }
Exemplo n.º 15
0
 public static IDisposable SubscribeSafe <T>(this INatsObservable <T> ob, IObserver <T> observer) where T : class
 => ob.Subscribe(NatsObserver.Safe <T>(observer.OnNext, observer.OnError, observer.OnCompleted));
Exemplo n.º 16
0
 public static IDisposable SubscribeSafe <T>(this INatsObservable <T> ob, Action <T> onNext, Action <Exception> onError = null, Action onCompleted = null)
 => ob.Subscribe(NatsObserver.Safe(onNext, onError, onCompleted));