Skip to content

Commit f458dbc

Browse files
timbussmannjpalac
authored andcommitted
acceptance test
1 parent df231de commit f458dbc

2 files changed

Lines changed: 154 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
namespace ServiceBus.Tests
2+
{
3+
using System.Threading.Tasks;
4+
using Microsoft.Azure.WebJobs;
5+
using NServiceBus;
6+
using NServiceBus.AcceptanceTesting;
7+
using NServiceBus.AcceptanceTesting.Customization;
8+
using NUnit.Framework;
9+
10+
public class When_using_sendonly
11+
{
12+
[Test]
13+
public async Task Should_send_messages()
14+
{
15+
await Scenario.Define<Context>()
16+
.WithEndpoint<ReceivingEndpoint>()
17+
.WithComponent(new SendOnlyFunction())
18+
.Done(c => c.HandlerReceivedMessage)
19+
.Run();
20+
}
21+
22+
class Context : ScenarioContext
23+
{
24+
public bool HandlerReceivedMessage { get; set; }
25+
}
26+
27+
class ReceivingEndpoint : EndpointConfigurationBuilder
28+
{
29+
public ReceivingEndpoint() => EndpointSetup<DefaultEndpoint>();
30+
31+
public class TestMessageHandler : IHandleMessages<TestMessage>
32+
{
33+
readonly Context testContext;
34+
35+
public TestMessageHandler(Context testContext) => this.testContext = testContext;
36+
37+
public Task Handle(TestMessage message, IMessageHandlerContext context)
38+
{
39+
testContext.HandlerReceivedMessage = true;
40+
return Task.CompletedTask;
41+
}
42+
}
43+
}
44+
45+
class SendOnlyFunction : SimpleTriggerFunctionComponent
46+
{
47+
public SendOnlyFunction()
48+
{
49+
CustomizeConfiguration = configuration =>
50+
{
51+
configuration.AdvancedConfiguration.SendOnly();
52+
53+
configuration.Routing.RouteToEndpoint(typeof(TestMessage), typeof(ReceivingEndpoint));
54+
};
55+
}
56+
57+
public override Task TriggerAction(IFunctionEndpoint endpoint, ExecutionContext executionContext)
58+
{
59+
return endpoint.Send(new TestMessage(), executionContext);
60+
}
61+
}
62+
63+
class TestMessage : IMessage
64+
{
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)