コード例 #1
0
ファイル: UnitTestConn.cs プロジェクト: bendan365/csnats-wip
        public void TestCloseDisconnectedHandler()
        {
            bool disconnected = false;
            Object mu = new Object();

            Options o = ConnectionFactory.GetDefaultOptions();
            o.AllowReconnect = false;
            o.DisconnectedEventHandler += (sender, args) => {
                lock (mu)
                {
                    disconnected = true;
                    Monitor.Pulse(mu);
                }
            };

            IConnection c = new ConnectionFactory().CreateConnection(o);
            lock (mu)
            {
                c.Close();
                Monitor.Wait(mu, 20000);
            }
            Assert.IsTrue(disconnected);

            // now test using.
            disconnected = false;
            lock (mu)
            {
                using (c = new ConnectionFactory().CreateConnection(o)) { };
                Monitor.Wait(mu);
            }
            Assert.IsTrue(disconnected);
        }
コード例 #2
0
ファイル: UnitTestAuth.cs プロジェクト: bendan365/csnats-wip
        private void connectAndFail(String url)
        {
            try
            {
                System.Console.WriteLine("Trying: " + url);

                hitDisconnect = 0;
                Options opts = ConnectionFactory.GetDefaultOptions();
                opts.Url = url;
                opts.DisconnectedEventHandler += handleDisconnect;
                IConnection c = new ConnectionFactory().CreateConnection(url);

                Assert.Fail("Expected a failure; did not receive one");
                
                c.Close();
            }
            catch (Exception e)
            {
                if (e.Message.Contains("Authorization"))
                {
                    System.Console.WriteLine("Success with expected failure: " + e.Message);
                }
                else
                {
                    Assert.Fail("Unexpected exception thrown: " + e);
                }
            }
            finally
            {
                if (hitDisconnect > 0)
                    Assert.Fail("The disconnect event handler was incorrectly invoked.");
            }
        }
コード例 #3
0
ファイル: UnitTestConn.cs プロジェクト: bendan365/csnats-wip
 public void TestConnectionStatus()
 {
     IConnection c = new ConnectionFactory().CreateConnection();
     Assert.AreEqual(ConnState.CONNECTED, c.State);
     c.Close();
     Assert.AreEqual(ConnState.CLOSED, c.State);
 }
コード例 #4
0
ファイル: UnitTestAuth.cs プロジェクト: ColinSullivan1/csnats
 public void TestAuthSuccess()
 {
     using (NATSServer s = util.CreateServerWithConfig(TestContext, "auth_1222.conf"))
     {
         IConnection c = new ConnectionFactory().CreateConnection("nats://*****:*****@localhost:1222");
         c.Close();
     }
 }
コード例 #5
0
ファイル: UnitTestConn.cs プロジェクト: ColinSullivan1/csnats
        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));
        }
