示例#1
0
        public void TestAsyncSubHandlerAPI()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                int received = 0;

                EventHandler<MsgHandlerEventArgs> h = (sender, args) =>
                {
                    Interlocked.Increment(ref received);
                };

                using (IAsyncSubscription s = c.SubscribeAsync("foo", h))
                {
                    c.Publish("foo", null);
                    c.Flush();
                    Thread.Sleep(500);
                }

                using (IAsyncSubscription s = c.SubscribeAsync("foo", "bar", h))
                {
                    c.Publish("foo", null);
                    c.Flush();
                    Thread.Sleep(500);
                }

                if (received != 2)
                {
                    Assert.Fail("Received ({0}) != 2", received);
                }
            }
        }
示例#2
0
        public void TestServerAutoUnsub()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                long received = 0;
                int max = 10;

                using (IAsyncSubscription s = c.SubscribeAsync("foo"))
                {
                    s.MessageHandler += (sender, arg) =>
                    {
                        System.Console.WriteLine("Received msg.");
                        received++;
                    };

                    s.AutoUnsubscribe(max);
                    s.Start();

                    for (int i = 0; i < (max * 2); i++)
                    {
                        c.Publish("foo", Encoding.UTF8.GetBytes("hello"));
                    }
                    c.Flush();

                    Thread.Sleep(500);

                    if (received != max)
                    {
                        Assert.Fail("Recieved ({0}) != max ({1})",
                            received, max);
                    }
                    Assert.IsFalse(s.IsValid);
                }
            }
        }
示例#3
0
        public void TestBasicReconnectFunctionality()
        {
            Options opts = utils.DefaultTestOptions;
            opts.Url = "nats://localhost:22222";
            opts.MaxReconnect = 2;
            opts.ReconnectWait = 1000;

            Object testLock = new Object();
            Object msgLock = new Object();

            opts.DisconnectedEventHandler = (sender, args) =>
            {
                lock (testLock)
                {
                    Monitor.Pulse(testLock);
                }
            };

            opts.ReconnectedEventHandler = (sender, args) =>
            {
                // NOOP
            };

            NATSServer ns = utils.CreateServerOnPort(22222);

            using (IConnection c = new ConnectionFactory().CreateConnection(opts))
            {
                IAsyncSubscription s = c.SubscribeAsync("foo");
                s.MessageHandler += (sender, args) =>
                {
                    lock (msgLock)
                    {
                        Monitor.Pulse(msgLock);
                    }
                };

                s.Start();
                c.Flush();

                lock (testLock)
                {
                    ns.Shutdown();
                    Assert.True(Monitor.Wait(testLock, 100000));
                }

                c.Publish("foo", Encoding.UTF8.GetBytes("Hello"));

                // restart the server.
                using (ns = utils.CreateServerOnPort(22222))
                {
                    lock (msgLock)
                    {
                        c.Flush(50000);
                        Assert.True(Monitor.Wait(msgLock, 10000));
                    }

                    Assert.True(c.Stats.Reconnects == 1);
                }
            }
        }
示例#4
0
        public void TestCustomObjectSerialization()
        {
            using (IEncodedConnection c = new ConnectionFactory().CreateEncodedConnection())
            {
                Object mu = new Object();
                SerializationTestObj origObj = new SerializationTestObj();

                EventHandler<EncodedMessageEventArgs> eh = (sender, args) =>
                {
                    // Ensure we blow up in the cast
                    SerializationTestObj so = (SerializationTestObj)args.ReceivedObject;
                    Assert.IsTrue(so.Equals(origObj));

                    lock (mu)
                    {
                        Monitor.Pulse(mu);
                    }
                };

                using (IAsyncSubscription s = c.SubscribeAsync("foo", eh))
                {
                    lock (mu)
                    {
                        c.Publish("foo", new SerializationTestObj());
                        c.Flush();

                        Monitor.Wait(mu, 1000);
                    }
                }
            }
        }
示例#5
0
        public void Run(string[] args)
        {
            Stopwatch sw = null;

            parseArgs(args);
            banner();

            Options opts = ConnectionFactory.GetDefaultOptions();
            opts.Url = url;

            using (IConnection c = new ConnectionFactory().CreateConnection(opts))
            {
                sw = Stopwatch.StartNew();

                for (int i = 0; i < count; i++)
                {
                    c.Request(subject, payload);
                }
                c.Flush();

                sw.Stop();

                System.Console.Write("Completed {0} requests in {1} seconds ", count, sw.Elapsed.TotalSeconds);
                System.Console.WriteLine("({0} requests/second).",
                    (int)(count / sw.Elapsed.TotalSeconds));
                printStats(c);

            }
        }
