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


C# ConnectionFactory.Flush方法代码示例

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


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

示例1: TestAsyncSubHandlerAPI

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

示例2: TestServerAutoUnsub

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

示例3: TestBasicReconnectFunctionality

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

示例4: TestCustomObjectSerialization

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

示例5: Run

        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);

            }
        }
开发者ID:bendan365,项目名称:csnats-wip,代码行数:29,代码来源:Requestor.cs

示例6: TestSubDelTaskCountReconnect

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

示例7: TestRequest

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

示例8: TestSubDelTaskCountSlowConsumer

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

示例9: TestAsyncSubscriberStarvation

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

示例10: TestSlowAsyncSubscriber

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

示例11: TestAsyncSubscribe

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

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

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

示例14: TestEncodedInvalidObjectSerialization

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

示例15: TestEncodedSerizationOverrides

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


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