Skip to content

Commit

Permalink
Add CreateOrUpdateStoreAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivandemidov00 committed Jan 8, 2025
1 parent 4ea0dfc commit 3aad1b7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
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

0 comments on commit 3aad1b7

Please sign in to comment.