//! PostgreSQL 特有行锁、失租和角色状态验收;测试库由外部 runner 独立建立。 use super::*; use dever_runtime::{auth, job}; #[test] #[ignore = "requires an isolated PostgreSQL database; run test/postgres.py"] fn postgres_uuid_columns_keep_values_and_null_contract() { use dever_runtime::value::uuid::Uuid; let url = std::env::var("DEVER_TEST_POSTGRES").expect("isolated PostgreSQL test connection"); task::run(true, 16, async |scope| { let database = Database::postgres(url.parse().unwrap(), None, options()).await?; let mut executor = database.acquire().await?; let expected = Uuid::parse("018f040a-785e-7000-8000-000000000001").unwrap(); assert_eq!( executor .query_one( scope, Sql::same("SELECT $1::uuid"), vec![Parameter::Uuid(expected.into())], |row| Ok((row.get::(0)?, row.get::>(0)?)), ) .await?, Some((expected, Some(expected))) ); assert_eq!( executor .query_one(scope, Sql::same("SELECT NULL::uuid"), vec![], |row| { row.get::>(0) }) .await?, Some(None) ); let error = executor .query_one(scope, Sql::same("SELECT NULL::uuid"), vec![], |row| { row.get::(0) }) .await .unwrap_err(); assert_eq!(error.kind, Kind::InvalidData); let error = executor .query_one(scope, Sql::same("SELECT 1"), vec![], |row| { row.get::(0) }) .await .unwrap_err(); assert!(std::error::Error::source(&error).is_some()); Ok::<_, Error>(()) }) .unwrap() .unwrap(); } #[test] #[ignore = "requires an isolated PostgreSQL database; run test/postgres.py"] fn postgres_row_locks_queue_ownership_and_live_permissions() { let url = std::env::var("DEVER_TEST_POSTGRES").expect("isolated PostgreSQL test connection"); task::run(true, 32, async |scope| { let mut settings = options(); settings.max_connections = 4; settings.io_timeout = Duration::from_secs(10); let database = Database::postgres(url.parse().unwrap(), None, settings).await?; database .acquire() .await? .migrate_group( scope, auth::schema::CATALOG .into_iter() .chain(auth::schema::ROLES) .chain(job::schema::TABLES) .map(|(name, sql)| (name, sql, None)) .collect(), ) .await?; let mut first = database.acquire().await?; let mut second = database.acquire().await?; let execution = job::Execution::System { tenant: None }; let submit = |key| job::Submission { target: "demo.work.save", schema: "one", execution: &execution, policy: job::Policy { retries: 3, timeout_ms: 100, }, items: job::Item { key, payload: None, at: 0, }, }; let one = job::enqueue(&mut first, scope, submit("first"), 0).await?; assert_eq!( one, job::enqueue(&mut second, scope, submit("first"), 0).await? ); let two = job::enqueue(&mut second, scope, submit("second"), 0).await?; let owner = first.begin(scope).await?; first .query( scope, Sql::same("SELECT id FROM _dever_job WHERE id=$1 FOR UPDATE"), vec![Parameter::Text(one.clone().into_text())], |row| row.get::(0), ) .await?; // 第一条持有真实行锁,另一连接领取第二条,不等待第一条释放。 let claim = tokio::time::timeout( Duration::from_secs(1), job::claim(&mut second, scope, 0, 1000), ) .await .unwrap()? .unwrap(); assert_eq!(claim.id, two.into_text()); first.finish(scope, owner, true).await?; let old = job::claim(&mut first, scope, 0, 100).await?.unwrap(); assert_eq!(old.id, one.into_text()); let renewed = job::claim(&mut second, scope, 101, 1000).await?.unwrap(); assert_ne!(old.token, renewed.token); assert!( job::complete(&mut first, scope, &old, 102, false) .await .is_err() ); job::complete(&mut second, scope, &renewed, 102, false).await?; job::complete(&mut second, scope, &claim, 102, false).await?; let owner = first.begin(scope).await?; job::enqueue(&mut first, scope, submit("rollback"), 200).await?; first.finish(scope, owner, false).await?; assert!(job::claim(&mut second, scope, 200, 1000).await?.is_none()); drop(first); drop(second); database .acquire() .await? .execute( scope, Sql::same( "INSERT INTO _dever_permission VALUES ($1,$2,$3,$4,$5,TRUE,'GET','http')", ), vec![ Parameter::Text("demo.article.admin.read".into()), Parameter::Text("admin".into()), Parameter::Text("demo".into()), Parameter::Text("article".into()), Parameter::Text("read".into()), ], ) .await?; auth::initialize_owner(scope, &database, "admin", 1).await?; let identity = crate::auth::verified("admin", 2, None).await; let mut context = Context::default(); let mut access = auth::Access::global(&identity, &database, &mut context)?; assert!(matches!( access .authorize(scope, &database, "demo.article.admin.read") .await, Err(auth::Error::Forbidden) )); access .save_role( scope, &database, auth::Role { id: "reader".into(), name: "Reader".into(), all_permissions: false, permissions: vec!["demo.article.admin.read".into()], }, ) .await?; access.grant_role(scope, 2, "reader").await?; access .authorize(scope, &database, "demo.article.admin.read") .await?; assert_eq!( access.current_permissions(scope, &database).await?, ["demo.article.admin.read"] ); access.revoke_role(scope, 2, "reader").await?; assert!(matches!( access .authorize(scope, &database, "demo.article.admin.read") .await, Err(auth::Error::Forbidden) )); assert!(matches!( access.disable_role(scope, "owner").await, Err(auth::Error::Forbidden) )); Ok::<_, auth::Error>(()) }) .unwrap() .unwrap(); }