Esempio n. 1
0
        public void TestDoubleUnsubscribe()
        {
            using (new NATSServer())
            {
                using (IConnection c = utils.DefaultTestConnection)
                {
                    using (ISyncSubscription s = c.SubscribeSync("foo"))
                    {
                        s.Unsubscribe();

                        Assert.ThrowsAny <Exception>(() => s.Unsubscribe());
                    }
                }
            }
        }
Esempio n. 2
0
        public void TestSyncSubscriptionPendingDrain()
        {
            int total = 100;

            byte[] data = System.Text.Encoding.UTF8.GetBytes("0123456789");

            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                ISyncSubscription s = c.SubscribeSync("foo");

                for (int i = 0; i < total; i++)
                {
                    c.Publish("foo", data);
                }
                c.Flush();

                while (s.Delivered != total)
                {
                    s.NextMessage(100);
                }

                Assert.IsTrue(s.Dropped == 0);
                Assert.IsTrue(s.PendingBytes == 0);
                Assert.IsTrue(s.PendingMessages == 0);

                s.Unsubscribe();
            }
        }
    public async Task StopAsync(CancellationToken token = default)
    {
        _subscription?.Unsubscribe();
        await _client.DrainAsync();

        _client.Close();
    }
Esempio n. 4
0
        public void TestClosedConnections()
        {
            using (new NATSServer())
            {
                IConnection       c = utils.DefaultTestConnection;
                ISyncSubscription s = c.SubscribeSync("foo");

                c.Close();

                // While we can annotate all the exceptions in the test framework,
                // just do it manually.
                Assert.ThrowsAny <NATSConnectionClosedException>(() => c.Publish("foo", null));

                Assert.ThrowsAny <NATSConnectionClosedException>(() => c.Publish(new Msg("foo")));

                Assert.ThrowsAny <NATSConnectionClosedException>(() => c.SubscribeAsync("foo"));

                Assert.ThrowsAny <NATSConnectionClosedException>(() => c.SubscribeSync("foo"));

                Assert.ThrowsAny <NATSConnectionClosedException>(() => c.SubscribeAsync("foo", "bar"));

                Assert.ThrowsAny <NATSConnectionClosedException>(() => c.SubscribeSync("foo", "bar"));

                Assert.ThrowsAny <NATSConnectionClosedException>(() => c.Request("foo", null));

                Assert.ThrowsAny <NATSConnectionClosedException>(() => s.NextMessage());

                Assert.ThrowsAny <NATSConnectionClosedException>(() => s.NextMessage(100));

                Assert.ThrowsAny <NATSConnectionClosedException>(() => s.Unsubscribe());

                Assert.ThrowsAny <NATSConnectionClosedException>(() => s.AutoUnsubscribe(1));
            }
        }
Esempio n. 5
0
        public void TestSyncSubscriptionPendingDrain()
        {
            int total = 100;

            byte[] data = Encoding.UTF8.GetBytes("0123456789");

            using (new NATSServer())
            {
                using (IConnection c = utils.DefaultTestConnection)
                {
                    ISyncSubscription s = c.SubscribeSync("foo");

                    for (int i = 0; i < total; i++)
                    {
                        c.Publish("foo", data);
                    }
                    c.Flush();

                    while (s.Delivered != total)
                    {
                        s.NextMessage(100);
                    }

                    Assert.True(s.Dropped == 0);
                    Assert.True(s.PendingBytes == 0);
                    Assert.True(s.PendingMessages == 0);

                    s.Unsubscribe();
                }
            }
        }
Esempio n. 6
0
        public void TestClosedConnections()
        {
            using (NATSServer.CreateFastAndVerify(Context.Server1.Port))
            {
                IConnection       c = Context.OpenConnection(Context.Server1.Port);
                ISyncSubscription s = c.SubscribeSync("foo");

                c.Close();

                // While we can annotate all the exceptions in the test framework,
                // just do it manually.
                Assert.ThrowsAny <NATSConnectionClosedException>(() => c.Publish("foo", null));

                Assert.ThrowsAny <NATSConnectionClosedException>(() => c.Publish(new Msg("foo")));

                Assert.ThrowsAny <NATSConnectionClosedException>(() => c.SubscribeAsync("foo"));

                Assert.ThrowsAny <NATSConnectionClosedException>(() => c.SubscribeSync("foo"));

                Assert.ThrowsAny <NATSConnectionClosedException>(() => c.SubscribeAsync("foo", "bar"));

                Assert.ThrowsAny <NATSConnectionClosedException>(() => c.SubscribeSync("foo", "bar"));

                Assert.ThrowsAny <NATSConnectionClosedException>(() => c.Request("foo", null));

                Assert.ThrowsAny <NATSConnectionClosedException>(() => s.NextMessage());

                Assert.ThrowsAny <NATSConnectionClosedException>(() => s.NextMessage(100));

                Assert.ThrowsAny <NATSConnectionClosedException>(() => s.Unsubscribe());

                Assert.ThrowsAny <NATSConnectionClosedException>(() => s.AutoUnsubscribe(1));
            }
        }
