Пример #1
0
        public async Task SendMessageWithFeedbackExactlyOnceModeTest()
        {
            // Arrange
            var feedbackStatus = FeedbackStatus.Abandon;
            var deviceListener = new Mock <IDeviceListener>();

            deviceListener.Setup(d => d.ProcessMessageFeedbackAsync(It.IsAny <string>(), It.IsAny <FeedbackStatus>()))
            .Callback <string, FeedbackStatus>((m, s) => feedbackStatus = s)
            .Returns(Task.CompletedTask);
            AmqpMessage receivedAmqpMessage = null;
            var         identity            = Mock.Of <IIdentity>(i => i.Id == "d1");
            var         connectionHandler   = Mock.Of <IConnectionHandler>(c => c.GetDeviceListener() == Task.FromResult(deviceListener.Object));
            var         amqpAuthenticator   = new Mock <IAmqpAuthenticator>();

            amqpAuthenticator.Setup(c => c.AuthenticateAsync("d1")).ReturnsAsync(true);
            Mock <ICbsNode> cbsNodeMock    = amqpAuthenticator.As <ICbsNode>();
            ICbsNode        cbsNode        = cbsNodeMock.Object;
            var             amqpConnection = Mock.Of <IAmqpConnection>(
                c =>
                c.FindExtension <IConnectionHandler>() == connectionHandler &&
                c.FindExtension <ICbsNode>() == cbsNode);
            var amqpSession      = Mock.Of <IAmqpSession>(s => s.Connection == amqpConnection);
            var amqpLinkSettings = new AmqpLinkSettings();
            var sendingLink      = Mock.Of <ISendingAmqpLink>(l => l.Session == amqpSession && !l.IsReceiver && l.Settings == amqpLinkSettings && l.State == AmqpObjectState.Opened);

            Mock.Get(sendingLink).Setup(s => s.SendMessageNoWait(It.IsAny <AmqpMessage>(), It.IsAny <ArraySegment <byte> >(), It.IsAny <ArraySegment <byte> >()))
            .Callback <AmqpMessage, ArraySegment <byte>, ArraySegment <byte> >((m, d, t) => { receivedAmqpMessage = m; });

            var requestUri     = new Uri("amqps://foo.bar/devices/d1");
            var boundVariables = new Dictionary <string, string> {
                { "deviceid", "d1" }
            };
            var messageConverter = new AmqpMessageConverter();

            var      sendingLinkHandler = new TestSendingLinkHandler(identity, sendingLink, requestUri, boundVariables, connectionHandler, messageConverter, QualityOfService.ExactlyOnce);
            var      body          = new byte[] { 0, 1, 2, 3 };
            IMessage message       = new EdgeMessage.Builder(body).Build();
            var      deliveryState = new Mock <DeliveryState>(new AmqpSymbol(string.Empty), AmqpConstants.AcceptedOutcome.DescriptorCode);
            var      delivery      = Mock.Of <Delivery>(
                d => d.State == deliveryState.Object &&
                d.DeliveryTag == new ArraySegment <byte>(Guid.NewGuid().ToByteArray()));

            // Act
            await sendingLinkHandler.OpenAsync(TimeSpan.FromSeconds(5));

            await sendingLinkHandler.SendMessage(message);

            // Assert
            Assert.NotNull(receivedAmqpMessage);
            Assert.Equal(body, receivedAmqpMessage.GetPayloadBytes());

            // Act
            sendingLinkHandler.DispositionListener(delivery);
            await Task.Delay(TimeSpan.FromSeconds(5));

            // Assert
            Assert.Equal(feedbackStatus, FeedbackStatus.Complete);
            Assert.Equal(null, amqpLinkSettings.SndSettleMode);
            Assert.Equal((byte)ReceiverSettleMode.Second, amqpLinkSettings.RcvSettleMode);
        }
Пример #2
0
        public async Task SendMessageWithNoFeedbackTest()
        {
            // Arrange
            var productInfoStore = Mock.Of <IProductInfoStore>();
            var modelIdStore     = Mock.Of <IModelIdStore>();
            var identity         = Mock.Of <IIdentity>(i => i.Id == "d1");
            var deviceListener   = new Mock <IDeviceListener>();

            deviceListener.Setup(d => d.ProcessMessageFeedbackAsync(It.IsAny <string>(), It.IsAny <FeedbackStatus>()))
            .Returns(Task.CompletedTask);
            AmqpMessage receivedAmqpMessage = null;
            var         connectionHandler   = Mock.Of <IConnectionHandler>(c => c.GetDeviceListener() == Task.FromResult(deviceListener.Object));
            var         amqpAuthenticator   = new Mock <IAmqpAuthenticator>();

            amqpAuthenticator.Setup(c => c.AuthenticateAsync("d1")).ReturnsAsync(true);
            Mock <ICbsNode> cbsNodeMock    = amqpAuthenticator.As <ICbsNode>();
            ICbsNode        cbsNode        = cbsNodeMock.Object;
            var             amqpConnection = Mock.Of <IAmqpConnection>(
                c =>
                c.FindExtension <IConnectionHandler>() == connectionHandler &&
                c.FindExtension <ICbsNode>() == cbsNode);
            var amqpSession      = Mock.Of <IAmqpSession>(s => s.Connection == amqpConnection);
            var amqpLinkSettings = new AmqpLinkSettings();
            var sendingLink      = Mock.Of <ISendingAmqpLink>(l => l.Session == amqpSession && !l.IsReceiver && l.Settings == amqpLinkSettings && l.State == AmqpObjectState.Opened);

            Mock.Get(sendingLink).Setup(s => s.SendMessageAsync(It.IsAny <AmqpMessage>(), It.IsAny <ArraySegment <byte> >(), It.IsAny <ArraySegment <byte> >(), It.IsAny <TimeSpan>()))
            .Callback <AmqpMessage, ArraySegment <byte>, ArraySegment <byte>, TimeSpan>((m, d, t, ts) => { receivedAmqpMessage = m; })
            .Returns(Task.CompletedTask);

            var requestUri     = new Uri("amqps://foo.bar/devices/d1");
            var boundVariables = new Dictionary <string, string> {
                { "deviceid", "d1" }
            };
            var messageConverter = new AmqpMessageConverter();

            var      sendingLinkHandler = new TestSendingLinkHandler(identity, sendingLink, requestUri, boundVariables, connectionHandler, messageConverter, QualityOfService.AtMostOnce, productInfoStore, modelIdStore);
            var      body    = new byte[] { 0, 1, 2, 3 };
            IMessage message = new EdgeMessage.Builder(body).Build();

            // Act
            await sendingLinkHandler.OpenAsync(TimeSpan.FromSeconds(5));

            await sendingLinkHandler.SendMessage(message);

            // Assert
            Assert.NotNull(receivedAmqpMessage);
            Assert.Equal(body, receivedAmqpMessage.GetPayloadBytes());
            Assert.Equal((byte)SenderSettleMode.Settled, amqpLinkSettings.SndSettleMode);
            Assert.Equal((byte)ReceiverSettleMode.First, amqpLinkSettings.RcvSettleMode);
        }