I try to test MassTransit v8
consumer with TestServer
by creating whole application with all DI services. I have created TestServer
instance with replaced some dependencies but I am most concerned with adding MasstransitTestHarness
with consumer. In test message is sent and consumed by harness correctly, but I can't understand why my consumer can't consume message. TestStartup inherits from my application Startup when I have defined all DI services.
Here is an example of my code:
TestServer program
public class TestProgram
{
private const string API_URL = "http://localhost:5010";
public TestServer Server { get; set; }
public void SetUpTestServer()
{
var host = CreateWebHostBuilder();
Server = new TestServer(host);
}
private IWebHostBuilder CreateWebHostBuilder()
{
return new WebHostBuilder()
.ConfigureTestServices(serviceCollection =>
{
serviceCollection.AddMassTransitTestHarness(cfg =>
{
cfg.AddConsumer<MyConsumer>();
cfg.UsingInMemory((provider, config) =>
{
config.ReceiveEndpoint("MyMessageQueue",
e =>
{
e.Batch<IMyMessage>(b =>
{
b.Consumer<MyConsumer, IMyMessage>(provider);
});
});
});
});
})
.UseStartup<TestStartup>()
.UseUrls(API_URL)
.UseEnvironment("Test");
}
}
Single test
public async Task Test()
{
EndpointConvention.Map<IMyMessage>(new Uri($"queue:MyMessageQueue"));
var program = new TestProgram();
program.SetUpTestServer();
var harness = program.Server.Services.GetTestHarness();
await harness.Start();
try
{
await harness.Bus.Send<IMyMessage>(new MyMessage());
// It's OK
Assert.IsTrue(await harness.Sent.Any<IMyMessage>());
Assert.IsTrue(await harness.Consumed.Any<IMyMessage>());
var consumer = harness.GetConsumerHarness<MyConsumer>();
// It's wrong
Assert.That(await consumer.Consumed.Any<IMyMessage>());
}
finally
{
await harness.Stop();
}
}
Does anyone know why my consumer not consume sent message?
Your configuration is strange, you should follow the configuration guide:
private IWebHostBuilder CreateWebHostBuilder()
{
return new WebHostBuilder()
.ConfigureTestServices(serviceCollection =>
{
serviceCollection.AddMassTransitTestHarness(cfg =>
{
cfg.AddConsumer<MyConsumer>(c =>
c.Options<BatchOptions>(o => o.SetMessageLimit(5).SetTimeLimit(2000)))
.Endpoint(e => e.Name = "MyMessageQueue");;
});
})
.UseStartup<TestStartup>()
.UseUrls(API_URL)
.UseEnvironment("Test");
}
Also, the consumer doesn't consume IMyMessage
, it consumes Batch<IMyMessage>
, and I'm not entirely sure that consuming batch messages is visible in the harness, I'd have to check and be sure.
Also, as a complete test for comparison:
[TestFixture]
public class When_batch_limit_is_reached
{
[Test]
public async Task Should_deliver_the_batch_to_the_consumer()
{
await using var provider = new ServiceCollection()
.AddMassTransitTestHarness(x =>
{
x.AddConsumer<TestBatchConsumer>();
})
.BuildServiceProvider(true);
var harness = provider.GetTestHarness();
await harness.Start();
await harness.Bus.PublishBatch(new[] { new BatchItem(), new BatchItem() });
Assert.That(await harness.Consumed.SelectAsync<BatchItem>().Take(2).Count(), Is.EqualTo(2));
Assert.That(await harness.GetConsumerHarness<TestBatchConsumer>().Consumed.Any<Batch<BatchItem>>(), Is.True);
Assert.That(await harness.Published.Any<BatchResult>(x => x.Context.Message.Count == 2), Is.True);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.