修复归档大型任务导致的崩溃

This commit is contained in:
pphua committed 2026-08-28 15:20:52 +08:00
1 parent 9da521ee4e
commit 01c6d73614
11 files changed
+651 -60

No files matched your search

+47
View File
@@ -1,9 +1,56 @@
package db
import (
"context"
"database/sql"
"errors"
"testing"
"time"
"github.com/jackc/pgx/v5/pgconn"
)
type schemaExecFunc func(context.Context, string, ...any) (sql.Result, error)
func (f schemaExecFunc) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
return f(ctx, query, args...)
}
func TestApplySchemaRetriesOnlyDeadlocks(t *testing.T) {
deadlockAttempts := 0
delays := []time.Duration{}
err := applySchemaWithRetry(t.Context(), schemaExecFunc(func(context.Context, string, ...any) (sql.Result, error) {
deadlockAttempts++
if deadlockAttempts < 3 {
return nil, &pgconn.PgError{Code: "40P01", Message: "deadlock detected"}
}
return nil, nil
}), func(delay time.Duration) {
delays = append(delays, delay)
})
if err != nil {
t.Fatal(err)
}
if deadlockAttempts != 3 {
t.Fatalf("schema attempts=%d, want 3", deadlockAttempts)
}
if len(delays) != 2 || delays[0] != schemaDeadlockRetryDelays[0] || delays[1] != schemaDeadlockRetryDelays[1] {
t.Fatalf("schema retry delays=%v", delays)
}
nonRetryable := errors.New("permission denied")
nonRetryableAttempts := 0
err = applySchemaWithRetry(t.Context(), schemaExecFunc(func(context.Context, string, ...any) (sql.Result, error) {
nonRetryableAttempts++
return nil, nonRetryable
}), func(time.Duration) {
t.Fatal("non-deadlock error must not be retried")
})
if !errors.Is(err, nonRetryable) || nonRetryableAttempts != 1 {
t.Fatalf("non-retryable result: attempts=%d err=%v", nonRetryableAttempts, err)
}
}
// testDSN returns the configured DSN, skipping the test when neither the env var
// nor a config file supplies one (DSN no longer has a built-in default).
func testDSN(t *testing.T) string {