Using the follow template, if I do not have the Task.Delay(5000) in the test, the YourMessage is never received at the endpoint.
Of course, if I do have the Task.Delay(5000) then I do receive it. Is there a way to wait for the InMemory bus to 'finish' before stopping it?
(all this done with just setting a simple breakpoint and debugging the unit test)
[TestFixture]
public class MassTransitTests
{
IBusControl _busControl;
[SetUp]
public void Setup()
{
_busControl = Bus.Factory.CreateUsingInMemory(cfg =>
{
cfg.ReceiveEndpoint("queue_name", ep =>
{
ep.Handler<YourMessage>(context =>
{
return Console.Out.WriteLineAsync($"Received: {context.Message.Text}");
});
});
});
_busControl.Start();
}
[TearDown]
public void TearDown()
{
_busControl.Stop(new CancellationTokenSource(Debugger.IsAttached ? TimeSpan.FromMinutes(5) : TimeSpan.FromSeconds(30)).Token);
}
[Test]
public async Task TestMessage()
{
await _busControl.Publish(new YourMessage { Text = "Test" });
await Task.Delay(5000);
}
}
public class YourMessage { public string Text { get; set; } }