Created
January 25, 2018 16:00
-
-
Save riezebosch/ea759ac20d17650d3391a4f12720c0ee to your computer and use it in GitHub Desktop.
Send drops message if no queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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