Skip to content

Commit beb1258

Browse files
authored
Merge pull request #47 from neuroglia-io/fix-samples
Fix the StreetLightsApi sample project by rolling back to a previous version of MQTTNet
2 parents df6377b + b9159b6 commit beb1258

File tree

8 files changed

+77
-9
lines changed

8 files changed

+77
-9
lines changed

.github/workflows/build-and-test.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ jobs:
1616
- name: Setup .NET
1717
uses: actions/setup-dotnet@v3
1818
with:
19-
dotnet-version: 9.0.x
19+
dotnet-version: |
20+
8.0.x
21+
9.0.x
2022
- name: Restore dependencies
2123
run: dotnet restore
2224
- name: Build

.github/workflows/publish.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ jobs:
1414
- name: Setup .NET
1515
uses: actions/setup-dotnet@v3
1616
with:
17-
dotnet-version: 9.0.x
17+
dotnet-version: |
18+
8.0.x
19+
9.0.x
1820
- name: Restore dependencies
1921
run: dotnet restore
2022
- name: Build

samples/StreetLightsApi/StreetLightsApi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<ItemGroup>
1212
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="8.0.14" Condition="'$(TargetFramework)' == 'net8.0'" />
1313
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="9.0.3" Condition="'$(TargetFramework)' == 'net9.0'" />
14-
<PackageReference Include="MQTTnet" Version="5.0.1.1416" />
14+
<PackageReference Include="MQTTnet" Version="4.3.7.1207" />
1515
</ItemGroup>
1616

1717
<ItemGroup>

