コード例 #1
0
        public void TestConsumerBackoff()
        {
            var msgIDGood = Encoding.UTF8.GetBytes("1234567890asdfgh");
            var msgGood   = new Message(msgIDGood, Encoding.UTF8.GetBytes("good"));

            var msgIDBad = Encoding.UTF8.GetBytes("zxcvb67890asdfgh");
            var msgBad   = new Message(msgIDBad, Encoding.UTF8.GetBytes("bad"));

            var script = new[]
            {
                // SUB
                new instruction(0, FrameType.Response, "OK"),
                // IDENTIFY
                new instruction(0, FrameType.Response, "OK"),
                new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)),
                new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)),
                new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)),
                new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgBad)),
                new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgBad)),
                new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)),
                new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)),
                // needed to exit test
                new instruction(200 * Time.Millisecond, -1, "exit")
            };
            var n = new mockNSQD(script, IPAddress.Loopback);

            var topicName = "test_consumer_commands" + DateTime.Now.Unix();
            var config    = new Config();

            config.MaxInFlight       = 5;
            config.BackoffMultiplier = Time.Duration(10 * Time.Millisecond);
            var q = new Consumer(topicName, "ch", new ConsoleLogger(LogLevel.Debug), config);

            q.AddHandler(new testHandler());
            q.ConnectToNsqd(n.tcpAddr);

            bool timeout = false;

            Select
            .CaseReceive(n.exitChan, o => { })
            .CaseReceive(Time.After(TimeSpan.FromMilliseconds(5000)), o => { timeout = true; })
            .NoDefault();

            Assert.IsFalse(timeout, "timeout");

            for (int i = 0; i < n.got.Count; i++)
            {
                Console.WriteLine("[{0}] {1}: {2}", n.gotTime[i].Formatted(), i, Encoding.UTF8.GetString(n.got[i]));
            }

            var expected = new[]
            {
                "IDENTIFY",
                "SUB " + topicName + " ch",
                "RDY 5",
                string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)),
                string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)),
                string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)),
                "RDY 5",
                "RDY 0",
                string.Format("REQ {0} 0", Encoding.UTF8.GetString(msgIDBad)),
                "RDY 1",
                "RDY 0",
                string.Format("REQ {0} 0", Encoding.UTF8.GetString(msgIDBad)),
                "RDY 1",
                "RDY 0",
                string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)),
                "RDY 1",
                "RDY 5",
                string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)),
            };

            var actual = new List <string>();

            foreach (var g in n.got)
            {
                actual.Add(Encoding.UTF8.GetString(g));
            }

            q.DisconnectFromNsqd(n.tcpAddr);

            Assert.AreEqual(expected, actual);
        }
