-
Notifications
You must be signed in to change notification settings - Fork 2
#43 Log swallowed TryGetAsync exceptions #52
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| using System.Buffers; | ||
| using CodeCargo.Nats.DistributedCache.TestUtils.Services.Logging; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace CodeCargo.Nats.DistributedCache.IntegrationTests.Cache; | ||
|
|
||
| public class TryGetAsyncTests(NatsIntegrationFixture fixture) : TestBase(fixture) | ||
| { | ||
| [Fact] | ||
| public async Task TryGetAsyncSwallowsFailureAndLogsOnceAtWarning() | ||
| { | ||
| var cache = CreateFailingCache(out var logger); | ||
| var destination = new ArrayBufferWriter<byte>(); | ||
|
|
||
| var result = await cache.TryGetAsync(MethodKey(), destination, TestContext.Current.CancellationToken); | ||
|
|
||
| Assert.False(result); | ||
| Assert.Equal(0, destination.WrittenCount); | ||
|
|
||
| // The failure is logged exactly once, at warning, with no redundant error-level entry. | ||
| var record = Assert.Single(logger.Records); | ||
| Assert.Equal(LogLevel.Warning, record.LogLevel); | ||
| Assert.Equal("Exception", record.EventId.Name); | ||
| Assert.NotNull(record.Exception); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetAsyncPropagatesFailureAndLogsOnceAtError() | ||
| { | ||
| var cache = CreateFailingCache(out var logger); | ||
|
|
||
| await Assert.ThrowsAnyAsync<Exception>( | ||
| () => cache.GetAsync(MethodKey(), TestContext.Current.CancellationToken)); | ||
|
|
||
| // A propagating read logs exactly once, at error, and is not double-logged by shared helpers. | ||
| var record = Assert.Single(logger.Records); | ||
| Assert.Equal(LogLevel.Error, record.LogLevel); | ||
| Assert.Equal("Exception", record.EventId.Name); | ||
| Assert.NotNull(record.Exception); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TryGetAsyncPropagatesCancellationWithoutLogging() | ||
| { | ||
| // Use the real bucket so the store resolves; the cancelled token then fails the read itself. | ||
| var logger = new RecordingLogger<NatsCache>(); | ||
| var cache = new NatsCache( | ||
| Options.Create(new NatsCacheOptions { BucketName = "cache" }), | ||
| NatsConnection, | ||
| logger); | ||
| var destination = new ArrayBufferWriter<byte>(); | ||
| using var cts = new CancellationTokenSource(); | ||
| await cts.CancelAsync(); | ||
|
|
||
| // Cancellation is not a cache failure: it propagates instead of becoming a false miss... | ||
| await Assert.ThrowsAnyAsync<OperationCanceledException>( | ||
| () => cache.TryGetAsync(MethodKey(), destination, cts.Token).AsTask()); | ||
|
|
||
| // ...and it is not logged as an exception (a benign "Connected" info entry may be present). | ||
| Assert.DoesNotContain(logger.Records, r => r.EventId.Name == "Exception"); | ||
| } | ||
|
|
||
| // Points a cache at a bucket that does not exist so the read path throws when it resolves the KV | ||
| // store, without depending on the shared fixture connection being torn down. | ||
| private NatsCache CreateFailingCache(out RecordingLogger<NatsCache> logger) | ||
| { | ||
| logger = new RecordingLogger<NatsCache>(); | ||
| return new NatsCache( | ||
| Options.Create(new NatsCacheOptions { BucketName = "does-not-exist" }), | ||
| NatsConnection, | ||
| logger); | ||
| } | ||
| } |
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,55 @@ | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace CodeCargo.Nats.DistributedCache.TestUtils.Services.Logging; | ||
|
|
||
| /// <summary> | ||
| /// A captured log entry recorded by <see cref="RecordingLogger{T}" /> | ||
| /// </summary> | ||
| /// <param name="LogLevel">The level the entry was logged at</param> | ||
| /// <param name="EventId">The event id associated with the entry</param> | ||
| /// <param name="Exception">The exception attached to the entry, if any</param> | ||
| /// <param name="Message">The formatted log message</param> | ||
| public sealed record LogRecord(LogLevel LogLevel, EventId EventId, Exception? Exception, string Message); | ||
|
|
||
| /// <summary> | ||
| /// An <see cref="ILogger{T}" /> that captures log entries in memory so tests can assert on them | ||
| /// </summary> | ||
| /// <typeparam name="T">The category type</typeparam> | ||
| public sealed class RecordingLogger<T> : ILogger<T> | ||
| { | ||
| private readonly List<LogRecord> _records = new(); | ||
|
|
||
| /// <summary> | ||
| /// Gets a snapshot of the entries captured so far | ||
| /// </summary> | ||
| public IReadOnlyList<LogRecord> Records | ||
| { | ||
| get | ||
| { | ||
| lock (_records) | ||
| { | ||
| return _records.ToArray(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void Log<TState>( | ||
| LogLevel logLevel, | ||
| EventId eventId, | ||
| TState state, | ||
| Exception? exception, | ||
| Func<TState, Exception?, string> formatter) | ||
| { | ||
| var record = new LogRecord(logLevel, eventId, exception, formatter(state, exception)); | ||
| lock (_records) | ||
| { | ||
| _records.Add(record); | ||
| } | ||
| } | ||
|
|
||
| public bool IsEnabled(LogLevel logLevel) => true; | ||
|
|
||
| public IDisposable? BeginScope<TState>(TState state) | ||
| where TState : notnull => | ||
| null; | ||
| } |
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.