Skip to content
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

Add CreateOrUpdateStoreAsync #707

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/NATS.Client.KeyValueStore/INatsKVContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ public interface INatsKVContext
/// <exception cref="NatsJSApiException">Server responded with an error.</exception>
ValueTask<INatsKVStore> UpdateStoreAsync(NatsKVConfig config, CancellationToken cancellationToken = default);

/// <summary>
/// Creates a new Key Value Store if it doesn't exist or update if the store already exists.
/// </summary>
/// <param name="config">Key Value Store configuration</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> used to cancel the API call.</param>
/// <returns>Key Value Store</returns>
/// <exception cref="NatsKVException">There was an issue with configuration</exception>
/// <exception cref="NatsJSException">There was an issue retrieving the response.</exception>
/// <exception cref="NatsJSApiException">Server responded with an error.</exception>
ValueTask<INatsKVStore> CreateOrUpdateStoreAsync(NatsKVConfig config, CancellationToken cancellationToken = default);

/// <summary>
/// Delete a Key Value Store
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions src/NATS.Client.KeyValueStore/NatsKVContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ public async ValueTask<INatsKVStore> UpdateStoreAsync(NatsKVConfig config, Cance
return new NatsKVStore(config.Bucket, JetStreamContext, stream);
}

/// <inheritdoc />
public async ValueTask<INatsKVStore> CreateOrUpdateStoreAsync(NatsKVConfig config, CancellationToken cancellationToken = default)
{
ValidateBucketName(config.Bucket);

var streamConfig = NatsKVContext.CreateStreamConfig(config);

var stream = await JetStreamContext.CreateOrUpdateStreamAsync(streamConfig, cancellationToken);

return new NatsKVStore(config.Bucket, JetStreamContext, stream);
}

/// <inheritdoc />
public ValueTask<bool> DeleteStoreAsync(string bucket, CancellationToken cancellationToken = default)
{
Expand Down
48 changes: 48 additions & 0 deletions tests/NATS.Client.KeyValueStore.Tests/KeyValueContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,54 @@ public async Task Update_store_test()
updatedStatus.Info.Config.Description.Should().Be(natsKVConfig.Description);
}

[Fact]
public async Task Create_store_via_create_or_update_store_test()
{
const string expectedBuketName = "kv1";
const string expectedDescription = "description";

var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60));
var cancellationToken = cts.Token;

await using var server = NatsServer.StartJS();
await using var nats = server.CreateClientConnection();

var js = new NatsJSContext(nats);
var kv = new NatsKVContext(js);

var natsKVConfig = new NatsKVConfig(expectedBuketName) { Description = expectedDescription };
var store = await kv.CreateOrUpdateStoreAsync(natsKVConfig, cancellationToken);
var status = await store.GetStatusAsync(cancellationToken);

status.Bucket.Should().Be(expectedBuketName);
status.Info.Config.Description.Should().Be(expectedDescription);
}

[Fact]
public async Task Update_store_via_create_or_update_store_test()
{
var buketName = "kv1";
var expectedDescription = "Updated description";

var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60));
var cancellationToken = cts.Token;

await using var server = NatsServer.StartJS();
await using var nats = server.CreateClientConnection();

var js = new NatsJSContext(nats);
var kv = new NatsKVContext(js);

var store = await kv.CreateStoreAsync(buketName, cancellationToken);
var status = await store.GetStatusAsync(cancellationToken);
status.Info.Config.Description.Should().BeNull();

var natsKVConfig = new NatsKVConfig(buketName) { Description = expectedDescription };
await kv.CreateOrUpdateStoreAsync(natsKVConfig, cancellationToken);
var updatedStatus = await store.GetStatusAsync(cancellationToken);
updatedStatus.Info.Config.Description.Should().Be(natsKVConfig.Description);
}

[Fact]
public async Task Delete_store_test()
{
Expand Down
Loading