コード例 #6
0
 private bool isNatsServerRunning()
 {
     try
     {
         IConnection c = new NATS.Client.ConnectionFactory().CreateConnection();
         c.Close();
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
コード例 #7
0
ファイル: UnitTestBasic.cs プロジェクト: bendan365/csnats-wip
        public void TestMultipleClose()
        {
            IConnection c = new ConnectionFactory().CreateConnection();
            
            Task[] tasks = new Task[10];

            for (int i = 0; i < 10; i++)
            {

                tasks[i] = new Task(() => { c.Close(); });
                tasks[i].Start();
            }

            Task.WaitAll(tasks);
        }
コード例 #8
0
ファイル: UnitTestConn.cs プロジェクト: bendan365/csnats-wip
        public void TestCloseHandler()
        {
            bool closed = false;

            Options o = ConnectionFactory.GetDefaultOptions();
            o.ClosedEventHandler += (sender, args) => { closed = true; };
            IConnection c = new ConnectionFactory().CreateConnection(o);
            c.Close();
            Assert.IsTrue(closed);

            // now test using.
            closed = false;
            using (c = new ConnectionFactory().CreateConnection(o)) { };
            Assert.IsTrue(closed);
        }
コード例 #9
0
ファイル: UnitTestBasic.cs プロジェクト: bendan365/csnats-wip
        public void TestConnectedServer()
        {
            IConnection c = new ConnectionFactory().CreateConnection();
           
            string u = c.ConnectedUrl;
            
            if (string.IsNullOrWhiteSpace(u))
                Assert.Fail("Invalid connected url {0}.", u);
                
            if (!Defaults.Url.Equals(u))
                Assert.Fail("Invalid connected url {0}.", u);

            c.Close();
            u = c.ConnectedUrl;

            if (u != null)
                Assert.Fail("Url is not null after connection is closed.");
        }
コード例 #10
0
ファイル: UnitTestUtilities.cs プロジェクト: nats-io/csnats
 public NATSServer(bool verify)
 {
     createProcessStartInfo();
     p = Process.Start(psInfo);
     if (verify)
     {
         for (int i = 0; i < 10; i++)
         {
             try
             {
                 var c = new ConnectionFactory().CreateConnection();
                 c.Close();
                 break;
             }
             catch
             {
                 Thread.Sleep(i * 250);
             }
         }
     }
 }
コード例 #11
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();
        }
コード例 #12
0
 public void TestCloseAndDispose()
 {
     using (IConnection c = new ConnectionFactory().CreateConnection())
     {
         c.Close();
     }
 }
コード例 #13
0
        public void TestClose()
        {
            Options opts = ConnectionFactory.GetDefaultOptions();
            opts.Url = "nats://localhost:22222";
            opts.AllowReconnect = true;
            opts.MaxReconnect = 60;

            using (NATSServer s1 = utils.CreateServerOnPort(22222))
            {
                IConnection c = new ConnectionFactory().CreateConnection(opts);
                Assert.IsFalse(c.IsClosed());

                s1.Shutdown();

                Thread.Sleep(100);
                if (c.IsClosed())
                {
                    Assert.Fail("Invalid state, expecting not closed, received: "
                        + c.State.ToString());
                }

                using (NATSServer s2 = utils.CreateServerOnPort(22222))
                {
                    Thread.Sleep(1000);
                    Assert.IsFalse(c.IsClosed());

                    c.Close();
                    Assert.IsTrue(c.IsClosed());
                }
            }
        }
コード例 #14
0
ファイル: UnitTestAuth.cs プロジェクト: nats-io/csnats
        private void connectAndFail(String url)
        {
            try
            {
                hitDisconnect = 0;
                Options opts = util.DefaultTestOptions;
                opts.Url = url;
                opts.DisconnectedEventHandler += handleDisconnect;
                IConnection c = new ConnectionFactory().CreateConnection(url);
                Assert.True(false, "Expected a failure; did not receive one");

                c.Close();
            }
            catch (Exception e)
            {
                Assert.True(e.Message.Contains("Authorization"));
            }
            finally
            {
                Assert.False(hitDisconnect > 0, "The disconnect event handler was incorrectly invoked.");
            }
        }
コード例 #15
0
ファイル: UnitTestConn.cs プロジェクト: bendan365/csnats-wip
        public void TestServerStopDisconnectedHandler()
        {
            bool disconnected = false;
            Object mu = new Object();

            Options o = ConnectionFactory.GetDefaultOptions();
            o.AllowReconnect = false;
            o.DisconnectedEventHandler += (sender, args) =>
            {
                lock (mu)
                {
                    disconnected = true;
                    Monitor.Pulse(mu);
                }
            };

            IConnection c = new ConnectionFactory().CreateConnection(o);
            lock (mu)
            {
                utils.bounceDefaultServer(1000);
                Monitor.Wait(mu);
            }
            c.Close();
            Assert.IsTrue(disconnected);
        }
コード例 #16
0
ファイル: UnitTestReconnect.cs プロジェクト: nats-io/csnats
        public void TestIsReconnectingAndStatus()
        {
            bool disconnected = false;
            object disconnectedLock = new object();

            bool reconnected = false;
            object reconnectedLock = new object();

            IConnection c = null;

            Options opts = utils.DefaultTestOptions;
            opts.Url = "nats://localhost:22222";
            opts.AllowReconnect = true;
            opts.MaxReconnect = 10000;
            opts.ReconnectWait = 100;

            opts.DisconnectedEventHandler += (sender, args) =>
            {
                lock (disconnectedLock)
                {
                    disconnected = true;
                    Monitor.Pulse(disconnectedLock);
                }
            };

            opts.ReconnectedEventHandler += (sender, args) =>
            {
                lock (reconnectedLock)
                {
                    reconnected = true;
                    Monitor.Pulse(reconnectedLock);
                }
            };

            using (NATSServer s = utils.CreateServerOnPort(22222))
            {
                c = new ConnectionFactory().CreateConnection(opts);

                Assert.True(c.State == ConnState.CONNECTED);
                Assert.True(c.IsReconnecting() == false);
            }
            // server stops here...

            lock (disconnectedLock)
            {
                if (!disconnected)
                    Assert.True(Monitor.Wait(disconnectedLock, 10000));
            }

            Assert.True(c.State == ConnState.RECONNECTING);
            Assert.True(c.IsReconnecting() == true);

            // restart the server
            using (NATSServer s = utils.CreateServerOnPort(22222))
            {
                lock (reconnectedLock)
                {
                    // may have reconnected, if not, wait
                    if (!reconnected)
                        Assert.True(Monitor.Wait(reconnectedLock, 10000));
                }

                Assert.True(c.IsReconnecting() == false);
                Assert.True(c.State == ConnState.CONNECTED);

                c.Close();
            }

            Assert.True(c.IsReconnecting() == false);
            Assert.True(c.State == ConnState.CLOSED);
        }
コード例 #17
0
ファイル: UnitTestSub.cs プロジェクト: bendan365/csnats-wip
        public void TestCloseSubRelease()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                using (ISyncSubscription s = c.SubscribeSync("foo"))
                {
                    Stopwatch sw = new Stopwatch();
                    sw.Start();
                    try
                    {
                        new Task(() => { Thread.Sleep(100); c.Close(); }).Start();
                         s.NextMessage(10000);
                    }
                    catch (Exception) { /* ignore */ }

                    sw.Stop();

                    Assert.IsTrue(sw.ElapsedMilliseconds < 10000);
                }
            }
        }
