public IotHubSingleTokenConnection(IotHubScopeConnectionPool iotHubScopeConnectionPool, IotHubConnectionString connectionString, AmqpTransportSettings amqpTransportSettings)
     :base(connectionString.HostName, connectionString.AmqpEndpoint.Port, amqpTransportSettings)
 {
     this.iotHubScopeConnectionPool = iotHubScopeConnectionPool;
     this.ConnectionString = connectionString;
     this.FaultTolerantSession = new FaultTolerantAmqpObject<AmqpSession>(this.CreateSessionAsync, this.CloseConnection);
 }
Esempio n. 2
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)).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);
        }
        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));
        }