-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMassTransitComponent.cs
More file actions
75 lines (67 loc) · 3.06 KB
/
MassTransitComponent.cs
File metadata and controls
75 lines (67 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using MassTransit;
using MassTransit.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NServiceBus.AcceptanceTesting;
using NServiceBus.AcceptanceTesting.Support;
using ServiceControl.Connector.MassTransit.AcceptanceTesting;
public class MassTransitComponent<TContext>(string name, Action<IBusRegistrationConfigurator> busConfig, Action<HostBuilderContext, IServiceCollection> hostConfig) : IComponentBehavior
where TContext : ScenarioContext
{
public Task<ComponentRunner> CreateRunner(RunDescriptor run) => Task.FromResult<ComponentRunner>(new Runner(name, busConfig, hostConfig, run.ScenarioContext, new ScenarioContextLoggerProvider(run.ScenarioContext)));
class Runner(string name,
Action<IBusRegistrationConfigurator> busConfig,
Action<HostBuilderContext, IServiceCollection> hostConfig,
ScenarioContext scenarioContext,
ScenarioContextLoggerProvider loggerProvider) : ComponentRunner
{
public override string Name { get; } = name;
public override async Task Start(CancellationToken cancellationToken = default)
{
var transportConfig = TestSuiteConfiguration.Current.CreateTransportConfiguration();
var builder = Host.CreateDefaultBuilder()
.ConfigureLogging(cfg => cfg.ClearProviders().SetMinimumLevel(LogLevel.Debug).AddProvider(loggerProvider))
.ConfigureServices((hostContext, services) =>
{
services.AddMassTransit(x =>
{
busConfig(x);
cleanup = transportConfig.ConfigureTransportForMassTransitEndpoint(x);
});
hostConfig(hostContext, services);
services.AddSingleton((TContext)scenarioContext);
});
host = builder.Build();
await host.StartAsync(cancellationToken).ConfigureAwait(false);
}
public override async Task Stop(CancellationToken cancellationToken = default)
{
if (host is null)
{
return;
}
var queuesToDelete = new List<string>();
try
{
var consumerRegistrations = host.Services.GetServices<IConsumerRegistration>();
foreach (var registration in consumerRegistrations)
{
// we assume the endpoint name is already set and formatted
queuesToDelete.Add(registration.GetDefinition(null!).GetEndpointName(null!));
}
await host.StopAsync(cancellationToken).ConfigureAwait(false);
}
finally
{
host.Dispose();
if (cleanup != null)
{
await cleanup(queuesToDelete, cancellationToken).ConfigureAwait(false);
}
}
}
IHost? host;
Func<IReadOnlyCollection<string>, CancellationToken, Task>? cleanup;
}
}