src/Neuroglia.AsyncApi.Client.Bindings.Mqtt/MqttBindingHandler.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ public virtual async Task<IAsyncApiPublishOperationResult> PublishAsync(AsyncApi
5757
var serverBinding = context.ChannelBinding as MqttServerBindingDefinition;
5858
var operationBinding = context.OperationBinding as MqttOperationBindingDefinition;
5959
var messageBinding = context.MessageBinding as MqttMessageBindingDefinition;
60-
var clientOptionsBuilder = new MqttClientOptionsBuilder().WithTcpServer(context.Host);
60+
var hostComponents = context.Host.Split(':');
61+
var host = hostComponents[0];
62+
var port = hostComponents.Length == 2 ? int.Parse(hostComponents[1]) : (int?)null;
63+
var clientOptionsBuilder = new MqttClientOptionsBuilder().WithTcpServer(host, port);
6164
if (!string.IsNullOrWhiteSpace(serverBinding?.ClientId)) clientOptionsBuilder.WithClientId(serverBinding.ClientId);
6265
if (serverBinding?.CleanSession == true) clientOptionsBuilder.WithCleanSession();
6366
if (serverBinding?.KeepAlive.HasValue == true) clientOptionsBuilder.WithKeepAlivePeriod(TimeSpan.FromSeconds(serverBinding.KeepAlive.Value));
@@ -99,7 +102,10 @@ public virtual async Task<IAsyncApiSubscribeOperationResult> SubscribeAsync(Asyn
99102
var mqttClientFactory = new MqttClientFactory();
100103
var mqttClient = mqttClientFactory.CreateMqttClient();
101104
var subscription = ActivatorUtilities.CreateInstance<MqttSubscription>(ServiceProvider, mqttClient, context.Document, context.Messages);
102-
var clientOptionsBuilder = new MqttClientOptionsBuilder().WithTcpServer(context.Host);
105+
var hostComponents = context.Host.Split(':');
106+
var host = hostComponents[0];
107+
var port = hostComponents.Length == 2 ? int.Parse(hostComponents[1]) : (int?)null;
108+
var clientOptionsBuilder = new MqttClientOptionsBuilder().WithTcpServer(host, port);
103109
if (!string.IsNullOrWhiteSpace(serverBinding?.ClientId)) clientOptionsBuilder.WithClientId(serverBinding.ClientId);
104110
if (serverBinding?.CleanSession == true) clientOptionsBuilder.WithCleanSession();
105111
if (serverBinding?.KeepAlive.HasValue == true) clientOptionsBuilder.WithKeepAlivePeriod(TimeSpan.FromSeconds(serverBinding.KeepAlive.Value));
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
allow_anonymous true
2+
listener 1883 0.0.0.0

tests/Neuroglia.AsyncApi.UnitTests/Cases/Client/Bindings/MqttBindingHandlerTests.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Neuroglia.AsyncApi.Bindings.Mqtt;
1616
using Neuroglia.AsyncApi.Client;
1717
using Neuroglia.AsyncApi.Client.Bindings;
18+
using Neuroglia.AsyncApi.UnitTests.Containers;
1819
using System.Text;
1920

2021
namespace Neuroglia.AsyncApi.UnitTests.Cases.Client.Bindings;
@@ -23,11 +24,13 @@ public class MqttBindingHandlerTests
2324
: BindingHandlerTestsBase
2425
{
2526
public MqttBindingHandlerTests()
26-
: base(builder => builder.AddMqttBindingHandler())
27+
: base(builder => builder.AddMqttBindingHandler(), ConfigureServices)
2728
{
2829

2930
}
3031

32+
string MqttServerAddress => $"localhost:{ServiceProvider.GetRequiredKeyedService<DotNet.Testcontainers.Containers.IContainer>("mqtt").GetMappedPublicPort(MqttContainerBuilder.PublicPort)}";
33+
3134
[Fact]
3235
public async Task Publish_Should_Work()
3336
{
@@ -43,7 +46,7 @@ public async Task Publish_Should_Work()
4346
.WithTitle("Test MQTT API")
4447
.WithVersion("1.0.0")
4548
.WithServer(serverId, server => server
46-
.WithHost("test.mosquitto.org")
49+
.WithHost(MqttServerAddress)
4750
.WithProtocol(AsyncApiProtocol.Mqtt)
4851
.WithBinding(new MqttServerBindingDefinition()))
4952
.WithChannel(channelId, channel => channel
@@ -106,7 +109,10 @@ public async Task Subscribe_Should_Work()
106109
{
107110
//arrange
108111
var serverId = "mqtt-server";
109-
var serverAddress = "test.mosquitto.org";
112+
var serverAddress = MqttServerAddress;
113+
var serverAddressComponents = serverAddress.Split(':');
114+
var serverHost = serverAddressComponents[0];
115+
var serverPort = int.Parse(serverAddressComponents[1]);
110116
var channelId = "cloud-events";
111117
var channelAddress = "cloud-event";
112118
var operationId = "subscribeToCloudEvents";
@@ -194,7 +200,7 @@ public async Task Subscribe_Should_Work()
194200
var subscription = result.Messages?.Subscribe(messagesReceived.Add);
195201
var mqttClientFactory = new MqttClientFactory();
196202
using var mqttClient = mqttClientFactory.CreateMqttClient();
197-
var options = new MqttClientOptionsBuilder().WithTcpServer(serverAddress).Build();
203+
var options = new MqttClientOptionsBuilder().WithTcpServer(serverHost, serverPort).Build();
198204
await mqttClient.ConnectAsync(options);
199205
foreach (var message in messagesToSend) await mqttClient.PublishAsync(message);
200206
await Task.Delay(3500);
@@ -206,4 +212,11 @@ public async Task Subscribe_Should_Work()
206212
messagesReceived.Should().NotBeEmpty();
207213
}
208214

215+
static void ConfigureServices(IServiceCollection services)
216+
{
217+
services.AddKeyedSingleton("mqtt", MqttContainerBuilder.Build());
218+
services.AddSingleton(provider => provider.GetRequiredKeyedService<DotNet.Testcontainers.Containers.IContainer>("mqtt"));
219+
services.AddHostedService<ContainerBootstrapper>();
220+
}
221+
209222
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright © 2021-Present Neuroglia SRL. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"),
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
using DotNet.Testcontainers.Builders;
15+
using DotNet.Testcontainers.Containers;
16+
17+
namespace Neuroglia.AsyncApi.UnitTests.Containers;
18+
19+
public static class MqttContainerBuilder
20+
{
21+
22+
public const int PublicPort = 1883;
23+
24+
public static IContainer Build()
25+
{
26+
return new ContainerBuilder()
27+
.WithName($"mqtt-{Guid.NewGuid():N}")
28+
.WithImage("eclipse-mosquitto")
29+
.WithBindMount(Path.Combine(AppContext.BaseDirectory, "Assets", "mqtt", "mosquitto.conf"), "/mosquitto/config/mosquitto.conf")
30+
.WithPortBinding(PublicPort, true)
31+
.WithWaitStrategy(Wait
32+
.ForUnixContainer()
33+
.UntilMessageIsLogged("mosquitto version .* running"))
34+
.Build();
35+
}
36+
37+
}

tests/Neuroglia.AsyncApi.UnitTests/Neuroglia.AsyncApi.UnitTests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,10 @@
3333
<ProjectReference Include="..\..\src\Neuroglia.AsyncApi.IO\Neuroglia.AsyncApi.IO.csproj" />
3434
</ItemGroup>
3535

36+
<ItemGroup>
37+
<None Update="Assets\mqtt\mosquitto.conf">
38+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
39+
</None>
40+
</ItemGroup>
41+
3642
</Project>

0 commit comments

Comments
 (0)