public async Task DeviceScopeMuxConnection_ConnectionIdleTimeoutTest() { // Arrange var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings(); amqpConnectionPoolSettings.ConnectionIdleTimeout = TimeSpan.FromSeconds(5); var amqpTransportSettings = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only, 200, amqpConnectionPoolSettings); string connectionString = "HostName=acme.azure-devices.net;DeviceId=device1;SharedAccessKey=CQN2K33r45/0WeIjpqmErV5EIvX8JZrozt3NEHCEkG8="; var iotHubConnectionString = IotHubConnectionStringBuilder.Create(connectionString).ToIotHubConnectionString(); var connectionCache = new Mock <IotHubConnectionCache>(); var connectionPool = new IotHubDeviceScopeConnectionPool(connectionCache.Object, iotHubConnectionString, amqpTransportSettings); // Act var connections = new IotHubDeviceMuxConnection[10]; // Create 10 Muxed Device Connections - these should hash into different mux connections for (int i = 0; i < 10; i++) { connections[i] = (IotHubDeviceMuxConnection)connectionPool.GetConnection(i.ToString()); } for (int j = 0; j < 10; j++) { connectionPool.RemoveDeviceFromConnection(connections[j], j.ToString()); } await Task.Delay(TimeSpan.FromSeconds(6)).ConfigureAwait(false); // Assert Assert.IsTrue(connectionPool.GetCount() == 0, "Did not cleanup all Connection objects"); }
public void ConnectionPoolSettingsTest_ZeroPoolSize() { var connectionPoolSettings = new AmqpConnectionPoolSettings(); connectionPoolSettings.MaxPoolSize = 0; var transportSetting = new AmqpTransportSettings(TransportType.Amqp, 200, connectionPoolSettings); }
public void DeviceScopeMuxConnection_MaxDevicesPerConnectionTest() { // Arrange var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings(); // Reduce poolsize to 1. This will mux all devices onto one connection amqpConnectionPoolSettings.MaxPoolSize = 1; var amqpTransportSettings = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only, 200, amqpConnectionPoolSettings); string connectionString = "HostName=acme.azure-devices.net;DeviceId=device1;SharedAccessKey=CQN2K33r45/0WeIjpqmErV5EIvX8JZrozt3NEHCEkG8="; var iotHubConnectionString = IotHubConnectionStringBuilder.Create(connectionString).ToIotHubConnectionString(); var connectionCache = new Mock <IotHubConnectionCache>(); var connectionPool = new IotHubDeviceScopeConnectionPool(connectionCache.Object, iotHubConnectionString, amqpTransportSettings); // Act // Create 995 Muxed Device Connections for (int i = 0; i < AmqpConnectionPoolSettings.MaxDevicesPerConnection; i++) { connectionPool.GetConnection(iotHubConnectionString + "DeviceId=" + Guid.NewGuid().ToString()); } // try one more. This should throw invalid operation exception var connection = connectionPool.GetConnection(iotHubConnectionString + "DeviceId=" + Guid.NewGuid().ToString()); }
public void ConnectionPoolSettingsTest_4SecsIdleTimeout() { var connectionPoolSettings = new AmqpConnectionPoolSettings(); connectionPoolSettings.ConnectionIdleTimeout = TimeSpan.FromSeconds(4); var transportSetting = new AmqpTransportSettings(TransportType.Amqp, 200, connectionPoolSettings); }
public void TransportSettingsTest_ZeroOpenTimeout() { var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings(); var transportSetting = new AmqpTransportSettings(TransportType.Amqp, 200, amqpConnectionPoolSettings); transportSetting.OpenTimeout = TimeSpan.Zero; }
public void TransportSettingsTest_TransportType_Amqp_WebSocket_Tcp() { var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings(); var transportSetting = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only, 200, amqpConnectionPoolSettings); Assert.IsTrue(transportSetting.GetTransportType() == TransportType.Amqp_Tcp_Only, "Should be TransportType.Amqp_Tcp_Only"); Assert.IsTrue(transportSetting.PrefetchCount == 200, "Should be value of 200"); }
public void ConnectionPoolSettingsTest_MaxPoolSizeTest() { var connectionPoolSettings = new AmqpConnectionPoolSettings(); connectionPoolSettings.MaxPoolSize = ushort.MaxValue; var transportSetting = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only, 200, connectionPoolSettings); Assert.IsTrue(transportSetting.AmqpConnectionPoolSettings.MaxPoolSize == ushort.MaxValue, "MaxPoolSize should be 64K"); }
public void ConnectionPoolSettingsTest_PoolingOff() { var connectionPoolSettings = new AmqpConnectionPoolSettings(); connectionPoolSettings.Pooling = false; var transportSetting = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only, 200, connectionPoolSettings); Assert.IsTrue(transportSetting.AmqpConnectionPoolSettings.Pooling == false, "Pooling should be off"); }
public void TransportSettingsTest_Timeouts() { var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings(); var transportSetting = new AmqpTransportSettings(TransportType.Amqp_WebSocket_Only, 200, amqpConnectionPoolSettings); transportSetting.OpenTimeout = TimeSpan.FromMinutes(5); transportSetting.OperationTimeout = TimeSpan.FromMinutes(10); Assert.IsTrue(transportSetting.OpenTimeout == TimeSpan.FromMinutes(5), "OpenTimeout not set correctly"); Assert.IsTrue(transportSetting.OperationTimeout == TimeSpan.FromMinutes(10), "OperationTimeout not set correctly"); }
public void X509CertificateMutipleClientAuthMechanism() { string hostName = "acme.azure-devices.net"; var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings(); var transportSetting = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only, 200, amqpConnectionPoolSettings); var authMethod1 = new DeviceAuthenticationWithRegistrySymmetricKey("device1", "CQN2K33r45/0WeIjpqmErV5EIvX8JZrozt3NEHCEkG8="); var deviceClient = DeviceClient.Create(hostName, authMethod1, new ITransportSettings[] { transportSetting }); var cert = CertificateHelper.InstallCertificateFromFile(LocalCertFilename, LocalCertPasswordFile); var authMethod2 = new DeviceAuthenticationWithX509Certificate("device2", cert); var device2Client = DeviceClient.Create(hostName, authMethod2, new ITransportSettings[] { new AmqpTransportSettings(TransportType.Amqp_Tcp_Only, 100) }); }
public void AmqpConnectionPoolSettings_MaxPoolSizeTest() { // arrange const uint maxPoolSize = AmqpConnectionPoolSettings.AbsoluteMaxPoolSize; // act var connectionPoolSettings = new AmqpConnectionPoolSettings { MaxPoolSize = maxPoolSize }; // assert Assert.AreEqual(maxPoolSize, connectionPoolSettings.MaxPoolSize, "Should match initialized value"); }
public void DeviceScopeMuxConnection_PoolingOnNegativeReleaseTest() { // Arrange var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings(); var amqpTransportSettings = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only, 200, amqpConnectionPoolSettings); string connectionString = "HostName=acme.azure-devices.net;DeviceId=device1;SharedAccessKey=CQN2K33r45/0WeIjpqmErV5EIvX8JZrozt3NEHCEkG8="; var iotHubConnectionString = IotHubConnectionStringBuilder.Create(connectionString).ToIotHubConnectionString(); var connectionCache = new Mock <IotHubConnectionCache>(); var connectionPool = new IotHubDeviceScopeConnectionPool(connectionCache.Object, iotHubConnectionString, amqpTransportSettings); connectionCache.Setup(cache => cache.GetConnection(It.IsAny <IotHubConnectionString>(), It.IsAny <AmqpTransportSettings>())).Returns(connectionPool.GetConnection("device1")); // Act var connection = connectionCache.Object.GetConnection(iotHubConnectionString, amqpTransportSettings); // throw exception if you release a device that is not in the pool connection.Release("device2"); }
public static async Task <IMessagingServiceClient> CreateFromConnectionStringAsync(string connectionString, int connectionPoolSize, TimeSpan?connectionIdleTimeout) { DeviceClient client; if (connectionPoolSize > 0) { var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings { MaxPoolSize = unchecked ((uint)connectionPoolSize), Pooling = connectionPoolSize > 0 }; if (connectionIdleTimeout.HasValue) { amqpConnectionPoolSettings.ConnectionIdleTimeout = connectionIdleTimeout.Value; } var transportSettings = new ITransportSettings[] { new AmqpTransportSettings(TransportType.Amqp_Tcp_Only) { AmqpConnectionPoolSettings = amqpConnectionPoolSettings }, new AmqpTransportSettings(TransportType.Amqp_WebSocket_Only) { AmqpConnectionPoolSettings = amqpConnectionPoolSettings } }; client = DeviceClient.CreateFromConnectionString(connectionString, transportSettings); } else { client = DeviceClient.CreateFromConnectionString(connectionString); } try { await client.OpenAsync(); } catch (IotHubException ex) { throw ComposeIotHubCommunicationException(ex); } return(new IotHubClient(client)); }
public void DeviceScopeMuxConnection_PoolingOnPositiveReleaseTest() { // Arrange var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings(); var amqpTransportSettings = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only, 200, amqpConnectionPoolSettings); string connectionString = "HostName=acme.azure-devices.net;DeviceId=device1;SharedAccessKey=dGVzdFN0cmluZzE="; var iotHubConnectionString = IotHubConnectionStringBuilder.Create(connectionString).ToIotHubConnectionString(); var connectionCache = new Mock <IotHubConnectionCache>(); var connectionPool = new IotHubDeviceScopeConnectionPool(connectionCache.Object, iotHubConnectionString, amqpTransportSettings); connectionCache.Setup( cache => cache.GetConnection(It.IsAny <IotHubConnectionString>(), It.IsAny <AmqpTransportSettings>())).Returns(connectionPool.GetConnection("device1")); // Act var connection = connectionCache.Object.GetConnection(iotHubConnectionString, amqpTransportSettings); connection.Release("device1"); // Success - Device1 was in the pool and released }
public static async Task <IMessagingServiceClient> CreateFromConnectionStringAsync(string deviceId, string connectionString, int connectionPoolSize, TimeSpan?connectionIdleTimeout, IotHubClientSettings settings, IByteBufferAllocator allocator, IMessageAddressConverter messageAddressConverter) { int maxPendingOutboundMessages = settings.MaxPendingOutboundMessages; var tcpSettings = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only); var webSocketSettings = new AmqpTransportSettings(TransportType.Amqp_WebSocket_Only); webSocketSettings.PrefetchCount = tcpSettings.PrefetchCount = (uint)maxPendingOutboundMessages; if (connectionPoolSize > 0) { var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings { MaxPoolSize = unchecked ((uint)connectionPoolSize), Pooling = connectionPoolSize > 0 }; if (connectionIdleTimeout.HasValue) { amqpConnectionPoolSettings.ConnectionIdleTimeout = connectionIdleTimeout.Value; } tcpSettings.AmqpConnectionPoolSettings = amqpConnectionPoolSettings; tcpSettings.ClientCertificate = null; webSocketSettings.AmqpConnectionPoolSettings = amqpConnectionPoolSettings; webSocketSettings.ClientCertificate = null; } DeviceClient client = DeviceClient.CreateFromConnectionString(connectionString, new ITransportSettings[] { tcpSettings, webSocketSettings }); try { await client.OpenAsync(); } catch (IotHubException ex) { throw ComposeIotHubCommunicationException(ex); } return(new IotHubClient(client, deviceId, settings, allocator, messageAddressConverter)); }
public void AmqpConnectionPoolSettingsComparisonTests() { var amqpConnectionPoolSettings1 = new AmqpConnectionPoolSettings(); amqpConnectionPoolSettings1.Pooling = true; amqpConnectionPoolSettings1.MaxPoolSize = 10; amqpConnectionPoolSettings1.ConnectionIdleTimeout = TimeSpan.FromSeconds(5); Assert.IsTrue(amqpConnectionPoolSettings1.Equals(amqpConnectionPoolSettings1)); Assert.IsFalse(amqpConnectionPoolSettings1.Equals(null)); Assert.IsFalse(amqpConnectionPoolSettings1.Equals(new AmqpConnectionPoolSettings())); var amqpConnectionPoolSettings2 = new AmqpConnectionPoolSettings(); amqpConnectionPoolSettings2.Pooling = false; amqpConnectionPoolSettings2.MaxPoolSize = 10; amqpConnectionPoolSettings2.ConnectionIdleTimeout = TimeSpan.FromSeconds(5); Assert.IsFalse(amqpConnectionPoolSettings1.Equals(amqpConnectionPoolSettings2)); var amqpConnectionPoolSettings3 = new AmqpConnectionPoolSettings(); amqpConnectionPoolSettings3.Pooling = true; amqpConnectionPoolSettings3.MaxPoolSize = 9; amqpConnectionPoolSettings3.ConnectionIdleTimeout = TimeSpan.FromSeconds(5); Assert.IsFalse(amqpConnectionPoolSettings1.Equals(amqpConnectionPoolSettings3)); var amqpConnectionPoolSettings4 = new AmqpConnectionPoolSettings(); amqpConnectionPoolSettings4.Pooling = true; amqpConnectionPoolSettings4.MaxPoolSize = 10; amqpConnectionPoolSettings4.ConnectionIdleTimeout = TimeSpan.FromSeconds(6); Assert.IsFalse(amqpConnectionPoolSettings1.Equals(amqpConnectionPoolSettings4)); var amqpConnectionPoolSettings5 = new AmqpConnectionPoolSettings(); amqpConnectionPoolSettings5.Pooling = true; amqpConnectionPoolSettings5.MaxPoolSize = 10; amqpConnectionPoolSettings5.ConnectionIdleTimeout = TimeSpan.FromSeconds(5); Assert.IsTrue(amqpConnectionPoolSettings1.Equals(amqpConnectionPoolSettings5)); }
public void DeviceScopeMuxConnection_NumberOfPoolsTest() { // Arrange var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings(); var amqpTransportSettings = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only, 200, amqpConnectionPoolSettings); string connectionString = "HostName=acme.azure-devices.net;DeviceId=device1;SharedAccessKey=CQN2K33r45/0WeIjpqmErV5EIvX8JZrozt3NEHCEkG8="; var iotHubConnectionString = IotHubConnectionStringBuilder.Create(connectionString).ToIotHubConnectionString(); var connectionCache = new Mock <IotHubConnectionCache>(); var connectionPool = new IotHubDeviceScopeConnectionPool(connectionCache.Object, iotHubConnectionString, amqpTransportSettings); // Act // Create 10 Muxed Device Connections - these should hash into different mux connections for (int i = 0; i < 10; i++) { var connection = connectionPool.GetConnection(i.ToString()); } // Assert Assert.IsTrue(connectionPool.GetCount() == 10, "Did not create 10 different Connection objects"); }
public void DeviceScopeMuxConnection_PoolingOffReleaseTest() { // Arrange var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings(); amqpConnectionPoolSettings.Pooling = false; var amqpTransportSettings = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only, 200, amqpConnectionPoolSettings); string connectionString = "HostName=acme.azure-devices.net;DeviceId=device1;SharedAccessKey=CQN2K33r45/0WeIjpqmErV5EIvX8JZrozt3NEHCEkG8="; var iotHubConnectionString = IotHubConnectionStringBuilder.Create(connectionString).ToIotHubConnectionString(); var connectionCache = new Mock <IotHubConnectionCache>(); // Pooling is off - pass null for ConnectionPoolCache var iotHubConnection = new IotHubDeviceMuxConnection(null, 1, iotHubConnectionString, amqpTransportSettings); connectionCache.Setup(cache => cache.GetConnection(It.IsAny <IotHubConnectionString>(), It.IsAny <AmqpTransportSettings>())).Returns(iotHubConnection); // Act var connection = connectionCache.Object.GetConnection(iotHubConnectionString, amqpTransportSettings); connection.Release("device"); // does not match "device1" above, However pooling is off. Thus, this iothubconnection object is closed // Success }
public async Task HubScopeMuxConnection_ConnectionIdleTimeoutTest() { // Arrange var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings(); amqpConnectionPoolSettings.ConnectionIdleTimeout = TimeSpan.FromSeconds(5); var amqpTransportSettings = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only, 200, amqpConnectionPoolSettings); string connectionString = "HostName=acme.azure-devices.net;DeviceId=device1;SharedAccessKey=CQN2K33r45/0WeIjpqmErV5EIvX8JZrozt3NEHCEkG8="; var iotHubConnectionString = IotHubConnectionStringBuilder.Create(connectionString).ToIotHubConnectionString(); var connectionCache = new Mock <IotHubConnectionCache>(); connectionCache.Setup(cache => cache.RemoveHubScopeConnectionPool(It.IsAny <IotHubConnectionString>())).Returns(true); var hubscopeConnectionPool = new IotHubScopeConnectionPool(connectionCache.Object, iotHubConnectionString, amqpTransportSettings); // Act for (int i = 0; i < 10; i++) { hubscopeConnectionPool.TryAddRef(); } // Assert Assert.IsTrue(hubscopeConnectionPool.GetCount() == 10, "Reference count should be ten"); for (int i = 0; i < 10; i++) { hubscopeConnectionPool.RemoveRef(); } // Assert Assert.IsTrue(hubscopeConnectionPool.GetCount() == 0, "Reference count should be zero"); await Task.Delay(TimeSpan.FromSeconds(6)).ConfigureAwait(false); // Hacky way to verify that the SingleTokenConnection object has been closed. var singleConnection = (IotHubSingleTokenConnection)hubscopeConnectionPool.Connection; await singleConnection.CreateSendingLinkAsync("test", iotHubConnectionString, "device", IotHubConnection.SendingLinkType.TelemetryEvents, TimeSpan.FromMinutes(2), new ProductInfo(), CancellationToken.None).ConfigureAwait(false); }
static async Task <IotHubBridge> CreateFromConnectionStringAsync(string deviceId, string connectionString, int connectionPoolSize, TimeSpan?connectionIdleTimeout, IotHubClientSettings settings, CancellationToken cancellationToken) { int maxPendingOutboundMessages = settings.MaxPendingOutboundMessages; var tcpSettings = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only); var webSocketSettings = new AmqpTransportSettings(TransportType.Amqp_WebSocket_Only); webSocketSettings.PrefetchCount = tcpSettings.PrefetchCount = (uint)maxPendingOutboundMessages; if (connectionPoolSize > 0) { var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings { MaxPoolSize = unchecked ((uint)connectionPoolSize), Pooling = connectionPoolSize > 0 }; if (connectionIdleTimeout.HasValue) { amqpConnectionPoolSettings.ConnectionIdleTimeout = connectionIdleTimeout.Value; } tcpSettings.AmqpConnectionPoolSettings = amqpConnectionPoolSettings; webSocketSettings.AmqpConnectionPoolSettings = amqpConnectionPoolSettings; } var client = DeviceClient.CreateFromConnectionString(connectionString, new ITransportSettings[] { tcpSettings, webSocketSettings }); client.SetRetryPolicy(DeviceClientRetryPolicy.Instance); var bridge = new IotHubBridge(client, deviceId, settings); client.SetConnectionStatusChangesHandler((status, reason) => { if (status != ConnectionStatus.Connected) { var cause = new IotHubCommunicationException("Connection to IoT Hub is closed"); if (Interlocked.CompareExchange(ref bridge.closedCause, cause, null) == null) { bridge.messagingChannel?.Close(cause); } } }); // This helps in usage instrumentation at IotHub service. client.ProductInfo = $"protocolgateway/poolsize={connectionPoolSize}"; try { await client.OpenAsync(cancellationToken); cancellationToken.ThrowIfCancellationRequested(); // in case SDK does not always honor cancellation token in async operations } catch (IotHubException ex) { client.Dispose(); throw ex.ToMessagingException(); } catch (Exception) { client.Dispose(); throw; } return(bridge); }
public void TransportSettingsTest_TransportType_AmqpTcp_Prefetch_0() { var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings(); var transportSetting = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only, 0, amqpConnectionPoolSettings); }
public void AmqpConnectionPoolSettings_UnderMinPoolSize() { _ = new AmqpConnectionPoolSettings { MaxPoolSize = 0 }; }
public void AmqpConnectionPoolSettings_OverMaxPoolSize() { _ = new AmqpConnectionPoolSettings { MaxPoolSize = AmqpConnectionPoolSettings.AbsoluteMaxPoolSize + 1 }; }