示例#6
0
        public void TestSubDelTaskCountReconnect()
        {
            bool disconnected = false;
            AutoResetEvent reconnectEv = new AutoResetEvent(false);

            var opts = utils.DefaultTestOptions;
            opts.SubscriberDeliveryTaskCount = 2;
            opts.DisconnectedEventHandler = (obj, args) => { disconnected = true;};
            opts.ReconnectedEventHandler = (obj, args) => { reconnectEv.Set(); };

            using (var server = new NATSServer())
            {
                using (IConnection c = new ConnectionFactory().CreateConnection(opts))
                {
                    long received = 0;
                    int max = 10;
                    AutoResetEvent ev = new AutoResetEvent(false);

                    using (var s = c.SubscribeAsync("foo", (obj, args) =>
                    {
                        received++;
                        if (received == max)
                            ev.Set();
                    }))
                    {
                        for (int i = 0; i < max / 2; i++)
                        {
                            c.Publish("foo", null);
                        }
                        c.Flush();

                        // bounce the server, we should reconnect, then
                        // be able to receive messages.
                        server.Bounce(100);

                        Assert.True(reconnectEv.WaitOne(20000));
                        Assert.True(disconnected);

                        for (int i = 0; i < max / 2; i++)
                        {
                            c.Publish("foo", null);
                        }
                        c.Flush();

                        Assert.True(ev.WaitOne(10000));
                        Assert.True(received == max);
                    }
                }
            }
        }
示例#7
0
        public void TestRequest()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                using (IAsyncSubscription s = c.SubscribeAsync("foo"))
                {
                    byte[] response = Encoding.UTF8.GetBytes("I will help you.");

                    s.MessageHandler += (sender, args) =>
                    {
                        c.Publish(args.Message.Reply, response);
                        c.Flush();
                    };

                    s.Start();

                    Msg m = c.Request("foo", Encoding.UTF8.GetBytes("help."),
                        5000);

                    if (!compare(m.Data, response))
                    {
                        Assert.Fail("Response isn't valid");
                    }
                }
            }
        }
示例#8
0
        public void TestSubDelTaskCountSlowConsumer()
        {
            AutoResetEvent errorEv = new AutoResetEvent(false);

            var opts = utils.DefaultTestOptions;
            opts.SubscriberDeliveryTaskCount = 1;
            opts.SubChannelLength = 10;

            opts.AsyncErrorEventHandler = (obj, args) => { errorEv.Set(); };

            using (new NATSServer())
            {
                using (IConnection c = new ConnectionFactory().CreateConnection(opts))
                {
                    AutoResetEvent cbEv = new AutoResetEvent(false);

                    using (var s = c.SubscribeAsync("foo", (obj, args) =>
                    {
                        cbEv.WaitOne();
                    }))
                    {
                        for (int i = 0; i < opts.SubChannelLength * 2; i++)
                        {
                            c.Publish("foo", null);
                        }
                        c.Flush();

                        // make sure we hit the error.
                        Assert.True(errorEv.WaitOne(10000));

                        // unblock the callback.
                        cbEv.Set();
                    }
                }
            }
        }
示例#9
0
        public void TestAsyncSubscriberStarvation()
        {
            Object waitCond = new Object();

            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                using (IAsyncSubscription helper = c.SubscribeAsync("helper"),
                                          start = c.SubscribeAsync("start"))
                {
                    helper.MessageHandler += (sender, arg) =>
                    {
                        System.Console.WriteLine("Helper");
                        c.Publish(arg.Message.Reply,
                            Encoding.UTF8.GetBytes("Hello"));
                    };
                    helper.Start();

                    start.MessageHandler += (sender, arg) =>
                    {
                        System.Console.WriteLine("Responsder");
		                string responseIB = c.NewInbox();
                        IAsyncSubscription ia = c.SubscribeAsync(responseIB);

                        ia.MessageHandler += (iSender, iArgs) =>
                        {
                            System.Console.WriteLine("Internal subscriber.");
                            lock (waitCond) { Monitor.Pulse(waitCond); }
                        };
                        ia.Start();
 
		                c.Publish("helper", responseIB,
                            Encoding.UTF8.GetBytes("Help me!"));
                    };

                    start.Start();
                     
                    c.Publish("start", Encoding.UTF8.GetBytes("Begin"));
                    c.Flush();

                    lock (waitCond) 
                    { 
                        Assert.IsTrue(Monitor.Wait(waitCond, 2000));
                    }
                }
            }
        }
