Skip to content

Commit

Permalink
slightly improve worker processing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcountryman committed Nov 13, 2024
1 parent 4701ab7 commit b44c2c7
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,12 +963,24 @@ mod tests {

// Enqueue a task.
let task = TestTask;
queue.enqueue(&pool, &task, &()).await?;
assert!(queue.dequeue().await?.is_some());
let task_id = queue.enqueue(&pool, &task, &()).await?;

// Process the task.
let worker = Worker::new(queue.clone(), task);
worker.process_next_task().await?;
let processed_task_id = worker
.process_next_task()
.await?
.expect("A task should be processed");
assert_eq!(task_id, processed_task_id);

// Check that the task was processed successfully.
let task_row = sqlx::query!(
r#"select state as "state: TaskState" from underway.task where id = $1"#,
task_id as TaskId
)
.fetch_one(&pool)
.await?;
assert_eq!(task_row.state, TaskState::Succeeded);

// Ensure the task is no longer available on the queue.
assert!(queue.dequeue().await?.is_none());
Expand All @@ -979,7 +991,7 @@ mod tests {
#[sqlx::test]
async fn process_retries(pool: PgPool) -> sqlx::Result<(), Error> {
let queue = Queue::builder()
.name("retry_test_queue")
.name("process_retries")
.pool(pool.clone())
.build()
.await?;
Expand All @@ -997,7 +1009,11 @@ mod tests {
for retries in 0..3 {
let delay = task.retry_policy().calculate_delay(retries);
tokio::time::sleep(delay.try_into()?).await;
worker.process_next_task().await?;
let processed_task_id = worker
.process_next_task()
.await?
.expect("A task should be processed");
assert_eq!(task_id, processed_task_id);
}

// Verify that the fail_times counter has reached zero
Expand All @@ -1011,7 +1027,7 @@ mod tests {
from underway.task
where id = $1
"#,
task_id as _
task_id as TaskId
)
.fetch_one(&pool)
.await?;
Expand Down

0 comments on commit b44c2c7

Please sign in to comment.