Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Send drops message if no queue
public class RabbitMqSpecificTests : IClassFixture<RabbitMqOptions>
{
readonly RabbitMqOptions options;
string queue = $"{nameof(RabbitMqSpecificTests)}{nameof(CommandDissapearsIfNoQueue)}";
public RabbitMqSpecificTests(RabbitMqOptions options)
{
this.options = options;
EnsureQueueDeleted();
}
[Fact]
public async Task CommandDissapearsIfNoQueue()
{
await SendFirst();
Should.Throw<TimeoutException>(ReceiveLater());
}
[Fact]
public async Task CommandArrivesIfQueueIsAvailable()
{
var bus = await InitializeBus(TestConsumer.For<KlantAanmelden>());
await bus.StopAsync();
await SendFirst();
await ReceiveLater();
}
void EnsureQueueDeleted()
{
var factory = new ConnectionFactory();
factory.UserName = options.Username;
factory.Password = options.Password;
factory.HostName = options.Uri.Host;
var connection = factory.CreateConnection();
var channel = connection.CreateModel();
channel.QueueDelete(queue);
}
private async Task SendFirst()
{
var bus = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
var host = cfg.Host(options);
});
await bus.StartAsync();
var endpoint = await bus.GetSendEndpoint(new Uri(options.Uri, queue));
await endpoint.Send<KlantAanmelden>(new { CorrelationId = Guid.NewGuid(), Naam = "Manuel Riezebosch" });
await bus.StopAsync();
}
private async Task ReceiveLater()
{
var service = TestConsumer.For<KlantAanmelden>();
var bus = await InitializeBus(service);
try
{
await service
.ScheduleTimeout()
.Done();
}
finally
{
await bus.StopAsync();
}
}
private async Task<IBusControl> InitializeBus(ITestConsumer<KlantAanmelden> service)
{
var bus = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
var host = cfg.Host(options);
cfg.ReceiveEndpoint(host, queue, e =>
{
e.Instance(service);
});
});
await bus.StartAsync();
return bus;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.