コード例 #18
0
ファイル: UnitTestReconnect.cs プロジェクト: nats-io/csnats
        public void TestClose()
        {
            Options opts = utils.DefaultTestOptions;
            opts.Url = "nats://localhost:22222";
            opts.AllowReconnect = true;
            opts.MaxReconnect = 60;

            using (NATSServer s1 = utils.CreateServerOnPort(22222))
            {
                IConnection c = new ConnectionFactory().CreateConnection(opts);
                Assert.False(c.IsClosed());

                s1.Shutdown();

                Thread.Sleep(100);
                Assert.False(c.IsClosed(), string.Format("Invalid state, expecting not closed, received: {0}", c.State));

                using (NATSServer s2 = utils.CreateServerOnPort(22222))
                {
                    Thread.Sleep(1000);
                    Assert.False(c.IsClosed());

                    c.Close();
                    Assert.True(c.IsClosed());
                }
            }
        }
コード例 #19
0
ファイル: UnitTestSub.cs プロジェクト: bendan365/csnats-wip
        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);
                }
            }
        }
コード例 #20
0
ファイル: UnitTestConn.cs プロジェクト: ColinSullivan1/csnats
        public void TestConnectVerbose()
        {
            Options o = ConnectionFactory.GetDefaultOptions();
            o.Verbose = true;

            IConnection c = new ConnectionFactory().CreateConnection(o);
            c.Close();
        }