Skip to content

Commit b76879c

Browse files
committed
Register fencing regressions directly with shared fixtures
Signed-off-by: Ravneet Arora <[email protected]>
1 parent 21f8dc6 commit b76879c

11 files changed

Lines changed: 412 additions & 380 deletions

‎crates/buzz-db/src/store/mod.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,8 @@ mod unlisted_channels_postgres_tests;
101101

102102
#[cfg(test)]
103103
mod admission_postgres_tests;
104+
105+
#[cfg(test)]
106+
mod user_deletion_lock_order_postgres_tests;
107+
#[cfg(test)]
108+
mod user_deletion_test_support;

‎crates/buzz-db/src/store/user_deletion_lock_order_postgres_tests.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
use super::*;
1+
use super::user_deletion_fences::{self as fences, FenceIdentity};
2+
use super::user_deletion_test_support::{message, restricted, wait_for_waiter, Fixture};
3+
use crate::{event, Db, DbError};
4+
use nostr::Keys;
5+
use std::time::Duration;
6+
use uuid::Uuid;
27

38
/// Exercise the actual TTL-before-author settings writer against a message
49
/// holding the author's shared lock and a queued exclusive fence. PostgreSQL
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
use super::user_deletion_fences::{self as fences, FenceIdentity};
2+
use crate::{Db, DbError};
3+
use buzz_core::CommunityId;
4+
use nostr::{Event, EventBuilder, Keys, Kind};
5+
use sqlx::{PgPool, Postgres, Transaction};
6+
use std::time::Duration;
7+
use uuid::Uuid;
8+
9+
pub(super) struct Fixture {
10+
pub(super) pool: PgPool,
11+
pub(super) db: Db,
12+
pub(super) community: CommunityId,
13+
pub(super) lease: crate::entity_deletion::EntityLease,
14+
}
15+
16+
impl Fixture {
17+
pub(super) async fn new() -> Self {
18+
let pool = PgPool::connect(&crate::test_support::database_url())
19+
.await
20+
.unwrap();
21+
let db = Db::from_pool(pool.clone());
22+
let community = db
23+
.ensure_configured_community(&format!("message-fences-{}.test", Uuid::new_v4()))
24+
.await
25+
.unwrap()
26+
.id;
27+
let request = db
28+
.user_deletion_store()
29+
.submit(&format!("demo:{}", Uuid::new_v4()), "operator", None)
30+
.await
31+
.unwrap();
32+
let store = db.entity_deletion_store();
33+
store
34+
.approve(
35+
request.id,
36+
"reviewer",
37+
request.inventory_digest.as_deref().unwrap(),
38+
)
39+
.await
40+
.unwrap();
41+
let lease = store
42+
.claim(request.id, "worker", Duration::from_secs(300))
43+
.await
44+
.unwrap()
45+
.unwrap();
46+
Self {
47+
pool,
48+
db,
49+
community,
50+
lease,
51+
}
52+
}
53+
54+
pub(super) async fn fence(&self, keys: &Keys, owner: Option<&Keys>) {
55+
let mut tx = self.pool.begin().await.unwrap();
56+
self.fence_in(&mut tx, keys, owner).await;
57+
tx.commit().await.unwrap();
58+
}
59+
60+
pub(super) async fn fence_in(
61+
&self,
62+
tx: &mut Transaction<'_, Postgres>,
63+
keys: &Keys,
64+
owner: Option<&Keys>,
65+
) {
66+
let identity = if owner.is_some() {
67+
FenceIdentity::agent(self.community, keys.public_key())
68+
} else {
69+
FenceIdentity::user(keys.public_key())
70+
};
71+
fences::insert(tx, identity, &self.lease, owner.map(Keys::public_key))
72+
.await
73+
.unwrap();
74+
}
75+
76+
pub(super) async fn agent(&self, owner: &Keys) -> Keys {
77+
self.db
78+
.ensure_user(self.community, owner.public_key().as_bytes())
79+
.await
80+
.unwrap();
81+
let agent = Keys::generate();
82+
self.db
83+
.ensure_user(self.community, agent.public_key().as_bytes())
84+
.await
85+
.unwrap();
86+
self.db
87+
.set_agent_owner(
88+
self.community,
89+
agent.public_key().as_bytes(),
90+
owner.public_key().as_bytes(),
91+
)
92+
.await
93+
.unwrap();
94+
agent
95+
}
96+
}
97+
98+
pub(super) fn message(keys: &Keys, kind: u16) -> Event {
99+
EventBuilder::new(Kind::Custom(kind), Uuid::new_v4().to_string())
100+
.sign_with_keys(keys)
101+
.unwrap()
102+
}
103+
104+
pub(super) fn restricted(result: crate::Result<(buzz_core::StoredEvent, bool)>) {
105+
assert!(
106+
matches!(result, Err(DbError::UserDeletionRestricted)),
107+
"{result:?}"
108+
);
109+
}
110+
111+
pub(super) async fn wait_for_waiter(pool: &PgPool, pid: i32) {
112+
tokio::time::timeout(Duration::from_secs(4), async {
113+
loop {
114+
let waiting: bool = sqlx::query_scalar("SELECT EXISTS (SELECT 1 FROM pg_locks WHERE pid = $1 AND locktype = 'advisory' AND NOT granted)").bind(pid).fetch_one(pool).await.unwrap();
115+
if waiting { break; }
116+
tokio::time::sleep(Duration::from_millis(10)).await;
117+
}
118+
}).await.expect("production writer must wait for the identity lock");
119+
}

