58 lines
1.9 KiB
Go
58 lines
1.9 KiB
Go
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 := sanitizedDatabaseError(original)
|
|
if strings.Contains(err.Error(), "secret-payload") {
|
|
t.Fatalf("sanitizedDatabaseError() kept the message: %v", err)
|
|
}
|
|
var stateError sqlStateError
|
|
if !errors.As(err, &stateError) || stateError.SQLState() != "22P02" {
|
|
t.Fatalf("sanitizedDatabaseError() = %v, want SQLSTATE 22P02", err)
|
|
}
|
|
if conflict := sanitizedDatabaseError(&pgconn.PgError{Code: deadlockDetectedCode}); !errors.Is(conflict, outbound.ErrTransactionConflict) {
|
|
t.Fatalf("sanitizedDatabaseError(deadlock) = %v, want ErrTransactionConflict", conflict)
|
|
}
|
|
}
|