Skip to content
Closed
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
6 changes: 5 additions & 1 deletion test/IntegrationTests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ public virtual async ValueTask InitializeAsync()
// Create or ensure KV store exists
var jsContext = NatsConnection.CreateJetStreamContext();
var kvContext = new NatsKVContext(jsContext);
await kvContext.CreateOrUpdateStoreAsync(new NatsKVConfig("cache"));
var kvConfig = new NatsKVConfig("cache")
{
LimitMarkerTTL = TimeSpan.MaxValue,
};
await kvContext.CreateOrUpdateStoreAsync(kvConfig);
}

/// <summary>
Expand Down
10 changes: 4 additions & 6 deletions test/IntegrationTests/TimeExpirationAsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Options;
using NATS.Client.KeyValueStore;
using Xunit;

namespace CodeCargo.NatsDistributedCache.IntegrationTests;
Expand Down Expand Up @@ -121,8 +122,7 @@ public async Task SlidingExpirationExpiresIfNotAccessedAsync()

await Task.Delay(TimeSpan.FromSeconds(3));

result = await cache.GetAsync(key);
Assert.Null(result);
await Assert.ThrowsAsync<NatsKVKeyNotFoundException>(async () => await cache.GetAsync(key));
}

[Fact]
Expand All @@ -148,8 +148,7 @@ public async Task SlidingExpirationRenewedByAccessAsync()

await Task.Delay(TimeSpan.FromSeconds(3));

result = await cache.GetAsync(key);
Assert.Null(result);
await Assert.ThrowsAsync<NatsKVKeyNotFoundException>(async () => await cache.GetAsync(key));
}

[Fact]
Expand Down Expand Up @@ -181,8 +180,7 @@ public async Task SlidingExpirationRenewedByAccessUntilAbsoluteExpirationAsync()
await Task.Delay(TimeSpan.FromSeconds(0.5));
}

result = await cache.GetAsync(key);
Assert.Null(result);
await Assert.ThrowsAsync<NatsKVKeyNotFoundException>(async () => await cache.GetAsync(key));
}

private static async Task<string> GetNameAndReset(IDistributedCache cache, [CallerMemberName] string caller = "")
Expand Down
3 changes: 3 additions & 0 deletions test/UnitTests/CacheServiceExtensionsUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ public void AddNatsCache_UsesCacheOptionsAction()
wasInvoked = true;
});

var sp = services.BuildServiceProvider();
_ = sp.GetRequiredService<IDistributedCache>();

// Assert
Assert.True(wasInvoked);
}
Expand Down
11 changes: 6 additions & 5 deletions test/UnitTests/TimeExpirationUnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using CodeCargo.NatsDistributedCache.UnitTests.TestHelpers;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Logging;
using Moq;
using NATS.Client.Core;
using Xunit;

namespace CodeCargo.NatsDistributedCache.UnitTests;

Expand All @@ -17,6 +13,11 @@ public class TimeExpirationUnitTests
public TimeExpirationUnitTests()
{
_mockNatsConnection = new Mock<INatsConnection>();
// Setup the mock to properly handle the Opts property
var opts = new NatsOpts { LoggerFactory = new LoggerFactory() };
_mockNatsConnection.SetupGet(m => m.Opts).Returns(opts);
var connection = new NatsConnection(opts);
_mockNatsConnection.SetupGet(m => m.Connection).Returns(connection);
}

private IDistributedCache CreateCacheInstance()
Expand Down
Loading