Beispiel #1
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();
            }
        }
Beispiel #2
0
        public void TestCallbacksOrder()
        {
            bool firstDisconnect = true;

            long orig = DateTime.Now.Ticks;

            long dtime1 = orig;
            long dtime2 = orig;
            long rtime  = orig;
            long atime1 = orig;
            long atime2 = orig;
            long ctime  = orig;

            ConditionalObj reconnected = new ConditionalObj();
            ConditionalObj closed      = new ConditionalObj();
            ConditionalObj asyncErr1   = new ConditionalObj();
            ConditionalObj asyncErr2   = new ConditionalObj();
            ConditionalObj recvCh      = new ConditionalObj();
            ConditionalObj recvCh1     = new ConditionalObj();
            ConditionalObj recvCh2     = new ConditionalObj();

            using (NATSServer s = utils.CreateServerWithConfig(TestContext, "auth_1222.conf"))
            {
                Options o = ConnectionFactory.GetDefaultOptions();

                o.DisconnectedEventHandler += (sender, args) =>
                {
                    Thread.Sleep(100);
                    if (firstDisconnect)
                    {
                        System.Console.WriteLine("First disconnect.");
                        firstDisconnect = false;
                        dtime1          = DateTime.Now.Ticks;
                    }
                    else
                    {
                        System.Console.WriteLine("Second disconnect.");
                        dtime2 = DateTime.Now.Ticks;
                    }
                };

                o.ReconnectedEventHandler += (sender, args) =>
                {
                    System.Console.WriteLine("Reconnected.");
                    Thread.Sleep(50);
                    rtime = DateTime.Now.Ticks;
                    reconnected.notify();
                };

                o.AsyncErrorEventHandler += (sender, args) =>
                {
                    if (args.Subscription.Subject.Equals("foo"))
                    {
                        System.Console.WriteLine("Error handler foo.");
                        Thread.Sleep(200);
                        atime1 = DateTime.Now.Ticks;
                        asyncErr1.notify();
                    }
                    else
                    {
                        System.Console.WriteLine("Error handler bar.");
                        atime2 = DateTime.Now.Ticks;
                        asyncErr2.notify();
                    }
                };

                o.ClosedEventHandler += (sender, args) =>
                {
                    System.Console.WriteLine("Closed handler.");
                    ctime = DateTime.Now.Ticks;
                    closed.notify();
                };

                o.ReconnectWait    = 50;
                o.NoRandomize      = true;
                o.Servers          = new string[] { "nats://localhost:4222", "nats:localhost:1222" };
                o.SubChannelLength = 1;

                using (IConnection nc = new ConnectionFactory().CreateConnection(o),
                       ncp = new ConnectionFactory().CreateConnection())
                {
                    utils.StopDefaultServer();

                    Thread.Sleep(1000);

                    utils.StartDefaultServer();

                    reconnected.wait(3000);

                    EventHandler <MsgHandlerEventArgs> eh = (sender, args) =>
                    {
                        System.Console.WriteLine("Received message on subject: " + args.Message.Subject);
                        recvCh.notify();
                        if (args.Message.Subject.Equals("foo"))
                        {
                            recvCh1.notify();
                        }
                        else
                        {
                            recvCh2.notify();
                        }
                    };

                    IAsyncSubscription sub1 = nc.SubscribeAsync("foo", eh);
                    IAsyncSubscription sub2 = nc.SubscribeAsync("bar", eh);

                    nc.Flush();

                    ncp.Publish("foo", System.Text.Encoding.UTF8.GetBytes("hello"));
                    ncp.Publish("bar", System.Text.Encoding.UTF8.GetBytes("hello"));
                    ncp.Flush();

                    recvCh.wait(3000);

                    for (int i = 0; i < 3; i++)
                    {
                        ncp.Publish("foo", System.Text.Encoding.UTF8.GetBytes("hello"));
                        ncp.Publish("bar", System.Text.Encoding.UTF8.GetBytes("hello"));
                    }

                    ncp.Flush();

                    asyncErr1.wait(3000);
                    asyncErr2.wait(3000);

                    utils.StopDefaultServer();

                    Thread.Sleep(1000);
                    closed.reset();
                    nc.Close();

                    closed.wait(3000);
                }


                if (dtime1 == orig || dtime2 == orig || rtime == orig ||
                    atime1 == orig || atime2 == orig || ctime == orig)
                {
                    System.Console.WriteLine("Error = callback didn't fire: {0}\n{1}\n{2}\n{3}\n{4}\n{5}\n",
                                             dtime1, dtime2, rtime, atime1, atime2, ctime);
                    throw new Exception("Callback didn't fire.");
                }

                if (rtime < dtime1 || dtime2 < rtime || atime2 < atime1 || ctime < atime2)
                {
                    System.Console.WriteLine("Wrong callback order:{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n",
                                             dtime1, rtime, atime1, atime2, dtime2, ctime);
                    throw new Exception("Invalid callback order.");
                }
            }
        }
