Ejemplo n.º 1
0
        public void RespectsConnectionOptionsForProcessor(string expectedPathName, string connectionString)
        {
            var             testEndpoint = new Uri("http://mycustomendpoint.com");
            EventHubOptions options      = new EventHubOptions
            {
                CustomEndpointAddress = testEndpoint,
                TransportType         = EventHubsTransportType.AmqpWebSockets,
                WebProxy           = new WebProxy("http://proxyserver/"),
                ClientRetryOptions = new EventHubsRetryOptions
                {
                    MaximumRetries = 10
                }
            };

            var configuration = CreateConfiguration(new KeyValuePair <string, string>("connection", connectionString));
            var factory       = new EventHubClientFactory(configuration, Mock.Of <AzureComponentFactory>(), Options.Create(options), new DefaultNameResolver(configuration), new AzureEventSourceLogForwarder(new NullLoggerFactory()));

            var processor = factory.GetEventProcessorHost(expectedPathName, "connection", "consumer");
            EventProcessorOptions processorOptions = (EventProcessorOptions)typeof(EventProcessor <EventProcessorHostPartition>)
                                                     .GetProperty("Options", BindingFlags.NonPublic | BindingFlags.Instance)
                                                     .GetValue(processor);

            Assert.AreEqual(testEndpoint, processorOptions.ConnectionOptions.CustomEndpointAddress);
            Assert.AreEqual(EventHubsTransportType.AmqpWebSockets, processorOptions.ConnectionOptions.TransportType);
            Assert.AreEqual("http://proxyserver/", ((WebProxy)processorOptions.ConnectionOptions.Proxy).Address.AbsoluteUri);
            Assert.AreEqual(10, processorOptions.RetryOptions.MaximumRetries);
            Assert.AreEqual(expectedPathName, processor.EventHubName);
        }
        public void RespectsConnectionOptionsForProcessor(string expectedPathName, string connectionString)
        {
            var             testEndpoint = new Uri("http://mycustomendpoint.com");
            EventHubOptions options      = new EventHubOptions
            {
                ConnectionOptions = new EventHubConnectionOptions
                {
                    CustomEndpointAddress = testEndpoint
                },
                RetryOptions = new EventHubsRetryOptions
                {
                    MaximumRetries = 10
                }
            };

            var configuration = CreateConfiguration(new KeyValuePair <string, string>("connection", connectionString));
            var factory       = new EventHubClientFactory(configuration, Mock.Of <AzureComponentFactory>(), Options.Create(options), new DefaultNameResolver(configuration));

            var processor = factory.GetEventProcessorHost(expectedPathName, "connection", "consumer");
            EventProcessorOptions processorOptions = (EventProcessorOptions)typeof(EventProcessor <EventProcessorHostPartition>)
                                                     .GetProperty("Options", BindingFlags.NonPublic | BindingFlags.Instance)
                                                     .GetValue(processor);

            Assert.AreEqual(testEndpoint, processorOptions.ConnectionOptions.CustomEndpointAddress);

            Assert.AreEqual(10, processorOptions.RetryOptions.MaximumRetries);
            Assert.AreEqual(expectedPathName, processor.EventHubName);
        }