示例#10
0
        public void TestSlowAsyncSubscriber()
        {
            Options opts = ConnectionFactory.GetDefaultOptions();
            opts.SubChannelLength = 10;

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

                    s.MessageHandler += (sender, args) =>
                    {
                        lock (mu)
                        {
                            Console.WriteLine("Subscriber Waiting....");
                            Assert.IsTrue(Monitor.Wait(mu, 20000));
                            Console.WriteLine("Subscriber done.");
                        }
                    };

                    s.Start();

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

                    int flushTimeout = 1000;

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

                    bool flushFailed = false;
                    try
                    {
                        c.Flush(flushTimeout);
                    }
                    catch (Exception)
                    {
                        flushFailed = true;
                    }

                    sw.Stop();

                    lock (mu)
                    {
                        Monitor.Pulse(mu);
                    }

                    if (sw.ElapsedMilliseconds < flushTimeout)
                    {
                        Assert.Fail("elapsed ({0}) < timeout ({1})",
                            sw.ElapsedMilliseconds, flushTimeout);
                    }
                    
                    Assert.IsTrue(flushFailed);
                }
            }
        }
示例#11
0
        public void TestAsyncSubscribe()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                using (IAsyncSubscription s = c.SubscribeAsync("foo"))
                {
                    asyncSub = s;
                    s.MessageHandler += CheckReceivedAndValidHandler;
                    s.Start();

                    lock (mu)
                    {
                        received = false;
                        c.Publish("foo", omsg);
                        c.Flush();
                        Monitor.Wait(mu, 30000);
                    }

                    if (!received)
                        Assert.Fail("Did not receive message.");
                }
            }
        }
示例#12
0
        public void TestSyncReplyArg()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                using (ISyncSubscription s = c.SubscribeSync("foo"))
                {
                    c.Publish("foo", "bar", null);
                    c.Flush(30000);

                    Msg m = s.NextMessage(1000);
                    if ("bar".Equals(m.Reply) == false)
                        Assert.Fail("Expected \"bar\", received: " + m);
                }
            }
        }
示例#13
0
 public void TestFlush()
 {
     using (IConnection c = new ConnectionFactory().CreateConnection())
     {
         using (ISyncSubscription s = c.SubscribeSync("foo"))
         {
             c.Publish("foo", "reply", omsg);
             c.Flush();
         }
     }
 }
示例#14
0
        public void TestEncodedInvalidObjectSerialization()
        {
            using (IEncodedConnection c = new ConnectionFactory().CreateEncodedConnection())
            {
                String myStr = "value";
                Object mu = new Object();

                bool hitException = false;

                EventHandler<EncodedMessageEventArgs> eh = (sender, args) =>
                {
                    // Ensure we blow up in the cast
                    try
                    {
                        Exception invalid = (Exception)args.ReceivedObject;
                    }
                    catch (Exception e)
                    {
                        hitException = true;
                        System.Console.WriteLine("Expected exception: " + e.Message);
                    }

                    Assert.IsTrue(hitException);

                    lock (mu)
                    {
                        Monitor.Pulse(mu);
                    }
                };

                using (IAsyncSubscription s = c.SubscribeAsync("foo", eh))
                {
                    lock (mu)
                    {
                        c.Publish("foo", myStr);
                        c.Flush();

                        Monitor.Wait(mu, 1000);
                    }
                }
            }
        }
示例#15
0
        public void TestEncodedSerizationOverrides()
        {
            using (IEncodedConnection c = new ConnectionFactory().CreateEncodedConnection())
            {
                c.OnDeserialize = deserializeFromXML;
                c.OnSerialize = serializeToXML;

                Object mu = new Object();
                SerializationTestObj origObj = new SerializationTestObj();
                origObj.a = 99;

                EventHandler<EncodedMessageEventArgs> eh = (sender, args) =>
                {
                    SerializationTestObj so = (SerializationTestObj)args.ReceivedObject;
                    Assert.IsTrue(so.Equals(origObj));

                    lock (mu)
                    {
                        Monitor.Pulse(mu);
                    }
                };

                using (IAsyncSubscription s = c.SubscribeAsync("foo", eh))
                {
                    lock (mu)
                    {
                        c.Publish("foo", origObj);
                        c.Flush();

                        Monitor.Wait(mu, 1000);
                    }
                }
            }
        }
