-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigureRabbitMQTransportTestExecution.cs
More file actions
106 lines (92 loc) · 3.72 KB
/
ConfigureRabbitMQTransportTestExecution.cs
File metadata and controls
106 lines (92 loc) · 3.72 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using MassTransit;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NServiceBus.AcceptanceTesting.Support;
using NServiceBus.Transport;
using ServiceControl.Connector.MassTransit.AcceptanceTests.RabbitMQ;
class ConfigureRabbitMQTransportTestExecution(QueueType queueType = QueueType.Quorum) : IConfigureTransportTestExecution
{
TestRabbitMQTransport? transport;
public Func<CancellationToken, Task> ConfigureTransportForEndpoint(EndpointConfiguration endpointConfiguration, PublisherMetadata publisherMetadata)
{
transport = new TestRabbitMQTransport(RoutingTopology.Conventional(queueType), "host=localhost", false);
endpointConfiguration.UseTransport(transport);
return Cleanup;
}
public Func<IReadOnlyCollection<string>, CancellationToken, Task> ConfigureTransportForMassTransitEndpoint(IBusRegistrationConfigurator configurator)
{
configurator.UsingRabbitMq((context, cfg) =>
{
cfg.Host("localhost", "/", h =>
{
h.Username("guest");
h.Password("guest");
});
cfg.ConfigureEndpoints(context);
});
configurator.AddSingleton<IRetryMessageVerification>(new RabbitMQRetryMessageVerification());
configurator.AddConfigureEndpointsCallback((name, cfg) =>
{
if (cfg is IRabbitMqReceiveEndpointConfigurator rmq)
{
if (queueType == QueueType.Quorum)
{
rmq.SetQuorumQueue();
}
}
});
return (queuesToDelete, _) =>
{
DeleteQueues(queuesToDelete);
return Task.CompletedTask;
};
}
public Func<IReadOnlyCollection<string>, CancellationToken, Task> ConfigureTransportForConnector(IServiceCollection services, IConfiguration configuration)
{
services.UsingRabbitMQ("host=localhost", new Uri("http://localhost:15672/"), "guest", "guest", queueType);
return (queuesToDelete, _) =>
{
DeleteQueues(queuesToDelete);
return Task.CompletedTask;
};
}
Task Cleanup(CancellationToken cancellationToken)
{
PurgeQueues();
return Task.CompletedTask;
}
void PurgeQueues()
{
if (transport == null)
{
return;
}
DeleteQueues(transport.QueuesToCleanup.ToHashSet());
}
static void DeleteQueues(IReadOnlyCollection<string> queues)
{
using var connection = ConnectionHelper.ConnectionFactory.CreateConnection("Test Queue Purger");
using var channel = connection.CreateModel();
foreach (var queue in queues)
{
try
{
channel.QueueDelete(queue, false, false);
}
catch (Exception ex)
{
Console.WriteLine("Unable to clear queue {0}: {1}", queue, ex);
}
}
}
class TestRabbitMQTransport(RoutingTopology routingTopology, string connectionString, bool enableDelayedDelivery) : RabbitMQTransport(routingTopology, connectionString, enableDelayedDelivery)
{
public override async Task<TransportInfrastructure> Initialize(HostSettings hostSettings, ReceiveSettings[] receivers, string[] sendingAddresses, CancellationToken cancellationToken = default)
{
var infrastructure = await base.Initialize(hostSettings, receivers, sendingAddresses, cancellationToken);
QueuesToCleanup.AddRange(infrastructure.Receivers.Select(x => x.Value.ReceiveAddress).Concat(sendingAddresses).Distinct());
return infrastructure;
}
public List<string> QueuesToCleanup { get; } = [];
}
}