|
| 1 | +package queue_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/redis/go-redis/v9" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "github.com/tinh-tinh/queue/v2" |
| 11 | +) |
| 12 | + |
| 13 | +func Test_GetFailedJobs(t *testing.T) { |
| 14 | + failedQueue := queue.New("failed_jobs_test", &queue.Options{ |
| 15 | + Connect: &redis.Options{ |
| 16 | + Addr: "localhost:6379", |
| 17 | + Password: "", |
| 18 | + DB: 0, |
| 19 | + }, |
| 20 | + Workers: 3, |
| 21 | + RetryFailures: 0, // No retries, so jobs fail immediately |
| 22 | + }) |
| 23 | + |
| 24 | + // Clear any existing failed jobs first |
| 25 | + err := failedQueue.ClearFailedJobs() |
| 26 | + require.Nil(t, err) |
| 27 | + |
| 28 | + failedQueue.Process(func(job *queue.Job) { |
| 29 | + job.Process(func() error { |
| 30 | + // All jobs will fail |
| 31 | + return errors.New("intentional failure for test") |
| 32 | + }) |
| 33 | + }) |
| 34 | + |
| 35 | + // Add multiple jobs that will fail |
| 36 | + failedQueue.AddJob(queue.AddJobOptions{ |
| 37 | + Id: "fail1", |
| 38 | + Data: "value 1", |
| 39 | + }) |
| 40 | + failedQueue.AddJob(queue.AddJobOptions{ |
| 41 | + Id: "fail2", |
| 42 | + Data: "value 2", |
| 43 | + }) |
| 44 | + failedQueue.AddJob(queue.AddJobOptions{ |
| 45 | + Id: "fail3", |
| 46 | + Data: "value 3", |
| 47 | + }) |
| 48 | + |
| 49 | + // Wait a bit for jobs to be processed |
| 50 | + time.Sleep(500 * time.Millisecond) |
| 51 | + |
| 52 | + // Retrieve failed jobs |
| 53 | + failedJobs, err := failedQueue.GetFailedJobs() |
| 54 | + require.Nil(t, err) |
| 55 | + require.Equal(t, 3, len(failedJobs)) |
| 56 | + |
| 57 | + // Verify job IDs are present |
| 58 | + jobIds := make(map[string]bool) |
| 59 | + for _, job := range failedJobs { |
| 60 | + jobIds[job.Id] = true |
| 61 | + require.NotEmpty(t, job.FailedReason) |
| 62 | + require.Contains(t, job.FailedReason, "intentional failure for test") |
| 63 | + require.Equal(t, queue.FailedStatus, job.Status) |
| 64 | + } |
| 65 | + require.True(t, jobIds["fail1"]) |
| 66 | + require.True(t, jobIds["fail2"]) |
| 67 | + require.True(t, jobIds["fail3"]) |
| 68 | + |
| 69 | + // Clean up |
| 70 | + err = failedQueue.ClearFailedJobs() |
| 71 | + require.Nil(t, err) |
| 72 | +} |
| 73 | + |
| 74 | +func Test_GetFailedJob(t *testing.T) { |
| 75 | + singleFailQueue := queue.New("single_fail_test", &queue.Options{ |
| 76 | + Connect: &redis.Options{ |
| 77 | + Addr: "localhost:6379", |
| 78 | + Password: "", |
| 79 | + DB: 0, |
| 80 | + }, |
| 81 | + Workers: 3, |
| 82 | + RetryFailures: 0, |
| 83 | + }) |
| 84 | + |
| 85 | + // Clear any existing failed jobs first |
| 86 | + err := singleFailQueue.ClearFailedJobs() |
| 87 | + require.Nil(t, err) |
| 88 | + |
| 89 | + singleFailQueue.Process(func(job *queue.Job) { |
| 90 | + job.Process(func() error { |
| 91 | + return errors.New("specific error for job " + job.Id) |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + // Add a job that will fail |
| 96 | + singleFailQueue.AddJob(queue.AddJobOptions{ |
| 97 | + Id: "specific_fail", |
| 98 | + Data: "test data", |
| 99 | + }) |
| 100 | + |
| 101 | + // Wait for job to be processed |
| 102 | + time.Sleep(500 * time.Millisecond) |
| 103 | + |
| 104 | + // Retrieve the specific failed job |
| 105 | + reason, err := singleFailQueue.GetFailedJob("specific_fail") |
| 106 | + require.Nil(t, err) |
| 107 | + require.Contains(t, reason, "specific error for job specific_fail") |
| 108 | + |
| 109 | + // Try to get a non-existent failed job |
| 110 | + _, err = singleFailQueue.GetFailedJob("non_existent") |
| 111 | + require.NotNil(t, err) |
| 112 | + require.Contains(t, err.Error(), "not found") |
| 113 | + |
| 114 | + // Clean up |
| 115 | + err = singleFailQueue.ClearFailedJobs() |
| 116 | + require.Nil(t, err) |
| 117 | +} |
| 118 | + |
| 119 | +func Test_ClearFailedJobs(t *testing.T) { |
| 120 | + clearQueue := queue.New("clear_test", &queue.Options{ |
| 121 | + Connect: &redis.Options{ |
| 122 | + Addr: "localhost:6379", |
| 123 | + Password: "", |
| 124 | + DB: 0, |
| 125 | + }, |
| 126 | + Workers: 3, |
| 127 | + RetryFailures: 0, |
| 128 | + }) |
| 129 | + |
| 130 | + // Clear any existing failed jobs first |
| 131 | + err := clearQueue.ClearFailedJobs() |
| 132 | + require.Nil(t, err) |
| 133 | + |
| 134 | + clearQueue.Process(func(job *queue.Job) { |
| 135 | + job.Process(func() error { |
| 136 | + return errors.New("error for clearing test") |
| 137 | + }) |
| 138 | + }) |
| 139 | + |
| 140 | + // Add multiple jobs that will fail |
| 141 | + clearQueue.BulkAddJob([]queue.AddJobOptions{ |
| 142 | + {Id: "clear1", Data: "value 1"}, |
| 143 | + {Id: "clear2", Data: "value 2"}, |
| 144 | + {Id: "clear3", Data: "value 3"}, |
| 145 | + {Id: "clear4", Data: "value 4"}, |
| 146 | + {Id: "clear5", Data: "value 5"}, |
| 147 | + }) |
| 148 | + |
| 149 | + // Wait for jobs to be processed |
| 150 | + time.Sleep(500 * time.Millisecond) |
| 151 | + |
| 152 | + // Verify failed jobs exist |
| 153 | + failedJobs, err := clearQueue.GetFailedJobs() |
| 154 | + require.Nil(t, err) |
| 155 | + require.Equal(t, 5, len(failedJobs)) |
| 156 | + |
| 157 | + // Clear all failed jobs |
| 158 | + err = clearQueue.ClearFailedJobs() |
| 159 | + require.Nil(t, err) |
| 160 | + |
| 161 | + // Verify all failed jobs are cleared |
| 162 | + failedJobs, err = clearQueue.GetFailedJobs() |
| 163 | + require.Nil(t, err) |
| 164 | + require.Equal(t, 0, len(failedJobs)) |
| 165 | + |
| 166 | + // Clearing again should not cause an error |
| 167 | + err = clearQueue.ClearFailedJobs() |
| 168 | + require.Nil(t, err) |
| 169 | +} |
| 170 | + |
| 171 | +func Test_GetFailedJobs_RedisError(t *testing.T) { |
| 172 | + // Create a queue with invalid Redis connection |
| 173 | + invalidQueue := queue.New("redis_error_test", &queue.Options{ |
| 174 | + Connect: &redis.Options{ |
| 175 | + Addr: "localhost:9999", // Invalid port |
| 176 | + Password: "", |
| 177 | + DB: 0, |
| 178 | + }, |
| 179 | + Workers: 3, |
| 180 | + RetryFailures: 0, |
| 181 | + }) |
| 182 | + |
| 183 | + // Attempt to get failed jobs should return an error |
| 184 | + // This tests that SCAN errors are propagated |
| 185 | + _, err := invalidQueue.GetFailedJobs() |
| 186 | + require.NotNil(t, err) |
| 187 | + require.Contains(t, err.Error(), "failed to scan Redis keys") |
| 188 | +} |
| 189 | + |
| 190 | +func Test_ClearFailedJobs_RedisError(t *testing.T) { |
| 191 | + // Create a queue with invalid Redis connection |
| 192 | + invalidQueue := queue.New("redis_clear_error_test", &queue.Options{ |
| 193 | + Connect: &redis.Options{ |
| 194 | + Addr: "localhost:9999", // Invalid port |
| 195 | + Password: "", |
| 196 | + DB: 0, |
| 197 | + }, |
| 198 | + Workers: 3, |
| 199 | + RetryFailures: 0, |
| 200 | + }) |
| 201 | + |
| 202 | + // Attempt to clear failed jobs should return an error |
| 203 | + // This tests that SCAN errors are propagated |
| 204 | + err := invalidQueue.ClearFailedJobs() |
| 205 | + require.NotNil(t, err) |
| 206 | + require.Contains(t, err.Error(), "failed to scan Redis keys") |
| 207 | +} |
0 commit comments