-
Notifications
You must be signed in to change notification settings - Fork 1
HybridCache Integration Test #15
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
86fa2e0
define AddHybridCacheTestClient
SuddyN 89b251c
Merge branch 'main' into suddy/hybridCacheIntegrationTest
SuddyN 4b46785
implement hybridcache integration test
SuddyN 0762d9e
clean up NatsTestExtensions
SuddyN f4892b6
check serialize primitives
SuddyN f4b3699
check serialized value in NATS KV bucket
SuddyN 3869458
use Assert.Equivalent
SuddyN 8188689
rewrite assert
SuddyN e479045
UTF8 -> ASCII
SuddyN 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
89 changes: 89 additions & 0 deletions
89
test/IntegrationTests/Cache/HybridCacheSetAndRemoveTests.cs
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,89 @@ | ||
| using System.Buffers; | ||
| using System.Globalization; | ||
| using System.Text; | ||
| using Microsoft.Extensions.Caching.Hybrid; | ||
| using NATS.Client.Core; | ||
| using NATS.Client.KeyValueStore; | ||
| using NATS.Net; | ||
| using StreamJsonRpc; | ||
|
|
||
| namespace CodeCargo.Nats.DistributedCache.IntegrationTests.Cache; | ||
|
|
||
| public class HybridCacheGetSetRemoveTests(NatsIntegrationFixture fixture) : TestBase(fixture) | ||
| { | ||
| [Fact] | ||
| public async Task HybridCacheGetSetRemoveTest() | ||
| { | ||
| // Arrange | ||
| var key = MethodKey(); | ||
| var value = Encoding.UTF8.GetBytes($"test-value-{Guid.NewGuid()}"); | ||
| var options = new HybridCacheEntryOptions | ||
| { | ||
| Expiration = TimeSpan.FromMinutes(10) | ||
| }; | ||
|
|
||
| // Assert - Verify the value is not stored in NATS KV store | ||
| var kvStore = await NatsConnection.CreateKeyValueStoreContext().GetStoreAsync("cache"); | ||
| await Assert.ThrowsAsync<NatsKVKeyNotFoundException>(async () => await kvStore.GetEntryAsync<byte[]>(key)); | ||
|
|
||
| // Act | ||
| await HybridCache.SetAsync(key, value, options); | ||
|
|
||
| // Assert - Verify the value is stored in NATS KV store | ||
| var kvEntry = await kvStore.GetEntryAsync<byte[]>(key); | ||
| Assert.NotNull(kvEntry.Value); | ||
|
|
||
| // Assert - Verify the value is retrievable from hybrid cache | ||
| var result = await HybridCache.GetOrCreateAsync(key, async ct => await Task.FromResult(Array.Empty<byte>())); | ||
| Assert.NotEmpty(result); | ||
| Assert.Equal(value, result); | ||
|
|
||
| // Act | ||
| await HybridCache.RemoveAsync(key); | ||
|
|
||
| // Assert - Verify the value is not stored in NATS KV store | ||
| await Assert.ThrowsAsync<NatsKVKeyDeletedException>(async () => await kvStore.GetEntryAsync<byte[]>(key)); | ||
|
|
||
| // Assert - Verify the value is not retrievable from hybrid cache | ||
| result = await HybridCache.GetOrCreateAsync(key, async ct => await Task.FromResult(Array.Empty<byte>())); | ||
| Assert.Empty(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task HybridCacheSerializesDateTime() | ||
| { | ||
| // Arrange | ||
| var key = MethodKey(); | ||
| const string invariant = "2025-05-15T17:18:58.7503097Z"; | ||
| var date = DateTime.Parse(invariant, CultureInfo.InvariantCulture); | ||
|
|
||
| var options = new HybridCacheEntryOptions | ||
| { | ||
| Expiration = TimeSpan.FromMinutes(10) | ||
| }; | ||
|
|
||
| // Act - Store the complex object in the cache | ||
| await HybridCache.SetAsync(key, date, options); | ||
|
|
||
| // Assert - date is serialized as expected | ||
| var writer = new ArrayBufferWriter<byte>(); | ||
| new NatsUtf8PrimitivesSerializer<DateTime>().Serialize(writer, date); | ||
| var serializedDateString = Encoding.ASCII.GetString(writer.WrittenSpan.ToArray()); | ||
|
|
||
| var kvStore = await NatsConnection.CreateKeyValueStoreContext().GetStoreAsync("cache"); | ||
| NatsJsonContextSerializer<CacheEntry> cacheEntrySerializer = new(CacheEntryJsonContext.Default); | ||
| var kvEntry = await kvStore.GetEntryAsync(key, serializer: cacheEntrySerializer); | ||
| Assert.NotNull(kvEntry.Value?.Data); | ||
| var storedDateString = Encoding.ASCII.GetString(kvEntry.Value.Data); | ||
|
|
||
| // HybridCache adds additional data to the front of the serialized value, so we're matching only the relevant data | ||
| Assert.Contains(serializedDateString, storedDateString); | ||
|
|
||
| // Assert - date is deserialized as expected | ||
| var retrieved = await HybridCache.GetOrCreateAsync(key, async ct => await Task.FromResult(DateTime.UnixEpoch)); | ||
| Assert.Equal(date, retrieved); | ||
|
|
||
| // Cleanup | ||
| await HybridCache.RemoveAsync(key); | ||
| } | ||
| } | ||
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.