download_test.go
| 1 | package repository |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "vidarchive/internal/models" |
| 7 | ) |
| 8 | |
| 9 | // MarkStarted must claim a download exactly once: the worker pool's Submit and |
| 10 | // its 2s queue checker can both enqueue the same queued download, and only one |
| 11 | // worker may actually process it. |
| 12 | func TestMarkStartedClaimsOnce(t *testing.T) { |
| 13 | db := setupTestDB(t) |
| 14 | defer db.Close() |
| 15 | |
| 16 | repo := NewDownloadRepository(db) |
| 17 | d := &models.Download{URL: "https://example.com/v", Status: "queued"} |
| 18 | if err := repo.Create(d); err != nil { |
| 19 | t.Fatalf("create: %v", err) |
| 20 | } |
| 21 | |
| 22 | claimed, err := repo.MarkStarted(d.ID) |
| 23 | if err != nil { |
| 24 | t.Fatalf("first MarkStarted: %v", err) |
| 25 | } |
| 26 | if !claimed { |
| 27 | t.Fatal("first MarkStarted should claim the queued download") |
| 28 | } |
| 29 | |
| 30 | claimed, err = repo.MarkStarted(d.ID) |
| 31 | if err != nil { |
| 32 | t.Fatalf("second MarkStarted: %v", err) |
| 33 | } |
| 34 | if claimed { |
| 35 | t.Error("second MarkStarted should not re-claim a download that is already downloading") |
| 36 | } |
| 37 | } |
| 38 |