fix(kafka): retain failed message persistence
This commit is contained in:
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -47,6 +48,33 @@ func TestConsumeKafkaMessageBatchRetriesHandlerAndCommit(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestConsumeKafkaMessageBatchDoesNotCommitWhileHandlerFails(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
handlerCalls := 0
|
||||
commitCalls := 0
|
||||
|
||||
err := consumeKafkaMessageBatch(ctx, []kafka.Message{{Offset: 1}},
|
||||
func(context.Context, []kafka.Message) error {
|
||||
handlerCalls++
|
||||
return errors.New("database unavailable")
|
||||
},
|
||||
func(context.Context, ...kafka.Message) error {
|
||||
commitCalls++
|
||||
return nil
|
||||
},
|
||||
func(ctx context.Context, _ time.Duration) error {
|
||||
cancel()
|
||||
return ctx.Err()
|
||||
})
|
||||
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("consumeKafkaMessageBatch() error = %v, want context canceled", err)
|
||||
}
|
||||
if handlerCalls != 1 || commitCalls != 0 {
|
||||
t.Fatalf("calls handler=%d commit=%d, want 1/0", handlerCalls, commitCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConsumeKafkaMalformedMessageRetriesCommit(t *testing.T) {
|
||||
commitCalls := 0
|
||||
waitCalls := 0
|
||||
@@ -76,10 +104,9 @@ func TestConsumeKafkaMalformedMessageRetriesCommit(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunKafkaMessageSingleRetriesThenDeadLetters(t *testing.T) {
|
||||
func TestRunKafkaMessageSingleRetriesReturnsErrorWithoutTerminalCommit(t *testing.T) {
|
||||
operationCalls := 0
|
||||
markFailedCalls := 0
|
||||
markDeadCalls := 0
|
||||
waitCalls := 0
|
||||
|
||||
err := runKafkaMessageSingleRetries(context.Background(), 3,
|
||||
@@ -91,31 +118,23 @@ func TestRunKafkaMessageSingleRetriesThenDeadLetters(t *testing.T) {
|
||||
markFailedCalls++
|
||||
return &types.KafkaConsumeRecord{RetryCount: markFailedCalls}, nil
|
||||
},
|
||||
func(record *types.KafkaConsumeRecord, err error) error {
|
||||
markDeadCalls++
|
||||
if record == nil || record.RetryCount != 4 || err == nil {
|
||||
t.Fatalf("unexpected dead-letter input: record=%+v err=%v", record, err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
func(context.Context, time.Duration) error {
|
||||
waitCalls++
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("runKafkaMessageSingleRetries() error = %v", err)
|
||||
if err == nil || !strings.Contains(err.Error(), "message persistence retries exhausted") {
|
||||
t.Fatalf("runKafkaMessageSingleRetries() error = %v, want retry exhaustion", err)
|
||||
}
|
||||
if operationCalls != 4 || markFailedCalls != 4 || waitCalls != 3 || markDeadCalls != 1 {
|
||||
t.Fatalf("calls operation=%d failed=%d wait=%d dead=%d, want 4/4/3/1",
|
||||
operationCalls, markFailedCalls, waitCalls, markDeadCalls)
|
||||
if operationCalls != 4 || markFailedCalls != 4 || waitCalls != 3 {
|
||||
t.Fatalf("calls operation=%d failed=%d wait=%d, want 4/4/3",
|
||||
operationCalls, markFailedCalls, waitCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunKafkaMessageSingleRetriesStopsOnSuccess(t *testing.T) {
|
||||
operationCalls := 0
|
||||
markFailedCalls := 0
|
||||
markDeadCalls := 0
|
||||
err := runKafkaMessageSingleRetries(context.Background(), 3,
|
||||
func() error {
|
||||
operationCalls++
|
||||
@@ -128,16 +147,12 @@ func TestRunKafkaMessageSingleRetriesStopsOnSuccess(t *testing.T) {
|
||||
markFailedCalls++
|
||||
return &types.KafkaConsumeRecord{RetryCount: markFailedCalls}, nil
|
||||
},
|
||||
func(*types.KafkaConsumeRecord, error) error {
|
||||
markDeadCalls++
|
||||
return nil
|
||||
},
|
||||
func(context.Context, time.Duration) error { return nil })
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("runKafkaMessageSingleRetries() error = %v", err)
|
||||
}
|
||||
if operationCalls != 3 || markFailedCalls != 2 || markDeadCalls != 0 {
|
||||
t.Fatalf("calls operation=%d failed=%d dead=%d, want 3/2/0", operationCalls, markFailedCalls, markDeadCalls)
|
||||
if operationCalls != 3 || markFailedCalls != 2 {
|
||||
t.Fatalf("calls operation=%d failed=%d, want 3/2", operationCalls, markFailedCalls)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user