public async Task ConnectionTransportCannotRetrieveMetadataWhenProxyIsInvalid()
        {
            await using (EventHubScope scope = await EventHubScope.CreateAsync(1))
            {
                var connectionString = EventHubsTestEnvironment.Instance.BuildConnectionStringForEventHub(scope.EventHubName);
                var retryOptions     = new EventHubsRetryOptions {
                    TryTimeout = TimeSpan.FromMinutes(2)
                };

                var clientOptions = new EventHubConnectionOptions
                {
                    Proxy         = new WebProxy("http://1.2.3.4:9999"),
                    TransportType = EventHubsTransportType.AmqpWebSockets
                };

                await using (var connection = new TestConnectionWithRetryPolicy(connectionString))
                    await using (var invalidProxyConnection = new TestConnectionWithRetryPolicy(connectionString, clientOptions))
                    {
                        connection.RetryPolicy             = new BasicRetryPolicy(retryOptions);
                        invalidProxyConnection.RetryPolicy = new BasicRetryPolicy(retryOptions);

                        var partition = (await connection.GetPartitionIdsAsync()).First();

                        Assert.That(async() => await invalidProxyConnection.GetPartitionIdsAsync(), Throws.InstanceOf <WebSocketException>().Or.InstanceOf <TimeoutException>());
                        Assert.That(async() => await invalidProxyConnection.GetPropertiesAsync(), Throws.InstanceOf <WebSocketException>().Or.InstanceOf <TimeoutException>());
                        Assert.That(async() => await invalidProxyConnection.GetPartitionPropertiesAsync(partition), Throws.InstanceOf <WebSocketException>().Or.InstanceOf <TimeoutException>());
                    }
            }
        }
        public async Task ConnectionTransportCannotRetrieveMetadataWhenCustomValidationRejectsTheCertificate()
        {
            await using (EventHubScope scope = await EventHubScope.CreateAsync(1))
            {
                var connectionString = EventHubsTestEnvironment.Instance.BuildConnectionStringForEventHub(scope.EventHubName);
                var retryOptions     = new EventHubsRetryOptions {
                    TryTimeout = TimeSpan.FromMinutes(2)
                };

                var clientOptions = new EventHubConnectionOptions
                {
                    CertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => false
                };

                await using (var connection = new TestConnectionWithRetryPolicy(connectionString))
                    await using (var certificateRejectingConnection = new TestConnectionWithRetryPolicy(connectionString, clientOptions))
                    {
                        connection.RetryPolicy = new BasicRetryPolicy(retryOptions);
                        certificateRejectingConnection.RetryPolicy = new BasicRetryPolicy(retryOptions);

                        var partition = (await connection.GetPartitionIdsAsync()).First();

                        Assert.That(async() => await certificateRejectingConnection.GetPartitionIdsAsync(), Throws.InstanceOf <AuthenticationException>());
                        Assert.That(async() => await certificateRejectingConnection.GetPropertiesAsync(), Throws.InstanceOf <AuthenticationException>());
                        Assert.That(async() => await certificateRejectingConnection.GetPartitionPropertiesAsync(partition), Throws.InstanceOf <AuthenticationException>());
                    }
            }
        }
        public async Task ConnectionTransportCannotRetrieveMetadataWhenClosed()
        {
            await using (EventHubScope scope = await EventHubScope.CreateAsync(1))
            {
                var connectionString = EventHubsTestEnvironment.Instance.BuildConnectionStringForEventHub(scope.EventHubName);

                await using (var connection = new TestConnectionWithRetryPolicy(connectionString))
                {
                    var partition = (await connection.GetPartitionIdsAsync()).First();

                    Assert.That(async() => await connection.GetPropertiesAsync(), Throws.Nothing);
                    Assert.That(async() => await connection.GetPartitionPropertiesAsync(partition), Throws.Nothing);

                    await connection.CloseAsync();

                    await Task.Delay(TimeSpan.FromSeconds(5));

                    Assert.That(async() => await connection.GetPartitionIdsAsync(), Throws.InstanceOf <EventHubsException>().And.Property(nameof(EventHubsException.Reason)).EqualTo(EventHubsException.FailureReason.ClientClosed));
                    Assert.That(async() => await connection.GetPropertiesAsync(), Throws.InstanceOf <EventHubsException>().And.Property(nameof(EventHubsException.Reason)).EqualTo(EventHubsException.FailureReason.ClientClosed));
                    Assert.That(async() => await connection.GetPartitionPropertiesAsync(partition), Throws.InstanceOf <EventHubsException>().And.Property(nameof(EventHubsException.Reason)).EqualTo(EventHubsException.FailureReason.ClientClosed));
                }
            }
        }
        public async Task ConnectionTransportPartitionIdsMatchPartitionProperties()
        {
            await using (EventHubScope scope = await EventHubScope.CreateAsync(4))
            {
                var connectionString = EventHubsTestEnvironment.Instance.BuildConnectionStringForEventHub(scope.EventHubName);

                await using (var connection = new TestConnectionWithRetryPolicy(connectionString))
                {
                    EventHubProperties properties = await connection.GetPropertiesAsync();

                    var partitions = await connection.GetPartitionIdsAsync();

                    Assert.That(properties, Is.Not.Null, "A set of properties should have been returned.");
                    Assert.That(properties.PartitionIds, Is.Not.Null, "A set of partition identifiers for the properties should have been returned.");
                    Assert.That(partitions, Is.Not.Null, "A set of partition identifiers should have been returned.");
                    Assert.That(partitions, Is.EquivalentTo(properties.PartitionIds), "The partition identifiers returned directly should match those returned with properties.");
                }
            }
        }