MassTransit: Slow InMemoryTestHarness?

I can not catch actual issue yet, but I’ll try to explain what I’m noticing and may be that will lead somewhere.

After starting to use InMemoryTestHarness, we’ve noticed that our unit test suite slowed down, a lot. Unit tests, that were running in matter of few milliseconds now take 6s each, which does not align with sentence in documentation “The in-memory harness is the easiest to use, is lightning fast, and runs entirely in-memory without any external dependencies”.

Interestingly enough if I profile single test, I don’t see such numbers and even if our tests do not run concurrently, I was suspecting something with locking or our extra harsh antiviruses.

So I have following method:

private static async Task TestHarness()
{
    var harness = new InMemoryTestHarness();

    await harness.Start();
    try
    {
    }
    finally
    {
        await harness.Stop();
    }
}

And if I run like this:

var start = DateTime.UtcNow;
        
for (int i = 0; i < 100; i++)
{
    await TestHarness();
}

Console.WriteLine(DateTime.UtcNow - start);

It is fast, less than 0.5s for 100 invocations. Now if I run it like this:

var start = DateTime.UtcNow;
            
Parallel.For(0, 100, async i =>
{
    await TestHarness();
});

Console.WriteLine(DateTime.UtcNow - start);

that becomes 30+s and in profiler I see, that most of the time is spent in waiting: image

Also I’ve noticed one interesting thing: image

If it is InMemory harness, does it actually need to rad all network interfaces in each unit test?

Sorry, that I did not had time to dig deeper in the issue and may be searching issues in obvious stuff.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 25 (14 by maintainers)

Commits related to this issue

Most upvoted comments

The default inactivity timeout is 6 seconds. A properly written test should never see this duration, and it’s usually because developers are selecting counts, or waiting for things that shouldn’t happen.

The default values can be changed using:

harness.TestTimeout = TimeSpan.FromMinutes(1);
harness.TestInactivityTimeout = TimeSpan.FromSeconds(1);

Single(), by its very nature, is the same as Count(). Use .First() instead.