Ejemplo n.º 3
0
        public void RespectsConnectionOptionsForProducer(string expectedPathName, string connectionString)
        {
            var             testEndpoint = new Uri("http://mycustomendpoint.com");
            EventHubOptions options      = new EventHubOptions
            {
                CustomEndpointAddress = testEndpoint,
                ClientRetryOptions    = new EventHubsRetryOptions
                {
                    MaximumRetries = 10
                }
            };

            var configuration = CreateConfiguration(new KeyValuePair <string, string>("connection", connectionString));
            var factory       = new EventHubClientFactory(configuration, Mock.Of <AzureComponentFactory>(), Options.Create(options), new DefaultNameResolver(configuration), new AzureEventSourceLogForwarder(new NullLoggerFactory()));

            var producer = factory.GetEventHubProducerClient(expectedPathName, "connection");
            EventHubConnection connection = (EventHubConnection)typeof(EventHubProducerClient).GetProperty("Connection", BindingFlags.NonPublic | BindingFlags.Instance)
                                            .GetValue(producer);
            EventHubConnectionOptions connectionOptions = (EventHubConnectionOptions)typeof(EventHubConnection).GetProperty("Options", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(connection);

            Assert.AreEqual(testEndpoint, connectionOptions.CustomEndpointAddress);
            Assert.AreEqual(expectedPathName, producer.EventHubName);

            EventHubProducerClientOptions producerOptions = (EventHubProducerClientOptions)typeof(EventHubProducerClient).GetProperty("Options", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(producer);

            Assert.AreEqual(10, producerOptions.RetryOptions.MaximumRetries);
            Assert.AreEqual(expectedPathName, producer.EventHubName);
        }
Ejemplo n.º 4
0
        public void GetEventHubClient_AddsConnection(string expectedPathName, string connectionString)
        {
            EventHubOptions options       = new EventHubOptions();
            var             configuration = CreateConfiguration(new KeyValuePair <string, string>("connection", connectionString));

            var factory = new EventHubClientFactory(configuration, Mock.Of <AzureComponentFactory>(), Options.Create(options), new DefaultNameResolver(configuration), new AzureEventSourceLogForwarder(new NullLoggerFactory()));

            var client = factory.GetEventHubProducerClient(expectedPathName, "connection");

            Assert.AreEqual(expectedPathName, client.EventHubName);
        }
Ejemplo n.º 5
0
        public void UsesDefaultConnectionToStorageAccount()
        {
            EventHubOptions options = new EventHubOptions();

            var configuration = CreateConfiguration(new KeyValuePair <string, string>("AzureWebJobsStorage", "UseDevelopmentStorage=true"));

            var factory = new EventHubClientFactory(configuration, Mock.Of <AzureComponentFactory>(), Options.Create(options), new DefaultNameResolver(configuration));

            var client = factory.GetCheckpointStoreClient();

            Assert.AreEqual("azure-webjobs-eventhub", client.Name);
            Assert.AreEqual("devstoreaccount1", client.AccountName);
        }
Ejemplo n.º 6
0
        public void ConsumersAndProducersAreCached()
        {
            EventHubOptions options       = new EventHubOptions();
            var             configuration = CreateConfiguration(new KeyValuePair <string, string>("connection", ConnectionString));

            var factory   = new EventHubClientFactory(configuration, Mock.Of <AzureComponentFactory>(), Options.Create(options), new DefaultNameResolver(configuration), new AzureEventSourceLogForwarder(new NullLoggerFactory()));
            var producer  = factory.GetEventHubProducerClient("k1", "connection");
            var consumer  = factory.GetEventHubConsumerClient("k1", "connection", null);
            var producer2 = factory.GetEventHubProducerClient("k1", "connection");
            var consumer2 = factory.GetEventHubConsumerClient("k1", "connection", null);

            Assert.AreSame(producer, producer2);
            Assert.AreSame(consumer, consumer2);
        }
Ejemplo n.º 7
0
        public void DefaultStrategyIsGreedy()
        {
            EventHubOptions options = new EventHubOptions();

            var configuration = CreateConfiguration(new KeyValuePair <string, string>("connection", ConnectionString));
            var factory       = new EventHubClientFactory(configuration, Mock.Of <AzureComponentFactory>(), Options.Create(options), new DefaultNameResolver(configuration), new AzureEventSourceLogForwarder(new NullLoggerFactory()));

            var processor = factory.GetEventProcessorHost("connection", "connection", "consumer");
            EventProcessorOptions processorOptions = (EventProcessorOptions)typeof(EventProcessor <EventProcessorHostPartition>)
                                                     .GetProperty("Options", BindingFlags.NonPublic | BindingFlags.Instance)
                                                     .GetValue(processor);

            Assert.AreEqual(LoadBalancingStrategy.Greedy, processorOptions.LoadBalancingStrategy);
        }
Ejemplo n.º 8
0
        public void EntityPathInConnectionString(string expectedPathName, string connectionString)
        {
            EventHubOptions options = new EventHubOptions();

            // Test sender
            options.AddSender("k1", connectionString);

            var configuration = CreateConfiguration();
            var factory       = new EventHubClientFactory(configuration, Mock.Of <AzureComponentFactory>(), Options.Create(options), new DefaultNameResolver(configuration));

            var client = factory.GetEventHubProducerClient("k1", null);

            Assert.AreEqual(expectedPathName, client.EventHubName);
        }
Ejemplo n.º 9
0
        public void RespectsConnectionOptionsForConsumer(string expectedPathName, string connectionString)
        {
            var             testEndpoint = new Uri("http://mycustomendpoint.com");
            EventHubOptions options      = new EventHubOptions
            {
                ConnectionOptions = new EventHubConnectionOptions
                {
                    CustomEndpointAddress = testEndpoint
                },
                RetryOptions = new EventHubsRetryOptions
                {
                    MaximumRetries = 10
                }
            };

            options.AddReceiver(expectedPathName, connectionString);

            var configuration = CreateConfiguration();
            var factory       = new EventHubClientFactory(configuration, Mock.Of <AzureComponentFactory>(), Options.Create(options), new DefaultNameResolver(configuration));

            var consumer       = factory.GetEventHubConsumerClient(expectedPathName, null, "consumer");
            var consumerClient = (EventHubConsumerClient)typeof(EventHubConsumerClientImpl)
                                 .GetField("_client", BindingFlags.NonPublic | BindingFlags.Instance)
                                 .GetValue(consumer);
            EventHubConnection connection = (EventHubConnection)typeof(EventHubConsumerClient)
                                            .GetProperty("Connection", BindingFlags.NonPublic | BindingFlags.Instance)
                                            .GetValue(consumerClient);
            EventHubConnectionOptions connectionOptions = (EventHubConnectionOptions)typeof(EventHubConnection)
                                                          .GetProperty("Options", BindingFlags.NonPublic | BindingFlags.Instance)
                                                          .GetValue(connection);

            Assert.AreEqual(testEndpoint, connectionOptions.CustomEndpointAddress);

            EventHubsRetryPolicy retryPolicy = (EventHubsRetryPolicy)typeof(EventHubConsumerClient)
                                               .GetProperty("RetryPolicy", BindingFlags.NonPublic | BindingFlags.Instance)
                                               .GetValue(consumerClient);

            // Reflection was still necessary here because BasicRetryOptions (which is the concrete derived type)
            // is internal.
            EventHubsRetryOptions retryOptions = (EventHubsRetryOptions)retryPolicy.GetType()
                                                 .GetProperty("Options", BindingFlags.Public | BindingFlags.Instance)
                                                 .GetValue(retryPolicy);

            Assert.AreEqual(10, retryOptions.MaximumRetries);
            Assert.AreEqual(expectedPathName, consumer.EventHubName);
        }
Ejemplo n.º 10
0
        public void CreatesClientsFromConfigWithConnectionString()
        {
            EventHubOptions options       = new EventHubOptions();
            var             configuration = CreateConfiguration(new KeyValuePair <string, string>("connection", ConnectionString));

            var factory  = new EventHubClientFactory(configuration, Mock.Of <AzureComponentFactory>(), Options.Create(options), new DefaultNameResolver(configuration), new AzureEventSourceLogForwarder(new NullLoggerFactory()));
            var producer = factory.GetEventHubProducerClient("k1", "connection");
            var consumer = factory.GetEventHubConsumerClient("k1", "connection", null);
            var host     = factory.GetEventProcessorHost("k1", "connection", null);

            Assert.AreEqual("k1", producer.EventHubName);
            Assert.AreEqual("k1", consumer.EventHubName);
            Assert.AreEqual("k1", host.EventHubName);

            Assert.AreEqual("test89123-ns-x.servicebus.windows.net", producer.FullyQualifiedNamespace);
            Assert.AreEqual("test89123-ns-x.servicebus.windows.net", consumer.FullyQualifiedNamespace);
            Assert.AreEqual("test89123-ns-x.servicebus.windows.net", host.FullyQualifiedNamespace);
        }
Ejemplo n.º 11
0
        public void UsesRegisteredConnectionToStorageAccount()
        {
            EventHubOptions options = new EventHubOptions();

            // Test sender
            options.AddReceiver("k1",
                                ConnectionString,
                                "BlobEndpoint=http://blobs/;AccountName=test;AccountKey=abc2564=");

            var configuration = CreateConfiguration();

            var factory = new EventHubClientFactory(configuration, Mock.Of <AzureComponentFactory>(), Options.Create(options), new DefaultNameResolver(configuration));

            var client = factory.GetCheckpointStoreClient("k1");

            Assert.AreEqual("azure-webjobs-eventhub", client.Name);
            Assert.AreEqual("http://blobs/azure-webjobs-eventhub", client.Uri.ToString());
        }
Ejemplo n.º 12
0
        public void UsesDefaultConnectionToStorageAccount()
        {
            EventHubOptions options = new EventHubOptions();

            var configuration = CreateConfiguration(new KeyValuePair <string, string>("AzureWebJobsStorage", "UseDevelopmentStorage=true"));

            var factoryMock = new Mock <AzureComponentFactory>();

            factoryMock.Setup(m => m.CreateClient(
                                  typeof(BlobServiceClient),
                                  It.Is <ConfigurationSection>(c => c.Path == "AzureWebJobsStorage"),
                                  null, null))
            .Returns(new BlobServiceClient(configuration["AzureWebJobsStorage"]));

            var factory = new EventHubClientFactory(configuration, factoryMock.Object, Options.Create(options), new DefaultNameResolver(configuration), new AzureEventSourceLogForwarder(new NullLoggerFactory()));

            var client = factory.GetCheckpointStoreClient();

            Assert.AreEqual("azure-webjobs-eventhub", client.Name);
            Assert.AreEqual("devstoreaccount1", client.AccountName);
        }
Ejemplo n.º 13
0
        public void CreatesClientsFromConfigWithFullyQualifiedNamespace()
        {
            EventHubOptions options = new EventHubOptions();
            var             componentFactoryMock = new Mock <AzureComponentFactory>();

            componentFactoryMock.Setup(c => c.CreateTokenCredential(
                                           It.Is <IConfiguration>(c => c["fullyQualifiedNamespace"] != null)))
            .Returns(new DefaultAzureCredential());

            var configuration = CreateConfiguration(new KeyValuePair <string, string>("connection:fullyQualifiedNamespace", "test89123-ns-x.servicebus.windows.net"));

            var factory  = new EventHubClientFactory(configuration, componentFactoryMock.Object, Options.Create(options), new DefaultNameResolver(configuration), new AzureEventSourceLogForwarder(new NullLoggerFactory()));
            var producer = factory.GetEventHubProducerClient("k1", "connection");
            var consumer = factory.GetEventHubConsumerClient("k1", "connection", null);
            var host     = factory.GetEventProcessorHost("k1", "connection", null);

            Assert.AreEqual("k1", producer.EventHubName);
            Assert.AreEqual("k1", consumer.EventHubName);
            Assert.AreEqual("k1", host.EventHubName);

            Assert.AreEqual("test89123-ns-x.servicebus.windows.net", producer.FullyQualifiedNamespace);
            Assert.AreEqual("test89123-ns-x.servicebus.windows.net", consumer.FullyQualifiedNamespace);
            Assert.AreEqual("test89123-ns-x.servicebus.windows.net", host.FullyQualifiedNamespace);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Sending task - this reads EventData from the buffer - gethers up a batch (if avaialable) and send to the event hub
        /// </summary>
        /// <returns></returns>
        private async Task Sender()
        {
            var connectionString = GetConfiguredConnectionString(EventHubNamespaceConnectionStringName);
            var eventHubName     = GetConfiguredEventHubName();

            try
            {
                // Open session with the Event Hub.
                //
                IEventHubClient eventHubClient = EventHubClientFactory.GetEventHubClient(connectionString, eventHubName);

                while (!Buffer.IsCompleted)  // run while there is data avaialble
                {
                    EventData item = null;

                    try
                    {
                        item = Buffer.Take();  // Get the first available item - this will block until there is data
                    }
                    catch (InvalidOperationException)
                    {
                        continue;  // Nothing left to do - this task is done - clean up and leave
                    }

                    int additional = GetAdditionalItemCount(Buffer.Count);

                    // Build the batch
                    List <EventData> batch = new List <EventData> {
                        item
                    };

                    while (additional > 0)
                    {
                        batch.Add(Buffer.Take());
                        additional--;
                    }

                    bool batchSent = false;
                    int  attempt   = 0;

                    while (!batchSent)
                    {
                        try
                        {
                            attempt++;
                            // Send the batch
                            await eventHubClient.SendBatchAsync(batch).ConfigureAwait(false);

                            batchSent = true;
                        }
                        catch (Exception exception)
                        {
                            string message = $"Exception while sending Batch to EventHub({EventHubName})";
                            Trace.TraceError(message + " - " + exception.ToString());
                            ErrorHandler.Error(message, exception);

                            if (attempt > MaxRetries)
                            {
                                Trace.TraceError($"Tried {attempt} time to send a batch to Event Hub({EventHubName}) - giving up");
                                ErrorHandler.Error($"Tried {attempt} time to send a batch to Event Hub({EventHubName}) - giving up");
                                batchSent = true;
                            }
                            else
                            {
                                await Task.Delay(EventHubRetryDelay).ConfigureAwait(false);
                            }
                        }
                    }

                    // Not really an error - but debugging information so see how things are working (or not).
                    //
                    ErrorHandler.Error($"Sent batch of size {batch.Count}");
                }

                await eventHubClient.CloseAsync().ConfigureAwait(false);
            }
            catch (Exception exception)
            {
                string message = $"There was a problem with the EventHub({EventHubName})";
                Trace.TraceError(message);
                ErrorHandler.Error(message, exception);
            }
        }
Ejemplo n.º 15
0
        private async Task Sender()
        {
            var connectionString = this.ConnectionString;
            var eventHubName     = this.EventHubName;

            try
            {
                // Open session with the Event Hub.
                if (this.eventHubClient == null && !string.IsNullOrEmpty(this.ConnectionString))
                {
                    this.eventHubClient = EventHubClientFactory.GetEventHubClient(this.ConnectionString);
                }
                while (!Buffer.IsCompleted)  // run while there is data avaialble
                {
                    EventData item = null;

                    try
                    {
                        item = Buffer.Take();  // Get the first available item - this will block until there is data
                    }
                    catch (InvalidOperationException)
                    {
                        continue;  // Nothing left to do - this task is done - clean up and leave
                    }

                    int additional = GetAdditionalItemCount(Buffer.Count);

                    // Build the batch
                    List <EventData> batch = new List <EventData> {
                        item
                    };

                    while (additional > 0)
                    {
                        batch.Add(Buffer.Take());
                        additional--;
                    }

                    bool batchSent = false;
                    int  attempt   = 0;

                    while (!batchSent)
                    {
                        try
                        {
                            attempt++;
                            // Send the batch
                            await eventHubClient.SendAsync(batch).ConfigureAwait(false);

                            batchSent = true;
                        }
                        catch (Exception exception)
                        {
                            string message = $"Exception while sending Batch to EventHub({EventHubName})";
                            Trace.TraceError(message + " - " + exception.ToString());
                            ErrorHandler.Error(message, exception);
                            //If the connection string is wrong, we'll never get the messages through, so stop trying (covers bad host name, bad eventhub name (status 404) and bad signature (status 401)
                            if (exception.Message.ToLower().Contains("no such host") || exception.Message.ToLower().Contains("status-code: 404") || exception.Message.ToLower().Contains("status-code: 401"))
                            {
                                return;
                            }

                            if (attempt > MaxRetries)
                            {
                                Trace.TraceError($"Tried {attempt} time to send a batch to Event Hub({EventHubName}) - giving up");
                                ErrorHandler.Error($"Tried {attempt} time to send a batch to Event Hub({EventHubName}) - giving up");
                                batchSent = true;
                            }
                            else
                            {
                                await Task.Delay(EventHubRetryDelay).ConfigureAwait(false);
                            }
                        }
                    }

                    // Not really an error - but debugging information so see how things are working (or not).
                    //
                    ErrorHandler.Error($"Sent batch of size {batch.Count}");
                }

                await eventHubClient.CloseAsync().ConfigureAwait(false);
            }
            catch (Exception exception)
            {
                string message = $"There was a problem with the EventHub({EventHubName})";
                Trace.TraceError(message);
                ErrorHandler.Error(message, exception);
            }
        }