/// <summary>
        ///   Initializes a new instance of the <see cref="EventHubProducerClient"/> class.
        /// </summary>
        ///
        /// <param name="connection">The connection to use as the basis for delegation of client-type operations.</param>
        /// <param name="transportProducer">The transport producer instance to use as the basis for service communication.</param>
        ///
        /// <remarks>
        ///   This constructor is intended to be used internally for functional
        ///   testing only.
        /// </remarks>
        ///
        internal EventHubProducerClient(EventHubConnection connection,
                                        TransportProducer transportProducer)
        {
            Argument.AssertNotNull(connection, nameof(connection));
            Argument.AssertNotNull(transportProducer, nameof(transportProducer));

            OwnsConnection  = false;
            Connection      = connection;
            GatewayProducer = transportProducer;
        }
        /// <summary>
        ///   Initializes a new instance of the <see cref="EventHubProducerClient" /> class.
        /// </summary>
        ///
        /// <param name="connection">The connection to use as the basis for delegation of client-type operations.</param>
        /// <param name="transportProducer">The transport producer instance to use as the basis for service communication.</param>
        /// <param name="partitionProducerPool">A <see cref="TransportProducerPool" /> used to manage a set of partition specific <see cref="TransportProducer" />.</param>
        ///
        /// <remarks>
        ///   This constructor is intended to be used internally for functional
        ///   testing only.
        /// </remarks>
        ///
        internal EventHubProducerClient(EventHubConnection connection,
                                        TransportProducer transportProducer,
                                        TransportProducerPool partitionProducerPool = default)
        {
            Argument.AssertNotNull(connection, nameof(connection));
            Argument.AssertNotNull(transportProducer, nameof(transportProducer));

            OwnsConnection        = false;
            Connection            = connection;
            RetryPolicy           = new EventHubsRetryOptions().ToRetryPolicy();
            PartitionProducerPool = partitionProducerPool ?? new TransportProducerPool(Connection, RetryPolicy, eventHubProducer: transportProducer);
        }
        /// <summary>
        ///   Initializes a new instance of the <see cref="EventHubProducerClient"/> class.
        /// </summary>
        ///
        /// <param name="connection">The <see cref="EventHubConnection" /> connection to use for communication with the Event Hubs service.</param>
        /// <param name="producerOptions">A set of options to apply when configuring the producer.</param>
        ///
        public EventHubProducerClient(EventHubConnection connection,
                                      EventHubProducerClientOptions producerOptions = default)
        {
            Argument.AssertNotNull(connection, nameof(connection));
            producerOptions = producerOptions?.Clone() ?? new EventHubProducerClientOptions();

            OwnsConnection = false;
            Connection     = connection;
            PartitionId    = producerOptions.PartitionId;
            RetryPolicy    = producerOptions.RetryOptions.ToRetryPolicy();
            InnerProducer  = Connection.CreateTransportProducer(producerOptions);
        }
        /// <summary>
        ///   Initializes a new instance of the <see cref="EventHubProducerClient"/> class.
        /// </summary>
        ///
        /// <param name="connectionString">The connection string to use for connecting to the Event Hubs namespace; it is expected that the Event Hub name and SAS token are contained in this connection string.</param>
        /// <param name="eventHubName">The name of the specific Event Hub to associate the producer with.</param>
        /// <param name="producerOptions">A set of options to apply when configuring the producer.</param>
        ///
        /// <remarks>
        ///   If the connection string is copied from the Event Hub itself, it will contain the name of the desired Event Hub,
        ///   and can be used directly without passing the <paramref name="eventHubName" />.  The name of the Event Hub should be
        ///   passed only once, either as part of the connection string or separately.
        /// </remarks>
        ///
        public EventHubProducerClient(string connectionString,
                                      string eventHubName,
                                      EventHubProducerClientOptions producerOptions)
        {
            Argument.AssertNotNullOrEmpty(connectionString, nameof(connectionString));
            producerOptions = producerOptions?.Clone() ?? new EventHubProducerClientOptions();

            OwnsConnection = true;
            Connection     = new EventHubConnection(connectionString, eventHubName, producerOptions.ConnectionOptions);
            PartitionId    = producerOptions.PartitionId;
            RetryPolicy    = producerOptions.RetryOptions.ToRetryPolicy();
            InnerProducer  = Connection.CreateTransportProducer(producerOptions);
        }
        /// <summary>
        ///   Initializes a new instance of the <see cref="EventHubProducerClient"/> class.
        /// </summary>
        ///
        /// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace to connect to.  This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
        /// <param name="eventHubName">The name of the specific Event Hub to associated the producer with.</param>
        /// <param name="credential">The Azure managed identity credential to use for authorization.  Access controls may be specified by the Event Hubs namespace or the requested Event Hub, depending on Azure configuration.</param>
        /// <param name="producerOptions">A set of options to apply when configuring the producer.</param>
        ///
        public EventHubProducerClient(string fullyQualifiedNamespace,
                                      string eventHubName,
                                      TokenCredential credential,
                                      EventHubProducerClientOptions producerOptions = default)
        {
            Argument.AssertNotNullOrEmpty(fullyQualifiedNamespace, nameof(fullyQualifiedNamespace));
            Argument.AssertNotNullOrEmpty(eventHubName, nameof(eventHubName));
            Argument.AssertNotNull(credential, nameof(credential));

            producerOptions = producerOptions?.Clone() ?? new EventHubProducerClientOptions();

            OwnsConnection = true;
            Connection     = new EventHubConnection(fullyQualifiedNamespace, eventHubName, credential, producerOptions.ConnectionOptions);
            PartitionId    = producerOptions.PartitionId;
            RetryPolicy    = producerOptions.RetryOptions.ToRetryPolicy();
            InnerProducer  = Connection.CreateTransportProducer(producerOptions);
        }
 public MockConnection(TransportProducer transportProducer) : this(transportProducer, "fakeNamespace", "fakeEventHub")
 {
 }
 public MockConnection(TransportProducer transportProducer,
                       string namespaceName,
                       string eventHubName) : this(namespaceName, eventHubName)
 {
     TransportProducer = transportProducer;
 }
 /// <summary>
 ///   Checks if the <see cref="TransportProducer" /> returned by the <see cref="TransportProducerPool" /> is still open.
 /// </summary>
 ///
 /// <param name="producer">The <see cref="TransportProducer" /> that has being checked.</param>
 /// <param name="partitionId">The unique identifier of a partition associated with the Event Hub.</param>
 ///
 /// <returns><c>true</c> if the specified <see cref="TransportProducer" /> is closed; otherwise, <c>false</c>.</returns>
 ///
 private bool ShouldRecreateProducer(TransportProducer producer,
                                     string partitionId) => !string.IsNullOrEmpty(partitionId) &&
 producer.IsClosed &&
 !IsClosed &&
 !Connection.IsClosed;