From b717bf4617e164824213ceafee0f48187dab0157 Mon Sep 17 00:00:00 2001 From: Ziya Suzen Date: Thu, 25 Sep 2025 11:51:09 +0100 Subject: [PATCH] Add support for message schedules --- .../Models/StreamConfig.cs | 7 ++++ .../ManageStreamTest.cs | 39 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/NATS.Client.JetStream/Models/StreamConfig.cs b/src/NATS.Client.JetStream/Models/StreamConfig.cs index e1b2a16c8..5ef7dc05d 100644 --- a/src/NATS.Client.JetStream/Models/StreamConfig.cs +++ b/src/NATS.Client.JetStream/Models/StreamConfig.cs @@ -270,4 +270,11 @@ public StreamConfig() [System.Text.Json.Serialization.JsonPropertyName("allow_msg_counter")] [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)] public bool AllowMsgCounter { get; set; } + + /// + /// AllowMsgSchedules enables the scheduling of messages. + /// + [System.Text.Json.Serialization.JsonPropertyName("allow_msg_schedules")] + [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)] + public bool AllowMsgSchedules { get; set; } } diff --git a/tests/NATS.Client.JetStream.Tests/ManageStreamTest.cs b/tests/NATS.Client.JetStream.Tests/ManageStreamTest.cs index 2f938df3a..41fb1d542 100644 --- a/tests/NATS.Client.JetStream.Tests/ManageStreamTest.cs +++ b/tests/NATS.Client.JetStream.Tests/ManageStreamTest.cs @@ -2,6 +2,7 @@ using NATS.Client.Core2.Tests; using NATS.Client.JetStream.Models; using NATS.Client.Platform.Windows.Tests; +using NATS.Client.TestUtilities; using NATS.Client.TestUtilities2; namespace NATS.Client.JetStream.Tests; @@ -219,4 +220,42 @@ public async Task Create_or_update_stream_should_be_throwing_update_operation_er await js.CreateOrUpdateStreamAsync(streamConfig, cts.Token); await Assert.ThrowsAsync(async () => await js.CreateOrUpdateStreamAsync(streamConfigForUpdated, cts.Token)); } + + [SkipIfNatsServer(versionEarlierThan: "2.12")] + public async Task AllowMsgSchedules_property_should_be_set_on_stream() + { + await using var nats = new NatsConnection(new NatsOpts { Url = _server.Url }); + var prefix = _server.GetNextId(); + await nats.ConnectRetryAsync(); + + var js = new NatsJSContext(nats); + + var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); + + // Create a stream with AllowMsgSchedules enabled + var streamConfig = new StreamConfig($"{prefix}schedules", [$"{prefix}schedules.*"]) + { + AllowMsgSchedules = true, + }; + + var stream = await js.CreateStreamAsync(streamConfig, cts.Token); + + // Verify the property is set on the created stream + Assert.True(stream.Info.Config.AllowMsgSchedules); + + // Get the stream and verify the property is persisted + var retrievedStream = await js.GetStreamAsync($"{prefix}schedules", cancellationToken: cts.Token); + Assert.True(retrievedStream.Info.Config.AllowMsgSchedules); + + // Update stream with AllowMsgSchedules disabled should error + var updatedConfig = streamConfig with { AllowMsgSchedules = false }; + var exception = await Assert.ThrowsAsync(async () => await js.UpdateStreamAsync(updatedConfig, cts.Token)); + Assert.Equal(500, exception.Error.Code); + Assert.Equal(10052, exception.Error.ErrCode); + Assert.Equal("message schedules can not be disabled", exception.Error.Description); + + // Get the stream and verify the update has failed + var reRetrievedStream = await js.GetStreamAsync($"{prefix}schedules", cancellationToken: cts.Token); + Assert.True(reRetrievedStream.Info.Config.AllowMsgSchedules); + } }