Exemple #1
0
        public void ReadDefaultConnectionString(string defaultConnectionString, string defaultConnectionSettingString, string expectedValue)
        {
            var configuration = ConfigurationUtilities.CreateConfiguration(
                new KeyValuePair <string, string>("ConnectionStrings:" + Constants.DefaultConnectionStringName, defaultConnectionString),
                new KeyValuePair <string, string>(Constants.DefaultConnectionSettingStringName, defaultConnectionSettingString));

            var mockProvider = new Mock <MessagingProvider>();

            mockProvider.Setup(
                p => p.CreateClient(expectedValue))
            .Returns(Mock.Of <ServiceBusClient>());

            var factory = new ServiceBusClientFactory(configuration, Mock.Of <AzureComponentFactory>(), mockProvider.Object, new AzureEventSourceLogForwarder(new NullLoggerFactory()));

            if (expectedValue == null)
            {
                Assert.That(
                    () => factory.CreateClientFromSetting(null),
                    Throws.InstanceOf <InvalidOperationException>());
            }
            else
            {
                factory.CreateClientFromSetting(null);
                mockProvider.VerifyAll();
            }
        }
Exemple #2
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 Setup()
        {
            _mockExecutor = new Mock <ITriggeredFunctionExecutor>(MockBehavior.Strict);
            _client       = new ServiceBusClient(_testConnection);
            ServiceBusProcessorOptions processorOptions = new ServiceBusProcessorOptions();
            ServiceBusProcessor        messageProcessor = _client.CreateProcessor(_entityPath);
            ServiceBusReceiver         receiver         = _client.CreateReceiver(_entityPath);

            _mockMessageProcessor = new Mock <MessageProcessor>(MockBehavior.Strict, messageProcessor);
            var configuration = ConfigurationUtilities.CreateConfiguration(new KeyValuePair <string, string>(_connection, _testConnection));

            _serviceBusOptions = new ServiceBusOptions();
            _mockProvider      = new Mock <MessagingProvider>(new OptionsWrapper <ServiceBusOptions>(_serviceBusOptions));
            _mockClientFactory = new Mock <ServiceBusClientFactory>(
                configuration,
                Mock.Of <AzureComponentFactory>(),
                _mockProvider.Object,
                new AzureEventSourceLogForwarder(new NullLoggerFactory()),
                new OptionsWrapper <ServiceBusOptions>(_serviceBusOptions));

            _mockProvider
            .Setup(p => p.CreateMessageProcessor(_client, _entityPath, It.IsAny <ServiceBusProcessorOptions>()))
            .Returns(_mockMessageProcessor.Object);

            _mockProvider
            .Setup(p => p.CreateClient(_testConnection, It.IsAny <ServiceBusClientOptions>()))
            .Returns(_client);

            _loggerFactory  = new LoggerFactory();
            _loggerProvider = new TestLoggerProvider();
            _loggerFactory.AddProvider(_loggerProvider);

            var concurrencyOptions             = new OptionsWrapper <ConcurrencyOptions>(new ConcurrencyOptions());
            var mockConcurrencyThrottleManager = new Mock <IConcurrencyThrottleManager>(MockBehavior.Strict);
            var concurrencyManager             = new ConcurrencyManager(concurrencyOptions, _loggerFactory, mockConcurrencyThrottleManager.Object);

            _listener = new ServiceBusListener(
                _functionId,
                ServiceBusEntityType.Queue,
                _entityPath,
                false,
                _serviceBusOptions.AutoCompleteMessages,
                _mockExecutor.Object,
                _serviceBusOptions,
                _connection,
                _mockProvider.Object,
                _loggerFactory,
                false,
                _mockClientFactory.Object,
                concurrencyManager);

            _scaleMonitor = (ServiceBusScaleMonitor)_listener.GetMonitor();
        }