Esempio n. 7
0
        public void TestValidSubscriber()
        {
            using (new NATSServer())
            {
                using (IConnection c = utils.DefaultTestConnection)
                {
                    using (ISyncSubscription s = c.SubscribeSync("foo"))
                    {
                        Assert.True(s.IsValid);

                        try { s.NextMessage(100); }
                        catch (NATSTimeoutException) { }

                        Assert.True(s.IsValid);

                        s.Unsubscribe();

                        Assert.False(s.IsValid);

                        try { s.NextMessage(100); }
                        catch (NATSBadSubscriptionException) { }
                    }
                }
            }
        }
Esempio n. 8
0
        public void TestRespondWithAutoUnsubscribe()
        {
            using (new NATSServer())
            {
                using (IConnection c = utils.DefaultTestConnection)
                    using (ISyncSubscription s = c.SubscribeSync("foo"))
                    {
                        s.AutoUnsubscribe(1);

                        string replyTo = c.NewInbox();
                        using (ISyncSubscription r = c.SubscribeSync(replyTo))
                        {
                            c.Publish("foo", replyTo, Encoding.UTF8.GetBytes("message"));

                            Msg m = s.NextMessage(1000);
                            Assert.NotNull(m);
                            Assert.Equal(replyTo, m.Reply);

                            byte[] reply = Encoding.UTF8.GetBytes("reply");
                            m.Respond(reply);

                            m = r.NextMessage(1000);
                            Assert.NotNull(m);
                            Assert.Equal(replyTo, m.Subject);
                            Assert.Equal(reply, m.Data);

                            r.Unsubscribe();
                        }
                    }
            }
        }
Esempio n. 9
0
        public void TestValidSubscriber()
        {
            using (NATSServer.CreateFastAndVerify(Context.Server1.Port))
            {
                using (IConnection c = Context.OpenConnection(Context.Server1.Port))
                {
                    using (ISyncSubscription s = c.SubscribeSync("foo"))
                    {
                        Assert.True(s.IsValid);

                        try { s.NextMessage(100); }
                        catch (NATSTimeoutException) { }

                        Assert.True(s.IsValid);

                        s.Unsubscribe();

                        Assert.False(s.IsValid);

                        try { s.NextMessage(100); }
                        catch (NATSBadSubscriptionException) { }
                    }
                }
            }
        }
Esempio n. 10
0
        public void TestRespondWithAutoUnsubscribe()
        {
            using (NATSServer.CreateFastAndVerify(Context.Server1.Port))
            {
                using (IConnection c = Context.OpenConnection(Context.Server1.Port))
                    using (ISyncSubscription s = c.SubscribeSync("foo"))
                    {
                        s.AutoUnsubscribe(1);

                        string replyTo = c.NewInbox();
                        using (ISyncSubscription r = c.SubscribeSync(replyTo))
                        {
                            c.Publish("foo", replyTo, Encoding.UTF8.GetBytes("message"));

                            Msg m = s.NextMessage(1000);
                            Assert.NotNull(m);
                            Assert.Equal(replyTo, m.Reply);

                            byte[] reply = Encoding.UTF8.GetBytes("reply");
                            m.Respond(reply);

                            m = r.NextMessage(1000);
                            Assert.NotNull(m);
                            Assert.Equal(replyTo, m.Subject);
                            Assert.Equal(reply, m.Data);

                            r.Unsubscribe();
                        }
                    }
            }
        }
Esempio n. 11
0
        public void TestSyncSubscriptionPendingDrain()
        {
            int total = 100;

            byte[] data = Encoding.UTF8.GetBytes("0123456789");

            using (NATSServer.CreateFastAndVerify(Context.Server1.Port))
            {
                using (IConnection c = Context.OpenConnection(Context.Server1.Port))
                {
                    ISyncSubscription s = c.SubscribeSync("foo");

                    for (int i = 0; i < total; i++)
                    {
                        c.Publish("foo", data);
                    }
                    c.Flush();

                    while (s.Delivered != total)
                    {
                        s.NextMessage(100);
                    }

                    Assert.True(s.Dropped == 0);
                    Assert.True(s.PendingBytes == 0);
                    Assert.True(s.PendingMessages == 0);

                    s.Unsubscribe();
                }
            }
        }
