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");
 }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        /// <param name="transportType">The AMQP transport type.</param>
        /// <param name="prefetchCount">The pre-fetch count.</param>
        /// <param name="amqpConnectionPoolSettings">AMQP connection pool settings.</param>
        public AmqpTransportSettings(TransportType transportType, uint prefetchCount, AmqpConnectionPoolSettings amqpConnectionPoolSettings)
        {
            OperationTimeout = DefaultOperationTimeout;
            OpenTimeout      = DefaultOpenTimeout;
            IdleTimeout      = DefaultIdleTimeout;

            PrefetchCount = prefetchCount <= 0
                ? throw new ArgumentOutOfRangeException(nameof(prefetchCount), "Must be greater than zero")
                : prefetchCount;

            switch (transportType)
            {
            case TransportType.Amqp_WebSocket_Only:
                Proxy          = DefaultWebProxySettings.Instance;
                _transportType = transportType;
                break;

            case TransportType.Amqp_Tcp_Only:
                _transportType = transportType;
                break;

            case TransportType.Amqp:
                throw new ArgumentOutOfRangeException(
                          nameof(transportType),
                          transportType,
                          "Must specify Amqp_WebSocket_Only or Amqp_Tcp_Only");

            default:
                throw new ArgumentOutOfRangeException(nameof(transportType), transportType, null);
            }

            AmqpConnectionPoolSettings = amqpConnectionPoolSettings;
        }
        public bool Equals(AmqpConnectionPoolSettings other)
        {
            if (other == null)
            {
                return(false);
            }

            if (object.ReferenceEquals(this, other))
            {
                return(true);
            }

            return(this.Pooling == other.Pooling && this.MaxPoolSize == other.MaxPoolSize && this.ConnectionIdleTimeout == other.ConnectionIdleTimeout);
        }
        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 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 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 AmqpTransportSettings(TransportType transportType, uint prefetchCount, AmqpConnectionPoolSettings amqpConnectionPoolSettings)
        {
            if (prefetchCount <= 0)
            {
                throw new ArgumentOutOfRangeException("prefetchCount", "Must be greater than zero");
            }

            switch (transportType)
            {
                case TransportType.Amqp_WebSocket_Only:
                case TransportType.Amqp_Tcp_Only:
                    this.transportType = transportType;
                    break;
                case TransportType.Amqp:
                    throw new ArgumentOutOfRangeException(nameof(transportType), transportType, "Must specify Amqp_WebSocket_Only or Amqp_Tcp_Only");
                default:
                    throw new ArgumentOutOfRangeException(nameof(transportType), transportType, null);
            }

            this.PrefetchCount = prefetchCount;
            this.AmqpConnectionPoolSettings = amqpConnectionPoolSettings;
        }
 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;
         webSocketSettings.AmqpConnectionPoolSettings = amqpConnectionPoolSettings;
     }
     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 ConnectionPoolSettingsTest_ZeroPoolSize()
 {
     var connectionPoolSettings = new AmqpConnectionPoolSettings();
     connectionPoolSettings.MaxPoolSize = 0;
     var transportSetting = new AmqpTransportSettings(TransportType.Amqp, 200, connectionPoolSettings);
 }
Пример #10
0
        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));

            // Hacky way to verify that the SingleTokenConnection object has been closed.
            var singleConnection = (IotHubSingleTokenConnection)hubscopeConnectionPool.Connection;
            await singleConnection.CreateSendingLinkAsync("test", iotHubConnectionString, TimeSpan.FromMinutes(2));
        }
 public void TransportSettingsTest_ZeroOpenTimeout()
 {
     var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings();
     var transportSetting = new AmqpTransportSettings(TransportType.Amqp, 200, amqpConnectionPoolSettings);
     transportSetting.OpenTimeout = TimeSpan.Zero;
 }
Пример #12
0
        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 TransportSettingsTest_TransportType_AmqpTcp_Prefetch_0()
 {
     var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings();
     var transportSetting = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only, 0, amqpConnectionPoolSettings);
 }
        public void X509Certificate_MutipleClientAuthMechanism()
        {
            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 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 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");
 }
Пример #17
0
        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));

            // Assert
            Assert.IsTrue(connectionPool.GetCount() == 0, "Did not cleanup all Connection objects");
        }
Пример #18
0
        public AmqpTransportSettings(TransportType transportType, uint prefetchCount, AmqpConnectionPoolSettings amqpConnectionPoolSettings)
        {
            this.operationTimeout = DefaultOperationTimeout;
            this.openTimeout      = DefaultOpenTimeout;

            if (prefetchCount <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(prefetchCount), "Must be greater than zero");
            }

            switch (transportType)
            {
            case TransportType.Amqp_WebSocket_Only:
            case TransportType.Amqp_Tcp_Only:
                this.transportType = transportType;
                break;

            case TransportType.Amqp:
                throw new ArgumentOutOfRangeException(nameof(transportType), transportType, "Must specify Amqp_WebSocket_Only or Amqp_Tcp_Only");

            default:
                throw new ArgumentOutOfRangeException(nameof(transportType), transportType, null);
            }

            this.PrefetchCount = prefetchCount;
            this.AmqpConnectionPoolSettings = amqpConnectionPoolSettings;
        }
 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 ConnectionPoolSettingsTest_4SecsIdleTimeout()
 {
     var connectionPoolSettings = new AmqpConnectionPoolSettings();
     connectionPoolSettings.ConnectionIdleTimeout = TimeSpan.FromSeconds(4);
     var transportSetting = new AmqpTransportSettings(TransportType.Amqp, 200, connectionPoolSettings);
 }
Пример #21
0
        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");
        }