コード例 #1
0
        public void Test_Publish_OK()
        {
            string msg = "this is sample message", msgSaved = "";
            var    logger = new TextLogger();
            // mock
            var mockConn    = new Mock <IStanConnection>();
            var mockFactory = new Mock <INatsFactory>();

            mockFactory.Setup(m => m.CreateConnection(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <StanOptions>())).Returns(mockConn.Object);
            mockConn.Setup(m => m.Publish(It.IsAny <string>(), It.IsAny <byte[]>())).Callback <string, byte[]>((sub, data) =>
            {
                msgSaved = Encoding.UTF8.GetString(data);
            });
            nats.DefaultPublishRetryDelays = new TimeSpan[] { TimeSpan.FromMilliseconds(1) };
            nats.DefaultFactory            = mockFactory.Object;
            nats.DefaultLogger             = logger;
            // run
            nats.Connect("", "", "");
            try
            {
                nats.Publish("subject", msg);
            }
            catch { }
            nats.Close();
            // verify
            Assert.AreEqual(msg, msgSaved);
            StringAssert.Contains(logger.ToString(), "nats publish completed");
        }
コード例 #2
0
        public void Test_Publish_Error()
        {
            var errmsg = "nats publish error";
            var logger = new TextLogger();
            // mock
            var mockConn    = new Mock <IStanConnection>();
            var mockFactory = new Mock <INatsFactory>();

            mockFactory.Setup(m => m.CreateConnection(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <StanOptions>())).Returns(mockConn.Object);
            mockConn.Setup(m => m.Publish(It.IsAny <string>(), It.IsAny <byte[]>())).Callback(() =>
            {
                throw new Exception(errmsg);
            });
            nats.DefaultPublishRetryDelays = new TimeSpan[] { TimeSpan.FromMilliseconds(1) };
            nats.DefaultFactory            = mockFactory.Object;
            nats.DefaultLogger             = logger;
            // run
            nats.Connect("", "", "");
            try
            {
                nats.Publish("subject", "message");
            }
            catch { }
            nats.Close();
            // verify
            StringAssert.Contains(logger.ToString(), errmsg);
        }
コード例 #3
0
        public void Test_Connect_OK()
        {
            var logger = new TextLogger();
            // mock
            var mockFactory = new Mock <INatsFactory>();
            var mockConn    = new Mock <IStanConnection>();

            mockFactory.Setup(m => m.CreateConnection(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <StanOptions>())).Returns(mockConn.Object);
            nats.DefaultFactory = mockFactory.Object;
            nats.DefaultLogger  = logger;
            // run
            nats.Connect("", "", "");
            nats.Close();
            // verify
            StringAssert.Contains(logger.ToString(), "nats connection completed");
        }
コード例 #4
0
        public void Test_Connect_Error()
        {
            var errmsg = "nats connection failed";
            var logger = new TextLogger();
            // mock
            var mockFactory = new Mock <INatsFactory>();

            mockFactory.Setup(m => m.CreateConnection(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <StanOptions>())).Returns((IStanConnection)null);
            nats.DefaultFactory = mockFactory.Object;
            nats.DefaultLogger  = logger;
            // run
            nats.Connect("", "", "");
            nats.Close();
            // verify
            StringAssert.Contains(logger.ToString(), errmsg);
        }
コード例 #5
0
        public void Test_QueueSubscribe_OK()
        {
            var logger = new TextLogger();
            // mock
            var mockSub     = new Mock <IStanSubscription>();
            var mockConn    = new Mock <IStanConnection>();
            var mockFactory = new Mock <INatsFactory>();

            mockFactory.Setup(m => m.CreateConnection(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <StanOptions>())).Returns(mockConn.Object);
            mockConn.Setup(m => m.Subscribe(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <StanSubscriptionOptions>(), It.IsAny <EventHandler <StanMsgHandlerArgs> >())).Returns(mockSub.Object);
            nats.DefaultFactory = mockFactory.Object;
            nats.DefaultLogger  = logger;
            // run
            nats.Connect("", "", "");
            nats.QueueSubscribe("subject", "queue", "durable", (e, args) => { });
            nats.Close();
            // verify
            StringAssert.Contains(logger.ToString(), "nats subscription completed");
        }
コード例 #6
0
        public void Test_Connect_Exception()
        {
            var errmsg = "error at connection";
            var logger = new TextLogger();
            // mock
            var mockFactory = new Mock <INatsFactory>();

            mockFactory.Setup(m => m.CreateConnection(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <StanOptions>()))
            .Callback(() =>
            {
                throw new Exception(errmsg);
            });
            nats.DefaultFactory = mockFactory.Object;
            nats.DefaultLogger  = logger;
            // run
            nats.Connect("", "", "");
            nats.Close();
            // verify
            StringAssert.Contains(logger.ToString(), errmsg);
        }
コード例 #7
0
        public void Test_QueueSubscribe_Error()
        {
            var errmsg = "nats subscription error";
            var logger = new TextLogger();
            // mock
            var mockConn    = new Mock <IStanConnection>();
            var mockFactory = new Mock <INatsFactory>();

            mockFactory.Setup(m => m.CreateConnection(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <StanOptions>())).Returns(mockConn.Object);
            mockConn.Setup(m => m.Subscribe(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <StanSubscriptionOptions>(), It.IsAny <EventHandler <StanMsgHandlerArgs> >())).Callback(() =>
            {
                throw new Exception(errmsg);
            });
            nats.DefaultPublishRetryDelays = new TimeSpan[] { TimeSpan.FromMilliseconds(1) };
            nats.DefaultFactory            = mockFactory.Object;
            nats.DefaultLogger             = logger;
            // run
            nats.Connect("", "", "");
            nats.QueueSubscribe("subject", "queue", "durable", (e, args) => { });
            nats.Close();
            // verify
            StringAssert.Contains(logger.ToString(), errmsg);
        }