package library import ( "context" "errors" "testing" "github.com/jackc/pgx/v5/pgtype" ) // A scan the last shutdown cut off is closed at startup with the reason, so // it no longer reads as a scan in progress. func TestRecoverScanRuns_ClosesARunTheLastShutdownCutOff_Integration(t *testing.T) { pool := triggerTestPool(t) ctx := context.Background() var id pgtype.UUID if err := pool.QueryRow(ctx, `INSERT INTO scan_runs (started_at) VALUES (now() - interval '5 minutes') RETURNING id`).Scan(&id); err != nil { t.Fatal(err) } RecoverScanRuns(ctx, pool, quietLogger()) var finished bool var reason *string if err := pool.QueryRow(ctx, `SELECT finished_at IS NOT NULL, error_message FROM scan_runs WHERE id = $1`, id).Scan(&finished, &reason); err != nil { t.Fatal(err) } if !finished || reason == nil || *reason == "" { t.Errorf("finished %v, reason %v; want the run closed with why", finished, reason) } } // Saving library folders waits for a scan that is running, but not for an // unfinished run older than StuckScanThreshold: that one is dead. func TestSetRoots_OnlyALiveScanHoldsUpSavingFolders_Integration(t *testing.T) { pool := triggerTestPool(t) clearSavedRoots(t, pool) ctx := context.Background() s := New(pool, quietLogger(), nil, nil) if _, err := pool.Exec(ctx, `INSERT INTO scan_runs (started_at) VALUES (now() - interval '2 hours')`); err != nil { t.Fatal(err) } if _, err := s.SetRoots(ctx, []RootChange{{Path: t.TempDir()}}); err != nil { t.Errorf("a dead scan blocked saving folders: %v", err) } if _, err := pool.Exec(ctx, `INSERT INTO scan_runs (started_at) VALUES (now())`); err != nil { t.Fatal(err) } var re *RootsError if _, err := s.SetRoots(ctx, []RootChange{{Path: t.TempDir()}}); !errors.As(err, &re) { t.Errorf("err %v; want a refusal while a scan is running", err) } }