1+ namespace ServiceBus . Tests
2+ {
3+ using System ;
4+ using System . Threading ;
5+ using System . Threading . Tasks ;
6+ using Microsoft . Extensions . DependencyInjection ;
7+ using NServiceBus ;
8+ using NServiceBus . AcceptanceTesting ;
9+ using NServiceBus . AcceptanceTesting . Customization ;
10+ using NServiceBus . AcceptanceTesting . Support ;
11+ using NServiceBus . MessageMutator ;
12+ using Conventions = NServiceBus . AcceptanceTesting . Customization . Conventions ;
13+ using ExecutionContext = Microsoft . Azure . WebJobs . ExecutionContext ;
14+
15+ abstract class SimpleTriggerFunctionComponent : IComponentBehavior
16+ {
17+ public Task < ComponentRunner > CreateRunner ( RunDescriptor runDescriptor ) =>
18+ Task . FromResult < ComponentRunner > (
19+ new FunctionRunner (
20+ CustomizeConfiguration ,
21+ runDescriptor . ScenarioContext ,
22+ GetType ( ) ,
23+ TriggerAction ) ) ;
24+
25+ public abstract Task TriggerAction ( IFunctionEndpoint endpoint , ExecutionContext executionContext ) ;
26+
27+ public Action < ServiceBusTriggeredEndpointConfiguration > CustomizeConfiguration { private get ; set ; } = _ => { } ;
28+
29+ class FunctionRunner : ComponentRunner
30+ {
31+ public FunctionRunner ( Action < ServiceBusTriggeredEndpointConfiguration > configurationCustomization ,
32+ ScenarioContext scenarioContext ,
33+ Type functionComponentType ,
34+ Func < IFunctionEndpoint , ExecutionContext , Task > triggerAction )
35+ {
36+ this . configurationCustomization = configurationCustomization ;
37+ this . scenarioContext = scenarioContext ;
38+ this . functionComponentType = functionComponentType ;
39+ this . triggerAction = triggerAction ;
40+
41+ Name = Conventions . EndpointNamingConvention ( functionComponentType ) ;
42+ }
43+
44+ public override string Name { get ; }
45+
46+ public override Task Start ( CancellationToken token )
47+ {
48+ var functionEndpointConfiguration = new ServiceBusTriggeredEndpointConfiguration ( Name , default , null ) ;
49+ var endpointConfiguration = functionEndpointConfiguration . AdvancedConfiguration ;
50+
51+ endpointConfiguration . TypesToIncludeInScan ( functionComponentType . GetTypesScopedByTestClass ( ) ) ;
52+
53+ endpointConfiguration . Recoverability ( )
54+ . Immediate ( i => i . NumberOfRetries ( 0 ) )
55+ . Delayed ( d => d . NumberOfRetries ( 0 ) ) ;
56+
57+ configurationCustomization ( functionEndpointConfiguration ) ;
58+ var serverless = functionEndpointConfiguration . MakeServerless ( ) ;
59+
60+ endpointConfiguration . RegisterComponents ( c => c . AddSingleton ( scenarioContext . GetType ( ) , scenarioContext ) ) ;
61+
62+ endpointConfiguration . RegisterComponents ( c => c . AddSingleton < IMutateOutgoingTransportMessages > ( b => new TestIndependenceMutator ( scenarioContext ) ) ) ;
63+
64+ var serviceCollection = new ServiceCollection ( ) ;
65+ var startableEndpointWithExternallyManagedContainer = EndpointWithExternallyManagedContainer . Create ( endpointConfiguration , serviceCollection ) ;
66+ var serviceProvider = serviceCollection . BuildServiceProvider ( ) ;
67+
68+ endpoint = new InProcessFunctionEndpoint ( startableEndpointWithExternallyManagedContainer , serverless , serviceProvider ) ;
69+
70+ return Task . CompletedTask ;
71+ }
72+
73+ public override async Task ComponentsStarted ( CancellationToken cancellationToken )
74+ {
75+ await triggerAction ( endpoint , new ExecutionContext ( ) ) ;
76+ await base . ComponentsStarted ( cancellationToken ) ;
77+ }
78+
79+ IFunctionEndpoint endpoint ;
80+
81+ readonly Action < ServiceBusTriggeredEndpointConfiguration > configurationCustomization ;
82+ readonly ScenarioContext scenarioContext ;
83+ readonly Type functionComponentType ;
84+ readonly Func < IFunctionEndpoint , ExecutionContext , Task > triggerAction ;
85+ }
86+ }
87+ }
0 commit comments