@@ -26,6 +26,18 @@ func seedTask(t *testing.T, repos *repository.Repositories, taskType model.TaskT
2626 return task
2727}
2828
29+ func assertTaskIDs (t * testing.T , tasks []model.Task , want ... int64 ) {
30+ t .Helper ()
31+ if len (tasks ) != len (want ) {
32+ t .Fatalf ("task count = %d, want %d; tasks=%#v" , len (tasks ), len (want ), tasks )
33+ }
34+ for i , task := range tasks {
35+ if task .ID != want [i ] {
36+ t .Fatalf ("task[%d].ID = %d, want %d; tasks=%#v" , i , task .ID , want [i ], tasks )
37+ }
38+ }
39+ }
40+
2941func TestTaskRepo_ClaimReady (t * testing.T ) {
3042 db := testDB (t )
3143 repos := repository .NewRepositories (db )
@@ -470,7 +482,7 @@ func TestTaskRepo_Complete_NotRunning(t *testing.T) {
470482 }
471483}
472484
473- func TestTaskRepo_Fail (t * testing.T ) {
485+ func TestTaskRepo_FailRunning (t * testing.T ) {
474486 db := testDB (t )
475487 repos := repository .NewRepositories (db )
476488 ctx := context .Background ()
@@ -495,6 +507,21 @@ func TestTaskRepo_Fail(t *testing.T) {
495507 if task .CompletedAt == nil {
496508 t .Error ("expected completed_at to be set" )
497509 }
510+ if task .ClaimedAt != nil || task .LeaseUntil != nil || task .StartedAt != nil {
511+ t .Fatalf ("failed task lease fields = claimed:%v lease:%v started:%v, want cleared" , task .ClaimedAt , task .LeaseUntil , task .StartedAt )
512+ }
513+
514+ queued := seedTask (t , repos , model .TaskTypeUpload )
515+ now := time .Now ()
516+ leaseUntil := now .Add (5 * time .Minute )
517+ mustExec (t , db , `UPDATE tasks SET claimed_at = ?, lease_until = ?, started_at = ? WHERE id = ?` , now , leaseUntil , now , queued .ID )
518+ queued .ClaimedAt = & now
519+ queued .LeaseUntil = & leaseUntil
520+ queued .StartedAt = & now
521+ err := repos .Tasks .FailRunning (ctx , queued , "should fail" )
522+ if err == nil {
523+ t .Fatal ("expected error failing queued task" )
524+ }
498525}
499526
500527func TestTaskRepo_ReleaseExpiredLeases (t * testing.T ) {
@@ -856,15 +883,20 @@ func TestTaskRepo_List(t *testing.T) {
856883 }
857884
858885 // Seed tasks: 2 upload (queued), 1 evict_cache (queued).
859- seedTask (t , repos , model .TaskTypeUpload )
860- seedTask (t , repos , model .TaskTypeUpload )
861- seedTask (t , repos , model .TaskTypeEvictCache )
886+ firstUpload := seedTask (t , repos , model .TaskTypeUpload )
887+ secondUpload := seedTask (t , repos , model .TaskTypeUpload )
888+ evict := seedTask (t , repos , model .TaskTypeEvictCache )
862889
863890 // Claim one upload task to make it running.
864891 claimed , _ := repos .Tasks .ClaimReady (ctx , model .TaskTypeUpload , 5 * time .Minute )
865892 if claimed == nil {
866893 t .Fatal ("setup: could not claim task" )
867894 }
895+ runningUploadID := claimed .ID
896+ queuedUploadID := firstUpload .ID
897+ if runningUploadID == firstUpload .ID {
898+ queuedUploadID = secondUpload .ID
899+ }
868900
869901 // List all — should return 3.
870902 tasks , total , err = repos .Tasks .List (ctx , "" , "" , "" , 10 , 0 )
@@ -877,24 +909,27 @@ func TestTaskRepo_List(t *testing.T) {
877909 if len (tasks ) != 3 {
878910 t .Errorf ("expected 3 tasks, got %d" , len (tasks ))
879911 }
912+ assertTaskIDs (t , tasks , evict .ID , secondUpload .ID , firstUpload .ID )
880913
881914 // Filter by type.
882- _ , total , err = repos .Tasks .List (ctx , string (model .TaskTypeUpload ), "" , "" , 10 , 0 )
915+ tasks , total , err = repos .Tasks .List (ctx , string (model .TaskTypeUpload ), "" , "" , 10 , 0 )
883916 if err != nil {
884917 t .Fatalf ("List by type: %v" , err )
885918 }
886919 if total != 2 {
887920 t .Errorf ("expected 2 upload, got %d" , total )
888921 }
922+ assertTaskIDs (t , tasks , secondUpload .ID , firstUpload .ID )
889923
890924 // Filter by status.
891- _ , total , err = repos .Tasks .List (ctx , "" , "" , string (model .TaskStatusQueued ), 10 , 0 )
925+ tasks , total , err = repos .Tasks .List (ctx , "" , "" , string (model .TaskStatusQueued ), 10 , 0 )
892926 if err != nil {
893927 t .Fatalf ("List by status: %v" , err )
894928 }
895929 if total != 2 {
896930 t .Errorf ("expected 2 queued, got %d" , total )
897931 }
932+ assertTaskIDs (t , tasks , evict .ID , queuedUploadID )
898933
899934 // Filter by type + status.
900935 tasks , total , err = repos .Tasks .List (ctx , string (model .TaskTypeUpload ), "" , string (model .TaskStatusRunning ), 10 , 0 )
@@ -907,6 +942,7 @@ func TestTaskRepo_List(t *testing.T) {
907942 if len (tasks ) != 1 {
908943 t .Errorf ("expected 1 task, got %d" , len (tasks ))
909944 }
945+ assertTaskIDs (t , tasks , runningUploadID )
910946
911947 // Pagination: limit 2, offset 0.
912948 tasks , total , err = repos .Tasks .List (ctx , "" , "" , "" , 2 , 0 )
@@ -919,6 +955,7 @@ func TestTaskRepo_List(t *testing.T) {
919955 if len (tasks ) != 2 {
920956 t .Errorf ("expected 2 tasks with limit=2, got %d" , len (tasks ))
921957 }
958+ assertTaskIDs (t , tasks , evict .ID , secondUpload .ID )
922959
923960 // Pagination: limit 2, offset 2 — should return 1.
924961 tasks , total , err = repos .Tasks .List (ctx , "" , "" , "" , 2 , 2 )
@@ -931,6 +968,7 @@ func TestTaskRepo_List(t *testing.T) {
931968 if len (tasks ) != 1 {
932969 t .Errorf ("expected 1 task at offset 2, got %d" , len (tasks ))
933970 }
971+ assertTaskIDs (t , tasks , firstUpload .ID )
934972}
935973
936974func TestTaskRepo_ListFiltersByStage (t * testing.T ) {
0 commit comments