|
| 1 | +#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] |
| 2 | + |
| 3 | +mod common; |
| 4 | + |
| 5 | +use common::tasks::{EchoParams, EchoTask}; |
| 6 | +use durable::{Durable, DurableBuilder, MIGRATOR, WorkerOptions}; |
| 7 | +use sqlx::{AssertSqlSafe, PgPool}; |
| 8 | +use std::time::Duration; |
| 9 | + |
| 10 | +/// Helper to create a DurableBuilder from the test pool. |
| 11 | +fn create_client(pool: PgPool, queue_name: &str) -> DurableBuilder { |
| 12 | + Durable::builder().pool(pool).queue_name(queue_name) |
| 13 | +} |
| 14 | + |
| 15 | +// ============================================================================ |
| 16 | +// count_unclaimed_ready_tasks Tests |
| 17 | +// ============================================================================ |
| 18 | + |
| 19 | +#[sqlx::test(migrator = "MIGRATOR")] |
| 20 | +async fn test_count_unclaimed_empty_queue(pool: PgPool) -> sqlx::Result<()> { |
| 21 | + let client = create_client(pool.clone(), "count_empty") |
| 22 | + .register::<EchoTask>() |
| 23 | + .unwrap() |
| 24 | + .build() |
| 25 | + .await |
| 26 | + .unwrap(); |
| 27 | + client.create_queue(None).await.unwrap(); |
| 28 | + |
| 29 | + let count = client |
| 30 | + .count_unclaimed_ready_tasks(None) |
| 31 | + .await |
| 32 | + .expect("Failed to count unclaimed tasks"); |
| 33 | + assert_eq!(count, 0, "Empty queue should have 0 unclaimed tasks"); |
| 34 | + |
| 35 | + Ok(()) |
| 36 | +} |
| 37 | + |
| 38 | +#[sqlx::test(migrator = "MIGRATOR")] |
| 39 | +async fn test_count_unclaimed_after_spawning(pool: PgPool) -> sqlx::Result<()> { |
| 40 | + let client = create_client(pool.clone(), "count_spawn") |
| 41 | + .register::<EchoTask>() |
| 42 | + .unwrap() |
| 43 | + .build() |
| 44 | + .await |
| 45 | + .unwrap(); |
| 46 | + client.create_queue(None).await.unwrap(); |
| 47 | + |
| 48 | + // Spawn 3 tasks |
| 49 | + for i in 0..3 { |
| 50 | + client |
| 51 | + .spawn::<EchoTask>(EchoParams { |
| 52 | + message: format!("task {i}"), |
| 53 | + }) |
| 54 | + .await |
| 55 | + .expect("Failed to spawn task"); |
| 56 | + } |
| 57 | + |
| 58 | + let count = client |
| 59 | + .count_unclaimed_ready_tasks(None) |
| 60 | + .await |
| 61 | + .expect("Failed to count unclaimed tasks"); |
| 62 | + assert_eq!(count, 3, "Should have 3 unclaimed tasks after spawning 3"); |
| 63 | + |
| 64 | + Ok(()) |
| 65 | +} |
| 66 | + |
| 67 | +#[sqlx::test(migrator = "MIGRATOR")] |
| 68 | +async fn test_count_unclaimed_decreases_after_claim(pool: PgPool) -> sqlx::Result<()> { |
| 69 | + let client = create_client(pool.clone(), "count_claim") |
| 70 | + .register::<EchoTask>() |
| 71 | + .unwrap() |
| 72 | + .build() |
| 73 | + .await |
| 74 | + .unwrap(); |
| 75 | + client.create_queue(None).await.unwrap(); |
| 76 | + |
| 77 | + // Spawn 3 tasks |
| 78 | + for i in 0..3 { |
| 79 | + client |
| 80 | + .spawn::<EchoTask>(EchoParams { |
| 81 | + message: format!("task {i}"), |
| 82 | + }) |
| 83 | + .await |
| 84 | + .expect("Failed to spawn task"); |
| 85 | + } |
| 86 | + |
| 87 | + assert_eq!( |
| 88 | + client.count_unclaimed_ready_tasks(None).await.unwrap(), |
| 89 | + 3, |
| 90 | + "Should start with 3 unclaimed" |
| 91 | + ); |
| 92 | + |
| 93 | + // Start a worker that will claim and complete tasks |
| 94 | + let worker = client |
| 95 | + .start_worker(WorkerOptions { |
| 96 | + concurrency: 3, |
| 97 | + poll_interval: Duration::from_millis(100), |
| 98 | + ..Default::default() |
| 99 | + }) |
| 100 | + .await |
| 101 | + .unwrap(); |
| 102 | + |
| 103 | + // Wait for the worker to process all tasks |
| 104 | + tokio::time::sleep(Duration::from_secs(2)).await; |
| 105 | + |
| 106 | + let count = client |
| 107 | + .count_unclaimed_ready_tasks(None) |
| 108 | + .await |
| 109 | + .expect("Failed to count unclaimed tasks"); |
| 110 | + assert_eq!( |
| 111 | + count, 0, |
| 112 | + "Should have 0 unclaimed tasks after worker claims them" |
| 113 | + ); |
| 114 | + |
| 115 | + worker.shutdown().await; |
| 116 | + |
| 117 | + Ok(()) |
| 118 | +} |
| 119 | + |
| 120 | +#[sqlx::test(migrator = "MIGRATOR")] |
| 121 | +async fn test_count_unclaimed_with_explicit_queue_name(pool: PgPool) -> sqlx::Result<()> { |
| 122 | + let client = create_client(pool.clone(), "default") |
| 123 | + .register::<EchoTask>() |
| 124 | + .unwrap() |
| 125 | + .build() |
| 126 | + .await |
| 127 | + .unwrap(); |
| 128 | + |
| 129 | + // Create two queues |
| 130 | + client.create_queue(Some("queue_a")).await.unwrap(); |
| 131 | + client.create_queue(Some("queue_b")).await.unwrap(); |
| 132 | + |
| 133 | + // Spawn tasks into queue_a |
| 134 | + let client_a = create_client(pool.clone(), "queue_a") |
| 135 | + .register::<EchoTask>() |
| 136 | + .unwrap() |
| 137 | + .build() |
| 138 | + .await |
| 139 | + .unwrap(); |
| 140 | + for i in 0..2 { |
| 141 | + client_a |
| 142 | + .spawn::<EchoTask>(EchoParams { |
| 143 | + message: format!("a-{i}"), |
| 144 | + }) |
| 145 | + .await |
| 146 | + .unwrap(); |
| 147 | + } |
| 148 | + |
| 149 | + // Spawn tasks into queue_b |
| 150 | + let client_b = create_client(pool.clone(), "queue_b") |
| 151 | + .register::<EchoTask>() |
| 152 | + .unwrap() |
| 153 | + .build() |
| 154 | + .await |
| 155 | + .unwrap(); |
| 156 | + for i in 0..5 { |
| 157 | + client_b |
| 158 | + .spawn::<EchoTask>(EchoParams { |
| 159 | + message: format!("b-{i}"), |
| 160 | + }) |
| 161 | + .await |
| 162 | + .unwrap(); |
| 163 | + } |
| 164 | + |
| 165 | + // Count using explicit queue names |
| 166 | + let count_a = client |
| 167 | + .count_unclaimed_ready_tasks(Some("queue_a")) |
| 168 | + .await |
| 169 | + .unwrap(); |
| 170 | + let count_b = client |
| 171 | + .count_unclaimed_ready_tasks(Some("queue_b")) |
| 172 | + .await |
| 173 | + .unwrap(); |
| 174 | + |
| 175 | + assert_eq!(count_a, 2, "queue_a should have 2 unclaimed tasks"); |
| 176 | + assert_eq!(count_b, 5, "queue_b should have 5 unclaimed tasks"); |
| 177 | + |
| 178 | + Ok(()) |
| 179 | +} |
| 180 | + |
| 181 | +#[sqlx::test(migrator = "MIGRATOR")] |
| 182 | +async fn test_count_unclaimed_excludes_future_tasks(pool: PgPool) -> sqlx::Result<()> { |
| 183 | + let client = create_client(pool.clone(), "count_future") |
| 184 | + .register::<EchoTask>() |
| 185 | + .unwrap() |
| 186 | + .build() |
| 187 | + .await |
| 188 | + .unwrap(); |
| 189 | + client.create_queue(None).await.unwrap(); |
| 190 | + |
| 191 | + // Spawn two tasks (both ready now) |
| 192 | + client |
| 193 | + .spawn::<EchoTask>(EchoParams { |
| 194 | + message: "ready now".to_string(), |
| 195 | + }) |
| 196 | + .await |
| 197 | + .unwrap(); |
| 198 | + let delayed = client |
| 199 | + .spawn::<EchoTask>(EchoParams { |
| 200 | + message: "will be delayed".to_string(), |
| 201 | + }) |
| 202 | + .await |
| 203 | + .unwrap(); |
| 204 | + |
| 205 | + // Push one run's available_at into the future via direct SQL |
| 206 | + sqlx::query(AssertSqlSafe( |
| 207 | + "UPDATE durable.r_count_future SET available_at = now() + interval '1 hour' WHERE task_id = $1".to_string() |
| 208 | + )) |
| 209 | + .bind(delayed.task_id) |
| 210 | + .execute(&pool) |
| 211 | + .await?; |
| 212 | + |
| 213 | + let count = client |
| 214 | + .count_unclaimed_ready_tasks(None) |
| 215 | + .await |
| 216 | + .expect("Failed to count unclaimed tasks"); |
| 217 | + assert_eq!( |
| 218 | + count, 1, |
| 219 | + "Should only count the immediately-ready task, not the delayed one" |
| 220 | + ); |
| 221 | + |
| 222 | + Ok(()) |
| 223 | +} |
0 commit comments