コード例 #2
0
ファイル: MockTest.cs プロジェクト: yonglehou/NsqSharp
        public void TestConsumerBackoffDisconnect()
        {
            var msgIDGood = Encoding.UTF8.GetBytes("1234567890asdfgh");
            var msgIDRequeue = Encoding.UTF8.GetBytes("reqvb67890asdfgh");

            var msgGood = new Message(msgIDGood, Encoding.UTF8.GetBytes("good"));
            var msgRequeue = new Message(msgIDRequeue, Encoding.UTF8.GetBytes("requeue"));

            var script = new[]
                         {
                             // SUB
                             new instruction(0, FrameType.Response, "OK"),
                             // IDENTIFY
                             new instruction(0, FrameType.Response, "OK"),
                             new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)),
                             new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgRequeue)),
                             new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgRequeue)),
                             new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)),
                             // needed to exit test
                             new instruction(100 * Time.Millisecond, -1, "exit")
                         };

            var n = new mockNSQD(script, IPAddress.Loopback, 4154);

            var topicName = "test_backoff_disconnect" + DateTime.Now.Unix();
            var config = new Config();
            config.MaxInFlight = 5;
            config.BackoffMultiplier = Time.Duration(10 * Time.Millisecond);
            config.LookupdPollInterval = Time.Duration(10 * Time.Millisecond);
            config.RDYRedistributeInterval = Time.Duration(10 * Time.Millisecond);
            var q = new Consumer(topicName, "ch", new ConsoleLogger(LogLevel.Debug), config);
            q.AddHandler(new testHandler());
            q.ConnectToNsqd(n.tcpAddr);

            bool timeout = false;
            Select
                .CaseReceive(n.exitChan, o => { })
                .CaseReceive(Time.After(TimeSpan.FromMilliseconds(500)), o => { timeout = true; })
                .NoDefault();

            Assert.IsFalse(timeout, "timeout");

            for (int i = 0; i < n.got.Count; i++)
            {
                Console.WriteLine("[{0}] {1}: {2}", n.gotTime[i].Formatted(), i, Encoding.UTF8.GetString(n.got[i]));
            }

            var expected = new[]
                           {
                               "IDENTIFY",
                               "SUB " + topicName + " ch",
                               "RDY 5",
                               string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)),
                               "RDY 0",
                               string.Format("REQ {0} 0", Encoding.UTF8.GetString(msgIDRequeue)),
                               "RDY 1",
                               "RDY 0",
                               string.Format("REQ {0} 0", Encoding.UTF8.GetString(msgIDRequeue)),
                               "RDY 1",
                               "RDY 0",
                               string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)),
                               "RDY 1",
                           };

            var actual = new List<string>();
            foreach (var g in n.got)
            {
                actual.Add(Encoding.UTF8.GetString(g));
            }

            Assert.AreEqual(expected, actual, "test1 failed");

            ///////

            script = new[]
                         {
                             // SUB
                             new instruction(0, FrameType.Response, "OK"),
                             // IDENTIFY
                             new instruction(0, FrameType.Response, "OK"),
                             new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)),
                             new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)),
                             // needed to exit test
                             new instruction(100 * Time.Millisecond, -1, "exit")
                         };

            n = new mockNSQD(script, IPAddress.Loopback, 4154);

            bool timeout2 = false;
            Select
                .CaseReceive(n.exitChan, o => { })
                .CaseReceive(Time.After(TimeSpan.FromMilliseconds(500)), o => { timeout2 = true; })
                .NoDefault();

            Assert.IsFalse(timeout2, "timeout2");

            for (int i = 0; i < n.got.Count; i++)
            {
                Console.WriteLine("[{0}] {1}: {2}", DateTime.Now.Formatted(), i, Encoding.UTF8.GetString(n.got[i]));
            }

            expected = new[]
                           {
                               "IDENTIFY",
                               "SUB " + topicName + " ch",
                               "RDY 1",
                               "RDY 5",
                               string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)),
                               string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)),
                           };

            actual = new List<string>();
            foreach (var g in n.got)
            {
                actual.Add(Encoding.UTF8.GetString(g));
            }

            try
            {
                Assert.AreEqual(expected, actual, "test2 failed");
            }
            finally
            {
                q.StopAsync();
            }
        }
コード例 #3
0
ファイル: MockTest.cs プロジェクト: yonglehou/NsqSharp
        public void TestConsumerBackoff()
        {
            var msgIDGood = Encoding.UTF8.GetBytes("1234567890asdfgh");
            var msgGood = new Message(msgIDGood, Encoding.UTF8.GetBytes("good"));

            var msgIDBad = Encoding.UTF8.GetBytes("zxcvb67890asdfgh");
            var msgBad = new Message(msgIDBad, Encoding.UTF8.GetBytes("bad"));

            var script = new[]
                         {
                             // SUB
                             new instruction(0, FrameType.Response, "OK"),
                             // IDENTIFY
                             new instruction(0, FrameType.Response, "OK"),
                             new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)),
                             new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)),
                             new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)),
                             new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgBad)),
                             new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgBad)),
                             new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)),
                             new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)),
                             // needed to exit test
                             new instruction(200 * Time.Millisecond, -1, "exit")
                         };
            var n = new mockNSQD(script, IPAddress.Loopback, 4152);

            var topicName = "test_consumer_commands" + DateTime.Now.Unix();
            var config = new Config();
            config.MaxInFlight = 5;
            config.BackoffMultiplier = Time.Duration(10 * Time.Millisecond);
            var q = new Consumer(topicName, "ch", new ConsoleLogger(LogLevel.Debug), config);
            q.AddHandler(new testHandler());
            q.ConnectToNsqd(n.tcpAddr);

            n.exitChan.Receive();

            for (int i = 0; i < n.got.Count; i++)
            {
                Console.WriteLine("[{0}] {1}: {2}", n.gotTime[i].Formatted(), i, Encoding.UTF8.GetString(n.got[i]));
            }

            var expected = new[]
                           {
                               "IDENTIFY",
                               "SUB " + topicName + " ch",
                               "RDY 5",
                               string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)),
                               string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)),
                               string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)),
                               "RDY 5",
                               "RDY 0",
                               string.Format("REQ {0} 0", Encoding.UTF8.GetString(msgIDBad)),
                               "RDY 1",
                               "RDY 0",
                               string.Format("REQ {0} 0", Encoding.UTF8.GetString(msgIDBad)),
                               "RDY 1",
                               "RDY 0",
                               string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)),
                               "RDY 1",
                               "RDY 5",
                               string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)),
                           };

            var actual = new List<string>();
            foreach (var g in n.got)
            {
                actual.Add(Encoding.UTF8.GetString(g));
            }

            Assert.AreEqual(expected, actual);
        }
