Exemple #1
0
 public AmqpServiceClient(AmqpEventHubClient eventHubClient, string address)
     : base("AmqpServiceClient-" + StringUtility.GetRandomString())
 {
     this.eventHubClient = eventHubClient;
     Address             = address;
     link = new FaultTolerantAmqpObject <RequestResponseAmqpLink>(OpenLinkAsync, rrlink => rrlink.CloseAsync(TimeSpan.FromSeconds(10)));
 }
Exemple #2
0
        /// <summary>
        /// Creates a new instance of the Event Hubs client using the specified endpoint, entity path, and token provider.
        /// </summary>
        /// <param name="endpointAddress">Fully qualified domain name for Event Hubs. Most likely, {yournamespace}.servicebus.windows.net</param>
        /// <param name="entityPath">Event Hub path</param>
        /// <param name="tokenProvider">Token provider which will generate security tokens for authorization.</param>
        /// <param name="operationTimeout">Operation timeout for Event Hubs operations.</param>
        /// <param name="transportType">Transport type on connection.</param>
        /// <returns></returns>
        public static EventHubClient Create(
            Uri endpointAddress,
            string entityPath,
            ITokenProvider tokenProvider,
            TimeSpan?operationTimeout   = null,
            TransportType transportType = TransportType.Amqp)
        {
            if (endpointAddress == null)
            {
                throw Fx.Exception.ArgumentNull(nameof(endpointAddress));
            }

            if (string.IsNullOrWhiteSpace(entityPath))
            {
                throw Fx.Exception.ArgumentNullOrWhiteSpace(nameof(entityPath));
            }

            if (tokenProvider == null)
            {
                throw Fx.Exception.ArgumentNull(nameof(tokenProvider));
            }

            EventHubsEventSource.Log.EventHubClientCreateStart(endpointAddress.Host, entityPath);
            EventHubClient eventHubClient = new AmqpEventHubClient(
                endpointAddress,
                entityPath,
                tokenProvider,
                operationTimeout ?? ClientConstants.DefaultOperationTimeout,
                transportType);

            EventHubsEventSource.Log.EventHubClientCreateStop(eventHubClient.ClientId);
            return(eventHubClient);
        }
Exemple #3
0
 public AmqpServiceClient(AmqpEventHubClient eventHubClient, string address)
     : base("AmqpServiceClient-" + StringUtility.GetRandomString())
 {
     this.eventHubClient    = eventHubClient;
     this.Address           = address;
     this.link              = new FaultTolerantAmqpObject <RequestResponseAmqpLink>(t => this.OpenLinkAsync(t), rrlink => rrlink.CloseAsync(TimeSpan.FromSeconds(10)));
     this.clientLinkManager = new ActiveClientLinkManager(this.eventHubClient);
 }
Exemple #4
0
        public void GetPartitionPropertiesAsyncValidatesThePartition(string partition)
        {
            ExactTypeConstraint typeConstraint = partition is null ? Throws.ArgumentNullException : Throws.ArgumentException;

            var client = new AmqpEventHubClient("my.eventhub.com", "somePath", Mock.Of <TokenCredential>(), new EventHubClientOptions(), Mock.Of <EventHubRetryPolicy>());

            Assert.That(async() => await client.GetPartitionPropertiesAsync(partition, CancellationToken.None), typeConstraint);
        }
        public void CloseRespectsTheCancellationToken()
        {
            var client             = new AmqpEventHubClient("my.eventhub.com", "somePath", Mock.Of <TokenCredential>(), new EventHubClientOptions(), Mock.Of <EventHubRetryPolicy>());
            var cancellationSource = new CancellationTokenSource();

            cancellationSource.Cancel();
            Assert.That(async() => await client.CloseAsync(cancellationSource.Token), Throws.InstanceOf <TaskCanceledException>());
        }
Exemple #6
0
        public async Task GetPartitionPropertiesAsyncValidatesClosed()
        {
            using var cancellationSource = new CancellationTokenSource();

            var client = new AmqpEventHubClient("my.eventhub.com", "somePath", Mock.Of <TokenCredential>(), new EventHubClientOptions(), Mock.Of <EventHubRetryPolicy>());
            await client.CloseAsync(cancellationSource.Token);

            Assert.That(async() => await client.GetPartitionPropertiesAsync("Fred", cancellationSource.Token), Throws.InstanceOf <EventHubsObjectClosedException>());
        }
