fix crash
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2026-02-09 15:55:19 +11:00
parent 6dcbb9caef
commit b70dfcf5be
8 changed files with 162 additions and 73 deletions

View File

@@ -3,6 +3,7 @@ package db
import (
"context"
"database/sql"
"errors"
"fmt"
"testing"
"time"
@@ -32,6 +33,37 @@ func indexExists(t *testing.T, dbConn *sqlx.DB, name string) bool {
return count > 0
}
func TestEnsureOncePerDBRetriesUntilSuccess(t *testing.T) {
dbConn := newTestSQLiteDB(t)
attempts := 0
run := func() error {
attempts++
if attempts == 1 {
return errors.New("transient failure")
}
return nil
}
if err := ensureOncePerDB(dbConn, "test_once", run); err == nil {
t.Fatal("expected first ensureOncePerDB call to fail")
}
if attempts != 1 {
t.Fatalf("expected 1 attempt after first call, got %d", attempts)
}
if err := ensureOncePerDB(dbConn, "test_once", run); err != nil {
t.Fatalf("expected second ensureOncePerDB call to succeed, got %v", err)
}
if attempts != 2 {
t.Fatalf("expected 2 attempts after retry, got %d", attempts)
}
if err := ensureOncePerDB(dbConn, "test_once", run); err != nil {
t.Fatalf("expected third ensureOncePerDB call to reuse success, got %v", err)
}
if attempts != 2 {
t.Fatalf("expected no additional attempts after success, got %d", attempts)
}
}
func TestCleanupHourlySnapshotIndexesOlderThan(t *testing.T) {
ctx := context.Background()
dbConn := newTestSQLiteDB(t)