Jim Bolla
Jim Bolla

Reputation: 8295

How to detect when in-memory bus has finished processing all messages during test?

I'm struggling to find an API for MassTransit's in-memory bus that allows my test to know that all messages have been consumed by their matching consumers.

If I have a bus configured like this:

_bus = Bus.Factory.CreateUsingInMemory(cfg =>
{
    cfg.ReceiveEndpoint("server.fax.inbound.received", ep =>
    {
        InboundFaxReceivedConsumer.UseMessageRetry(ep);
        ep.Consumer(() => new InboundFaxReceivedConsumer(
            _db.Create(),
            new NullLogger<InboundFaxReceivedConsumer>()));
    });
});

then i'd like to be able to do something like this:

_bus.Start();
_controller.Post(xml); // controller post is gonna publish a message
_bus.WaitForInactivity(); // <-- this is what I don't know how to do
_bus.Stop();

Upvotes: 0

Views: 748

Answers (2)

ArDumez
ArDumez

Reputation: 1001

An other solution if it's difficult to take consumer:

_harness.Consumed.Select<InboundFaxReceived>().ToList()

Upvotes: 0

Chris Patterson
Chris Patterson

Reputation: 33447

If you want to test that the messages have actually been consumed, you should use the InMemoryTestHarness, which has methods to observer consumers and ensure that the messages were consumed. After the test assertions pass (or, well, fail), you can then stop the test harness.

An example consumer test is in the unit tests: https://github.com/MassTransit/MassTransit/blob/develop/src/MassTransit.Tests/Testing/ConsumerTest_Specs.cs

In your case, you'd setup the harness for your consumer:

var harness = new InMemoryTestHarness();
var consumer = _harness.Consumer<InboundFaxReceivedConsumer>();

Then, once your controller Post method is called, you would wait for the consumer to receive and process the messages:

Assert.That(consumer.Consumed.Select<InboundFaxReceived>().Any(), Is.True);

Upvotes: 2

Related Questions