public async Task SendAsyncReturnsTheRequestedTwin() { byte[] twinBytes = Encoding.UTF8.GetBytes("don't care"); var edgeHub = Mock.Of <IEdgeHub>(e => e.GetTwinAsync(It.IsAny <string>()) == Task.FromResult(new EdgeMessage.Builder(twinBytes).Build() as IMessage)); IDeviceListener listener = new DeviceMessageHandler(Mock.Of <IIdentity>(i => i.Id == "d1"), edgeHub, Mock.Of <IConnectionManager>()); var channel = new Mock <IMessagingChannel <IProtocolGatewayMessage> >(); channel.Setup(x => x.Handle(It.IsAny <IProtocolGatewayMessage>())) .Callback <IProtocolGatewayMessage>( msg => { Assert.Equal(twinBytes, ByteBufferConverter.ToByteArray(msg.Payload)); Assert.Equal("$iothub/twin/res/200/?$rid=123", msg.Address); Assert.Equal("r", msg.Id); }); ProtocolGatewayMessage message = new ProtocolGatewayMessage.Builder(ByteBufferConverter.ToByteBuffer(new byte[0]), "$iothub/twin/GET/?$rid=123") .Build(); var client = new MessagingServiceClient(listener, ProtocolGatewayMessageConverter.Value, ByteBufferConverter); client.BindMessagingChannel(channel.Object); await client.SendAsync(message); channel.Verify(x => x.Handle(It.IsAny <IProtocolGatewayMessage>()), Times.Once); }
public async Task SendAsyncSendsAPatchResponseWhenGivenACorrelationId() { var edgeHub = Mock.Of <IEdgeHub>(e => e.UpdateReportedPropertiesAsync(It.IsAny <IIdentity>(), It.IsAny <IMessage>()) == Task.CompletedTask); IDeviceListener listener = new DeviceMessageHandler(Mock.Of <IIdentity>(i => i.Id == "d1"), edgeHub, Mock.Of <IConnectionManager>()); var channel = new Mock <IMessagingChannel <IProtocolGatewayMessage> >(); channel.Setup(x => x.Handle(It.IsAny <IProtocolGatewayMessage>())) .Callback <IProtocolGatewayMessage>( msg => { Assert.Equal("$iothub/twin/res/204/?$rid=123", msg.Address); Assert.Equal("r", msg.Id); }); ProtocolGatewayMessage message = new ProtocolGatewayMessage.Builder(ByteBufferConverter.ToByteBuffer(new byte[0]), "$iothub/twin/PATCH/properties/reported/?$rid=123") .Build(); var client = new MessagingServiceClient(listener, ProtocolGatewayMessageConverter.Value, ByteBufferConverter); client.BindMessagingChannel(channel.Object); await client.SendAsync(message); channel.Verify(x => x.Handle(It.IsAny <IProtocolGatewayMessage>()), Times.Once); }
public async Task TestReceiveMessagingChannelDispose() { IProtocolGatewayMessage msg = null; IMessageConverter <IProtocolGatewayMessage> messageConverter = ProtocolGatewayMessageConverter.Value; var dp = new DeviceProxy(Channel.Object, MockIdentity, messageConverter, ByteBufferConverter); var cloudProxy = new Mock <ICloudProxy>(); cloudProxy.Setup(d => d.CloseAsync()).Callback( () => { }); var connectionManager = new Mock <IConnectionManager>(); connectionManager.Setup(c => c.GetCloudConnection(It.IsAny <string>())) .Returns(Task.FromResult(Option.Some(cloudProxy.Object))); var deviceListner = new DeviceMessageHandler(MockIdentity, EdgeHub.Object, connectionManager.Object, DefaultMessageAckTimeout); var messagingServiceClient = new MessagingServiceClient(deviceListner, messageConverter, ByteBufferConverter); Channel.Setup(r => r.Handle(It.IsAny <IProtocolGatewayMessage>())) .Callback <IProtocolGatewayMessage>( m => { msg = m; messagingServiceClient.DisposeAsync(new Exception("Some issue")); }); messagingServiceClient.BindMessagingChannel(Channel.Object); IMessage message = new EdgeMessage.Builder(new byte[] { 1, 2, 3 }).Build(); await dp.SendC2DMessageAsync(message); Assert.NotNull(msg); }
public async Task TestReceiveMessagingChannelAbandon() { IProtocolGatewayMessage msg = null; IMessageConverter <IProtocolGatewayMessage> messageConverter = ProtocolGatewayMessageConverter.Value; var dp = new DeviceProxy(Channel.Object, MockIdentity, messageConverter, ByteBufferConverter); var cloudProxy = new Mock <ICloudProxy>(); cloudProxy.Setup(d => d.SendFeedbackMessageAsync(It.IsAny <string>(), It.IsAny <FeedbackStatus>())).Callback <string, FeedbackStatus>( (mid, status) => { Assert.Equal(FeedbackStatus.Abandon, status); }); var connectionManager = new Mock <IConnectionManager>(); connectionManager.Setup(c => c.GetCloudConnection(It.IsAny <string>())) .Returns(Task.FromResult(Option.Some(cloudProxy.Object))); var deviceListner = new DeviceMessageHandler(MockIdentity, EdgeHub.Object, connectionManager.Object); var messagingServiceClient = new MessagingServiceClient(deviceListner, messageConverter, ByteBufferConverter); Channel.Setup(r => r.Handle(It.IsAny <IProtocolGatewayMessage>())) .Callback <IProtocolGatewayMessage>( m => { msg = m; messagingServiceClient.AbandonAsync(msg.Id); }); messagingServiceClient.BindMessagingChannel(Channel.Object); IMessage message = new EdgeMessage.Builder(new byte[] { 1, 2, 3 }).Build(); await dp.SendC2DMessageAsync(message); Assert.NotNull(msg); }
public async Task TestSubscriptionRecoveryFromSessionState() { var converter = Mock.Of <IMessageConverter <IProtocolGatewayMessage> >(); var addedSubscriptions = new List <DeviceSubscription>(); var removedSubscriptions = new List <DeviceSubscription>(); var channel = Mock.Of <IMessagingChannel <IProtocolGatewayMessage> >(); var deviceListener = MakeDeviceListenerSpy(addedSubscriptions, removedSubscriptions); deviceListener.Setup(x => x.BindDeviceProxy(It.IsAny <IDeviceProxy>(), It.IsAny <Action>())) .Callback <IDeviceProxy, Action>((p, a) => a?.Invoke()); var sut = new MessagingServiceClient(deviceListener.Object, converter, ByteBufferConverter, this.GetSessionStatePersistenceProvider()); sut.BindMessagingChannel(channel); var toBeAdded = new List <DeviceSubscription>() { DeviceSubscription.Methods, DeviceSubscription.DesiredPropertyUpdates }; var toBeRemoved = new List <DeviceSubscription>() { DeviceSubscription.TwinResponse }; // the initialization is happening async, so we need to use a loop to see if the lists are filled var sw = Stopwatch.StartNew(); while (sw.ElapsedMilliseconds < 2000) { int countRemoved; int countAdded; lock (addedSubscriptions) { countAdded = addedSubscriptions.Count; } lock (removedSubscriptions) { countRemoved = removedSubscriptions.Count; } if (countRemoved != toBeRemoved.Count && countAdded != toBeAdded.Count) { await Task.Delay(50); } else { break; } } Assert.Equal(toBeAdded.OrderBy(v => v), addedSubscriptions.OrderBy(v => v)); Assert.Equal(toBeRemoved.OrderBy(v => v), removedSubscriptions.OrderBy(v => v)); }
public async Task SendAsyncRecognizesAGetTwinMessage() { ProtocolGatewayMessage message = new ProtocolGatewayMessage.Builder(ByteBufferConverter.ToByteBuffer(new byte[0]), "$iothub/twin/GET/?$rid=123") .Build(); Mock <IDeviceListener> listener = MakeDeviceListenerSpy(); var channel = Mock.Of <IMessagingChannel <IProtocolGatewayMessage> >(); var client = new MessagingServiceClient(listener.Object, ProtocolGatewayMessageConverter.Value, ByteBufferConverter); client.BindMessagingChannel(channel); await client.SendAsync(message); listener.Verify(x => x.ProcessDeviceMessageAsync(It.IsAny <IMessage>()), Times.Never); listener.Verify(x => x.SendGetTwinRequest(It.IsAny <string>()), Times.Once); }
public async Task SendAsyncDoesNotSendAPatchResponseWithoutACorrelationId() { var edgeHub = Mock.Of <IEdgeHub>(e => e.UpdateReportedPropertiesAsync(It.IsAny <IIdentity>(), It.IsAny <IMessage>()) == Task.CompletedTask); IDeviceListener listener = new DeviceMessageHandler(Mock.Of <IIdentity>(i => i.Id == "d1"), edgeHub, Mock.Of <IConnectionManager>()); var channel = new Mock <IMessagingChannel <IProtocolGatewayMessage> >(); ProtocolGatewayMessage message = new ProtocolGatewayMessage.Builder(ByteBufferConverter.ToByteBuffer(new byte[0]), "$iothub/twin/PATCH/properties/reported/") .Build(); var client = new MessagingServiceClient(listener, ProtocolGatewayMessageConverter.Value, ByteBufferConverter); client.BindMessagingChannel(channel.Object); await client.SendAsync(message); channel.Verify(x => x.Handle(It.IsAny <IProtocolGatewayMessage>()), Times.Never); }
public async Task SendAsyncRecognizesAPatchTwinMessage() { Mock <IDeviceListener> listener = MakeDeviceListenerSpy(); var channel = Mock.Of <IMessagingChannel <IProtocolGatewayMessage> >(); var client = new MessagingServiceClient(listener.Object, ProtocolGatewayMessageConverter.Value, ByteBufferConverter); client.BindMessagingChannel(channel); string patch = "{\"name\":\"value\"}"; ProtocolGatewayMessage message = new ProtocolGatewayMessage.Builder(ByteBufferConverter.ToByteBuffer(Encoding.UTF8.GetBytes(patch)), "$iothub/twin/PATCH/properties/reported/?$rid=123") .Build(); await client.SendAsync(message); listener.Verify(x => x.UpdateReportedPropertiesAsync(It.Is((IMessage m) => Encoding.UTF8.GetString(m.Body).Equals(patch)), It.IsAny <string>()), Times.Once); }