Marek M.
Marek M.

Reputation: 3951

How to use in memory transport with shared transport provider in MassTransit?

I'm trying to follow this guide in order to be able to create some functional tests:

https://masstransittemp.readthedocs.io/en/latest/configuration/transports/in_memory.html

The thing is that neither InMemoryTransportCache class, nor SetTransportProvider method can be imported. I also can't find them declared in MassTransit sources on github. How do you achieve what that link up top is describing?

Upvotes: 0

Views: 1945

Answers (2)

Marek M.
Marek M.

Reputation: 3951

As mentioned in the comments - that documentation is outdated, but my questions was still valid. After all, I decided not to go with the suggested approach of setting up a Docker container for the message broker, but instead I used the test harness (also suggested above). I'm using SpecFlow, so the general code is to create and start the harness in the BeforeScenario step, like this:

[BeforeScenario]
public async Task BeforeScenario() {
    _testHarness = new InMemoryTestHarness();

    await _testHarness.Start();
    TestStartup1.SetHarness(_testHarness);
    TestStartup2.SetHarness(_testHarness);

    // build WebHost and start both asp.net TestServer instances
}

[AfterScenario]
public async Task Cleanup() {
    await _testHarness.Stop();
}

And get the IBusControl instance from the harness, inside both test startups:

// test startup1
public override void UseMassTransit(IServiceCollection services) {
    services.AddMassTransit(_ => {
        // configure endpoints here, etc.

        return _testHarness.BusControl;
    }, RegisterConsumers);
}

// test startup2
public override void UseMassTransit(IServiceCollection services) {
    services.AddMassTransit(_ => {
        // configure endpoints here, etc.

        _testHarness.Bus.ConnectReceiveEndpoint("your-endpoint", ep => ep.ConfigureConsumers(serviceProvider));

        return _testHarness.BusControl;
    }, RegisterConsumers);    
}

I have yet to see how to make messages handling synchronous, so that I won't have to await the results, but anyway the above code solves my problem.

Upvotes: 1

Alexey Zimarev
Alexey Zimarev

Reputation: 19610

As mentioned in the comments, the docs you are reading are beyond irrelevant.

Here is how you can test your application with test harnesses: https://masstransit-project.com/usage/testing.html

Basically, there's no need to spin up the test server, unless you want to test your registrations. It's quite a heavy job to be done for all your tests.

The docs supply a brief example for testing a consumer, so I just copy-paste it here:

public async Task Should_test_the_consumer()
{
    var harness = new InMemoryTestHarness();
    var consumerHarness = harness.Consumer<MyConsumer>();

    await harness.Start();
    try
    {
        await harness.InputQueueSendEndpoint.Send(new MyMessage());

        // did the endpoint consume the message
        Assert.IsTrue(harness.Consumed.Select<MyMessage>().Any());

        // did the actual consumer consume the message
        Assert.IsTrue(consumerHarness.Consumed.Select<MyMessage>().Any());
    }
    finally
    {
        await harness.Stop();
    }
}

The test project for test harnesses also have a bunch of good examples: https://github.com/MassTransit/MassTransit/tree/develop/src/MassTransit.Tests/Testing

Upvotes: 1

Related Questions