Esempio n. 12
0
        public void TestClosedConnections()
        {
            IConnection       c = new ConnectionFactory().CreateConnection();
            ISyncSubscription s = c.SubscribeSync("foo");

            c.Close();

            // While we can annotate all the exceptions in the test framework,
            // just do it manually.
            UnitTestUtilities.testExpectedException(
                () => { c.Publish("foo", null); },
                typeof(NATSConnectionClosedException));

            UnitTestUtilities.testExpectedException(
                () => { c.Publish(new Msg("foo")); },
                typeof(NATSConnectionClosedException));

            UnitTestUtilities.testExpectedException(
                () => { c.SubscribeAsync("foo"); },
                typeof(NATSConnectionClosedException));

            UnitTestUtilities.testExpectedException(
                () => { c.SubscribeSync("foo"); },
                typeof(NATSConnectionClosedException));

            UnitTestUtilities.testExpectedException(
                () => { c.SubscribeAsync("foo", "bar"); },
                typeof(NATSConnectionClosedException));

            UnitTestUtilities.testExpectedException(
                () => { c.SubscribeSync("foo", "bar"); },
                typeof(NATSConnectionClosedException));

            UnitTestUtilities.testExpectedException(
                () => { c.Request("foo", null); },
                typeof(NATSConnectionClosedException));

            UnitTestUtilities.testExpectedException(
                () => { s.NextMessage(); },
                typeof(NATSConnectionClosedException));

            UnitTestUtilities.testExpectedException(
                () => { s.NextMessage(100); },
                typeof(NATSConnectionClosedException));

            UnitTestUtilities.testExpectedException(
                () => { s.Unsubscribe(); },
                typeof(NATSConnectionClosedException));

            UnitTestUtilities.testExpectedException(
                () => { s.AutoUnsubscribe(1); },
                typeof(NATSConnectionClosedException));
        }
Esempio n. 13
0
        public void TestDoubleUnsubscribe()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                using (ISyncSubscription s = c.SubscribeSync("foo"))
                {
                    s.Unsubscribe();

                    try
                    {
                        s.Unsubscribe();
                        Assert.Fail("No Exception thrown.");
                    }
                    catch (Exception e)
                    {
                        System.Console.WriteLine("Expected exception {0}: {1}",
                                                 e.GetType(), e.Message);
                    }
                }
            }
        }
Esempio n. 14
0
        public void TestNextMessageOnClosedSub()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                ISyncSubscription s = c.SubscribeSync("foo");
                s.Unsubscribe();

                try
                {
                    s.NextMessage();
                }
                catch (NATSBadSubscriptionException) { } // ignore.

                // any other exceptions will fail the test.
            }
        }
Esempio n. 15
0
        public void TestSyncSubscriptionPending()
        {
            int total = 100;

            ConditionalObj subDoneCond     = new ConditionalObj();
            ConditionalObj startProcessing = new ConditionalObj();

            byte[] data = System.Text.Encoding.UTF8.GetBytes("0123456789");

            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                ISyncSubscription s = c.SubscribeSync("foo");

                for (int i = 0; i < total; i++)
                {
                    c.Publish("foo", data);
                }
                c.Flush();

                Assert.IsTrue(s.QueuedMessageCount == total);

                Assert.IsTrue((s.MaxPendingBytes == (data.Length * total)) ||
                              (s.MaxPendingBytes == (data.Length * total)));
                Assert.IsTrue((s.MaxPendingMessages == total) ||
                              (s.MaxPendingMessages == total));

                Assert.IsTrue(s.Delivered == 0);
                Assert.IsTrue(s.Dropped == 0);

                for (int i = 0; i < total; i++)
                {
                    s.NextMessage();
                }

                Assert.IsTrue(s.QueuedMessageCount == 0);

                Assert.IsTrue((s.MaxPendingBytes == (data.Length * total)) ||
                              (s.MaxPendingBytes == (data.Length * total)));
                Assert.IsTrue((s.MaxPendingMessages == total) ||
                              (s.MaxPendingMessages == total));

                Assert.IsTrue(s.Delivered == total);
                Assert.IsTrue(s.Dropped == 0);

                s.Unsubscribe();
            }
        }
Esempio n. 16
0
        public void TestNextMessageOnClosedSub()
        {
            using (new NATSServer())
            {
                using (IConnection c = utils.DefaultTestConnection)
                {
                    ISyncSubscription s = c.SubscribeSync("foo");
                    s.Unsubscribe();

                    try
                    {
                        s.NextMessage();
                    }
                    catch (NATSBadSubscriptionException) { } // ignore.

                    // any other exceptions will fail the test.
                }
            }
        }
Esempio n. 17
0
        public void TestNextMessageOnClosedSub()
        {
            using (NATSServer.CreateFastAndVerify(Context.Server1.Port))
            {
                using (IConnection c = Context.OpenConnection(Context.Server1.Port))
                {
                    ISyncSubscription s = c.SubscribeSync("foo");
                    s.Unsubscribe();

                    try
                    {
                        s.NextMessage();
                    }
                    catch (NATSBadSubscriptionException) { } // ignore.

                    // any other exceptions will fail the test.
                }
            }
        }
Esempio n. 18
0
        public void TestValidSubscriber()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                using (ISyncSubscription s = c.SubscribeSync("foo"))
                {
                    Assert.IsTrue(s.IsValid);

                    try { s.NextMessage(100); }
                    catch (NATSTimeoutException) { }

                    Assert.IsTrue(s.IsValid);

                    s.Unsubscribe();

                    Assert.IsFalse(s.IsValid);

                    try { s.NextMessage(100); }
                    catch (NATSBadSubscriptionException) { }
                }
            }
        }