コード例 #4
0
ファイル: MockTest.cs プロジェクト: daniellillja/NsqSharp
        public void TestConsumerBackoffDisconnect()
        {
            var msgIDGood    = Encoding.UTF8.GetBytes("1234567890asdfgh");
            var msgIDRequeue = Encoding.UTF8.GetBytes("reqvb67890asdfgh");

            var msgGood    = new Message(msgIDGood, Encoding.UTF8.GetBytes("good"));
            var msgRequeue = new Message(msgIDRequeue, Encoding.UTF8.GetBytes("requeue"));

            var script = new[]
            {
                // SUB
                new instruction(0, FrameType.Response, "OK"),
                // IDENTIFY
                new instruction(0, FrameType.Response, "OK"),
                new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)),
                new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgRequeue)),
                new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgRequeue)),
                new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)),
                // needed to exit test
                new instruction(100 * Time.Millisecond, -1, "exit")
            };

            var n = new mockNSQD(script, IPAddress.Loopback);

            var topicName = "test_backoff_disconnect" + DateTime.Now.Unix();
            var config    = new NsqConfig();

            config.MaxInFlight             = 5;
            config.BackoffMultiplier       = Time.Duration(10 * Time.Millisecond);
            config.LookupdPollInterval     = Time.Duration(10 * Time.Millisecond);
            config.RDYRedistributeInterval = Time.Duration(10 * Time.Millisecond);
            var q = new Consumer(topicName, "ch", new ConsoleLogger(LogLevel.Debug), config);

            q.AddHandler(new testHandler());
            q.ConnectToNsqd(n.tcpAddr);

            var timeout = false;

            Select
            .CaseReceive(n.exitChan, o => { })
            .CaseReceive(Time.After(TimeSpan.FromMilliseconds(500)), o => { timeout = true; })
            .NoDefault();

            Assert.IsFalse(timeout, "timeout");

            for (var i = 0; i < n.got.Count; i++)
            {
                Console.WriteLine("[{0}] {1}: {2}", n.gotTime[i].Formatted(), i, Encoding.UTF8.GetString(n.got[i]));
            }

            var expected = new[]
            {
                "IDENTIFY",
                "SUB " + topicName + " ch",
                "RDY 5",
                string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)),
                "RDY 0",
                string.Format("REQ {0} 0", Encoding.UTF8.GetString(msgIDRequeue)),
                "RDY 1",
                "RDY 0",
                string.Format("REQ {0} 0", Encoding.UTF8.GetString(msgIDRequeue)),
                "RDY 1",
                "RDY 0",
                string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)),
                "RDY 1"
            };

            var actual = new List <string>();

            foreach (var g in n.got)
            {
                actual.Add(Encoding.UTF8.GetString(g));
            }

            AssertHelper.AreEqual(expected, actual, "test1 failed");

            ///////

            script = new[]
            {
                // SUB
                new instruction(0, FrameType.Response, "OK"),
                // IDENTIFY
                new instruction(0, FrameType.Response, "OK"),
                new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)),
                new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)),
                // needed to exit test
                new instruction(100 * Time.Millisecond, -1, "exit")
            };

            n = new mockNSQD(script, IPAddress.Loopback, n.listenPort);

            var timeout2 = false;

            Select
            .CaseReceive(n.exitChan, o => { })
            .CaseReceive(Time.After(TimeSpan.FromMilliseconds(500)), o => { timeout2 = true; })
            .NoDefault();

            Assert.IsFalse(timeout2, "timeout2");

            for (var i = 0; i < n.got.Count; i++)
            {
                Console.WriteLine("[{0}] {1}: {2}", DateTime.Now.Formatted(), i, Encoding.UTF8.GetString(n.got[i]));
            }

            expected = new[]
            {
                "IDENTIFY",
                "SUB " + topicName + " ch",
                "RDY 1",
                "RDY 5",
                string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)),
                string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood))
            };

            actual = new List <string>();
            foreach (var g in n.got)
            {
                actual.Add(Encoding.UTF8.GetString(g));
            }

            AssertHelper.AreEqual(expected, actual, "test2 failed");
            q.Stop();
        }