Beispiel #1
0
        public EventHubTriggerAttributeBindingProviderTests()
        {
            var configuration =
                ConfigurationUtilities.CreateConfiguration(
                    new KeyValuePair <string, string>("connection", "Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=abc123=;"),
                    new KeyValuePair <string, string>("Storage", "Endpoint=sb://test.blob.core.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=abc123="));

            var options = new EventHubOptions();

            Mock <IConverterManager> convertManager = new Mock <IConverterManager>(MockBehavior.Default);

            // mock the BlobServiceClient and BlobContainerClient which are used for the checkpointing
            var blobServiceClient = new Mock <BlobServiceClient>();

            blobServiceClient.Setup(client => client.GetBlobContainerClient(It.IsAny <string>()))
            .Returns(Mock.Of <BlobContainerClient>());
            var componentFactory = new Mock <AzureComponentFactory>();

            componentFactory.Setup(
                factory => factory.CreateClient(
                    typeof(BlobServiceClient),
                    It.IsAny <IConfiguration>(),
                    It.IsAny <TokenCredential>(),
                    It.IsAny <BlobClientOptions>())).Returns(blobServiceClient.Object);

            var factory = ConfigurationUtilities.CreateFactory(configuration, options, componentFactory.Object);

            _provider = new EventHubTriggerAttributeBindingProvider(convertManager.Object, Options.Create(options), NullLoggerFactory.Instance, factory);
        }
        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
                },
                MaxEventBatchSize = 20
            };

            var configuration = ConfigurationUtilities.CreateConfiguration(new KeyValuePair <string, string>("connection", connectionString));
            var factory       = ConfigurationUtilities.CreateFactory(configuration, options);

            var processor = factory.GetEventProcessorHost(expectedPathName, "connection", "consumer", false);
            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);

            int batchSize = (int)typeof(EventProcessor <EventProcessorHostPartition>)
                            .GetProperty("EventBatchMaximumCount", BindingFlags.NonPublic | BindingFlags.Instance)
                            .GetValue(processor);

            Assert.AreEqual(20, batchSize);
        }
        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 = ConfigurationUtilities.CreateConfiguration(new KeyValuePair <string, string>("connection", connectionString));
            var factory       = ConfigurationUtilities.CreateFactory(configuration, options);

            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);
        }
        public void GetEventHubClient_AddsConnection(string expectedPathName, string connectionString)
        {
            EventHubOptions options       = new EventHubOptions();
            var             configuration = ConfigurationUtilities.CreateConfiguration(new KeyValuePair <string, string>("connection", connectionString));

            var factory = ConfigurationUtilities.CreateFactory(configuration, options);

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

            Assert.AreEqual(expectedPathName, client.EventHubName);
        }
        public void ConsumersAndProducersAreCached()
        {
            EventHubOptions options       = new EventHubOptions();
            var             configuration = ConfigurationUtilities.CreateConfiguration(new KeyValuePair <string, string>("connection", ConnectionString));

            var factory   = ConfigurationUtilities.CreateFactory(configuration, options);
            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);
        }
        public void DefaultStrategyIsGreedy()
        {
            EventHubOptions options = new EventHubOptions();

            var configuration = ConfigurationUtilities.CreateConfiguration(new KeyValuePair <string, string>("connection", ConnectionString));
            var factory       = ConfigurationUtilities.CreateFactory(configuration, options);

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

            Assert.AreEqual(LoadBalancingStrategy.Greedy, processorOptions.LoadBalancingStrategy);
        }
        public void CreatesClientsFromConfigWithConnectionString()
        {
            EventHubOptions options       = new EventHubOptions();
            var             configuration = ConfigurationUtilities.CreateConfiguration(new KeyValuePair <string, string>("connection", ConnectionString));

            var factory  = ConfigurationUtilities.CreateFactory(configuration, options);
            var producer = factory.GetEventHubProducerClient("k1", "connection");
            var consumer = factory.GetEventHubConsumerClient("k1", "connection", null);
            var host     = factory.GetEventProcessorHost("k1", "connection", null, true);

            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);
        }
        public void RespectsConnectionOptionsForConsumer(string expectedPathName, string connectionString)
        {
            var             testEndpoint = new Uri("http://mycustomendpoint.com");
            EventHubOptions options      = new EventHubOptions
            {
                CustomEndpointAddress = testEndpoint,
                ClientRetryOptions    = new EventHubsRetryOptions
                {
                    MaximumRetries = 10
                }
            };

            var configuration = ConfigurationUtilities.CreateConfiguration(new KeyValuePair <string, string>("connection", connectionString));
            var factory       = ConfigurationUtilities.CreateFactory(configuration, options);

            var consumer       = factory.GetEventHubConsumerClient(expectedPathName, "connection", "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);
        }
        public void UsesDefaultConnectionToStorageAccount()
        {
            EventHubOptions options = new EventHubOptions();

            var configuration = ConfigurationUtilities.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 = ConfigurationUtilities.CreateFactory(configuration, options, factoryMock.Object);

            var client = factory.GetCheckpointStoreClient();

            Assert.AreEqual("azure-webjobs-eventhub", client.Name);
            Assert.AreEqual("devstoreaccount1", client.AccountName);
        }
        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 = ConfigurationUtilities.CreateConfiguration(new KeyValuePair <string, string>("connection:fullyQualifiedNamespace", "test89123-ns-x.servicebus.windows.net"));

            var factory  = ConfigurationUtilities.CreateFactory(configuration, options, componentFactoryMock.Object);
            var producer = factory.GetEventHubProducerClient("k1", "connection");
            var consumer = factory.GetEventHubConsumerClient("k1", "connection", null);
            var host     = factory.GetEventProcessorHost("k1", "connection", null, true);

            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);
        }