-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support shared sessions in mocked MQTT broker
- Loading branch information
1 parent
f8f816b
commit b6894f6
Showing
6 changed files
with
271 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...erback.Integration.MQTT.Testing/Messaging/Broker/Mqtt/Mocks/SharedSubscriptionsManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) 2020 Sergio Aquilini | ||
// This code is licensed under MIT license (see LICENSE file for details) | ||
|
||
using System.Collections.Generic; | ||
using MQTTnet; | ||
|
||
namespace Silverback.Messaging.Broker.Mqtt.Mocks | ||
{ | ||
internal class SharedSubscriptionsManager | ||
{ | ||
private readonly Dictionary<string, int> _groups = new(); | ||
|
||
private readonly Dictionary<string, Dictionary<object, int>> _counters = new(); | ||
|
||
public void Add(string group) | ||
{ | ||
lock (_groups) | ||
{ | ||
if (_groups.ContainsKey(group)) | ||
{ | ||
_groups[group]++; | ||
} | ||
else | ||
{ | ||
_groups[group] = 0; | ||
_counters[group] = new Dictionary<object, int>(); | ||
} | ||
} | ||
} | ||
|
||
public bool IsFirstMatch(string group, MqttApplicationMessage message) | ||
{ | ||
lock (_counters) | ||
{ | ||
Dictionary<object, int> counter = _counters[group]; | ||
|
||
if (counter.ContainsKey(message)) | ||
{ | ||
counter[message]++; | ||
if (counter[message] >= _groups[group]) | ||
counter.Remove(message); | ||
|
||
return false; | ||
} | ||
|
||
counter[message] = 1; | ||
return true; | ||
} | ||
} | ||
} | ||
} |
154 changes: 154 additions & 0 deletions
154
tests/Silverback.Integration.Tests.E2E/Mqtt/SubscriptionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// Copyright (c) 2020 Sergio Aquilini | ||
// This code is licensed under MIT license (see LICENSE file for details) | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using MQTTnet.Formatter; | ||
using Silverback.Messaging; | ||
using Silverback.Messaging.Messages; | ||
using Silverback.Messaging.Publishing; | ||
using Silverback.Tests.Integration.E2E.TestHost; | ||
using Silverback.Tests.Integration.E2E.TestTypes.Messages; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Silverback.Tests.Integration.E2E.Mqtt | ||
{ | ||
public class SubscriptionTests : MqttTestFixture | ||
{ | ||
public SubscriptionTests(ITestOutputHelper testOutputHelper) | ||
: base(testOutputHelper) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public async Task Subscription_SingleLevelWildcard_MessagesConsumed() | ||
{ | ||
Host.ConfigureServices( | ||
services => services | ||
.AddLogging() | ||
.AddSilverback() | ||
.UseModel() | ||
.WithConnectionToMessageBroker(options => options.AddMockedMqtt()) | ||
.AddMqttEndpoints( | ||
endpoints => endpoints | ||
.Configure( | ||
config => config | ||
.WithClientId("e2e-test") | ||
.ConnectViaTcp("e2e-mqtt-broker")) | ||
.AddOutbound<TestEventOne>(endpoint => endpoint.ProduceTo("world/news")) | ||
.AddOutbound<TestEventTwo>( | ||
endpoint => endpoint.ProduceTo("world/europe/news")) | ||
.AddOutbound<TestEventThree>( | ||
endpoint => endpoint.ProduceTo("world/europe/switzerland/news")) | ||
.AddInbound( | ||
endpoint => endpoint | ||
.ConsumeFrom("world/+/news"))) | ||
.AddIntegrationSpyAndSubscriber()) | ||
.Run(); | ||
|
||
var publisher = Host.ScopedServiceProvider.GetRequiredService<IEventPublisher>(); | ||
|
||
await publisher.PublishAsync(new TestEventOne()); | ||
await publisher.PublishAsync(new TestEventTwo()); | ||
await publisher.PublishAsync(new TestEventThree()); | ||
|
||
await Helper.WaitUntilAllMessagesAreConsumedAsync(); | ||
|
||
Helper.Spy.OutboundEnvelopes.Should().HaveCount(3); | ||
Helper.Spy.InboundEnvelopes.Should().HaveCount(1); | ||
Helper.Spy.InboundEnvelopes[0].Message.Should().BeOfType<TestEventTwo>(); | ||
} | ||
|
||
[Fact] | ||
public async Task Subscription_MultiLevelWildcard_MessagesConsumed() | ||
{ | ||
Host.ConfigureServices( | ||
services => services | ||
.AddLogging() | ||
.AddSilverback() | ||
.UseModel() | ||
.WithConnectionToMessageBroker(options => options.AddMockedMqtt()) | ||
.AddMqttEndpoints( | ||
endpoints => endpoints | ||
.Configure( | ||
config => config | ||
.WithClientId("e2e-test") | ||
.ConnectViaTcp("e2e-mqtt-broker")) | ||
.AddOutbound<TestEventOne>(endpoint => endpoint.ProduceTo("world/news")) | ||
.AddOutbound<TestEventTwo>( | ||
endpoint => endpoint.ProduceTo("world/europe/news")) | ||
.AddOutbound<TestEventThree>( | ||
endpoint => endpoint.ProduceTo("world/europe/switzerland/news")) | ||
.AddInbound( | ||
endpoint => endpoint | ||
.ConsumeFrom("world/#/news"))) | ||
.AddIntegrationSpyAndSubscriber()) | ||
.Run(); | ||
|
||
var publisher = Host.ScopedServiceProvider.GetRequiredService<IEventPublisher>(); | ||
|
||
await publisher.PublishAsync(new TestEventOne()); | ||
await publisher.PublishAsync(new TestEventTwo()); | ||
await publisher.PublishAsync(new TestEventThree()); | ||
|
||
await Helper.WaitUntilAllMessagesAreConsumedAsync(); | ||
|
||
Helper.Spy.OutboundEnvelopes.Should().HaveCount(3); | ||
Helper.Spy.InboundEnvelopes.Should().HaveCount(2); | ||
Helper.Spy.InboundEnvelopes[0].Message.Should().BeOfType<TestEventTwo>(); | ||
Helper.Spy.InboundEnvelopes[1].Message.Should().BeOfType<TestEventThree>(); | ||
} | ||
|
||
[Fact] | ||
public async Task Subscription_Shared_MessagesConsumedOnce() | ||
{ | ||
Host.ConfigureServices( | ||
services => services | ||
.AddLogging() | ||
.AddSilverback() | ||
.UseModel() | ||
.WithConnectionToMessageBroker(options => options.AddMockedMqtt()) | ||
.AddMqttEndpoints( | ||
endpoints => endpoints | ||
.Configure( | ||
config => config | ||
.WithClientId("e2e-test") | ||
.ConnectViaTcp("e2e-mqtt-broker")) | ||
.AddOutbound<IIntegrationEvent>( | ||
endpoint => endpoint.ProduceTo(DefaultTopicName)) | ||
.AddInbound( | ||
endpoint => endpoint | ||
.Configure(config => config.WithClientId("consumer-1")) | ||
.ConsumeFrom("$share/group/" + DefaultTopicName)) | ||
.AddInbound( | ||
endpoint => endpoint | ||
.Configure(config => config.WithClientId("consumer-2")) | ||
.ConsumeFrom("$share/group/" + DefaultTopicName))) | ||
.AddIntegrationSpyAndSubscriber()) | ||
.Run(); | ||
|
||
var publisher = Host.ScopedServiceProvider.GetRequiredService<IEventPublisher>(); | ||
|
||
for (int i = 1; i <= 15; i++) | ||
{ | ||
await publisher.PublishAsync( | ||
new TestEventOne | ||
{ | ||
Content = $"{i}" | ||
}); | ||
} | ||
|
||
await Helper.WaitUntilAllMessagesAreConsumedAsync(); | ||
|
||
Helper.Spy.OutboundEnvelopes.Should().HaveCount(15); | ||
Helper.Spy.InboundEnvelopes.Should().HaveCount(15); | ||
Helper.Spy.InboundEnvelopes | ||
.Select(envelope => ((TestEventOne)envelope.Message!).Content) | ||
.Should().BeEquivalentTo(Enumerable.Range(1, 15).Select(i => $"{i}")); | ||
} | ||
} | ||
} |