-
Notifications
You must be signed in to change notification settings - Fork 89
Counter support for server v2.12 #960
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
mtmk
merged 5 commits into
main
from
2.12/support-for-JetStream-Distributed-Counter-CRDT
Sep 25, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9466adb
Counter support for server v2.12
mtmk cc8af03
Update src/NATS.Client.JetStream/Models/StreamConfig.cs
mtmk de558ec
Update src/NATS.Client.JetStream/Models/PubAckResponse.cs
mtmk 150ba3c
Update tests/NATS.Client.JetStream.Tests/CounterTest.cs
mtmk e3ee152
Fix error message assertion in CounterTest
mtmk 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
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
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
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,125 @@ | ||
| using NATS.Client.Core2.Tests; | ||
| using NATS.Client.JetStream.Models; | ||
| using NATS.Client.TestUtilities; | ||
|
|
||
| namespace NATS.Client.JetStream.Tests; | ||
|
|
||
| [Collection("nats-server")] | ||
| public class CounterTest | ||
| { | ||
| private readonly ITestOutputHelper _output; | ||
| private readonly NatsServerFixture _server; | ||
|
|
||
| public CounterTest(ITestOutputHelper output, NatsServerFixture server) | ||
| { | ||
| _output = output; | ||
| _server = server; | ||
| } | ||
|
|
||
| [SkipIfNatsServer(versionEarlierThan: "2.12")] | ||
| public async Task Counter_functionality_test() | ||
| { | ||
| await using var nats = new NatsConnection(new NatsOpts { Url = _server.Url }); | ||
| var prefix = _server.GetNextId(); | ||
| var js = new NatsJSContext(nats); | ||
|
|
||
| // Create a stream with counter support enabled | ||
| var streamConfig = new StreamConfig | ||
| { | ||
| Name = $"{prefix}counter-stream", | ||
| Subjects = new[] { $"{prefix}counter.>" }, | ||
| AllowMsgCounter = true, | ||
| }; | ||
|
|
||
| await js.CreateStreamAsync(streamConfig); | ||
|
|
||
| // Publish a message with the counter header | ||
| var headers = new NatsHeaders | ||
| { | ||
| { "Nats-Incr", "+5" }, | ||
| }; | ||
|
|
||
| var ack = await js.PublishAsync($"{prefix}counter.test", data: Array.Empty<byte>(), headers: headers); | ||
|
|
||
| Assert.Null(ack.Error); | ||
| Assert.NotNull(ack.Value); | ||
| Assert.Equal("5", ack.Value); | ||
| _output.WriteLine($"First publish - Value: {ack.Value}"); | ||
|
|
||
| // Publish another message to increment the counter | ||
| headers = new NatsHeaders | ||
| { | ||
| { "Nats-Incr", "+3" }, | ||
| }; | ||
|
|
||
| ack = await js.PublishAsync($"{prefix}counter.test", data: Array.Empty<byte>(), headers: headers); | ||
|
|
||
| Assert.Null(ack.Error); | ||
| Assert.NotNull(ack.Value); | ||
| Assert.Equal("8", ack.Value); | ||
| _output.WriteLine($"Second publish - Value: {ack.Value}"); | ||
|
|
||
| // Test subtract operation | ||
| headers = new NatsHeaders | ||
| { | ||
| { "Nats-Incr", "-2" }, | ||
| }; | ||
|
|
||
| ack = await js.PublishAsync($"{prefix}counter.test", data: Array.Empty<byte>(), headers: headers); | ||
|
|
||
| Assert.Null(ack.Error); | ||
| Assert.NotNull(ack.Value); | ||
| Assert.Equal("6", ack.Value); | ||
| _output.WriteLine($"Third publish (subtract) - Value: {ack.Value}"); | ||
|
|
||
| // Test a different counter (different subject) | ||
| headers = new NatsHeaders | ||
| { | ||
| { "Nats-Incr", "+10" }, | ||
| }; | ||
|
|
||
| ack = await js.PublishAsync($"{prefix}counter.test2", data: Array.Empty<byte>(), headers: headers); | ||
|
|
||
| Assert.Null(ack.Error); | ||
| Assert.NotNull(ack.Value); | ||
| Assert.Equal("10", ack.Value); | ||
| _output.WriteLine($"Different counter - Value: {ack.Value}"); | ||
|
|
||
| // Verify the stream message count | ||
| var stream = await js.GetStreamAsync($"{prefix}counter-stream"); | ||
| Assert.Equal(4u, stream.Info.State.Messages); | ||
| } | ||
|
|
||
| [SkipIfNatsServer(versionEarlierThan: "2.12")] | ||
| public async Task Counter_without_AllowMsgCounter_should_return_error() | ||
| { | ||
| await using var nats = new NatsConnection(new NatsOpts { Url = _server.Url }); | ||
| var prefix = _server.GetNextId(); | ||
| var js = new NatsJSContext(nats); | ||
|
|
||
| // Create a stream without counter support | ||
| var streamConfig = new StreamConfig | ||
| { | ||
| Name = $"{prefix}no-counter-stream", | ||
| Subjects = new[] { $"{prefix}nocounter.>" }, | ||
| AllowMsgCounter = false, // Explicitly disable counter | ||
| }; | ||
|
|
||
| await js.CreateStreamAsync(streamConfig); | ||
|
|
||
| // Publish a message with counter headers (should return an error) | ||
| var headers = new NatsHeaders | ||
| { | ||
| { "Nats-Incr", "+5" }, | ||
| }; | ||
|
|
||
| var ack = await js.PublishAsync($"{prefix}nocounter.test", data: Array.Empty<byte>(), headers: headers); | ||
|
|
||
| // When counter is disabled, the server returns an error | ||
| Assert.NotNull(ack.Error); | ||
| Assert.Equal(10168, ack.Error.ErrCode); // Error code for "message counters is disabled" | ||
| Assert.Contains("message counters is disabled", ack.Error.Description); | ||
| Assert.Null(ack.Value); // Value should be null when counter is not enabled | ||
| _output.WriteLine($"Error as expected: {ack.Error.Description}"); | ||
| } | ||
| } |
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.
this should be nullable