Beispiel #3
0
        public void TestAsyncSubscriptionPending()
        {
            int total         = 100;
            int receivedCount = 0;

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

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

            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                ISubscription s = c.SubscribeAsync("foo", (sender, args) =>
                {
                    startProcessing.wait(60000);

                    receivedCount++;
                    if (receivedCount == total)
                    {
                        subDoneCond.notify();
                    }
                });

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

                Thread.Sleep(1000);

                int expectedPendingCount = total - 1;

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

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

                long pendingBytes;
                long pendingMsgs;

                s.GetPending(out pendingBytes, out pendingMsgs);
                Assert.IsTrue(pendingBytes == s.PendingBytes);
                Assert.IsTrue(pendingMsgs == s.PendingMessages);

                long maxPendingBytes;
                long maxPendingMsgs;
                s.GetMaxPending(out maxPendingBytes, out maxPendingMsgs);
                Assert.IsTrue(maxPendingBytes == s.MaxPendingBytes);
                Assert.IsTrue(maxPendingMsgs == s.MaxPendingMessages);


                Assert.IsTrue((s.PendingMessages == total) ||
                              (s.PendingMessages == expectedPendingCount));

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

                startProcessing.notify();

                subDoneCond.wait(1000);

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

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

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

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

                s.Unsubscribe();

                try
                {
                    long i = s.MaxPendingBytes;
                    Assert.Fail("Should have receieved an exception.");
                }
                catch (Exception) { }
                try
                {
                    long i = s.MaxPendingMessages;
                    Assert.Fail("Should have receieved an exception.");
                }
                catch (Exception) { }
                try
                {
                    long i = s.PendingMessageLimit;
                    Assert.Fail("Should have receieved an exception.");
                }
                catch (Exception) { }
                try
                {
                    long i = s.PendingByteLimit;
                    Assert.Fail("Should have receieved an exception.");
                }
                catch (Exception) { }
                try
                {
                    s.SetPendingLimits(1, 10);
                    Assert.Fail("Should have receieved an exception.");
                }
                catch (Exception) { }
                try
                {
                    s.ClearMaxPending();
                    Assert.Fail("Should have receieved an exception.");
                }
                catch (Exception) { }
                try
                {
                    long i = s.Delivered;
                    Assert.Fail("Should have receieved an exception.");
                }
                catch (Exception) { }
                try
                {
                    long i = s.Dropped;
                    Assert.Fail("Should have receieved an exception.");
                }
                catch (Exception) { }
            }
        }
Beispiel #4
0
        public void TestSlowAsyncSubscriber()
        {
            ConditionalObj subCond = new ConditionalObj();

            Options opts = ConnectionFactory.GetDefaultOptions();

            opts.SubChannelLength = 100;

            using (IConnection c = new ConnectionFactory().CreateConnection(opts))
            {
                using (IAsyncSubscription s = c.SubscribeAsync("foo"))
                {
                    Object mu = new Object();

                    s.MessageHandler += (sender, args) =>
                    {
                        // block to back us up.
                        subCond.wait(2000);
                    };

                    s.Start();

                    Assert.IsTrue(s.PendingByteLimit == Defaults.SubPendingBytesLimit);
                    Assert.IsTrue(s.PendingMessageLimit == Defaults.SubPendingMsgsLimit);

                    long pml = 100;
                    long pbl = 1024 * 1024;

                    s.SetPendingLimits(pml, pbl);

                    Assert.IsTrue(s.PendingByteLimit == pbl);
                    Assert.IsTrue(s.PendingMessageLimit == pml);

                    for (int i = 0; i < (pml + 100); i++)
                    {
                        c.Publish("foo", null);
                    }

                    int flushTimeout = 5000;

                    Stopwatch sw = new Stopwatch();
                    sw.Start();

                    try
                    {
                        c.Flush(flushTimeout);
                    }
                    catch (Exception ex)
                    {
                        Assert.Fail("Flush failed." + ex);
                    }

                    sw.Stop();

                    subCond.notify();

                    if (sw.ElapsedMilliseconds >= flushTimeout)
                    {
                        Assert.Fail("elapsed ({0}) > timeout ({1})",
                                    sw.ElapsedMilliseconds, flushTimeout);
                    }
                }
            }
        }