‎crates/buzz-db/src/store/user_deletion_write_guard_postgres_tests.rs‎

Lines changed: 3 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,9 @@
11
use super::user_deletion_fences::{self as fences, FenceIdentity};
2-
use crate::{event, Db, DbError};
3-
use buzz_core::CommunityId;
4-
use nostr::{Event, EventBuilder, Keys, Kind};
5-
use sqlx::{PgPool, Postgres, Transaction};
2+
use super::user_deletion_test_support::{message, restricted, wait_for_waiter, Fixture};
3+
use crate::{event, DbError};
4+
use nostr::{Event, Keys};
65
use std::time::Duration;
76
use uuid::Uuid;
8-
9-
struct Fixture {
10-
pool: PgPool,
11-
db: Db,
12-
community: CommunityId,
13-
lease: crate::entity_deletion::EntityLease,
14-
}
15-
16-
impl Fixture {
17-
async fn new() -> Self {
18-
let pool = PgPool::connect(&crate::test_support::database_url())
19-
.await
20-
.unwrap();
21-
let db = Db::from_pool(pool.clone());
22-
let community = db
23-
.ensure_configured_community(&format!("message-fences-{}.test", Uuid::new_v4()))
24-
.await
25-
.unwrap()
26-
.id;
27-
let request = db
28-
.user_deletion_store()
29-
.submit(&format!("demo:{}", Uuid::new_v4()), "operator", None)
30-
.await
31-
.unwrap();
32-
let store = db.entity_deletion_store();
33-
store
34-
.approve(
35-
request.id,
36-
"reviewer",
37-
request.inventory_digest.as_deref().unwrap(),
38-
)
39-
.await
40-
.unwrap();
41-
let lease = store
42-
.claim(request.id, "worker", Duration::from_secs(300))
43-
.await
44-
.unwrap()
45-
.unwrap();
46-
Self {
47-
pool,
48-
db,
49-
community,
50-
lease,
51-
}
52-
}
53-
54-
async fn fence(&self, keys: &Keys, owner: Option<&Keys>) {
55-
let mut tx = self.pool.begin().await.unwrap();
56-
self.fence_in(&mut tx, keys, owner).await;
57-
tx.commit().await.unwrap();
58-
}
59-
60-
async fn fence_in(
61-
&self,
62-
tx: &mut Transaction<'_, Postgres>,
63-
keys: &Keys,
64-
owner: Option<&Keys>,
65-
) {
66-
let identity = if owner.is_some() {
67-
FenceIdentity::agent(self.community, keys.public_key())
68-
} else {
69-
FenceIdentity::user(keys.public_key())
70-
};
71-
fences::insert(tx, identity, &self.lease, owner.map(Keys::public_key))
72-
.await
73-
.unwrap();
74-
}
75-
76-
async fn agent(&self, owner: &Keys) -> Keys {
77-
self.db
78-
.ensure_user(self.community, owner.public_key().as_bytes())
79-
.await
80-
.unwrap();
81-
let agent = Keys::generate();
82-
self.db
83-
.ensure_user(self.community, agent.public_key().as_bytes())
84-
.await
85-
.unwrap();
86-
self.db
87-
.set_agent_owner(
88-
self.community,
89-
agent.public_key().as_bytes(),
90-
owner.public_key().as_bytes(),
91-
)
92-
.await
93-
.unwrap();
94-
agent
95-
}
96-
}
97-
98-
fn message(keys: &Keys, kind: u16) -> Event {
99-
EventBuilder::new(Kind::Custom(kind), Uuid::new_v4().to_string())
100-
.sign_with_keys(keys)
101-
.unwrap()
102-
}
103-
104-
fn restricted(result: crate::Result<(buzz_core::StoredEvent, bool)>) {
105-
assert!(
106-
matches!(result, Err(DbError::UserDeletionRestricted)),
107-
"{result:?}"
108-
);
109-
}
110-
111-
async fn wait_for_waiter(pool: &PgPool, pid: i32) {
112-
tokio::time::timeout(Duration::from_secs(4), async {
113-
loop {
114-
let waiting: bool = sqlx::query_scalar("SELECT EXISTS (SELECT 1 FROM pg_locks WHERE pid = $1 AND locktype = 'advisory' AND NOT granted)").bind(pid).fetch_one(pool).await.unwrap();
115-
if waiting { break; }
116-
tokio::time::sleep(Duration::from_millis(10)).await;
117-
}
118-
}).await.expect("production writer must wait for the identity lock");
119-
}
120-
1217
async fn writer(
1228
f: &Fixture,
1239
event: Event,
@@ -376,6 +262,3 @@ async fn ownership_change_while_waiting_rejects_and_retry_uses_current_owner() {
376262
.await,
377263
);
378264
}
379-
380-
#[path = "user_deletion_lock_order_postgres_tests.rs"]
381-
mod lock_order_postgres_tests;