示例#16
0
        public void TestEncodedObjectSerization()
        {
            using (IEncodedConnection c = new ConnectionFactory().CreateEncodedConnection())
            {
                String myStr = "value";
                Object mu = new Object();

                EventHandler<EncodedMessageEventArgs> eh = (sender, args) =>
                {
                    Assert.IsTrue(args.ReceivedObject.Equals(myStr));
                    lock (mu)
                    {
                        Monitor.Pulse(mu);
                    }
                };

                using (IAsyncSubscription s = c.SubscribeAsync("foo", eh))
                {
                    lock (mu)
                    {
                        for (int i = 0; i < 10; i++)
                            c.Publish("foo", myStr);

                        c.Flush();

                        Monitor.Wait(mu, 1000);
                    }
                }

                using (IAsyncSubscription s = c.SubscribeAsync("foo", eh))
                {
                    lock (mu)
                    {
                        c.Publish("foo", "bar", myStr);
                        c.Flush();

                        Monitor.Wait(mu, 1000);
                    }
                }
            }
        }
示例#17
0
        public void TestEncodedObjectRequestReply()
        {
            using (IEncodedConnection c = new ConnectionFactory().CreateEncodedConnection())
            {
                Object mu = new Object();
                SerializationTestObj origObj = new SerializationTestObj();

                EventHandler<EncodedMessageEventArgs> eh = (sender, args) =>
                {
                    SerializationTestObj so = (SerializationTestObj)args.ReceivedObject;
                    Assert.IsTrue(so.Equals(origObj));
                    String str = "Received";

                    c.Publish(args.Reply, str);
                    c.Flush();

                    lock (mu)
                    {
                        Monitor.Pulse(mu);
                    }
                };

                using (IAsyncSubscription s = c.SubscribeAsync("foo", eh))
                {
                    Assert.IsTrue("Received".Equals(c.Request("foo", origObj, 1000)));
                    Assert.IsTrue("Received".Equals(c.Request("foo", origObj)));
                }
            }
        }
示例#18
0
        public void TestSendAndRecv()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                using (IAsyncSubscription s = c.SubscribeAsync("foo"))
                {
                    int received = 0;
                    int count = 1000;

                    s.MessageHandler += (sender, args) =>
                    {
                        Interlocked.Increment(ref received);
                    };

                    s.Start();

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

                    Thread.Sleep(500);

                    if (received != count)
                    {
                        Assert.Fail("Received ({0}) != count ({1})", received, count);
                    }
                }
            }
        }
示例#19
0
        public void TestStats()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                byte[] data = Encoding.UTF8.GetBytes("The quick brown fox jumped over the lazy dog");
                int iter = 10;

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

                IStatistics stats = c.Stats;
                Assert.AreEqual(iter, stats.OutMsgs);
                Assert.AreEqual(iter * data.Length, stats.OutBytes);

                c.ResetStats();

                // Test both sync and async versions of subscribe.
                IAsyncSubscription s1 = c.SubscribeAsync("foo");
                s1.MessageHandler += (sender, arg) => { };
                s1.Start();

                ISyncSubscription s2 = c.SubscribeSync("foo");

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

                stats = c.Stats;
                Assert.AreEqual(2 * iter, stats.InMsgs);
                Assert.AreEqual(2 * iter * data.Length, stats.InBytes);
            }
        }
示例#20
0
        public void TestFlushInHandler()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                using (IAsyncSubscription s = c.SubscribeAsync("foo"))
                {
                    byte[] response = Encoding.UTF8.GetBytes("I will help you.");

                    s.MessageHandler += (sender, args) =>
                    {
                        try
                        {
                            c.Flush();
                            System.Console.WriteLine("Success.");
                        }
                        catch (Exception e)
                        {
                            Assert.Fail("Unexpected exception: " + e);
                        }

                        lock (mu)
                        {
                            Monitor.Pulse(mu);
                        }
                    };

                    s.Start();

                    lock (mu)
                    {
                        c.Publish("foo", Encoding.UTF8.GetBytes("Hello"));
                        Monitor.Wait(mu);
                    }
                }
            }
        }
