1+ // Copyright (c) .NET Foundation. All rights reserved.
2+ // Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+ using System . Collections . Generic ;
5+ using System . Reflection ;
6+ using System . Threading ;
7+ using System . Threading . Tasks ;
8+ using Azure . Core ;
9+ using Azure . Messaging . EventHubs ;
10+ using Azure . Messaging . EventHubs . Primitives ;
11+ using Azure . Storage . Blobs ;
12+ using Microsoft . Azure . WebJobs . EventHubs ;
13+ using Microsoft . Azure . WebJobs . EventHubs . Listeners ;
14+ using Microsoft . Azure . WebJobs . EventHubs . Processor ;
15+ using Microsoft . Azure . WebJobs . EventHubs . Tests ;
16+ using Microsoft . Azure . WebJobs . Host . Executors ;
17+ using Microsoft . Azure . WebJobs . Host . Listeners ;
18+ using Microsoft . Azure . WebJobs . Host . Protocols ;
19+ using Microsoft . Azure . WebJobs . Host . Triggers ;
20+ using Microsoft . Extensions . Azure ;
21+ using Microsoft . Extensions . Configuration ;
22+ using Microsoft . Extensions . Logging . Abstractions ;
23+ using Microsoft . Extensions . Options ;
24+ using Moq ;
25+ using NUnit . Framework ;
26+
27+ namespace Microsoft . Azure . WebJobs . Extensions . EventHubs . UnitTests
28+ {
29+ public class EventHubTriggerAttributeBindingProviderTests
30+ {
31+ private readonly EventHubTriggerAttributeBindingProvider _provider ;
32+
33+ public EventHubTriggerAttributeBindingProviderTests ( )
34+ {
35+ var configuration =
36+ ConfigurationUtilities . CreateConfiguration (
37+ new KeyValuePair < string , string > ( "connection" , "Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=abc123=;" ) ,
38+ new KeyValuePair < string , string > ( "Storage" , "Endpoint=sb://test.blob.core.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=abc123=" ) ) ;
39+
40+ var options = new EventHubOptions ( ) ;
41+
42+ Mock < IConverterManager > convertManager = new Mock < IConverterManager > ( MockBehavior . Default ) ;
43+
44+ // mock the BlobServiceClient and BlobContainerClient which are used for the checkpointing
45+ var blobServiceClient = new Mock < BlobServiceClient > ( ) ;
46+ blobServiceClient . Setup ( client => client . GetBlobContainerClient ( It . IsAny < string > ( ) ) )
47+ . Returns ( Mock . Of < BlobContainerClient > ( ) ) ;
48+ var componentFactory = new Mock < AzureComponentFactory > ( ) ;
49+ componentFactory . Setup (
50+ factory => factory . CreateClient (
51+ typeof ( BlobServiceClient ) ,
52+ It . IsAny < IConfiguration > ( ) ,
53+ It . IsAny < TokenCredential > ( ) ,
54+ It . IsAny < BlobClientOptions > ( ) ) ) . Returns ( blobServiceClient . Object ) ;
55+
56+ var factory = ConfigurationUtilities . CreateFactory ( configuration , options , componentFactory . Object ) ;
57+ _provider = new EventHubTriggerAttributeBindingProvider ( convertManager . Object , Options . Create ( options ) , NullLoggerFactory . Instance , factory ) ;
58+ }
59+
60+ [ Test ]
61+ [ TestCase ( nameof ( SingleDispatch ) , 1 ) ]
62+ [ TestCase ( nameof ( MultipleDispatch ) , 10 ) ]
63+ public async Task TryCreateAsync_BatchCountsDefaultedCorrectly ( string function , int expectedBatchCount )
64+ {
65+ ParameterInfo parameter = GetType ( ) . GetMethod ( function , BindingFlags . NonPublic | BindingFlags . Static ) . GetParameters ( ) [ 0 ] ;
66+ TriggerBindingProviderContext context = new TriggerBindingProviderContext ( parameter , CancellationToken . None ) ;
67+
68+ ITriggerBinding binding = await _provider . TryCreateAsync ( context ) ;
69+ Assert . NotNull ( binding ) ;
70+
71+ var listener = await binding . CreateListenerAsync ( new ListenerFactoryContext ( new FunctionDescriptor ( ) ,
72+ new Mock < ITriggeredFunctionExecutor > ( ) . Object , CancellationToken . None ) ) ;
73+
74+ var processorHost = ( EventProcessorHost ) typeof ( EventHubListener )
75+ . GetField ( "_eventProcessorHost" , BindingFlags . NonPublic | BindingFlags . Instance )
76+ . GetValue ( listener ) ;
77+ var batchCount = ( int ) typeof ( EventProcessor < EventProcessorHostPartition > ) . GetProperty ( "EventBatchMaximumCount" , BindingFlags . NonPublic | BindingFlags . Instance )
78+ . GetValue ( processorHost ) ;
79+ Assert . AreEqual ( expectedBatchCount , batchCount ) ;
80+ }
81+
82+ internal static void SingleDispatch (
83+ [ EventHubTrigger ( "test" , Connection = "connection" ) ]
84+ EventData eventData )
85+ {
86+ }
87+
88+ internal static void MultipleDispatch (
89+ [ EventHubTrigger ( "test" , Connection = "connection" ) ]
90+ EventData [ ] eventData )
91+ {
92+ }
93+ }
94+ }
0 commit comments