Skip to content

Commit 273fae7

Browse files
committed
test: Add multi-transport MassTransit container integration tests (#3519)
New MassTransitTestApp exercises three transports in a single container test: - Kafka Rider: validates Topic destination type and correct topic names - RabbitMQ: validates Queue publish/send/consume through MassTransit filter - InMemory MultiBus: validates loopback:// URI parsing via DI-registered bus Also updates existing MassTransit integration test regex to accommodate the parser change from underscore-split names to full path segments, and adds unit tests for fallback address and Rider prefix detection (49 total).
1 parent aef4271 commit 273fae7

15 files changed

Lines changed: 757 additions & 1 deletion

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2020 New Relic, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using System.Threading.Tasks;
5+
using MassTransit;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace MassTransitTestApp;
9+
10+
public class KafkaMessageConsumer : IConsumer<KafkaMessage>
11+
{
12+
private readonly ILogger<KafkaMessageConsumer> _logger;
13+
14+
public KafkaMessageConsumer(ILogger<KafkaMessageConsumer> logger)
15+
{
16+
_logger = logger;
17+
}
18+
19+
public Task Consume(ConsumeContext<KafkaMessage> context)
20+
{
21+
_logger.LogInformation("Kafka consumed: {Text}", context.Message.Text);
22+
return Task.CompletedTask;
23+
}
24+
}
25+
26+
public class RabbitMqMessageConsumer : IConsumer<RabbitMqMessage>
27+
{
28+
private readonly ILogger<RabbitMqMessageConsumer> _logger;
29+
30+
public RabbitMqMessageConsumer(ILogger<RabbitMqMessageConsumer> logger)
31+
{
32+
_logger = logger;
33+
}
34+
35+
public Task Consume(ConsumeContext<RabbitMqMessage> context)
36+
{
37+
_logger.LogInformation("RabbitMQ consumed: {Text}", context.Message.Text);
38+
return Task.CompletedTask;
39+
}
40+
}
41+
42+
public class InMemoryMessageConsumer : IConsumer<InMemoryMessage>
43+
{
44+
private readonly ILogger<InMemoryMessageConsumer> _logger;
45+
46+
public InMemoryMessageConsumer(ILogger<InMemoryMessageConsumer> logger)
47+
{
48+
_logger = logger;
49+
}
50+
51+
public Task Consume(ConsumeContext<InMemoryMessage> context)
52+
{
53+
_logger.LogInformation("InMemory consumed: {Text}", context.Message.Text);
54+
return Task.CompletedTask;
55+
}
56+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2020 New Relic, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.Extensions.Logging;
8+
9+
namespace MassTransitTestApp.Controllers;
10+
11+
[ApiController]
12+
[Route("inmemory")]
13+
public class InMemoryController : ControllerBase
14+
{
15+
private readonly ILogger<InMemoryController> _logger;
16+
private readonly IInMemoryBus _bus;
17+
18+
public InMemoryController(ILogger<InMemoryController> logger, IInMemoryBus bus)
19+
{
20+
_logger = logger;
21+
_bus = bus;
22+
}
23+
24+
[HttpGet("publish")]
25+
public async Task<string> Publish()
26+
{
27+
var message = new InMemoryMessage { Text = $"inmem-{Guid.NewGuid():N}" };
28+
await _bus.Publish(message);
29+
_logger.LogInformation("InMemory published: {Text}", message.Text);
30+
return "Complete";
31+
}
32+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2020 New Relic, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
using MassTransit;
7+
using Microsoft.AspNetCore.Mvc;
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace MassTransitTestApp.Controllers;
11+
12+
[ApiController]
13+
[Route("kafka")]
14+
public class KafkaController : ControllerBase
15+
{
16+
private readonly ILogger<KafkaController> _logger;
17+
private readonly ITopicProducer<KafkaMessage> _producer;
18+
19+
public KafkaController(ILogger<KafkaController> logger, ITopicProducer<KafkaMessage> producer)
20+
{
21+
_logger = logger;
22+
_producer = producer;
23+
}
24+
25+
[HttpGet("produce")]
26+
public async Task<string> Produce()
27+
{
28+
var message = new KafkaMessage { Text = $"kafka-{Guid.NewGuid():N}" };
29+
await _producer.Produce(message);
30+
_logger.LogInformation("Kafka produced: {Text}", message.Text);
31+
return "Complete";
32+
}
33+
34+
[HttpGet("bootstrap_server")]
35+
public string GetBootstrapServer() => Program.GetKafkaBootstrapServer();
36+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2020 New Relic, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
using MassTransit;
7+
using Microsoft.AspNetCore.Mvc;
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace MassTransitTestApp.Controllers;
11+
12+
[ApiController]
13+
[Route("rabbitmq")]
14+
public class RabbitMqController : ControllerBase
15+
{
16+
private readonly ILogger<RabbitMqController> _logger;
17+
private readonly IBus _bus;
18+
19+
public RabbitMqController(ILogger<RabbitMqController> logger, IBus bus)
20+
{
21+
_logger = logger;
22+
_bus = bus;
23+
}
24+
25+
[HttpGet("publish")]
26+
public async Task<string> Publish()
27+
{
28+
var message = new RabbitMqMessage { Text = $"rmq-pub-{Guid.NewGuid():N}" };
29+
await _bus.Publish(message);
30+
_logger.LogInformation("RabbitMQ published: {Text}", message.Text);
31+
return "Complete";
32+
}
33+
34+
[HttpGet("send")]
35+
public async Task<string> Send()
36+
{
37+
var queueName = Program.GetRabbitMqQueueName();
38+
var endpoint = await _bus.GetSendEndpoint(new Uri($"queue:{queueName}"));
39+
var message = new RabbitMqMessage { Text = $"rmq-send-{Guid.NewGuid():N}" };
40+
await endpoint.Send(message);
41+
_logger.LogInformation("RabbitMQ sent: {Text}", message.Text);
42+
return "Complete";
43+
}
44+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
ARG DOTNET_VERSION
2+
ARG BUILD_ARCH
3+
FROM --platform=amd64 mcr.microsoft.com/dotnet/aspnet:${DOTNET_VERSION} AS base
4+
WORKDIR /app
5+
EXPOSE 80
6+
7+
# build arch may be different from target arch (i.e., when running locally with QEMU)
8+
ARG DOTNET_VERSION
9+
ARG BUILD_ARCH
10+
FROM --platform=${BUILD_ARCH} mcr.microsoft.com/dotnet/sdk:${DOTNET_VERSION} AS build
11+
12+
WORKDIR /src
13+
COPY ["MassTransitTestApp/MassTransitTestApp.csproj", "MassTransitTestApp/"]
14+
ARG APP_DOTNET_VERSION
15+
RUN dotnet restore "MassTransitTestApp/MassTransitTestApp.csproj" -p:TargetFramework=net${APP_DOTNET_VERSION} -p:TargetFrameworks=net${APP_DOTNET_VERSION} --runtime linux-x64
16+
17+
COPY . .
18+
WORKDIR "/src/MassTransitTestApp"
19+
RUN dotnet build "MassTransitTestApp.csproj" -c Release -o /app/build --self-contained --framework net${APP_DOTNET_VERSION} -p:TargetFramework=net${APP_DOTNET_VERSION} -p:TargetFrameworks=net${APP_DOTNET_VERSION} --runtime linux-x64 --no-restore
20+
21+
22+
FROM build AS publish
23+
ARG APP_DOTNET_VERSION
24+
RUN dotnet publish "MassTransitTestApp.csproj" -c Release -o /app/publish --self-contained --framework net${APP_DOTNET_VERSION} -p:TargetFramework=net${APP_DOTNET_VERSION} -p:TargetFrameworks=net${APP_DOTNET_VERSION} --runtime linux-x64 --no-restore
25+
26+
FROM base AS final
27+
28+
# Enable the agent
29+
ARG NEW_RELIC_HOST
30+
ARG NEW_RELIC_LICENSE_KEY
31+
ARG NEW_RELIC_APP_NAME
32+
33+
ENV CORECLR_ENABLE_PROFILING=1 \
34+
CORECLR_PROFILER={36032161-FFC0-4B61-B559-F6C5D41BAE5A} \
35+
CORECLR_NEW_RELIC_HOME=/usr/local/newrelic-dotnet-agent \
36+
CORECLR_PROFILER_PATH=/usr/local/newrelic-dotnet-agent/libNewRelicProfiler.so \
37+
NEW_RELIC_HOST=${NEW_RELIC_HOST} \
38+
NEW_RELIC_LICENSE_KEY=${NEW_RELIC_LICENSE_KEY} \
39+
NEW_RELIC_APP_NAME=${NEW_RELIC_APP_NAME} \
40+
NEW_RELIC_LOG_DIRECTORY=/app/logs
41+
42+
WORKDIR /app
43+
COPY --from=publish /app/publish .
44+
45+
ENTRYPOINT ["dotnet", "MassTransitTestApp.dll"]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2020 New Relic, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using MassTransit;
5+
6+
namespace MassTransitTestApp;
7+
8+
/// <summary>
9+
/// Marker interface for the InMemory MultiBus instance.
10+
/// MassTransit MultiBus requires a unique interface per additional bus.
11+
/// </summary>
12+
public interface IInMemoryBus : IBus
13+
{
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<TargetFrameworks>net8.0;net10.0</TargetFrameworks>
4+
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
5+
<DockerfileContext>.</DockerfileContext>
6+
</PropertyGroup>
7+
<!-- net8.0: older MassTransit version -->
8+
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
9+
<PackageReference Include="MassTransit.Kafka" Version="8.1.3" />
10+
<PackageReference Include="MassTransit.RabbitMQ" Version="8.1.3" />
11+
</ItemGroup>
12+
<!-- net10.0: latest free MassTransit version -->
13+
<ItemGroup Condition="'$(TargetFramework)' == 'net10.0'">
14+
<PackageReference Include="MassTransit.Kafka" Version="8.5.8" />
15+
<PackageReference Include="MassTransit.RabbitMQ" Version="8.5.8" />
16+
</ItemGroup>
17+
<ItemGroup>
18+
<PackageReference Include="NewRelic.Agent.Api" Version="10.50.0" />
19+
</ItemGroup>
20+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2020 New Relic, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
namespace MassTransitTestApp;
5+
6+
/// <summary>Message type for Kafka Rider transport.</summary>
7+
public class KafkaMessage
8+
{
9+
public string Text { get; set; }
10+
}
11+
12+
/// <summary>Message type for RabbitMQ bus transport.</summary>
13+
public class RabbitMqMessage
14+
{
15+
public string Text { get; set; }
16+
}
17+
18+
/// <summary>Message type for InMemory bus transport.</summary>
19+
public class InMemoryMessage
20+
{
21+
public string Text { get; set; }
22+
}

0 commit comments

Comments
 (0)