Exemple #7
0
        public void GetPartitionPropertiesAsyncRespectsTheCancellationTokenIfSetWhenCalled()
        {
            using var cancellationSource = new CancellationTokenSource();
            cancellationSource.Cancel();

            var client = new AmqpEventHubClient("my.eventhub.com", "somePath", Mock.Of <TokenCredential>(), new EventHubClientOptions(), Mock.Of <EventHubRetryPolicy>());

            Assert.That(async() => await client.GetPartitionPropertiesAsync("Fred", cancellationSource.Token), Throws.InstanceOf <TaskCanceledException>());
        }
Exemple #8
0
        public async Task CloseMarksTheClientAsClosed()
        {
            var client = new AmqpEventHubClient("my.eventhub.com", "somePath", Mock.Of <TokenCredential>(), new EventHubClientOptions(), Mock.Of <EventHubRetryPolicy>());

            Assert.That(client.Closed, Is.False, "The client should not be closed on creation");

            await client.CloseAsync(CancellationToken.None);

            Assert.That(client.Closed, Is.True, "The client should be marked as closed after closing");
        }
Exemple #9
0
        public async Task DisposeClosesTheClient()
        {
            var client = new AmqpEventHubClient("my.eventhub.com", "somePath", Mock.Of <TokenCredential>(), new EventHubClientOptions(), Mock.Of <EventHubRetryPolicy>());

            Assert.That(client.Closed, Is.False, "The client should not be closed on creation");

            await client.DisposeAsync();

            Assert.That(client.Closed, Is.True, "The client should be closed after disposal");
        }
Exemple #10
0
        public async Task CreateConsumerValidatesClosed()
        {
            using var cancellationSource = new CancellationTokenSource();

            var mockRetryPolicy = Mock.Of <EventHubRetryPolicy>();
            var client          = new AmqpEventHubClient("my.eventhub.com", "somePath", Mock.Of <TokenCredential>(), new EventHubClientOptions(), mockRetryPolicy);
            await client.CloseAsync(cancellationSource.Token);

            Assert.That(() => client.CreateConsumer("group", "0", EventPosition.Earliest, new EventHubConsumerOptions(), mockRetryPolicy), Throws.InstanceOf <EventHubsObjectClosedException>());
        }
Exemple #11
0
        /// <summary>
        /// Creates a new instance of the Event Hubs client using the specified connection string builder.
        /// </summary>
        /// <param name="csb"></param>
        /// <returns></returns>
        public static EventHubClient Create(EventHubsConnectionStringBuilder csb)
        {
            Guard.ArgumentNotNull(nameof(csb), csb);
            Guard.ArgumentNotNullOrWhiteSpace(nameof(csb.EntityPath), csb.EntityPath);

            EventHubsEventSource.Log.EventHubClientCreateStart(csb.Endpoint.Host, csb.EntityPath);
            EventHubClient eventHubClient = new AmqpEventHubClient(csb);

            EventHubsEventSource.Log.EventHubClientCreateStop(eventHubClient.ClientId);
            return(eventHubClient);
        }
Exemple #12
0
        public void UpdateRetryPolicyUpdatesTheRetryPolicy()
        {
            var newPolicy = new BasicRetryPolicy(new RetryOptions {
                Delay = TimeSpan.FromMilliseconds(50)
            });
            var client = new AmqpEventHubClient("my.eventhub.com", "somePath", Mock.Of <TokenCredential>(), new EventHubClientOptions(), Mock.Of <EventHubRetryPolicy>());

            Assert.That(GetActiveRetryPolicy(client), Is.Not.SameAs(newPolicy), "The initial policy should be a unique instance");

            client.UpdateRetryPolicy(newPolicy);
            Assert.That(GetActiveRetryPolicy(client), Is.SameAs(newPolicy), "The updated policy should match");
        }
Exemple #13
0
        static EventHubClient Create(EventHubsConnectionStringBuilder csb)
        {
            if (string.IsNullOrWhiteSpace(csb.EntityPath))
            {
                throw Fx.Exception.ArgumentNullOrWhiteSpace(nameof(csb.EntityPath));
            }

            EventHubsEventSource.Log.EventHubClientCreateStart(csb.Endpoint.Host, csb.EntityPath);
            EventHubClient eventHubClient = new AmqpEventHubClient(csb);

            EventHubsEventSource.Log.EventHubClientCreateStop(eventHubClient.ClientId);
            return(eventHubClient);
        }
        public AmqpServiceClient(AmqpEventHubClient eventHubClient, string address)
            : base(nameof(AmqpServiceClient <T>) + StringUtility.GetRandomString())
        {
            if (!typeof(T).GetTypeInfo().IsInterface)
            {
                throw new NotSupportedException("Not an interface");
            }

            this.eventHubClient    = eventHubClient;
            this.Address           = address;
            this.Channel           = new AmqpClientProxy(this, typeof(T)).GetChannel();
            this.link              = new FaultTolerantAmqpObject <RequestResponseAmqpLink>(t => this.OpenLinkAsync(t), rrlink => rrlink.CloseAsync(TimeSpan.FromSeconds(10)));
            this.clientLinkManager = new ActiveClientLinkManager(this.eventHubClient);
        }
