當前位置: 首頁>>代碼示例>>C#>>正文


C# ConnectionFactory.Close方法代碼示例

本文整理匯總了C#中NATS.Client.ConnectionFactory.Close方法的典型用法代碼示例。如果您正苦於以下問題:C# ConnectionFactory.Close方法的具體用法?C# ConnectionFactory.Close怎麽用?C# ConnectionFactory.Close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在NATS.Client.ConnectionFactory的用法示例。


在下文中一共展示了ConnectionFactory.Close方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: TestCloseDisconnectedHandler

        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);
        }
開發者ID:bendan365,項目名稱:csnats-wip,代碼行數:32,代碼來源:UnitTestConn.cs

示例2: TestConnectionStatus

 public void TestConnectionStatus()
 {
     IConnection c = new ConnectionFactory().CreateConnection();
     Assert.AreEqual(ConnState.CONNECTED, c.State);
     c.Close();
     Assert.AreEqual(ConnState.CLOSED, c.State);
 }
開發者ID:bendan365,項目名稱:csnats-wip,代碼行數:7,代碼來源:UnitTestConn.cs

示例3: connectAndFail

        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.");
            }
        }
開發者ID:bendan365,項目名稱:csnats-wip,代碼行數:33,代碼來源:UnitTestAuth.cs

示例4: TestAuthSuccess

 public void TestAuthSuccess()
 {
     using (NATSServer s = util.CreateServerWithConfig(TestContext, "auth_1222.conf"))
     {
         IConnection c = new ConnectionFactory().CreateConnection("nats://username:[email protected]:1222");
         c.Close();
     }
 }
開發者ID:ColinSullivan1,項目名稱:csnats,代碼行數:8,代碼來源:UnitTestAuth.cs

示例5: TestClosedConnections

        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));
        }
開發者ID:ColinSullivan1,項目名稱:csnats,代碼行數:53,代碼來源:UnitTestConn.cs

示例6: TestCloseHandler

        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);
        }
開發者ID:bendan365,項目名稱:csnats-wip,代碼行數:15,代碼來源:UnitTestConn.cs

示例7: TestMultipleClose

        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);
        }
開發者ID:bendan365,項目名稱:csnats-wip,代碼行數:15,代碼來源:UnitTestBasic.cs

示例8: TestConnectedServer

        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.");
        }
開發者ID:bendan365,項目名稱:csnats-wip,代碼行數:18,代碼來源:UnitTestBasic.cs

示例9: NATSServer

 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);
             }
         }
     }
 }
開發者ID:nats-io,項目名稱:csnats,代碼行數:21,代碼來源:UnitTestUtilities.cs

示例10: connectAndFail

        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.");
            }
        }
開發者ID:nats-io,項目名稱:csnats,代碼行數:22,代碼來源:UnitTestAuth.cs

示例11: TestServerStopDisconnectedHandler

        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);
        }
開發者ID:bendan365,項目名稱:csnats-wip,代碼行數:25,代碼來源:UnitTestConn.cs

示例12: TestCloseSubRelease

        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);
                }
            }
        }
開發者ID:bendan365,項目名稱:csnats-wip,代碼行數:21,代碼來源:UnitTestSub.cs

示例13: TestIsReconnectingAndStatus

        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);
        }
開發者ID:nats-io,項目名稱:csnats,代碼行數:71,代碼來源:UnitTestReconnect.cs

示例14: TestClose

        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());
                }
            }
        }
開發者ID:ColinSullivan1,項目名稱:csnats,代碼行數:31,代碼來源:UnitTestReconnect.cs

示例15: TestCloseAndDispose

 public void TestCloseAndDispose()
 {
     using (IConnection c = new ConnectionFactory().CreateConnection())
     {
         c.Close();
     }
 }
開發者ID:ColinSullivan1,項目名稱:csnats,代碼行數:7,代碼來源:UnitTestBasic.cs


注:本文中的NATS.Client.ConnectionFactory.Close方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。