|
| 1 | +package pg |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/jackc/pgx/v5/pgconn" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestIsConnClosed(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + cases := []struct { |
| 18 | + name string |
| 19 | + err error |
| 20 | + want bool |
| 21 | + }{ |
| 22 | + {"nil", nil, false}, |
| 23 | + {"bare ErrConnClosed", pgconn.ErrConnClosed, true}, |
| 24 | + {"wrapped ErrConnClosed", fmt.Errorf("relay publish: %w", pgconn.ErrConnClosed), true}, |
| 25 | + {"doubly wrapped", fmt.Errorf("outer: %w", fmt.Errorf("inner: %w", pgconn.ErrConnClosed)), true}, |
| 26 | + {"unrelated error", errors.New("conn closed"), false}, |
| 27 | + {"pg error", &pgconn.PgError{Code: "57P03"}, false}, |
| 28 | + } |
| 29 | + for _, tc := range cases { |
| 30 | + t.Run(tc.name, func(t *testing.T) { |
| 31 | + t.Parallel() |
| 32 | + require.Equal(t, tc.want, IsConnClosed(tc.err)) |
| 33 | + }) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestPgCodeClassifiers(t *testing.T) { |
| 38 | + t.Parallel() |
| 39 | + |
| 40 | + require.True(t, IsLockTimeout(fmt.Errorf("take lock: %w", &pgconn.PgError{Code: "55P03"}))) |
| 41 | + require.False(t, IsLockTimeout(&pgconn.PgError{Code: "57P03"})) |
| 42 | + require.False(t, IsLockTimeout(pgconn.ErrConnClosed)) |
| 43 | + |
| 44 | + require.True(t, IsInRecovery(fmt.Errorf("query: %w", &pgconn.PgError{Code: "57P03"}))) |
| 45 | + require.False(t, IsInRecovery(&pgconn.PgError{Code: "55P03"})) |
| 46 | + require.False(t, IsInRecovery(errors.New("in recovery"))) |
| 47 | +} |
| 48 | + |
| 49 | +func TestRegistryStore_WithClock(t *testing.T) { |
| 50 | + t.Parallel() |
| 51 | + |
| 52 | + fixed := time.Date(2026, 8, 16, 4, 0, 0, 0, time.UTC) |
| 53 | + s := (&RegistryStore{}).WithClock(func() time.Time { return fixed }) |
| 54 | + require.Equal(t, fixed, s.now()) |
| 55 | +} |
| 56 | + |
| 57 | +func TestRepoQueue_WithClockAndIDGen(t *testing.T) { |
| 58 | + t.Parallel() |
| 59 | + |
| 60 | + fixed := time.Date(2026, 8, 16, 4, 0, 0, 0, time.UTC) |
| 61 | + q := (&RepoQueue{}).WithClock(func() time.Time { return fixed }).WithIDGen(func() string { return "req_stub" }) |
| 62 | + require.Equal(t, fixed, q.now()) |
| 63 | + require.Equal(t, "req_stub", q.newID()) |
| 64 | +} |
| 65 | + |
| 66 | +func TestDefaultRepoRequestID(t *testing.T) { |
| 67 | + t.Parallel() |
| 68 | + |
| 69 | + first := defaultRepoRequestID() |
| 70 | + require.True(t, strings.HasPrefix(first, "req_"), "got %q", first) |
| 71 | + require.Len(t, first, len("req_")+20) |
| 72 | + require.NotEqual(t, first, defaultRepoRequestID()) |
| 73 | +} |
0 commit comments