-
Notifications
You must be signed in to change notification settings - Fork 2
#49 Address unserializable entries #54
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
4 commits
Select commit
Hold shift + click to select a range
87f10ad
#49 Address unserializable entries
matthewdevenny 46a03ce
ensure set and get are consistent
matthewdevenny ee12ffa
#49 Bound sliding and absolute expirations to the NATS TTL encoding l…
matthewdevenny efea397
#49 Support MaxValue "never expire" sentinels; polish range errors an…
matthewdevenny 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| using System.Text; | ||
| using CodeCargo.Nats.DistributedCache.TestUtils.Services.Logging; | ||
| using Microsoft.Extensions.Caching.Distributed; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Options; | ||
| using NATS.Net; | ||
|
|
||
| namespace CodeCargo.Nats.DistributedCache.IntegrationTests.Cache; | ||
|
|
||
| public class UndeserializableEntryTests(NatsIntegrationFixture fixture) : TestBase(fixture) | ||
| { | ||
| // A legacy JSON envelope from a pre-binary release: the first byte is '{' (0x7B), which never | ||
| // matches the binary FormatVersion, so the serializer returns null. | ||
| private static readonly byte[] LegacyJsonEntry = | ||
| Encoding.UTF8.GetBytes("{\"absexp\":null,\"sldexp\":null,\"data\":\"AQID\"}"); | ||
|
|
||
| [Fact] | ||
| public async Task PresentButUndeserializableEntryIsReadAsMissAndLoggedAtDebug() | ||
| { | ||
| var key = MethodKey(); | ||
| var logger = new RecordingLogger<NatsCache>(); | ||
| var cache = new NatsCache(Options.Create(new NatsCacheOptions { BucketName = "cache" }), NatsConnection, logger); | ||
| await WriteRawEntryAsync(key, LegacyJsonEntry); | ||
|
|
||
| // The undeserializable entry reads as a cache miss rather than throwing... | ||
| var result = await cache.GetAsync(key, TestContext.Current.CancellationToken); | ||
| Assert.Null(result); | ||
|
|
||
| // ...and is surfaced once at Debug to aid diagnosis without failing the caller's operation. | ||
| var record = Assert.Single(logger.Records, r => r.EventId.Name == "UndeserializableEntry"); | ||
| Assert.Equal(LogLevel.Debug, record.LogLevel); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task UndeserializableEntryIsLeftInPlaceAndSelfHealsOnNextWrite() | ||
| { | ||
| var key = MethodKey(); | ||
| var cache = new NatsCache( | ||
| Options.Create(new NatsCacheOptions { BucketName = "cache" }), | ||
| NatsConnection, | ||
| new RecordingLogger<NatsCache>()); | ||
| await WriteRawEntryAsync(key, LegacyJsonEntry); | ||
|
|
||
| // The read leaves the entry untouched (no eviction): the original legacy bytes are still in | ||
| // the bucket afterwards, and it is still a miss on a second read. | ||
| Assert.Null(await cache.GetAsync(key, TestContext.Current.CancellationToken)); | ||
| var kvStore = await NatsConnection.CreateKeyValueStoreContext().GetStoreAsync("cache"); | ||
| var storedEntry = await kvStore.GetEntryAsync<byte[]>( | ||
| new NatsCacheKeyEncoder().Encode(key), | ||
| cancellationToken: TestContext.Current.CancellationToken); | ||
| Assert.Equal(LegacyJsonEntry, storedEntry.Value); | ||
|
|
||
| // Writing the key (a no-TTL entry, as in the migration case) overwrites the legacy bytes... | ||
| var value = Encoding.UTF8.GetBytes($"healed-{Guid.NewGuid()}"); | ||
| await cache.SetAsync(key, value, new DistributedCacheEntryOptions(), TestContext.Current.CancellationToken); | ||
|
|
||
| // ...and the entry is now readable, confirming the documented "re-populated on next write" path. | ||
| Assert.Equal(value, await cache.GetAsync(key, TestContext.Current.CancellationToken)); | ||
| } | ||
|
|
||
| // Writes raw bytes to the "cache" bucket at the key the cache reads, bypassing the binary | ||
| // serializer so the stored entry cannot be deserialized. | ||
| private async Task WriteRawEntryAsync(string key, byte[] raw) | ||
| { | ||
| var encodedKey = new NatsCacheKeyEncoder().Encode(key); | ||
| var kvStore = await NatsConnection.CreateKeyValueStoreContext().GetStoreAsync("cache"); | ||
| await kvStore.PutAsync(encodedKey, raw, cancellationToken: TestContext.Current.CancellationToken); | ||
| } | ||
| } |
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.
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.