Esempio n. 1
0
        void runPubSub(String testName, long testCount, long testSize)
        {
            Object pubSubLock = new Object();
            bool   finished   = false;
            int    subCount   = 0;

            byte[] payload = generatePayload(testSize);

            ConnectionFactory cf = new ConnectionFactory();

            Options o = ConnectionFactory.GetDefaultOptions();

            o.Url = url;
            o.SubChannelLength        = 10000000;
            o.AsyncErrorEventHandler += (sender, obj) => {
                System.Console.WriteLine("Error: " + obj.Error);
            };

            IConnection subConn = cf.CreateConnection(o);
            IConnection pubConn = cf.CreateConnection(url);

            IAsyncSubscription s = subConn.SubscribeAsync(subject, (sender, args) =>
            {
                subCount++;
                if (subCount == testCount)
                {
                    lock (pubSubLock)
                    {
                        finished = true;
                        Monitor.Pulse(pubSubLock);
                    }
                }
            });

            s.SetPendingLimits(10000000, 1000000000);
            subConn.Flush();

            Stopwatch sw = Stopwatch.StartNew();

            for (int i = 0; i < testCount; i++)
            {
                pubConn.Publish(subject, payload);
            }

            pubConn.Flush();

            lock (pubSubLock)
            {
                if (!finished)
                {
                    Monitor.Wait(pubSubLock);
                }
            }
            sw.Stop();

            PrintResults(testName, sw, testCount, testSize);

            pubConn.Close();
            subConn.Close();
        }
Esempio n. 2
0
        public void TestSlowAsyncSubscriber()
        {
            AutoResetEvent ev = new AutoResetEvent(false);

            Options opts = utils.DefaultTestOptions;

            opts.SubChannelLength = 100;

            using (new NATSServer())
            {
                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.
                            ev.WaitOne(2000);
                        };

                        s.Start();

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

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

                        s.SetPendingLimits(pml, pbl);

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

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

                        int flushTimeout = 5000;

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

                        c.Flush(flushTimeout);

                        sw.Stop();

                        ev.Set();

                        Assert.False(sw.ElapsedMilliseconds >= flushTimeout,
                                     string.Format("elapsed ({0}) > timeout ({1})",
                                                   sw.ElapsedMilliseconds, flushTimeout));
                    }
                }
            }
        }
Esempio n. 3
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);
                    }
                }
            }
        }