Skip to content

Commit 3d1ab95

Browse files
JoshLove-msftjongio
authored andcommitted
Connection options fix (Azure#18508)
* Pass options to consumer client * fix
1 parent 4d27fd1 commit 3d1ab95

File tree

2 files changed

+150
-6
lines changed

2 files changed

+150
-6
lines changed

sdk/eventhub/Microsoft.Azure.WebJobs.Extensions.EventHubs/src/Config/EventHubClientFactory.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,15 @@ internal IEventHubConsumerClient GetEventHubConsumerClient(string eventHubName,
129129
EventHubConsumerClient client = null;
130130
if (_options.RegisteredConsumerCredentials.TryGetValue(eventHubName, out var creds))
131131
{
132-
client = new EventHubConsumerClient(consumerGroup, creds.EventHubConnectionString, eventHubName);
132+
client = new EventHubConsumerClient(
133+
consumerGroup,
134+
creds.EventHubConnectionString,
135+
eventHubName,
136+
new EventHubConsumerClientOptions
137+
{
138+
RetryOptions = _options.RetryOptions,
139+
ConnectionOptions = _options.ConnectionOptions
140+
});
133141
}
134142
else if (!string.IsNullOrEmpty(connection))
135143
{
@@ -138,11 +146,27 @@ internal IEventHubConsumerClient GetEventHubConsumerClient(string eventHubName,
138146
if (info.FullyQualifiedEndpoint != null &&
139147
info.TokenCredential != null)
140148
{
141-
client = new EventHubConsumerClient(consumerGroup, info.FullyQualifiedEndpoint, eventHubName, info.TokenCredential);
149+
client = new EventHubConsumerClient(
150+
consumerGroup,
151+
info.FullyQualifiedEndpoint,
152+
eventHubName,
153+
info.TokenCredential,
154+
new EventHubConsumerClientOptions
155+
{
156+
RetryOptions = _options.RetryOptions,
157+
ConnectionOptions = _options.ConnectionOptions
158+
});
142159
}
143160
else
144161
{
145-
client = new EventHubConsumerClient(consumerGroup, NormalizeConnectionString(info.ConnectionString, eventHubName));
162+
client = new EventHubConsumerClient(
163+
consumerGroup,
164+
NormalizeConnectionString(info.ConnectionString, eventHubName),
165+
new EventHubConsumerClientOptions
166+
{
167+
RetryOptions = _options.RetryOptions,
168+
ConnectionOptions = _options.ConnectionOptions
169+
});
146170
}
147171
}
148172

sdk/eventhub/Microsoft.Azure.WebJobs.Extensions.EventHubs/tests/EventHubsClientFactoryTests.cs

Lines changed: 123 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Reflection;
67
using Azure.Identity;
8+
using Azure.Messaging.EventHubs;
9+
using Azure.Messaging.EventHubs.Consumer;
10+
using Azure.Messaging.EventHubs.Primitives;
11+
using Azure.Messaging.EventHubs.Producer;
12+
using Microsoft.Azure.WebJobs.EventHubs.Processor;
713
using Microsoft.Extensions.Azure;
814
using Microsoft.Extensions.Configuration;
915
using Microsoft.Extensions.Options;
@@ -25,12 +31,12 @@ public void EntityPathInConnectionString(string expectedPathName, string connect
2531
EventHubOptions options = new EventHubOptions();
2632

2733
// Test sender
28-
options.AddSender("k1", connectionString);
34+
options.AddSender(expectedPathName, connectionString);
2935

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

33-
var client = factory.GetEventHubProducerClient("k1", null);
39+
var client = factory.GetEventHubProducerClient(expectedPathName, null);
3440
Assert.AreEqual(expectedPathName, client.EventHubName);
3541
}
3642

@@ -44,7 +50,7 @@ public void GetEventHubClient_AddsConnection(string expectedPathName, string con
4450

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

47-
var client = factory.GetEventHubProducerClient("k1", "connection");
53+
var client = factory.GetEventHubProducerClient(expectedPathName, "connection");
4854
Assert.AreEqual(expectedPathName, client.EventHubName);
4955
}
5056

@@ -145,6 +151,120 @@ public void UsesRegisteredConnectionToStorageAccount()
145151
Assert.AreEqual("http://blobs/azure-webjobs-eventhub", client.Uri.ToString());
146152
}
147153

154+
[TestCase("k1", ConnectionString)]
155+
[TestCase("path2", ConnectionStringWithEventHub)]
156+
public void RespectsConnectionOptionsForProducer(string expectedPathName, string connectionString)
157+
{
158+
var testEndpoint = new Uri("http://mycustomendpoint.com");
159+
EventHubOptions options = new EventHubOptions
160+
{
161+
ConnectionOptions = new EventHubConnectionOptions
162+
{
163+
CustomEndpointAddress = testEndpoint
164+
},
165+
RetryOptions = new EventHubsRetryOptions
166+
{
167+
MaximumRetries = 10
168+
}
169+
};
170+
171+
options.AddSender(expectedPathName, connectionString);
172+
173+
var configuration = CreateConfiguration();
174+
var factory = new EventHubClientFactory(configuration, Mock.Of<AzureComponentFactory>(), Options.Create(options), new DefaultNameResolver(configuration));
175+
176+
var producer = factory.GetEventHubProducerClient(expectedPathName, null);
177+
EventHubConnection connection = (EventHubConnection)typeof(EventHubProducerClient).GetProperty("Connection", BindingFlags.NonPublic | BindingFlags.Instance)
178+
.GetValue(producer);
179+
EventHubConnectionOptions connectionOptions = (EventHubConnectionOptions)typeof(EventHubConnection).GetProperty("Options", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(connection);
180+
181+
Assert.AreEqual(testEndpoint, connectionOptions.CustomEndpointAddress);
182+
Assert.AreEqual(expectedPathName, producer.EventHubName);
183+
184+
EventHubProducerClientOptions producerOptions = (EventHubProducerClientOptions)typeof(EventHubProducerClient).GetProperty("Options", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(producer);
185+
Assert.AreEqual(10, producerOptions.RetryOptions.MaximumRetries);
186+
Assert.AreEqual(expectedPathName, producer.EventHubName);
187+
}
188+
189+
[TestCase("k1", ConnectionString)]
190+
[TestCase("path2", ConnectionStringWithEventHub)]
191+
public void RespectsConnectionOptionsForConsumer(string expectedPathName, string connectionString)
192+
{
193+
var testEndpoint = new Uri("http://mycustomendpoint.com");
194+
EventHubOptions options = new EventHubOptions
195+
{
196+
ConnectionOptions = new EventHubConnectionOptions
197+
{
198+
CustomEndpointAddress = testEndpoint
199+
},
200+
RetryOptions = new EventHubsRetryOptions
201+
{
202+
MaximumRetries = 10
203+
}
204+
};
205+
206+
options.AddReceiver(expectedPathName, connectionString);
207+
208+
var configuration = CreateConfiguration();
209+
var factory = new EventHubClientFactory(configuration, Mock.Of<AzureComponentFactory>(), Options.Create(options), new DefaultNameResolver(configuration));
210+
211+
var consumer = factory.GetEventHubConsumerClient(expectedPathName, null, "consumer");
212+
var consumerClient = (EventHubConsumerClient)typeof(EventHubConsumerClientImpl)
213+
.GetField("_client", BindingFlags.NonPublic | BindingFlags.Instance)
214+
.GetValue(consumer);
215+
EventHubConnection connection = (EventHubConnection)typeof(EventHubConsumerClient)
216+
.GetProperty("Connection", BindingFlags.NonPublic | BindingFlags.Instance)
217+
.GetValue(consumerClient);
218+
EventHubConnectionOptions connectionOptions = (EventHubConnectionOptions)typeof(EventHubConnection)
219+
.GetProperty("Options", BindingFlags.NonPublic | BindingFlags.Instance)
220+
.GetValue(connection);
221+
Assert.AreEqual(testEndpoint, connectionOptions.CustomEndpointAddress);
222+
223+
EventHubsRetryPolicy retryPolicy = (EventHubsRetryPolicy)typeof(EventHubConsumerClient)
224+
.GetProperty("RetryPolicy", BindingFlags.NonPublic | BindingFlags.Instance)
225+
.GetValue(consumerClient);
226+
227+
// Reflection was still necessary here because BasicRetryOptions (which is the concrete derived type)
228+
// is internal.
229+
EventHubsRetryOptions retryOptions = (EventHubsRetryOptions)retryPolicy.GetType()
230+
.GetProperty("Options", BindingFlags.Public | BindingFlags.Instance)
231+
.GetValue(retryPolicy);
232+
Assert.AreEqual(10, retryOptions.MaximumRetries);
233+
Assert.AreEqual(expectedPathName, consumer.EventHubName);
234+
}
235+
236+
[TestCase("k1", ConnectionString)]
237+
[TestCase("path2", ConnectionStringWithEventHub)]
238+
public void RespectsConnectionOptionsForProcessor(string expectedPathName, string connectionString)
239+
{
240+
var testEndpoint = new Uri("http://mycustomendpoint.com");
241+
EventHubOptions options = new EventHubOptions
242+
{
243+
ConnectionOptions = new EventHubConnectionOptions
244+
{
245+
CustomEndpointAddress = testEndpoint
246+
},
247+
RetryOptions = new EventHubsRetryOptions
248+
{
249+
MaximumRetries = 10
250+
}
251+
};
252+
253+
options.AddReceiver(expectedPathName, connectionString);
254+
255+
var configuration = CreateConfiguration();
256+
var factory = new EventHubClientFactory(configuration, Mock.Of<AzureComponentFactory>(), Options.Create(options), new DefaultNameResolver(configuration));
257+
258+
var processor = factory.GetEventProcessorHost(expectedPathName, null, "consumer");
259+
EventProcessorOptions processorOptions = (EventProcessorOptions)typeof(EventProcessor<EventProcessorHostPartition>)
260+
.GetProperty("Options", BindingFlags.NonPublic | BindingFlags.Instance)
261+
.GetValue(processor);
262+
Assert.AreEqual(testEndpoint, processorOptions.ConnectionOptions.CustomEndpointAddress);
263+
264+
Assert.AreEqual(10, processorOptions.RetryOptions.MaximumRetries);
265+
Assert.AreEqual(expectedPathName, processor.EventHubName);
266+
}
267+
148268
private IConfiguration CreateConfiguration(params KeyValuePair<string, string>[] data)
149269
{
150270
return new ConfigurationBuilder().AddInMemoryCollection(data).Build();

0 commit comments

Comments
 (0)