Exemple #7
0
        public ServiceBusListenerTests()
        {
            _mockExecutor = new Mock <ITriggeredFunctionExecutor>(MockBehavior.Strict);

            _client = new ServiceBusClient(_testConnection);
            ServiceBusProcessor processor = _client.CreateProcessor(_entityPath);
            ServiceBusReceiver  receiver  = _client.CreateReceiver(_entityPath);
            var configuration             = ConfigurationUtilities.CreateConfiguration(new KeyValuePair <string, string>("connection", _testConnection));

            ServiceBusOptions config = new ServiceBusOptions
            {
                ProcessErrorAsync = ExceptionReceivedHandler
            };

            _mockMessageProcessor = new Mock <MessageProcessor>(MockBehavior.Strict, processor);

            _mockMessagingProvider = new Mock <MessagingProvider>(new OptionsWrapper <ServiceBusOptions>(config));
            _mockClientFactory     = new Mock <ServiceBusClientFactory>(configuration, Mock.Of <AzureComponentFactory>(), _mockMessagingProvider.Object, new AzureEventSourceLogForwarder(new NullLoggerFactory()), new OptionsWrapper <ServiceBusOptions>(new ServiceBusOptions()));
            _mockMessagingProvider
            .Setup(p => p.CreateMessageProcessor(It.IsAny <ServiceBusClient>(), _entityPath, It.IsAny <ServiceBusProcessorOptions>()))
            .Returns(_mockMessageProcessor.Object);

            _mockMessagingProvider
            .Setup(p => p.CreateBatchMessageReceiver(It.IsAny <ServiceBusClient>(), _entityPath, default))
            .Returns(receiver);

            _loggerFactory  = new LoggerFactory();
            _loggerProvider = new TestLoggerProvider();
            _loggerFactory.AddProvider(_loggerProvider);

            var concurrencyOptions = new OptionsWrapper <ConcurrencyOptions>(new ConcurrencyOptions());

            _mockConcurrencyThrottleManager = new Mock <IConcurrencyThrottleManager>(MockBehavior.Strict);
            var concurrencyManager = new ConcurrencyManager(concurrencyOptions, _loggerFactory, _mockConcurrencyThrottleManager.Object);

            _listener = new ServiceBusListener(
                _functionId,
                ServiceBusEntityType.Queue,
                _entityPath,
                false,
                config.AutoCompleteMessages,
                _mockExecutor.Object,
                config,
                "connection",
                _mockMessagingProvider.Object,
                _loggerFactory,
                false,
                _mockClientFactory.Object,
                concurrencyManager);
            _listener.Started = true;
        }
        public ServiceBusTriggerAttributeBindingProviderTests()
        {
            var configuration = ConfigurationUtilities.CreateConfiguration(new KeyValuePair <string, string>(Constants.DefaultConnectionStringName, "defaultConnection"));

            Mock <INameResolver> mockResolver = new Mock <INameResolver>(MockBehavior.Strict);

            ServiceBusOptions options = new ServiceBusOptions();

            Mock <IConverterManager> convertManager = new Mock <IConverterManager>(MockBehavior.Default);
            var provider = new MessagingProvider(new OptionsWrapper <ServiceBusOptions>(options));
            var factory  = new ServiceBusClientFactory(configuration, new Mock <AzureComponentFactory>().Object, provider);

            _provider = new ServiceBusTriggerAttributeBindingProvider(mockResolver.Object, options, provider, NullLoggerFactory.Instance, convertManager.Object, factory);
        }
        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);
        }
