feat(outbox): relay committed outbox events to Kafka
This commit is contained in:
1 parent
78f1651f6b
commit
6c3b34f73e
42 files changed
+3731
-45
No files matched your search
@@ -0,0 +1,57 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
func TestClaimPendingRejectsAnInvalidLimitBeforeTheDatabase(t *testing.T) {
|
||||
// A nil pool proves that validation happens before any connection.
|
||||
valid := OutboxRelayTimeouts{Statement: time.Second, IdleInTransaction: time.Second}
|
||||
publish := func(context.Context, []outbound.OutboxEvent) []uuid.UUID {
|
||||
t.Fatal("publish called for an invalid claim")
|
||||
return nil
|
||||
}
|
||||
store := NewOutboxRelayStore(nil, valid)
|
||||
for _, limit := range []int{0, -1, 1 << 31} {
|
||||
if err := store.ClaimPending(t.Context(), limit, publish); err == nil {
|
||||
t.Fatalf("ClaimPending(limit %d) accepted the limit", limit)
|
||||
}
|
||||
}
|
||||
// A timeout that rounds to 0 ms would disable the server-side bound.
|
||||
for _, timeouts := range []OutboxRelayTimeouts{
|
||||
{Statement: time.Microsecond, IdleInTransaction: time.Second},
|
||||
{Statement: time.Second},
|
||||
} {
|
||||
if err := NewOutboxRelayStore(nil, timeouts).ClaimPending(t.Context(), 1, publish); err == nil {
|
||||
t.Fatalf("ClaimPending() accepted timeouts %+v", timeouts)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRelayDatabaseErrorKeepsOnlyTheSQLState(t *testing.T) {
|
||||
original := &pgconn.PgError{
|
||||
Code: "22P02",
|
||||
Message: `invalid input syntax for type json: "secret-payload"`,
|
||||
Detail: `Token "secret-payload" is invalid.`,
|
||||
}
|
||||
err := relayDatabaseError(original)
|
||||
if strings.Contains(err.Error(), "secret-payload") {
|
||||
t.Fatalf("relayDatabaseError() kept the message: %v", err)
|
||||
}
|
||||
var stateError sqlStateError
|
||||
if !errors.As(err, &stateError) || stateError.SQLState() != "22P02" {
|
||||
t.Fatalf("relayDatabaseError() = %v, want SQLSTATE 22P02", err)
|
||||
}
|
||||
if conflict := relayDatabaseError(&pgconn.PgError{Code: deadlockDetectedCode}); !errors.Is(conflict, outbound.ErrTransactionConflict) {
|
||||
t.Fatalf("relayDatabaseError(deadlock) = %v, want ErrTransactionConflict", conflict)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user