-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Connection options fix #18508
Merged
JoshLove-msft
merged 2 commits into
Azure:master
from
JoshLove-msft:connection-options-fix
Feb 8, 2021
Merged
Connection options fix #18508
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,13 @@ | |
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using Azure.Identity; | ||
using Azure.Messaging.EventHubs; | ||
using Azure.Messaging.EventHubs.Consumer; | ||
using Azure.Messaging.EventHubs.Primitives; | ||
using Azure.Messaging.EventHubs.Producer; | ||
using Microsoft.Azure.WebJobs.EventHubs.Processor; | ||
using Microsoft.Extensions.Azure; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Options; | ||
|
@@ -25,12 +31,12 @@ public void EntityPathInConnectionString(string expectedPathName, string connect | |
EventHubOptions options = new EventHubOptions(); | ||
|
||
// Test sender | ||
options.AddSender("k1", connectionString); | ||
options.AddSender(expectedPathName, connectionString); | ||
|
||
var configuration = CreateConfiguration(); | ||
var factory = new EventHubClientFactory(configuration, Mock.Of<AzureComponentFactory>(), Options.Create(options), new DefaultNameResolver(configuration)); | ||
|
||
var client = factory.GetEventHubProducerClient("k1", null); | ||
var client = factory.GetEventHubProducerClient(expectedPathName, null); | ||
Assert.AreEqual(expectedPathName, client.EventHubName); | ||
} | ||
|
||
|
@@ -44,7 +50,7 @@ public void GetEventHubClient_AddsConnection(string expectedPathName, string con | |
|
||
var factory = new EventHubClientFactory(configuration, Mock.Of<AzureComponentFactory>(), Options.Create(options), new DefaultNameResolver(configuration)); | ||
|
||
var client = factory.GetEventHubProducerClient("k1", "connection"); | ||
var client = factory.GetEventHubProducerClient(expectedPathName, "connection"); | ||
Assert.AreEqual(expectedPathName, client.EventHubName); | ||
} | ||
|
||
|
@@ -145,6 +151,120 @@ public void UsesRegisteredConnectionToStorageAccount() | |
Assert.AreEqual("http://blobs/azure-webjobs-eventhub", client.Uri.ToString()); | ||
} | ||
|
||
[TestCase("k1", ConnectionString)] | ||
[TestCase("path2", ConnectionStringWithEventHub)] | ||
public void RespectsConnectionOptionsForProducer(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.AddSender(expectedPathName, connectionString); | ||
|
||
var configuration = CreateConfiguration(); | ||
var factory = new EventHubClientFactory(configuration, Mock.Of<AzureComponentFactory>(), Options.Create(options), new DefaultNameResolver(configuration)); | ||
|
||
var producer = factory.GetEventHubProducerClient(expectedPathName, null); | ||
EventHubConnection connection = (EventHubConnection)typeof(EventHubProducerClient).GetProperty("Connection", BindingFlags.NonPublic | BindingFlags.Instance) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
.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); | ||
} | ||
|
||
[TestCase("k1", ConnectionString)] | ||
[TestCase("path2", ConnectionStringWithEventHub)] | ||
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); | ||
} | ||
|
||
[TestCase("k1", ConnectionString)] | ||
[TestCase("path2", ConnectionStringWithEventHub)] | ||
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 | ||
} | ||
}; | ||
|
||
options.AddReceiver(expectedPathName, connectionString); | ||
|
||
var configuration = CreateConfiguration(); | ||
var factory = new EventHubClientFactory(configuration, Mock.Of<AzureComponentFactory>(), Options.Create(options), new DefaultNameResolver(configuration)); | ||
|
||
var processor = factory.GetEventProcessorHost(expectedPathName, null, "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); | ||
} | ||
|
||
private IConfiguration CreateConfiguration(params KeyValuePair<string, string>[] data) | ||
{ | ||
return new ConfigurationBuilder().AddInMemoryCollection(data).Build(); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Helper method?