当前位置: 首页>>代码示例>>C#>>正文


C# ConnectionFactory.SubscribeSync方法代码示例

本文整理汇总了C#中NATS.Client.ConnectionFactory.SubscribeSync方法的典型用法代码示例。如果您正苦于以下问题:C# ConnectionFactory.SubscribeSync方法的具体用法?C# ConnectionFactory.SubscribeSync怎么用?C# ConnectionFactory.SubscribeSync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NATS.Client.ConnectionFactory的用法示例。


在下文中一共展示了ConnectionFactory.SubscribeSync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: 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

示例2: TestClientAutoUnsub

        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);
                }
            }
        }
开发者ID:bendan365,项目名称:csnats-wip,代码行数:34,代码来源:UnitTestSub.cs

示例3: TestSlowSubscriber

        // 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.");
                }
            } 
        }
开发者ID:bendan365,项目名称:csnats-wip,代码行数:40,代码来源:UnitTestSub.cs

示例4: TestValidSubscriber

        public void TestValidSubscriber()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                using (ISyncSubscription s = c.SubscribeSync("foo"))
                {
                    Assert.IsTrue(s.IsValid);

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

                    Assert.IsTrue(s.IsValid);

                    s.Unsubscribe();

                    Assert.IsFalse(s.IsValid);

                    try { s.NextMessage(100); }
                    catch (NATSBadSubscriptionException) { }
                }
            }
        }
开发者ID:bendan365,项目名称:csnats-wip,代码行数:22,代码来源:UnitTestSub.cs

示例5: 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

示例6: TestSyncSubscribe

 public void TestSyncSubscribe()
 {
     using (IConnection c = new ConnectionFactory().CreateConnection())
     {
         using (ISyncSubscription s = c.SubscribeSync("foo"))
         {
             c.Publish("foo", omsg);
             Msg m = s.NextMessage(1000);
             if (compare(omsg, m) == false)
                 Assert.Fail("Messages are not equal.");
         }
     }
 }
开发者ID:ColinSullivan1,项目名称:csnats,代码行数:13,代码来源:UnitTestBasic.cs

示例7: TestSyncReplyArg

        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);
                }
            }
        }
开发者ID:ColinSullivan1,项目名称:csnats,代码行数:15,代码来源:UnitTestBasic.cs

示例8: TestStats

        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);
            }
        }
开发者ID:ColinSullivan1,项目名称:csnats,代码行数:37,代码来源:UnitTestBasic.cs

示例9: TestQueueSubscriber

        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);
                    }
                }
            }
        }
开发者ID:ColinSullivan1,项目名称:csnats,代码行数:47,代码来源:UnitTestBasic.cs

示例10: TestFlush

 public void TestFlush()
 {
     using (IConnection c = new ConnectionFactory().CreateConnection())
     {
         using (ISyncSubscription s = c.SubscribeSync("foo"))
         {
             c.Publish("foo", "reply", omsg);
             c.Flush();
         }
     }
 }
开发者ID:ColinSullivan1,项目名称:csnats,代码行数:11,代码来源:UnitTestBasic.cs

示例11: TestDoubleUnsubscribe

        public void TestDoubleUnsubscribe()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                using (ISyncSubscription s = c.SubscribeSync("foo"))
                {
                    s.Unsubscribe();

                    try
                    {
                        s.Unsubscribe();
                        Assert.Fail("No Exception thrown.");
                    }
                    catch (Exception e)
                    {
                        System.Console.WriteLine("Expected exception {0}: {1}",
                            e.GetType(), e.Message);
                    }
                }
            }
        }
开发者ID:ColinSullivan1,项目名称:csnats,代码行数:21,代码来源:UnitTestBasic.cs

示例12: TestSubDelTaskCountWithSyncSub

        public void TestSubDelTaskCountWithSyncSub()
        {
            var opts = utils.DefaultTestOptions;
            opts.SubscriberDeliveryTaskCount = 1;

            using (new NATSServer())
            {
                using (IConnection c = new ConnectionFactory().CreateConnection(opts))
                {
                    ISyncSubscription s = c.SubscribeSync("foo");
                    c.Publish("foo", null);
                    s.NextMessage(10000);
                }
            }
        }
开发者ID:nats-io,项目名称:csnats,代码行数:15,代码来源:UnitTestSub.cs

示例13: TestSlowSubscriber

        public void TestSlowSubscriber()
        {
            Options opts = utils.DefaultTestOptions;
            opts.SubChannelLength = 10;

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

                            try
                            {
                                c.Flush();
                            }
                            catch (Exception)
                            {
                            // ignore
                        }

                            s.NextMessage();
                        });
                    }
                }
            }
        }
开发者ID:nats-io,项目名称:csnats,代码行数:33,代码来源:UnitTestSub.cs

示例14: TestTlsSuccessSecureConnect

        public void TestTlsSuccessSecureConnect()
        {
            using (NATSServer srv = util.CreateServerWithConfig(TestContext, "tls_1222.conf"))
            {
                // we can't call create secure connection w/ the certs setup as they are
                // so we'll override the
                Options opts = ConnectionFactory.GetDefaultOptions();
                opts.Secure = true;
                opts.TLSRemoteCertificationValidationCallback = verifyServerCert;
                opts.Url = "nats://localhost:1222";

                using (IConnection c = new ConnectionFactory().CreateConnection(opts))
                {
                    using (ISyncSubscription s = c.SubscribeSync("foo"))
                    {
                        c.Publish("foo", null);
                        c.Flush();
                        Msg m = s.NextMessage();
                        System.Console.WriteLine("Received msg over TLS conn.");
                    }
                }
            }
        }
开发者ID:ColinSullivan1,项目名称:csnats,代码行数:23,代码来源:UnitTestTLS.cs

示例15: TestTlsSuccessWithCert

        public void TestTlsSuccessWithCert()
        {
            using (NATSServer srv = util.CreateServerWithConfig(TestContext, "tls_1222_verify.conf"))
            {
                Options opts = ConnectionFactory.GetDefaultOptions();
                opts.Secure = true;
                opts.Url = "nats://localhost:1222";
                opts.TLSRemoteCertificationValidationCallback = verifyServerCert;

                // .NET requires the private key and cert in the
                //  same file. 'client.pfx' is generated from:
                //
                // openssl pkcs12 -export -out client.pfx
                //    -inkey client-key.pem -in client-cert.pem
                X509Certificate2 cert = new X509Certificate2(
                    UnitTestUtilities.GetFullCertificatePath(
                        TestContext, "client.pfx"), "password");

                opts.AddCertificate(cert);

                using (IConnection c = new ConnectionFactory().CreateConnection(opts))
                {
                    using (ISyncSubscription s = c.SubscribeSync("foo"))
                    {
                        c.Publish("foo", null);
                        c.Flush();
                        Msg m = s.NextMessage();
                        System.Console.WriteLine("Received msg over TLS conn.");
                    }
                }
            }
        }
开发者ID:ColinSullivan1,项目名称:csnats,代码行数:32,代码来源:UnitTestTLS.cs


注:本文中的NATS.Client.ConnectionFactory.SubscribeSync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。