Exemple #12
0
        public ServiceBusListenerTests()
        {
            _mockExecutor = new Mock <ITriggeredFunctionExecutor>(MockBehavior.Strict);

            var client = new ServiceBusClient(_testConnection);
            ServiceBusProcessor processor = client.CreateProcessor(_entityPath);
            ServiceBusReceiver  receiver  = client.CreateReceiver(_entityPath);
            var configuration             = ConfigurationUtilities.CreateConfiguration(new KeyValuePair <string, string>("connection", _testConnection));

            ServiceBusOptions config = new ServiceBusOptions
            {
                ExceptionHandler = ExceptionReceivedHandler
            };

            _mockMessageProcessor = new Mock <MessageProcessor>(MockBehavior.Strict, processor, receiver);

            _mockMessagingProvider = new Mock <MessagingProvider>(new OptionsWrapper <ServiceBusOptions>(config));
            _mockClientFactory     = new Mock <ServiceBusClientFactory>(configuration, Mock.Of <AzureComponentFactory>(), _mockMessagingProvider.Object);
            _mockMessagingProvider
            .Setup(p => p.CreateMessageProcessor(It.IsAny <ServiceBusClient>(), _entityPath))
            .Returns(_mockMessageProcessor.Object);

            _mockMessagingProvider
            .Setup(p => p.CreateBatchMessageReceiver(It.IsAny <ServiceBusClient>(), _entityPath))
            .Returns(receiver);

            _loggerFactory  = new LoggerFactory();
            _loggerProvider = new TestLoggerProvider();
            _loggerFactory.AddProvider(_loggerProvider);

            _listener = new ServiceBusListener(
                _functionId,
                EntityType.Queue,
                _entityPath,
                false,
                _mockExecutor.Object,
                config,
                "connection",
                _mockMessagingProvider.Object,
                _loggerFactory,
                false,
                _mockClientFactory.Object);
        }
        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);
        }
Exemple #15
0
        public async Task GetAutoCompleteMessagesOptionToUse_AutoCompleteDisabledOnTrigger()
        {
            var listenerContext = new ListenerFactoryContext(
                new Mock <FunctionDescriptor>().Object,
                new Mock <ITriggeredFunctionExecutor>().Object,
                CancellationToken.None);
            var parameters = new object[] { listenerContext };
            var entityPath = "autocompleteMessagesDisabled";

            var client = new ServiceBusClient(_testConnection);
            ServiceBusProcessor processor = client.CreateProcessor(entityPath);
            ServiceBusReceiver  receiver  = client.CreateReceiver(entityPath);
            var configuration             = ConfigurationUtilities.CreateConfiguration(new KeyValuePair <string, string>("connection", _testConnection));

            ServiceBusOptions config = new ServiceBusOptions();
            var mockMessageProcessor = new Mock <MessageProcessor>(MockBehavior.Strict, processor);

            var mockMessagingProvider = new Mock <MessagingProvider>(new OptionsWrapper <ServiceBusOptions>(config));
            var mockClientFactory     = new Mock <ServiceBusClientFactory>(configuration, Mock.Of <AzureComponentFactory>(), mockMessagingProvider.Object, new AzureEventSourceLogForwarder(new NullLoggerFactory()), new OptionsWrapper <ServiceBusOptions>(new ServiceBusOptions()));

            mockMessagingProvider
            .Setup(p => p.CreateMessageProcessor(It.IsAny <ServiceBusClient>(), entityPath, It.IsAny <ServiceBusProcessorOptions>()))
            .Returns(mockMessageProcessor.Object);

            var parameter = GetType().GetMethod("TestAutoCompleteMessagesDisbledOnTrigger", BindingFlags.NonPublic | BindingFlags.Static).GetParameters()[0];
            var context   = new TriggerBindingProviderContext(parameter, CancellationToken.None);
            var binding   = await _provider.TryCreateAsync(context);

            var createListenerTask = binding.GetType().GetMethod("CreateListenerAsync");
            var listener           = await(Task <IListener>) createListenerTask.Invoke(binding, parameters);
            var listenerOptions    = (ServiceBusOptions)listener.GetType().GetField("_serviceBusOptions", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(listener);
            var autoCompleteMessagesFlagSentToListener = (bool)listener.GetType().GetField("_autoCompleteMessages", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(listener);

            Assert.NotNull(listenerOptions);
            Assert.True(listenerOptions.AutoCompleteMessages);
            Assert.False(autoCompleteMessagesFlagSentToListener);
        }
        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);
        }