33
44using System ;
55using System . Collections . Generic ;
6+ using System . Reflection ;
67using 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 ;
713using Microsoft . Extensions . Azure ;
814using Microsoft . Extensions . Configuration ;
915using 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