package transfer import ( "context" "fmt" "os" "path/filepath" "testing" "time" ) func writeAgedFile(t *testing.T, dir, name string, age time.Duration) string { t.Helper() path := filepath.Join(dir, name) if err := os.WriteFile(path, []byte("segment"), 0644); err != nil { t.Fatalf("failed to write %s: %v", path, err) } modTime := time.Now().Add(-age) if err := os.Chtimes(path, modTime, modTime); err != nil { t.Fatalf("failed to age %s: %v", path, err) } return path } func TestCleanupRemovesTransferredFiles(t *testing.T) { dir := t.TempDir() cs := NewCleanupService(CleanupConfig{Enabled: true, BatchSize: 10, CheckInterval: time.Second}) path := writeAgedFile(t, dir, "seg_1001.ts", 0) if err := cs.ScheduleCleanup(path); err != nil { t.Fatalf("ScheduleCleanup failed: %v", err) } if err := cs.ExecuteCleanup(context.Background()); err != nil { t.Fatalf("ExecuteCleanup failed: %v", err) } if _, err := os.Stat(path); !os.IsNotExist(err) { t.Errorf("file should have been removed, stat err = %v", err) } if got := cs.GetPendingCount(); got != 0 { t.Errorf("pending count = %d, want 0", got) } } // TestCleanupRetainsThenRemoves is the regression test for a file inside its // retention window being dropped from the pending list: it was reported as // cleaned, never deleted, and never reconsidered, so local files accumulated // forever whenever RetainHours was set. func TestCleanupRetainsThenRemoves(t *testing.T) { dir := t.TempDir() cs := NewCleanupService(CleanupConfig{ Enabled: true, RetentionPeriod: time.Hour, BatchSize: 10, CheckInterval: time.Second, }) fresh := writeAgedFile(t, dir, "fresh.ts", time.Minute) old := writeAgedFile(t, dir, "old.ts", 2*time.Hour) for _, p := range []string{fresh, old} { if err := cs.ScheduleCleanup(p); err != nil { t.Fatalf("ScheduleCleanup failed: %v", err) } } if err := cs.ExecuteCleanup(context.Background()); err != nil { t.Fatalf("ExecuteCleanup failed: %v", err) } if _, err := os.Stat(old); !os.IsNotExist(err) { t.Errorf("a file past its retention period should be removed, stat err = %v", err) } if _, err := os.Stat(fresh); err != nil { t.Errorf("a file inside its retention period should survive: %v", err) } if got := cs.GetPendingCount(); got != 1 { t.Fatalf("the retained file must stay queued for a later pass, pending = %d", got) } // Once it ages past the window, the next pass must actually remove it. past := time.Now().Add(-2 * time.Hour) if err := os.Chtimes(fresh, past, past); err != nil { t.Fatalf("failed to age file: %v", err) } if err := cs.ExecuteCleanup(context.Background()); err != nil { t.Fatalf("ExecuteCleanup failed: %v", err) } if _, err := os.Stat(fresh); !os.IsNotExist(err) { t.Errorf("file should have been removed once past retention, stat err = %v", err) } if got := cs.GetPendingCount(); got != 0 { t.Errorf("pending count = %d, want 0", got) } } // TestForceCleanupAllTerminatesWithRetainedFiles guards shutdown: retained // files are requeued by design, so a loop demanding an empty list would spin // until the shutdown timeout expired. func TestForceCleanupAllTerminatesWithRetainedFiles(t *testing.T) { dir := t.TempDir() cs := NewCleanupService(CleanupConfig{ Enabled: true, RetentionPeriod: time.Hour, BatchSize: 10, CheckInterval: time.Second, }) if err := cs.ScheduleCleanup(writeAgedFile(t, dir, "fresh.ts", time.Minute)); err != nil { t.Fatalf("ScheduleCleanup failed: %v", err) } done := make(chan error, 1) go func() { done <- cs.ForceCleanupAll(context.Background()) }() select { case err := <-done: if err != nil { t.Fatalf("ForceCleanupAll failed: %v", err) } case <-time.After(3 * time.Second): t.Fatal("ForceCleanupAll did not terminate with a retained file pending") } if got := cs.GetPendingCount(); got != 1 { t.Errorf("the retained file should still be pending, got %d", got) } } func TestCleanupMissingFileIsNotAnError(t *testing.T) { cs := NewCleanupService(CleanupConfig{Enabled: true, BatchSize: 10, CheckInterval: time.Second}) // Already gone — a file transferred and removed by something else. if err := cs.ScheduleCleanup(filepath.Join(t.TempDir(), "gone.ts")); err != nil { t.Fatalf("ScheduleCleanup failed: %v", err) } if err := cs.ExecuteCleanup(context.Background()); err != nil { t.Fatalf("ExecuteCleanup failed: %v", err) } if got := cs.GetPendingCount(); got != 0 { t.Errorf("a missing file should not be requeued, pending = %d", got) } } func TestCleanupDisabledSchedulesNothing(t *testing.T) { dir := t.TempDir() cs := NewCleanupService(CleanupConfig{Enabled: false, BatchSize: 10, CheckInterval: time.Second}) path := writeAgedFile(t, dir, "seg.ts", time.Hour) if err := cs.ScheduleCleanup(path); err != nil { t.Fatalf("ScheduleCleanup failed: %v", err) } if got := cs.GetPendingCount(); got != 0 { t.Errorf("cleanup is disabled, nothing should be queued, got %d", got) } if err := cs.ExecuteCleanup(context.Background()); err != nil { t.Fatalf("ExecuteCleanup failed: %v", err) } if _, err := os.Stat(path); err != nil { t.Errorf("file must survive when cleanup is disabled: %v", err) } } // TestCleanupRequeuesOnCancellation covers shutdown arriving mid-batch: the // files already pulled off the pending list must go back, not vanish. func TestCleanupRequeuesOnCancellation(t *testing.T) { dir := t.TempDir() cs := NewCleanupService(CleanupConfig{Enabled: true, BatchSize: 10, CheckInterval: time.Second}) for i := 0; i < 5; i++ { path := writeAgedFile(t, dir, fmt.Sprintf("seg_%d.ts", i), 0) if err := cs.ScheduleCleanup(path); err != nil { t.Fatalf("ScheduleCleanup failed: %v", err) } } ctx, cancel := context.WithCancel(context.Background()) cancel() if err := cs.ExecuteCleanup(ctx); err == nil { t.Fatal("expected the cancelled context to be reported") } if got := cs.GetPendingCount(); got != 5 { t.Errorf("all files should be requeued after cancellation, pending = %d, want 5", got) } }