Exemplo n.º 1
0
        public async Task X509DeviceCanSendMessageTest()
        {
            var connMgr = new Mock <IConnectionManager>();

            connMgr.Setup(c => c.AddDeviceConnection(It.IsAny <IIdentity>(), It.IsAny <IDeviceProxy>()));
            var identity = Mock.Of <IModuleIdentity>(m => m.DeviceId == "device1" && m.ModuleId == "module1" && m.Id == "device1/module1");
            var edgeHub  = Mock.Of <IEdgeHub>();
            Option <ICloudProxy> cloudProxy = Option.None <ICloudProxy>();
            bool   messageReceived          = false;
            string lockToken             = null;
            var    underlyingDeviceProxy = new Mock <IDeviceProxy>();

            underlyingDeviceProxy.Setup(d => d.SendMessageAsync(It.IsAny <IMessage>(), It.IsAny <string>()))
            .Callback <IMessage, string>(
                (m, i) =>
            {
                messageReceived = true;
                lockToken       = m.SystemProperties[SystemProperties.LockToken];
            })
            .Returns(Task.CompletedTask);
            connMgr.Setup(c => c.GetCloudConnection(It.IsAny <string>())).Returns(Task.FromResult(cloudProxy));
            var deviceMessageHandler = new DeviceMessageHandler(identity, edgeHub, connMgr.Object, DefaultMessageAckTimeout);

            deviceMessageHandler.BindDeviceProxy(underlyingDeviceProxy.Object);

            IMessage message = new EdgeMessage.Builder(new byte[0]).Build();
            // send message to x509 device
            Task sendMessageTask = deviceMessageHandler.SendMessageAsync(message, "input1");
            await deviceMessageHandler.ProcessMessageFeedbackAsync(lockToken, FeedbackStatus.Complete);

            Assert.True(messageReceived);
            Assert.True(sendMessageTask.IsCompletedSuccessfully);
        }
Exemplo n.º 2
0
        public async Task MessageCompletionMismatchedResponseTest()
        {
            var connMgr = new Mock <IConnectionManager>();

            connMgr.Setup(c => c.AddDeviceConnection(It.IsAny <IIdentity>(), It.IsAny <IDeviceProxy>()));
            var identity              = Mock.Of <IModuleIdentity>(m => m.DeviceId == "device1" && m.ModuleId == "module1" && m.Id == "device1/module1");
            var cloudProxy            = new Mock <ICloudProxy>();
            var edgeHub               = Mock.Of <IEdgeHub>();
            var underlyingDeviceProxy = new Mock <IDeviceProxy>();

            underlyingDeviceProxy.Setup(d => d.SendMessageAsync(It.IsAny <IMessage>(), It.IsAny <string>()))
            .Returns(Task.CompletedTask);
            connMgr.Setup(c => c.GetCloudConnection(It.IsAny <string>())).Returns(Task.FromResult(Option.Some(cloudProxy.Object)));
            var deviceMessageHandler = new DeviceMessageHandler(identity, edgeHub, connMgr.Object, DefaultMessageAckTimeout);

            deviceMessageHandler.BindDeviceProxy(underlyingDeviceProxy.Object);

            IMessage message         = new EdgeMessage.Builder(new byte[0]).Build();
            Task     sendMessageTask = deviceMessageHandler.SendMessageAsync(message, "input1");

            Assert.False(sendMessageTask.IsCompleted);

            await deviceMessageHandler.ProcessMessageFeedbackAsync(Guid.NewGuid().ToString(), FeedbackStatus.Complete);

            Assert.False(sendMessageTask.IsCompleted);
        }
Exemplo n.º 3
0
        public async Task MessageCompletionLongAckTimeoutTest()
        {
            var connMgr = new Mock <IConnectionManager>();

            connMgr.Setup(c => c.AddDeviceConnection(It.IsAny <IIdentity>(), It.IsAny <IDeviceProxy>()));
            var      identity              = Mock.Of <IModuleIdentity>(m => m.DeviceId == "device1" && m.ModuleId == "module1" && m.Id == "device1/module1");
            var      cloudProxy            = new Mock <ICloudProxy>();
            var      edgeHub               = Mock.Of <IEdgeHub>();
            var      underlyingDeviceProxy = new Mock <IDeviceProxy>();
            IMessage receivedMessage       = null;

            underlyingDeviceProxy.Setup(d => d.SendMessageAsync(It.IsAny <IMessage>(), It.IsAny <string>()))
            .Callback <IMessage, string>((m, s) => receivedMessage = m)
            .Returns(Task.CompletedTask);
            connMgr.Setup(c => c.GetCloudConnection(It.IsAny <string>())).Returns(Task.FromResult(Option.Some(cloudProxy.Object)));
            TimeSpan messageAckTimeout    = TimeSpan.FromSeconds(15);
            var      deviceMessageHandler = new DeviceMessageHandler(identity, edgeHub, connMgr.Object, messageAckTimeout);

            deviceMessageHandler.BindDeviceProxy(underlyingDeviceProxy.Object);

            IMessage message         = new EdgeMessage.Builder(new byte[0]).Build();
            Task     sendMessageTask = deviceMessageHandler.SendMessageAsync(message, "input1");

            Assert.False(sendMessageTask.IsCompleted);

            await Task.Delay(TimeSpan.FromSeconds(10));

            string messageId = receivedMessage.SystemProperties[SystemProperties.LockToken];
            await deviceMessageHandler.ProcessMessageFeedbackAsync(messageId, FeedbackStatus.Complete);

            await Task.Delay(TimeSpan.FromSeconds(1));

            Assert.True(sendMessageTask.IsCompleted);
        }