示例#21
0
        public void TestUnsubscribe()
        {
            int count = 0;
            int max = 20;

            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                using (IAsyncSubscription s = c.SubscribeAsync("foo"))
                {
                    Boolean unsubscribed = false;
                    asyncSub = s;
                    //s.MessageHandler += UnsubscribeAfterCount;
                    s.MessageHandler += (sender, args) =>
                    {
                        count++;
                        System.Console.WriteLine("Count = {0}", count);
                        if (count == max)
                        {
                            asyncSub.Unsubscribe();
                            lock (mu)
                            {
                                unsubscribed = true;
                                Monitor.Pulse(mu);
                            }
                        }
                    };
                    s.Start();

                    max = 20;
                    for (int i = 0; i < max; i++)
                    {
                        c.Publish("foo", null, null);
                    }
                    Thread.Sleep(100);
                    c.Flush();

                    lock (mu)
                    {
                        if (!unsubscribed)
                        {
                            Monitor.Wait(mu, 5000);
                        }
                    }
                }

                if (count != max)
                    Assert.Fail("Received wrong # of messages after unsubscribe: {0} vs {1}", count, max);
            }
        }
示例#22
0
        public void TestLargeMessage()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                int msgSize = 51200;
                byte[] msg = new byte[msgSize];

                for (int i = 0; i < msgSize; i++)
                    msg[i] = (byte)'A';

                msg[msgSize-1] = (byte)'Z';

                using (IAsyncSubscription s = c.SubscribeAsync("foo"))
                {
                    Object testLock = new Object();

                    s.MessageHandler += (sender, args) =>
                    {
                        lock(testLock)
                        {
                            Monitor.Pulse(testLock);
                        }
                        Assert.IsTrue(compare(msg, args.Message.Data));
                    };

                    s.Start();

                    c.Publish("foo", msg);
                    c.Flush(1000);

                    lock(testLock)
                    {
                        Monitor.Wait(testLock, 2000);
                    }
                }
            }
        }
示例#23
0
        // TODO [TestMethod]
        public void TestSlowSubscriber()
        {
            Options opts = ConnectionFactory.GetDefaultOptions();
            opts.SubChannelLength = 10;

            using (IConnection c = new ConnectionFactory().CreateConnection(opts))
            {
                using (ISyncSubscription s = c.SubscribeSync("foo"))
                {
                    for (int i =0; i < (opts.SubChannelLength+100); i++)
                    {
                        c.Publish("foo", null);
                    }

                    try
                    {
                        c.Flush();
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex);
                        if (ex.InnerException != null)
                            System.Console.WriteLine(ex.InnerException);

                        throw ex;
                    }

                    try 
                    {
                        s.NextMessage();
                    }
                    catch (NATSSlowConsumerException)
                    {
                        return;
                    }
                    Assert.Fail("Did not receive an exception.");
                }
            } 
        }
示例#24
0
        public void TestLargeSubjectAndReply()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                String subject = "";
                for (int i = 0; i < 1024; i++)
                {
                    subject += "A";
                }

                String reply = "";
                for (int i = 0; i < 1024; i++)
                {
                    reply += "A";
                }

                using (IAsyncSubscription s = c.SubscribeAsync(subject))
                {
                    Object testLock = new Object();

                    s.MessageHandler += (sender, args) =>
                    {
                        if (!subject.Equals(args.Message.Subject))
                            Assert.Fail("Invalid subject received.");

                        if (!reply.Equals(args.Message.Reply))
                            Assert.Fail("Invalid subject received.");

                        lock (testLock)
                        {
                            Monitor.Pulse(testLock);
                        }
                    };

                    s.Start();

                    c.Publish(subject, reply, null);
                    c.Flush();

                    lock (testLock)
                    {
                        Assert.IsTrue(Monitor.Wait(testLock, 1000));
                    }
                }
            }
        }
示例#25
0
        public void TestAsyncErrHandler()
        {
            Object subLock = new Object();
            object testLock = new Object();
            IAsyncSubscription s;


            Options opts = ConnectionFactory.GetDefaultOptions();
            opts.SubChannelLength = 10;

            bool handledError = false;

            using (IConnection c = new ConnectionFactory().CreateConnection(opts))
            {
                using (s = c.SubscribeAsync("foo"))
                {
                    opts.AsyncErrorEventHandler = (sender, args) =>
                    {
                        lock (subLock)
                        {
                            if (handledError)
                                return;

                            handledError = true;

                            Assert.IsTrue(args.Subscription == s);

                            System.Console.WriteLine("Expected Error: " + args.Error);
                            Assert.IsTrue(args.Error.Contains("Slow"));

                            // release the subscriber
                            Monitor.Pulse(subLock);
                        }

                        // release the test
                        lock (testLock) { Monitor.Pulse(testLock); }
                    };

                    bool blockedOnSubscriber = false;
                    s.MessageHandler += (sender, args) =>
                    {
                        lock (subLock)
                        {
                            if (blockedOnSubscriber)
                                return;

                            Console.WriteLine("Subscriber Waiting....");
                            Assert.IsTrue(Monitor.Wait(subLock, 10000));
                            Console.WriteLine("Subscriber done.");
                            blockedOnSubscriber = true;
                        }
                    };

                    s.Start();

                    lock(testLock)
                    {

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

                        Assert.IsTrue(Monitor.Wait(testLock, 1000));
                    }
                }
            }
        }
