use crate::providers::{Provider, TagFilter};
use std::time::{Duration, Instant};
pub async fn test_short_poll_returns_immediately(provider: &dyn Provider, threshold: Duration) {
tracing::info!("→ Testing long polling: short poll returns immediately (threshold: {threshold:?})");
let lock_timeout = Duration::from_secs(30);
let poll_timeout = Duration::from_millis(500);
let start = Instant::now();
let result = provider
.fetch_orchestration_item(lock_timeout, poll_timeout, None)
.await
.expect("Fetch failed");
let elapsed = start.elapsed();
assert!(result.is_none(), "Queue should be empty");
assert!(
elapsed < threshold,
"Short polling provider should return immediately (elapsed: {elapsed:?}, threshold: {threshold:?})"
);
tracing::info!("✓ Short poll returned in {:?}", elapsed);
}
pub async fn test_long_poll_waits_for_timeout(provider: &dyn Provider) {
tracing::info!("→ Testing long polling: long poll waits for timeout");
let lock_timeout = Duration::from_secs(30);
let poll_timeout = Duration::from_millis(500);
let start = Instant::now();
let result = provider
.fetch_orchestration_item(lock_timeout, poll_timeout, None)
.await
.expect("Fetch failed");
let elapsed = start.elapsed();
assert!(result.is_none(), "Queue should be empty");
assert!(
elapsed >= poll_timeout,
"Long polling provider should wait for timeout (elapsed: {elapsed:?}, expected: {poll_timeout:?})"
);
tracing::info!("✓ Long poll waited {:?} (timeout was {:?})", elapsed, poll_timeout);
}
pub async fn test_fetch_respects_timeout_upper_bound(provider: &dyn Provider) {
tracing::info!("→ Testing long polling: fetch respects timeout upper bound");
let lock_timeout = Duration::from_secs(30);
let poll_timeout = Duration::from_millis(300);
let tolerance = Duration::from_millis(200);
let start = Instant::now();
let result = provider
.fetch_orchestration_item(lock_timeout, poll_timeout, None)
.await
.expect("Fetch failed");
let elapsed = start.elapsed();
assert!(result.is_none(), "Queue should be empty");
assert!(
elapsed < poll_timeout + tolerance,
"Provider blocked too long (elapsed: {:?}, max expected: {:?})",
elapsed,
poll_timeout + tolerance
);
tracing::info!("✓ Fetch completed in {:?} (within bounds)", elapsed);
}
pub async fn test_short_poll_work_item_returns_immediately(provider: &dyn Provider, threshold: Duration) {
tracing::info!("→ Testing long polling: short poll work item returns immediately (threshold: {threshold:?})");
let lock_timeout = Duration::from_secs(30);
let poll_timeout = Duration::from_millis(500);
let start = Instant::now();
let result = provider
.fetch_work_item(lock_timeout, poll_timeout, None, &TagFilter::default())
.await
.expect("Fetch failed");
let elapsed = start.elapsed();
assert!(result.is_none(), "Worker queue should be empty");
assert!(
elapsed < threshold,
"Short polling provider should return immediately (elapsed: {elapsed:?}, threshold: {threshold:?})"
);
tracing::info!("✓ Short poll work item returned in {:?}", elapsed);
}
pub async fn test_long_poll_work_item_waits_for_timeout(provider: &dyn Provider) {
tracing::info!("→ Testing long polling: long poll work item waits for timeout");
let lock_timeout = Duration::from_secs(30);
let poll_timeout = Duration::from_millis(500);
let start = Instant::now();
let result = provider
.fetch_work_item(lock_timeout, poll_timeout, None, &TagFilter::default())
.await
.expect("Fetch failed");
let elapsed = start.elapsed();
assert!(result.is_none(), "Worker queue should be empty");
assert!(
elapsed >= poll_timeout,
"Long polling provider should wait for timeout (elapsed: {elapsed:?}, expected: {poll_timeout:?})"
);
tracing::info!("✓ Long poll work item waited {:?}", elapsed);
}