Stop consumers from committing past failed offsets, harden Rabbit/DataLake/gateway backpressure, dispose JsonDocument/MinIO correctly, and batch expensive DB/HTTP paths. Track progress in docs/performance.
90 lines
3.0 KiB
C#
90 lines
3.0 KiB
C#
using System.Text.Json;
|
|
using Confluent.Kafka;
|
|
using FluentAssertions;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
|
|
public class KafkaConsumerRetryTests
|
|
{
|
|
[Fact]
|
|
public void PoisonPillGuard_PermanentError_SkipsImmediately()
|
|
{
|
|
var guard = new PoisonPillGuard("test-group", maxRetries: 5, NullLogger.Instance);
|
|
var result = BuildConsumeResult("test-topic", 0, 0, "bad payload");
|
|
|
|
guard.ShouldSkip(result, new JsonException("Invalid JSON"))
|
|
.Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void PoisonPillGuard_TransientError_RetriesUpToMax()
|
|
{
|
|
const int maxRetries = 3;
|
|
var guard = new PoisonPillGuard("test-group", maxRetries, NullLogger.Instance);
|
|
var result = BuildConsumeResult("test-topic", 0, 42, "payload");
|
|
var transientError = new TimeoutException("Connection timed out");
|
|
|
|
for (var i = 1; i < maxRetries; i++)
|
|
guard.ShouldSkip(result, transientError).Should().BeFalse();
|
|
|
|
guard.ShouldSkip(result, transientError).Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task KafkaConsumerRetry_TransientError_SeeksBackWithoutCommit()
|
|
{
|
|
var guard = new PoisonPillGuard("test-group", maxRetries: 5, NullLogger.Instance);
|
|
var result = BuildConsumeResult("test-topic", 0, 42, "payload");
|
|
TopicPartitionOffset? sought = null;
|
|
ConsumeResult<string, string>? committed = null;
|
|
|
|
await KafkaConsumerRetry.HandleFailureAsync(
|
|
seek: tpo => sought = tpo,
|
|
commit: r => committed = r,
|
|
result,
|
|
new TimeoutException("transient"),
|
|
guard,
|
|
NullLogger.Instance,
|
|
"TestService",
|
|
CancellationToken.None,
|
|
delayMs: 1);
|
|
|
|
sought.Should().NotBeNull();
|
|
sought!.Offset.Value.Should().Be(42);
|
|
committed.Should().BeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task KafkaConsumerRetry_PermanentError_CommitsWithoutSeek()
|
|
{
|
|
var guard = new PoisonPillGuard("test-group", maxRetries: 5, NullLogger.Instance);
|
|
var result = BuildConsumeResult("test-topic", 0, 7, "bad");
|
|
TopicPartitionOffset? sought = null;
|
|
ConsumeResult<string, string>? committed = null;
|
|
|
|
await KafkaConsumerRetry.HandleFailureAsync(
|
|
seek: tpo => sought = tpo,
|
|
commit: r => committed = r,
|
|
result,
|
|
new JsonException("bad json"),
|
|
guard,
|
|
NullLogger.Instance,
|
|
"TestService",
|
|
CancellationToken.None,
|
|
delayMs: 1);
|
|
|
|
sought.Should().BeNull();
|
|
committed.Should().NotBeNull();
|
|
committed!.Offset.Value.Should().Be(7);
|
|
}
|
|
|
|
private static ConsumeResult<string, string> BuildConsumeResult(
|
|
string topic, int partition, long offset, string value) =>
|
|
new()
|
|
{
|
|
Topic = topic,
|
|
Partition = new Partition(partition),
|
|
Offset = new Offset(offset),
|
|
Message = new Message<string, string> { Key = "test-key", Value = value }
|
|
};
|
|
}
|