Files
PiCloud/crates/manager-core/tests/migration_queue_messages.rs
MechaCat02 328d7d55e6 test(v1.1.9): queue_e2e + invoke_e2e + retry_e2e + migration test
Four new test binaries, DB-gated via the established pool_or_skip()
pattern (returns Some(pool) when DATABASE_URL is set; prints a skip
notice and returns None otherwise — so plain `cargo test` stays green
locally while CI runs them).

crates/picloud/tests/queue_e2e.rs (4 tests):
  - queue_receive_acks_on_success: enqueue → consumer fires → KV
    marker observable → queue row deleted (ack worked)
  - queue_receive_dead_letters_after_max_attempts: throwing handler
    retries 3× via the dispatcher's compute_backoff path → dead_letters
    row written, queue row deleted
  - queue_one_consumer_per_queue_rejected: second trigger create for the
    same (app_id, queue_name) returns 4xx with the documented "already
    has a consumer trigger" message (advisory-lock guard works)
  - queue_visibility_timeout_reclaim: insert message with stale claim
    (claimed_at = NOW() - 60s, visibility_timeout = 5s); either the
    dispatcher re-claims after reclaim runs, or the claim clears

crates/picloud/tests/invoke_e2e.rs (4 tests):
  - invoke_by_name_same_app_returns_value: callee returns body.x+1;
    caller invokes by name and writes the result to KV (42 round-trips)
  - invoke_cross_app_rejects: callee in app_B, caller in app_A; error
    string contains "different app" or "CrossApp"
  - invoke_depth_limit_exceeds_cleanly: recursive script throws with
    "depth" in the message at the bound (Limits::trigger_depth_max=8)
  - invoke_async_enqueues_outbox_row: invoke_async returns execution_id,
    dispatcher fires the OutboxSourceKind::Invoke arm, callee marker
    appears

crates/picloud/tests/retry_e2e.rs (3 tests):
  - retry_run_eventually_succeeds_inside_http_handler: counter mutates
    across 3 retries through a real HTTP route, returns 3
  - retry_run_surfaces_last_error_after_max_attempts: max_attempts=2,
    always throws — caller's try/catch surfaces "boom"
  - retry_on_codes_filters_unmatched_errors: counter == 1 after a
    throw NOT in the codes list (immediate surface, no retry)

crates/manager-core/tests/migration_queue_messages.rs (4 tests):
  - queue_messages_table_exists_with_expected_columns: shape +
    nullability of every column
  - queue_trigger_details_table_exists: shape
  - queue_widens_trigger_kind_and_outbox_source_kind: triggers.kind
    CHECK admits 'queue'; outbox.source_kind admits 'invoke'
  - queue_messages_dispatch_index_is_partial: idx_queue_messages_dispatch
    has WHERE claim_token IS NULL (guards against future migrations
    accidentally dropping the partial)

All 15 tests pass clean skip when DATABASE_URL is unset. Workspace unit
suite remains at 432 passing (306 manager-core + 74 executor-core + 34
shared + 18 orchestrator-core).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-07 10:25:22 +02:00

151 lines
5.0 KiB
Rust