示例#26
0
        public void TestAsyncSubscribersOnClose()
        {
            /// basically tests if the subscriber sub channel gets
            /// cleared on a close.
            Object waitCond = new Object();
            int callbacks = 0;

            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                using (IAsyncSubscription s = c.SubscribeAsync("foo"))
                {
                    s.MessageHandler += (sender, args) =>
                    {
                        callbacks++;
                        lock (waitCond)
                        {
                            Monitor.Wait(waitCond);
                        }
                    };

                    s.Start();

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

                    Thread.Sleep(500);
                    c.Close();

                    lock (waitCond)
                    {
                        Monitor.Pulse(waitCond);
                    }

                    Thread.Sleep(500);

                    Assert.IsTrue(callbacks == 1);
                }
            }
        }
示例#27
0
        public void TestQueueSubscriber()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                using (ISyncSubscription s1 = c.SubscribeSync("foo", "bar"),
                                         s2 = c.SubscribeSync("foo", "bar"))
                {
                    c.Publish("foo", omsg);
                    c.Flush(1000);

                    if (s1.QueuedMessageCount + s2.QueuedMessageCount != 1)
                        Assert.Fail("Invalid message count in queue.");

                    // Drain the messages.
                    try { s1.NextMessage(100); }
                    catch (NATSTimeoutException) { }

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

                    int total = 1000;

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

                    Thread.Sleep(1000);

                    int r1 = s1.QueuedMessageCount;
                    int r2 = s2.QueuedMessageCount;

                    if ((r1 + r2) != total)
                    {
                        Assert.Fail("Incorrect number of messages: {0} vs {1}",
                            (r1 + r2), total);
                    }

                    if (Math.Abs(r1 - r2) > (total * .15))
                    {
                        Assert.Fail("Too much variance between {0} and {1}",
                            r1, r2);
                    }
                }
            }
        }
示例#28
0
        public void TestSubDelTaskCountScaling()
        {
            int COUNT = 20000;
            var opts = utils.DefaultTestOptions;
            opts.SubscriberDeliveryTaskCount = 20;

            using (new NATSServer())
            {
                using (IConnection c = new ConnectionFactory().CreateConnection(opts))
                {
                    long recvCount = 0;

                    var subs = new List<IAsyncSubscription>();

                    EventHandler<MsgHandlerEventArgs> eh = (obj, args) =>
                    {
                        Interlocked.Increment(ref recvCount);
                    };

                    for (int i = 0; i < COUNT; i++)
                    {
                        subs.Add(c.SubscribeAsync("foo", eh));
                    }

                    c.Publish("foo", null);
                    c.Flush();

                    while (Interlocked.Read(ref recvCount) != (COUNT))
                    {
                        Thread.Sleep(100);
                    }

                    // ensure we are not creating a thread per subscriber.
                    Assert.True(Process.GetCurrentProcess().Threads.Count < 500);

                    subs.ForEach((s) => { s.Unsubscribe(); });
                }
            }
        }
示例#29
0
        public void TestClientAutoUnsub()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                long received = 0;
                int max = 10;

                using (ISyncSubscription s = c.SubscribeSync("foo"))
                {
                    s.AutoUnsubscribe(max);

                    for (int i = 0; i < max * 2; i++)
                    {
                        c.Publish("foo", null);
                    }
                    c.Flush();

                    Thread.Sleep(100);

                    try
                    {
                        while (true)
                        {
                            s.NextMessage(0);
                            received++;
                        }
                    }
                    catch (NATSBadSubscriptionException) { /* ignore */ }

                    Assert.IsTrue(received == max);
                    Assert.IsFalse(s.IsValid);
                }
            }
        }
示例#30
0
        public void TestReleaseFlush()
        {
            IConnection c = new ConnectionFactory().CreateConnection();

            for (int i = 0; i < 1000; i++)
            {
                c.Publish("foo", Encoding.UTF8.GetBytes("Hello"));
            }

            new Task(() => { c.Close(); }).Start();
            c.Flush();
        }