-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add wait-for-subscribers feature #7652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Aaronontheweb
merged 26 commits into
akkadotnet:dev
from
Arkatufus:Add-wait-for-subscribers-feature
May 19, 2025
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
acae403
Add wait-for-subscribers feature
Arkatufus e72cbd4
Merge remote-tracking branch 'upstream/dev' into Add-wait-for-subscri…
Arkatufus 8f05840
Fix missing code
Arkatufus 0177811
Update API approval list
Arkatufus d30b8a2
Fix HOCON errors
Arkatufus 001e056
Merge branch 'dev' into Add-wait-for-subscribers-feature
Arkatufus 98a6999
Merge branch 'dev' into Add-wait-for-subscribers-feature
Arkatufus 8083046
Merge branch 'dev' into Add-wait-for-subscribers-feature
Arkatufus c0af9fe
Trim features to fit
Arkatufus 89ecf1d
Merge branch 'Add-wait-for-subscribers-feature' of github.com:Arkatuf…
Arkatufus b4b1c5e
Simplify logic
Arkatufus 00761f5
Code cleanup
Arkatufus 6cf2e00
Code cleanup
Arkatufus 4c0f026
Update API Approval list
Arkatufus 6792915
Fix code
Arkatufus cba4f30
Fix codes
Arkatufus 13d05ca
Fix codes
Arkatufus 6f26d41
Update code
Arkatufus ef05c31
Update API Approval list
Arkatufus 1dbdd89
Fix PublishWithAck
Arkatufus 8a36742
Fix cleanup code
Arkatufus 71d4dd2
refactor DistributedPubSubSettings to record
Arkatufus ec979e2
Fix specs
Arkatufus 8635d6c
Add unit tests
Arkatufus 2021f83
Fix unit tests
Arkatufus 4d2130b
Merge branch 'dev' into Add-wait-for-subscribers-feature
Arkatufus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
...kka.Cluster.Tools.Tests.MultiNode/PublishSubscribe/DistributedPubSubPublishWithAckSpec.cs
This file contains hidden or 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,147 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="DistributedPubSubPublishWithAckSpec.cs" company="Akka.NET Project"> | ||
| // Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com> | ||
| // Copyright (C) 2013-2025 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| using System; | ||
| using Akka.Actor; | ||
| using Akka.Cluster.TestKit; | ||
| using Akka.Cluster.Tools.PublishSubscribe; | ||
| using Akka.Configuration; | ||
| using Akka.Event; | ||
| using Akka.MultiNode.TestAdapter; | ||
| using Akka.Remote.TestKit; | ||
| using FluentAssertions; | ||
| using FluentAssertions.Extensions; | ||
|
|
||
| namespace Akka.Cluster.Tools.Tests.MultiNode.PublishSubscribe; | ||
|
|
||
| public class DistributedPubSubPublishWithAckSpecSpecConfig : MultiNodeConfig | ||
| { | ||
| public readonly RoleName First; | ||
| public readonly RoleName Second; | ||
|
|
||
| public DistributedPubSubPublishWithAckSpecSpecConfig() | ||
| { | ||
| First = Role("first"); | ||
| Second = Role("second"); | ||
|
|
||
| CommonConfig = ConfigurationFactory.ParseString( | ||
| """ | ||
| akka.loglevel = INFO | ||
| akka.actor.provider = "Akka.Cluster.ClusterActorRefProvider, Akka.Cluster" | ||
| akka.actor.serialize-messages = off | ||
| akka.remote.log-remote-lifecycle-events = off | ||
| akka.cluster.auto-down-unreachable-after = 0s | ||
| akka.cluster.pub-sub.max-delta-elements = 500 | ||
| akka.cluster.pub-sub.buffered-messages.max-per-topic = 2 | ||
| akka.cluster.pub-sub.buffered-messages.timeout-check-interval = 200ms | ||
| akka.testconductor.query-timeout = 1m # we were having timeouts shutting down nodes with 5s default | ||
| """).WithFallback(DistributedPubSub.DefaultConfig()); | ||
| } | ||
| } | ||
|
|
||
| public class DistributedPubSubPublishWithAckSpec : MultiNodeClusterSpec | ||
| { | ||
| #region setup | ||
| private readonly RoleName _first; | ||
| private readonly RoleName _second; | ||
|
|
||
| public DistributedPubSubPublishWithAckSpec() : this(new DistributedPubSubPublishWithAckSpecSpecConfig()) | ||
| { | ||
| } | ||
|
|
||
| protected DistributedPubSubPublishWithAckSpec(DistributedPubSubPublishWithAckSpecSpecConfig config) : base(config, typeof(DistributedPubSubMediatorSpec)) | ||
| { | ||
| _first = config.First; | ||
| _second = config.Second; | ||
| } | ||
|
|
||
| public IActorRef Mediator => DistributedPubSub.Get(Sys).Mediator; | ||
|
|
||
| private void Join(RoleName from, RoleName to) | ||
| { | ||
| RunOn(() => | ||
| { | ||
| Cluster.Join(Node(to).Address); | ||
| _ = DistributedPubSub.Get(Sys).Mediator; | ||
| }, from); | ||
| EnterBarrier(from.Name + "-joined"); | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| [MultiNodeFact] | ||
| public void DistributedPubSubPublishWithAckSpecs() | ||
| { | ||
| DistributedPubSubMediator_must_startup_2_nodes_cluster(); | ||
| PublishWithAck_must_buffer_message(); | ||
| Second_node_must_subscribe(); | ||
| PublishWithAck_must_deliver_buffered_messages(); | ||
| } | ||
|
|
||
| public void DistributedPubSubMediator_must_startup_2_nodes_cluster() | ||
| { | ||
| Within(TimeSpan.FromSeconds(15), () => | ||
| { | ||
| Join(_first, _first); | ||
| Join(_second, _first); | ||
| EnterBarrier("after-1"); | ||
| }); | ||
| } | ||
|
|
||
| public void PublishWithAck_must_buffer_message() | ||
| { | ||
| Within(15.Seconds(), () => | ||
| { | ||
| RunOn(() => | ||
| { | ||
| Mediator.Tell(new PublishWithAck("content", "hi-1!", 20.Seconds())); | ||
| Mediator.Tell(new PublishWithAck("content", "hi-2!", 20.Seconds())); | ||
| ExpectNoMsg(200.Milliseconds()); | ||
| }, _first); | ||
|
|
||
| RunOn(() => | ||
| { | ||
| ExpectNoMsg(200.Milliseconds()); | ||
| }, _second); | ||
|
|
||
| EnterBarrier("after-2"); | ||
| }); | ||
| } | ||
|
|
||
| public void Second_node_must_subscribe() | ||
| { | ||
| RunOn(() => | ||
| { | ||
| Mediator.Tell(new Subscribe("content", TestActor)); | ||
| // SubscribeAck must arrive first | ||
| ExpectMsg<SubscribeAck>(); | ||
| Sys.Log.Info("Second node subscribed"); | ||
| }, _second); | ||
|
|
||
| EnterBarrier("after-3"); | ||
| } | ||
|
|
||
| public void PublishWithAck_must_deliver_buffered_messages() | ||
| { | ||
| Within(TimeSpan.FromSeconds(15), () => | ||
| { | ||
| RunOn(() => | ||
| { | ||
| ExpectMsg<PublishSucceeded>().Message.Message.Should().Be("hi-1!"); | ||
| ExpectMsg<PublishSucceeded>().Message.Message.Should().Be("hi-2!"); | ||
| }, _first); | ||
|
|
||
| RunOn(() => | ||
| { | ||
| ExpectMsg("hi-1!"); | ||
| ExpectMsg("hi-2!"); | ||
| }, _second); | ||
|
|
||
| EnterBarrier("after-4"); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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
128 changes: 128 additions & 0 deletions
128
.../cluster/Akka.Cluster.Tools.Tests/PublishSubscribe/DistributedPubSubPublishWithAckSpec.cs
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM |
This file contains hidden or 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,128 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="DistributedPubSubPublishWithAckSpec.cs" company="Akka.NET Project"> | ||
| // Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com> | ||
| // Copyright (C) 2013-2025 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using Akka.Actor; | ||
| using Akka.Cluster.Tools.PublishSubscribe; | ||
| using Akka.Configuration; | ||
| using Akka.TestKit; | ||
| using FluentAssertions.Extensions; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace Akka.Cluster.Tools.Tests.PublishSubscribe; | ||
|
|
||
| [Collection(nameof(DistributedPubSubMediatorSpec))] | ||
| public class DistributedPubSubPublishWithAckSpec : AkkaSpec | ||
| { | ||
| public DistributedPubSubPublishWithAckSpec(ITestOutputHelper output) : base(GetConfig(), output) | ||
| { | ||
| } | ||
|
|
||
| private static Config GetConfig() | ||
| { | ||
| return ConfigurationFactory.ParseString( | ||
| """ | ||
| akka.actor.provider = cluster | ||
| akka.cluster.pub-sub.buffered-messages.max-per-topic = 2 | ||
| akka.cluster.pub-sub.buffered-messages.timeout-check-interval = 100ms | ||
| """); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "PublishWithAck message should be buffered")] | ||
| public async Task PublishWithAckBufferTest() | ||
| { | ||
| var mediator = DistributedPubSub.Get(Sys).Mediator; | ||
| mediator.Tell(new PublishWithAck("topic", "msg-1", 10.Seconds())); | ||
| mediator.Tell(new PublishWithAck("topic", "msg-2", 10.Seconds())); | ||
|
|
||
| // Should not send NACKs | ||
| await ExpectNoMsgAsync(200.Milliseconds()); | ||
|
|
||
| // Should not re-send messages if topic does not match | ||
| mediator.Tell(new Subscribe("topic2", TestActor)); | ||
| var subAck = await ExpectMsgAsync<SubscribeAck>(); | ||
| subAck.Subscribe.Topic.ShouldBe("topic2"); | ||
| await ExpectNoMsgAsync(200.Milliseconds()); | ||
|
|
||
| // Should not re-send messages if topic match | ||
| mediator.Tell(new Subscribe("topic", TestActor)); | ||
| subAck = await ExpectMsgAsync<SubscribeAck>(); | ||
| subAck.Subscribe.Topic.ShouldBe("topic"); | ||
|
|
||
| await ExpectMsgAllOfMatchingPredicatesAsync([ | ||
| PredicateInfo.Create<string>(msg => msg is "msg-1"), | ||
| PredicateInfo.Create<PublishSucceeded>(msg => msg.Message is { Message: "msg-1", Topic: "topic" }), | ||
| PredicateInfo.Create<string>(msg => msg is "msg-2"), | ||
| PredicateInfo.Create<PublishSucceeded>(msg => msg.Message is { Message: "msg-2", Topic: "topic" }) ]).ToListAsync(); | ||
|
|
||
| // Should not send extra messages | ||
| await ExpectNoMsgAsync(200.Milliseconds()); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "PublishWithAck message buffer should NACK buffer overflowed messages")] | ||
| public async Task PublishWithAckBufferOverflowTest() | ||
| { | ||
| var mediator = DistributedPubSub.Get(Sys).Mediator; | ||
| mediator.Tell(new PublishWithAck("topic", "msg-1", 10.Seconds())); | ||
| mediator.Tell(new PublishWithAck("topic", "msg-2", 10.Seconds())); | ||
|
|
||
| // Should dequeue and NACK msg-1, the oldest message in the buffer | ||
| mediator.Tell(new PublishWithAck("topic", "msg-3", 10.Seconds())); | ||
| var failed = await ExpectMsgAsync<PublishFailed>(); | ||
| failed.Message.Topic.ShouldBe("topic"); | ||
| failed.Message.Message.ShouldBe("msg-1"); | ||
|
|
||
| // Should dequeue and NACK msg-2, the oldest message in the buffer | ||
| mediator.Tell(new PublishWithAck("topic", "msg-4", 10.Seconds())); | ||
| failed = await ExpectMsgAsync<PublishFailed>(); | ||
| failed.Message.Topic.ShouldBe("topic"); | ||
| failed.Message.Message.ShouldBe("msg-2"); | ||
|
|
||
| // Should not re-send messages if topic match. | ||
| // msg-1 and msg-2 should never be re-sent | ||
| mediator.Tell(new Subscribe("topic", TestActor)); | ||
| var subAck = await ExpectMsgAsync<SubscribeAck>(); | ||
| subAck.Subscribe.Topic.ShouldBe("topic"); | ||
|
|
||
| await ExpectMsgAllOfMatchingPredicatesAsync([ | ||
| PredicateInfo.Create<string>(msg => msg is "msg-3"), | ||
| PredicateInfo.Create<PublishSucceeded>(msg => msg.Message is { Message: "msg-3", Topic: "topic" }), | ||
| PredicateInfo.Create<string>(msg => msg is "msg-4"), | ||
| PredicateInfo.Create<PublishSucceeded>(msg => msg.Message is { Message: "msg-4", Topic: "topic" }) ]).ToListAsync(); | ||
|
|
||
| // Should not send extra messages | ||
| await ExpectNoMsgAsync(200.Milliseconds()); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "PublishWithAck message should fail when timed-out")] | ||
| public async Task PublishWithAckMessageTimeoutTest() | ||
| { | ||
| var mediator = DistributedPubSub.Get(Sys).Mediator; | ||
| mediator.Tell(new PublishWithAck("topic", "msg-1", 300.Milliseconds())); | ||
| mediator.Tell(new PublishWithAck("topic", "msg-2", 10.Milliseconds())); | ||
|
|
||
| // msg-2 should time out first | ||
| var failed = await ExpectMsgAsync<PublishFailed>(); | ||
| failed.Message.Topic.ShouldBe("topic"); | ||
| failed.Message.Message.ShouldBe("msg-2"); | ||
|
|
||
| // msg-1 should time out second | ||
| failed = await ExpectMsgAsync<PublishFailed>(); | ||
| failed.Message.Topic.ShouldBe("topic"); | ||
| failed.Message.Message.ShouldBe("msg-1"); | ||
|
|
||
| // Buffer is empty, should not re-send anything | ||
| mediator.Tell(new Subscribe("topic", TestActor)); | ||
| var subAck = await ExpectMsgAsync<SubscribeAck>(); | ||
| subAck.Subscribe.Topic.ShouldBe("topic"); | ||
|
|
||
| // Should not send extra messages | ||
| await ExpectNoMsgAsync(200.Milliseconds()); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM