Files
mirror-3x-ui/internal/web/service/server_pg_dump_test.go
T
Aleksey Fedorov 5b0cbe6473 fix(backup): give up on a pg_dump that does not finish within an hour (#7089)
exportPostgresDB started pg_dump with context.Background(), so a
PostgreSQL server that accepted the connection and then stopped answering
hung the database download and the scheduled Telegram and Discord
backups forever; SkipIfStillRunning then silently skipped every later
backup run.

pg_dump now runs under a one-hour deadline and the caller gets an error
that names the timeout.
2026-11-24 01:35:53 +03:30

37 lines
1.1 KiB
Go

package service
import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
)
// A PostgreSQL server that accepts the connection and then stops answering
// must not hang the database download and the scheduled backups forever.
func TestExportPostgresDBGivesUpOnAStalledServer(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the fake pg_dump is a POSIX shell script")
}
bin := t.TempDir()
if err := os.WriteFile(filepath.Join(bin, "pg_dump"), []byte("#!/bin/sh\nexec sleep 30\n"), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
t.Setenv("XUI_DB_DSN", "postgres://xui:secret@127.0.0.1:5432/xui?sslmode=disable")
prev := pgDumpTimeout
pgDumpTimeout = 300 * time.Millisecond
t.Cleanup(func() { pgDumpTimeout = prev })
start := time.Now()
_, err := (&ServerService{}).exportPostgresDB()
if err == nil || !strings.Contains(err.Error(), "pg_dump did not finish within 300ms") {
t.Fatalf("exportPostgresDB() error = %v, want the timeout", err)
}
if took := time.Since(start); took > 15*time.Second {
t.Fatalf("exportPostgresDB() returned after %v", took)
}
}