feat(events): add idempotent Kafka event consumer runtime
This commit is contained in:
1 parent
565ef8d4f5
commit
3adb0ef9e4
47 files changed
+5018
-105
No files matched your search
@@ -0,0 +1,80 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
|
||||
"lincodex/backend/internal/port/inbound"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
func TestClassifyCommitErrorSeparatesRefusalsFromTransportFailures(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
err error
|
||||
rejected bool
|
||||
conflict bool
|
||||
forbidText string
|
||||
}{
|
||||
{name: "aborted transaction", err: pgx.ErrTxCommitRollback, rejected: true},
|
||||
{
|
||||
name: "deferred unique violation",
|
||||
err: &pgconn.PgError{Code: "23505", ConstraintName: "effects_key", Message: "duplicate key secret-value"},
|
||||
rejected: true,
|
||||
forbidText: "secret-value",
|
||||
},
|
||||
{name: "serialization failure", err: &pgconn.PgError{Code: "40001", Message: "could not serialize"}, conflict: true},
|
||||
{name: "deadlock", err: &pgconn.PgError{Code: "40P01"}, conflict: true},
|
||||
{name: "network", err: &net.OpError{Op: "read", Err: errors.New("connection reset")}},
|
||||
{name: "server shutdown", err: &pgconn.PgError{Code: "57P01", Message: "terminating connection secret-value"}, forbidText: "secret-value"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
err := classifyCommitError(test.err)
|
||||
if got := errors.Is(err, outbound.ErrEventEffectRejected); got != test.rejected {
|
||||
t.Fatalf("classifyCommitError() = %v, rejected = %t, want %t", err, got, test.rejected)
|
||||
}
|
||||
if got := errors.Is(err, outbound.ErrTransactionConflict); got != test.conflict {
|
||||
t.Fatalf("classifyCommitError() = %v, conflict = %t, want %t", err, got, test.conflict)
|
||||
}
|
||||
if test.forbidText != "" && strings.Contains(err.Error(), test.forbidText) {
|
||||
t.Fatalf("classifyCommitError() kept the PostgreSQL message: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeApplyErrorKeepsIdentityButDropsThePostgreSQLMessage(t *testing.T) {
|
||||
pgError := &pgconn.PgError{Severity: "ERROR", Code: "22P02", Message: `invalid input syntax for type uuid: "secret-value"`}
|
||||
wrapped := fmt.Errorf("apply effect: %w: %w", inbound.ErrEventRejected, pgError)
|
||||
|
||||
err := sanitizeApplyError(wrapped)
|
||||
if strings.Contains(err.Error(), "secret-value") {
|
||||
t.Fatalf("sanitizeApplyError() kept the message: %v", err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "apply effect") || !strings.Contains(err.Error(), "22P02") {
|
||||
t.Fatalf("sanitizeApplyError() = %q, want the handler context and the SQLSTATE", err)
|
||||
}
|
||||
var found *pgconn.PgError
|
||||
if !errors.Is(err, inbound.ErrEventRejected) || !errors.As(err, &found) {
|
||||
t.Fatalf("sanitizeApplyError() lost the error identity: %v", err)
|
||||
}
|
||||
|
||||
// A handler that copied only the message into its own text.
|
||||
copied := fmt.Errorf("apply effect failed with %s: %w", pgError.Message, errors.New("cause"))
|
||||
copied = fmt.Errorf("%w (%w)", copied, pgError)
|
||||
if err := sanitizeApplyError(copied); strings.Contains(err.Error(), "secret-value") {
|
||||
t.Fatalf("sanitizeApplyError() kept a copied message: %v", err)
|
||||
}
|
||||
|
||||
plain := errors.New("plain handler failure")
|
||||
if err := sanitizeApplyError(plain); err != plain { //nolint:errorlint // The same value proves the pass-through.
|
||||
t.Fatalf("sanitizeApplyError() changed an error without PostgreSQL text: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user