//! v1.1.9 migration smoke test: applies all manager-core migrations to
//! `DATABASE_URL` and verifies the `queue_messages` + `queue_trigger_details`
//! shape (table existence, columns, partial indexes). Skips cleanly when
//! `DATABASE_URL` is unset.
//!
//! Heavier schema assertions live in the schema_snapshot golden — this
//! test is a quick gate for the migration itself + an easy place to
//! exercise the partial-index conditions.
#![allow(clippy::needless_pass_by_value)]
use sqlx::postgres::PgPoolOptions;
use sqlx::PgPool;
async fn pool_or_skip() -> Option<PgPool> {
let Ok(url) = std::env::var("DATABASE_URL") else {
eprintln!("migration_queue_messages: DATABASE_URL unset — skipping");
return None;
};
let pool = PgPoolOptions::new()
.max_connections(2)
.connect(&url)
.await
.expect("connect to DATABASE_URL");
sqlx::migrate!("./migrations")
.run(&pool)
.await
.expect("apply migrations");
Some(pool)
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn queue_messages_table_exists_with_expected_columns() {
let Some(pool) = pool_or_skip().await else { return };
let rows: Vec<(String, String, String)> = sqlx::query_as(
"SELECT column_name, data_type, is_nullable \
FROM information_schema.columns \
WHERE table_name = 'queue_messages' \
ORDER BY ordinal_position",
)
.fetch_all(&pool)
.await
.expect("column read");
let names: Vec<&str> = rows.iter().map(|(n, _, _)| n.as_str()).collect();
for required in [
"id",
"app_id",
"queue_name",
"payload",
"enqueued_at",
"deliver_after",
"claim_token",
"claimed_at",
"attempt",
"max_attempts",
"enqueued_by_principal",
] {
assert!(
names.contains(&required),
"queue_messages should have column {required}, columns are {names:?}"
);
}
// Verify nullability on the right columns.
let nullable: std::collections::HashMap<String, String> = rows
.into_iter()
.map(|(n, _, nl)| (n, nl))
.collect();
assert_eq!(nullable["app_id"], "NO");
assert_eq!(nullable["queue_name"], "NO");
assert_eq!(nullable["payload"], "NO");
assert_eq!(nullable["claim_token"], "YES");
assert_eq!(nullable["deliver_after"], "YES");
assert_eq!(nullable["enqueued_by_principal"], "YES");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn queue_trigger_details_table_exists() {
let Some(pool) = pool_or_skip().await else { return };
let rows: Vec<(String,)> = sqlx::query_as(
"SELECT column_name FROM information_schema.columns \
WHERE table_name = 'queue_trigger_details' \
ORDER BY ordinal_position",
)
.fetch_all(&pool)
.await
.expect("read");
let names: Vec<&str> = rows.iter().map(|(n,)| n.as_str()).collect();
for required in ["trigger_id", "queue_name", "visibility_timeout_secs", "last_fired_at"] {
assert!(
names.contains(&required),
"queue_trigger_details should have {required}, columns are {names:?}"
);
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn queue_widens_trigger_kind_and_outbox_source_kind() {
let Some(pool) = pool_or_skip().await else { return };
// The triggers.kind constraint must accept 'queue'.
sqlx::query(
"DO $$ BEGIN PERFORM 1 FROM pg_constraint WHERE conname = 'triggers_kind_check'; END $$;",
)
.execute(&pool)
.await
.expect("constraint exists");
let (def,): (String,) = sqlx::query_as(
"SELECT pg_get_constraintdef(oid) FROM pg_constraint \
WHERE conname = 'triggers_kind_check'",
)
.fetch_one(&pool)
.await
.expect("read constraint");
assert!(
def.contains("'queue'"),
"triggers_kind_check should admit 'queue', got: {def}"
);
let (outbox_def,): (String,) = sqlx::query_as(
"SELECT pg_get_constraintdef(oid) FROM pg_constraint \
WHERE conname = 'outbox_source_kind_check'",
)
.fetch_one(&pool)
.await
.expect("read constraint");
assert!(
outbox_def.contains("'invoke'"),
"outbox_source_kind_check should admit 'invoke', got: {outbox_def}"
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn queue_messages_dispatch_index_is_partial() {
let Some(pool) = pool_or_skip().await else { return };
let (defn,): (String,) = sqlx::query_as(
"SELECT indexdef FROM pg_indexes \
WHERE indexname = 'idx_queue_messages_dispatch'",
)
.fetch_one(&pool)
.await
.expect("read index");
// Partial WHERE (claim_token IS NULL) is the key feature; assert
// it's there so a future migration can't quietly drop it.
assert!(
defn.contains("claim_token IS NULL"),
"idx_queue_messages_dispatch should be partial on claim_token IS NULL, got: {defn}"
);
}