Exemple #15
0
        public void UpdateRetryPolicyUpdatesTheOperationTimeout()
        {
            var initialPolicy = new BasicRetryPolicy(new RetryOptions {
                TryTimeout = TimeSpan.FromSeconds(17)
            });
            TimeSpan initialTimeout = initialPolicy.CalculateTryTimeout(0);
            var      client         = new AmqpEventHubClient("my.eventhub.com", "somePath", Mock.Of <TokenCredential>(), new EventHubClientOptions(), initialPolicy);

            Assert.That(GetTimeout(client), Is.EqualTo(initialTimeout), "The initial timeout should match");

            var newPolicy = new BasicRetryPolicy(new RetryOptions {
                TryTimeout = TimeSpan.FromMilliseconds(50)
            });
            TimeSpan newTimeout = newPolicy.CalculateTryTimeout(0);

            client.UpdateRetryPolicy(newPolicy);
            Assert.That(GetTimeout(client), Is.EqualTo(newTimeout), "The updated timeout should match");
        }
Exemple #16
0
        /// <summary>
        /// Creates a new instance of the Event Hubs client using the specified endpoint, entity path, and token provider.
        /// </summary>
        /// <param name="endpointAddress">Fully qualified domain name for Event Hubs. Most likely, {yournamespace}.servicebus.windows.net</param>
        /// <param name="entityPath">Event Hub path</param>
        /// <param name="tokenProvider">Token provider which will generate security tokens for authorization.</param>
        /// <param name="operationTimeout">Operation timeout for Event Hubs operations.</param>
        /// <param name="transportType">Transport type on connection.</param>
        /// <returns></returns>
        public static EventHubClient CreateWithTokenProvider(
            Uri endpointAddress,
            string entityPath,
            ITokenProvider tokenProvider,
            TimeSpan?operationTimeout   = null,
            TransportType transportType = TransportType.Amqp)
        {
            Guard.ArgumentNotNull(nameof(endpointAddress), endpointAddress);
            Guard.ArgumentNotNull(nameof(tokenProvider), tokenProvider);
            Guard.ArgumentNotNullOrWhiteSpace(nameof(entityPath), entityPath);

            EventHubsEventSource.Log.EventHubClientCreateStart(endpointAddress.Host, entityPath);
            EventHubClient eventHubClient = new AmqpEventHubClient(
                endpointAddress,
                entityPath,
                tokenProvider,
                operationTimeout ?? ClientConstants.DefaultOperationTimeout,
                transportType);

            EventHubsEventSource.Log.EventHubClientCreateStop(eventHubClient.ClientId);
            return(eventHubClient);
        }
Exemple #17
0
 /// <summary>
 ///   Gets the active operation timeout for the given client, using the
 ///   private field.
 /// </summary>
 ///
 private static TimeSpan GetTimeout(AmqpEventHubClient target) =>
 (TimeSpan)
 typeof(AmqpEventHubClient)
 .GetField("_tryTimeout", BindingFlags.Instance | BindingFlags.NonPublic)
 .GetValue(target);
Exemple #18
0
 /// <summary>
 ///   Gets the active retry policy for the given client, using the
 ///   private field.
 /// </summary>
 ///
 private static EventHubRetryPolicy GetActiveRetryPolicy(AmqpEventHubClient target) =>
 (EventHubRetryPolicy)
 typeof(AmqpEventHubClient)
 .GetField("_retryPolicy", BindingFlags.Instance | BindingFlags.NonPublic)
 .GetValue(target);
Exemple #19
0
        public void UpdateRetryPolicyValidatesTheRetryPolicy()
        {
            var client = new AmqpEventHubClient("my.eventhub.com", "somePath", Mock.Of <TokenCredential>(), new EventHubClientOptions(), Mock.Of <EventHubRetryPolicy>());

            Assert.That(() => client.UpdateRetryPolicy(null), Throws.ArgumentNullException);
        }