public async Task ModuleMessageProcessor_ProcessAsyncTest_InactiveDeviceProxy() { Core.IMessageConverter <IRoutingMessage> routingMessageConverter = new RoutingMessageConverter(); var deviceProxy = new Mock <IDeviceProxy>(); deviceProxy.Setup(d => d.SendMessageAsync(It.IsAny <IMessage>(), It.IsAny <string>())).Returns(Task.CompletedTask); deviceProxy.Setup(d => d.IsActive).Returns(false); var routingMessage = Mock.Of <IRoutingMessage>(); string moduleId = "device1/module1"; string moduleEndpointAddress = "in1"; IReadOnlyDictionary <DeviceSubscription, bool> deviceSubscriptions = new ReadOnlyDictionary <DeviceSubscription, bool>( new Dictionary <DeviceSubscription, bool> { [DeviceSubscription.ModuleMessages] = true }); var connectionManager = new Mock <IConnectionManager>(); connectionManager.Setup(c => c.GetDeviceConnection(It.IsAny <string>())).Returns(Option.Some(deviceProxy.Object)); connectionManager.Setup(c => c.GetSubscriptions(It.IsAny <string>())).Returns(Option.Some(deviceSubscriptions)); var moduleEndpoint = new ModuleEndpoint($"{moduleId}/{moduleEndpointAddress}", moduleId, moduleEndpointAddress, connectionManager.Object, routingMessageConverter); IProcessor moduleMessageProcessor = moduleEndpoint.CreateProcessor(); ISinkResult <IRoutingMessage> sinkResult = await moduleMessageProcessor.ProcessAsync(routingMessage, CancellationToken.None); Assert.True(sinkResult.Failed.Contains(routingMessage)); Assert.Equal(FailureKind.None, sinkResult.SendFailureDetails.Map(x => x.FailureKind).GetOrElse(FailureKind.None)); }
public async Task ProcessAsyncTest() { Core.IMessageConverter <IRoutingMessage> routingMessageConverter = new RoutingMessageConverter(); const string Mod1Id = "device1/module1"; const string ModEndpointId = "in1"; var deviceProxyMock = new Mock <IDeviceProxy>(); deviceProxyMock.Setup(c => c.SendMessageAsync(It.IsAny <IMessage>(), It.Is <string>((ep) => ep.Equals(ModEndpointId, StringComparison.OrdinalIgnoreCase)))) .Returns(Task.CompletedTask); deviceProxyMock.SetupGet(p => p.IsActive).Returns(true); IReadOnlyDictionary <DeviceSubscription, bool> deviceSubscriptions = new ReadOnlyDictionary <DeviceSubscription, bool>( new Dictionary <DeviceSubscription, bool> { [DeviceSubscription.ModuleMessages] = true }); var connectionManager = new Mock <IConnectionManager>(); connectionManager.Setup(c => c.GetDeviceConnection(It.IsAny <string>())).Returns(Option.Some(deviceProxyMock.Object)); connectionManager.Setup(c => c.GetSubscriptions(It.IsAny <string>())).Returns(Option.Some(deviceSubscriptions)); byte[] messageBody = Encoding.UTF8.GetBytes("Message body"); var properties = new Dictionary <string, string>() { { "Prop1", "Val1" }, { "Prop2", "Val2" }, }; var systemProperties = new Dictionary <string, string> { { SystemProperties.DeviceId, Mod1Id } }; var message1 = new RoutingMessage(TelemetryMessageSource.Instance, messageBody, properties, systemProperties); var message2 = new RoutingMessage(TelemetryMessageSource.Instance, messageBody, properties, systemProperties); var moduleEndpoint = new ModuleEndpoint($"{Mod1Id}/{ModEndpointId}", Mod1Id, ModEndpointId, connectionManager.Object, routingMessageConverter); IProcessor moduleMessageProcessor = moduleEndpoint.CreateProcessor(); ISinkResult <IRoutingMessage> result = await moduleMessageProcessor.ProcessAsync(message1, CancellationToken.None); Assert.NotNull(result); Assert.NotEmpty(result.Succeeded); Assert.Empty(result.Failed); Assert.Empty(result.InvalidDetailsList); Assert.False(result.SendFailureDetails.HasValue); ISinkResult <IRoutingMessage> resultBatch = await moduleMessageProcessor.ProcessAsync(new[] { message1, message2 }, CancellationToken.None); Assert.NotNull(resultBatch); Assert.NotEmpty(resultBatch.Succeeded); Assert.Empty(resultBatch.Failed); Assert.Empty(resultBatch.InvalidDetailsList); Assert.False(resultBatch.SendFailureDetails.HasValue); }
public void BasicTest() { Core.IMessageConverter <IRoutingMessage> routingMessageConverter = new RoutingMessageConverter(); var connectionManager = new Mock <IConnectionManager>(); string modId = "device1/module1"; string moduleEndpointId = "in1"; var moduleEndpoint = new ModuleEndpoint($"{modId}/{moduleEndpointId}", modId, moduleEndpointId, connectionManager.Object, routingMessageConverter); IProcessor moduleMessageProcessor = moduleEndpoint.CreateProcessor(); Assert.Equal(moduleEndpoint, moduleMessageProcessor.Endpoint); Assert.False(moduleMessageProcessor.ErrorDetectionStrategy.IsTransient(new Exception())); }
public void ModuleMessageProcessor_CloseAsyncTest() { Core.IMessageConverter <IRoutingMessage> routingMessageConverter = new RoutingMessageConverter(); var connectionManager = Mock.Of <IConnectionManager>(); string moduleId = "device1/module1"; string moduleEndpointAddress = "in1"; var moduleEndpoint = new ModuleEndpoint($"{moduleId}/{moduleEndpointAddress}", moduleId, moduleEndpointAddress, connectionManager, routingMessageConverter); IProcessor moduleMessageProcessor = moduleEndpoint.CreateProcessor(); Task result = moduleMessageProcessor.CloseAsync(CancellationToken.None); Assert.Equal(TaskEx.Done, result); }
public void ModuleMessageProcessor_CreateProcessorTest() { Core.IMessageConverter <IRoutingMessage> routingMessageConverter = new RoutingMessageConverter(); var connectionManager = Mock.Of <IConnectionManager>(); string moduleId = "device1/module1"; string moduleEndpointAddress = "in1"; var moduleEndpoint = new ModuleEndpoint($"{moduleId}/{moduleEndpointAddress}", moduleId, moduleEndpointAddress, connectionManager, routingMessageConverter); IProcessor moduleMessageProcessor = moduleEndpoint.CreateProcessor(); Assert.NotNull(moduleMessageProcessor); Assert.Equal(moduleEndpoint, moduleMessageProcessor.Endpoint); }
public void ModuleEndpoint_MembersTest() { Core.IMessageConverter <IRoutingMessage> routingMessageConverter = new RoutingMessageConverter(); var connectionManager = Mock.Of <IConnectionManager>(); string moduleId = "device1/module1"; string moduleEndpointAddress = "in1"; string endpointId = $"{moduleId}/{moduleEndpointAddress}"; var moduleEndpoint = new ModuleEndpoint(endpointId, moduleId, moduleEndpointAddress, connectionManager, routingMessageConverter); Assert.Equal(endpointId, moduleEndpoint.Id); Assert.Equal("ModuleEndpoint", moduleEndpoint.Type); Assert.Equal(endpointId, moduleEndpoint.Name); Assert.Equal(string.Empty, moduleEndpoint.IotHubName); }
private IProcessor CreateMessageProcessor(IDeviceProxy deviceProxy) { IReadOnlyDictionary <DeviceSubscription, bool> deviceSubscriptions = new Dictionary <DeviceSubscription, bool>() { [DeviceSubscription.ModuleMessages] = true }; var connectionManager = new Mock <IConnectionManager>(); connectionManager.Setup(call => call.GetDeviceConnection(It.IsAny <string>())).Returns(Option.Some(deviceProxy)); connectionManager.Setup(c => c.GetSubscriptions(It.IsAny <string>())).Returns(Option.Some(deviceSubscriptions)); var moduleEndpoint = new ModuleEndpoint("device1/module1", "module1", "in1", connectionManager.Object, new RoutingMessageConverter()); return(moduleEndpoint.CreateProcessor()); }
public async Task ModuleMessageProcessor_RetryableExceptionTest(Exception exception, bool isRetryable) { // Arrange Core.IMessageConverter <IRoutingMessage> routingMessageConverter = new RoutingMessageConverter(); var deviceProxy = new Mock <IDeviceProxy>(); deviceProxy.Setup(d => d.SendMessageAsync(It.IsAny <IMessage>(), It.IsAny <string>())) .ThrowsAsync(exception); deviceProxy.Setup(d => d.IsActive).Returns(true); IReadOnlyDictionary <DeviceSubscription, bool> deviceSubscriptions = new ReadOnlyDictionary <DeviceSubscription, bool>( new Dictionary <DeviceSubscription, bool> { [DeviceSubscription.ModuleMessages] = true }); var connectionManager = new Mock <IConnectionManager>(); connectionManager.Setup(c => c.GetDeviceConnection(It.IsAny <string>())).Returns(Option.Some(deviceProxy.Object)); connectionManager.Setup(c => c.GetSubscriptions(It.IsAny <string>())).Returns(Option.Some(deviceSubscriptions)); var routingMessage = Mock.Of <IRoutingMessage>(); string moduleId = "device1/module1"; string moduleEndpointAddress = "in1"; var moduleEndpoint = new ModuleEndpoint($"{moduleId}/{moduleEndpointAddress}", moduleId, moduleEndpointAddress, connectionManager.Object, routingMessageConverter); // Act IProcessor moduleMessageProcessor = moduleEndpoint.CreateProcessor(); ISinkResult <IRoutingMessage> result = await moduleMessageProcessor.ProcessAsync(routingMessage, CancellationToken.None); // Assert Assert.NotNull(result); if (isRetryable) { Assert.Equal(1, result.Failed.Count); Assert.Equal(0, result.Succeeded.Count); Assert.Equal(0, result.InvalidDetailsList.Count); Assert.Equal(routingMessage, result.Failed.First()); } else { Assert.Equal(1, result.InvalidDetailsList.Count); Assert.Equal(0, result.Succeeded.Count); Assert.Equal(0, result.Failed.Count); Assert.Equal(routingMessage, result.InvalidDetailsList.First().Item); Assert.Equal(FailureKind.InvalidInput, result.InvalidDetailsList.First().FailureKind); } }