Exemplo n.º 4
0
            public static async Task <TestModule> Create(string deviceId, string moduleId, string outputEndpointId, List <string> inputEndpointIds, IEdgeHub edgeHub, IConnectionManager connectionManager)
            {
                IClientCredentials moduleCredentials = SetupModuleCredentials(moduleId, deviceId);
                Try <ICloudProxy>  cloudProxy        = await connectionManager.CreateCloudConnectionAsync(moduleCredentials);

                Assert.True(cloudProxy.Success);
                var deviceListener = new DeviceMessageHandler(moduleCredentials.Identity, edgeHub, connectionManager, DefaultMessageAckTimeout, Option.None <string>());

                var receivedMessages = new List <IMessage>();
                var deviceProxy      = new Mock <IDeviceProxy>();

                deviceProxy.Setup(d => d.SendMessageAsync(It.IsAny <IMessage>(), It.Is <string>(e => inputEndpointIds.Contains(e))))
                .Callback <IMessage, string>(
                    (m, e) =>
                {
                    receivedMessages.Add(m);
                    deviceListener.ProcessMessageFeedbackAsync(m.SystemProperties[SystemProperties.LockToken], FeedbackStatus.Complete).Wait();
                })
                .Returns(Task.CompletedTask);
                deviceProxy.SetupGet(d => d.IsActive).Returns(true);
                deviceListener.BindDeviceProxy(deviceProxy.Object);
                await deviceListener.AddSubscription(DeviceSubscription.ModuleMessages);

                return(new TestModule(moduleCredentials.Identity as IModuleIdentity, outputEndpointId, deviceListener, receivedMessages));
            }
Exemplo n.º 5
0
        public async Task ProcessDuplicateC2DMessageTest()
        {
            // Arrange
            var identity   = Mock.Of <IModuleIdentity>(m => m.DeviceId == "device1" && m.Id == "device1");
            var cloudProxy = new Mock <ICloudProxy>();

            cloudProxy.Setup(c => c.SendFeedbackMessageAsync(It.IsAny <string>(), It.IsAny <FeedbackStatus>()))
            .Returns(Task.CompletedTask);
            var edgeHub = Mock.Of <IEdgeHub>();
            var underlyingDeviceProxy = new Mock <IDeviceProxy>();

            underlyingDeviceProxy.Setup(d => d.SendC2DMessageAsync(It.IsAny <IMessage>())).Returns(Task.CompletedTask);

            var connMgr = new Mock <IConnectionManager>();

            connMgr.Setup(c => c.AddDeviceConnection(It.IsAny <IIdentity>(), It.IsAny <IDeviceProxy>()));
            connMgr.Setup(c => c.GetCloudConnection(It.IsAny <string>())).Returns(Task.FromResult(Option.Some(cloudProxy.Object)));
            var deviceMessageHandler = new DeviceMessageHandler(identity, edgeHub, connMgr.Object, DefaultMessageAckTimeout, Option.None <string>());

            deviceMessageHandler.BindDeviceProxy(underlyingDeviceProxy.Object);

            string lockToken         = Guid.NewGuid().ToString();
            var    systemProperties1 = new Dictionary <string, string>
            {
                [SystemProperties.LockToken] = lockToken
            };

            var message1 = Mock.Of <IMessage>(m => m.SystemProperties == systemProperties1);

            var systemProperties2 = new Dictionary <string, string>
            {
                [SystemProperties.LockToken] = lockToken
            };

            var message2 = Mock.Of <IMessage>(m => m.SystemProperties == systemProperties2);

            // Act
            await deviceMessageHandler.SendC2DMessageAsync(message1);

            // Assert
            underlyingDeviceProxy.Verify(d => d.SendC2DMessageAsync(It.IsAny <IMessage>()), Times.Once);

            // Act
            await deviceMessageHandler.SendC2DMessageAsync(message2);

            // Assert
            underlyingDeviceProxy.Verify(d => d.SendC2DMessageAsync(It.IsAny <IMessage>()), Times.Once);

            // Act
            await deviceMessageHandler.ProcessMessageFeedbackAsync(lockToken, FeedbackStatus.Complete);

            // Assert
            cloudProxy.Verify(c => c.SendFeedbackMessageAsync(It.IsAny <string>(), It.IsAny <FeedbackStatus>()), Times.Once);
        }
Exemplo n.º 6
0
        public async Task ProcessFeedbackMessageAsync_RouteAsyncTest()
        {
            var    cloudProxy        = Mock.Of <ICloudProxy>();
            var    connectionManager = Mock.Of <IConnectionManager>();
            var    edgeHub           = Mock.Of <IEdgeHub>();
            var    identity          = Mock.Of <IDeviceIdentity>();
            string messageId         = "messageId";
            var    status            = FeedbackStatus.Complete;

            Mock.Get(connectionManager).Setup(c => c.GetCloudConnection(It.IsAny <string>())).Returns(Option.Some(cloudProxy));
            var deviceListener = new DeviceMessageHandler(identity, edgeHub, connectionManager);
            await deviceListener.ProcessMessageFeedbackAsync(messageId, status);

            Mock.Get(cloudProxy).Verify(cp => cp.SendFeedbackMessageAsync(messageId, status), Times.Once());
        }