‎crates/buzz-relay/src/handlers/mod.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,14 @@ pub fn resolve_ttl(event: &nostr::Event, ephemeral_ttl_override: Option<i32>) ->
8585

8686
/// NIP-AR artifact lifecycle.
8787
pub mod artifact;
88+
89+
#[cfg(test)]
90+
mod user_deletion_archive_postgres_tests;
91+
#[cfg(test)]
92+
mod user_deletion_error_postgres_tests;
93+
#[cfg(test)]
94+
mod user_deletion_expanded_postgres_tests;
95+
#[cfg(test)]
96+
mod user_deletion_lifecycle_postgres_tests;
97+
#[cfg(test)]
98+
mod user_deletion_test_support;

‎crates/buzz-relay/src/handlers/user_deletion_archive_postgres_tests.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use super::*;
1+
use super::user_deletion_test_support::Fixture;
2+
use nostr::{Event, EventBuilder, Keys, Kind, Tag};
3+
use serde_json::Value;
4+
use std::{sync::Arc, time::Duration};
5+
use uuid::Uuid;
26

37
fn archive_request(key: &Keys, kind: u16) -> Event {
48
EventBuilder::new(Kind::Custom(kind), Uuid::new_v4().to_string())

‎crates/buzz-relay/src/handlers/user_deletion_error_postgres_tests.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use super::*;
1+
use super::group_state_postgres_tests::create_channel;
2+
use super::user_deletion_test_support::Fixture;
3+
use buzz_db::user_deletion_fences::{self as fences, FenceIdentity};
4+
use nostr::{EventBuilder, Kind, Tag};
5+
use std::{sync::Arc, time::Duration};
26

37
#[test]
48
fn identity_error_mapping_preserves_unrelated_failures() {

‎crates/buzz-relay/src/handlers/user_deletion_expanded_postgres_tests.rs‎

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,9 @@
11
//! Persistent insertion coverage through HTTP and WebSocket event handling.
2-
use super::*;
3-
4-
fn action(key: &Keys, channel: Uuid, kind: u16, content: &str, extra: Vec<Vec<String>>) -> Event {
5-
let mut tags = vec![Tag::parse(["h", &channel.to_string()]).unwrap()];
6-
tags.extend(extra.into_iter().map(|tag| Tag::parse(tag).unwrap()));
7-
EventBuilder::new(Kind::Custom(kind), content)
8-
.tags(tags)
9-
.sign_with_keys(key)
10-
.unwrap()
11-
}
12-
13-
async fn snapshot(f: &Fixture) -> Vec<Value> {
14-
let mut rows = Vec::new();
15-
for table in [
16-
"events",
17-
"channels",
18-
"channel_members",
19-
"reactions",
20-
"thread_metadata",
21-
"event_mentions",
22-
"operator_listener_outbox",
23-
"artifact_heads",
24-
"artifact_revisions",
25-
"workflows",
26-
] {
27-
let query = format!("SELECT COALESCE(jsonb_agg(to_jsonb(t) ORDER BY to_jsonb(t)::text), '[]'::jsonb) FROM {table} t WHERE community_id=$1");
28-
rows.push(
29-
sqlx::query_scalar(sqlx::AssertSqlSafe(query))
30-
.bind(f.tenant.community().as_uuid())
31-
.fetch_one(&f.pool)
32-
.await
33-
.unwrap(),
34-
);
35-
}
36-
rows
37-
}
38-
39-
#[path = "user_deletion_lifecycle_postgres_tests.rs"]
40-
mod lifecycle_postgres_tests;
2+
use super::group_state_postgres_tests::create_channel;
3+
use super::user_deletion_test_support::{action, snapshot, Fixture};
4+
use nostr::{EventBuilder, Keys, Kind, Tag};
5+
use std::{sync::Arc, time::Duration};
6+
use uuid::Uuid;
417

428
/// Each case must succeed before fencing, then leave every relevant table
439
/// unchanged on rejection. Both real HTTP routing and the WS EVENT handler run.

‎crates/buzz-relay/src/handlers/user_deletion_lifecycle_postgres_tests.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use super::*;
1+
use super::group_state_postgres_tests::create_channel;
2+
use super::user_deletion_test_support::{action, snapshot, Fixture};
3+
use uuid::Uuid;
24

35
#[tokio::test]
46
#[ignore = "requires Postgres"]

0 commit comments

Comments
 (0)