Skip to content

Commit c1aabfa

Browse files
loganjLarryclaude
committed
test(relay): bind the member/bot re-add report to the kind:9000 path
The earlier test checked only member_role_unchanged, so reverting the persist_put_user call site to an exact label comparison left it green. The new Postgres test sends a real kind:9000 through ingest. A SHARE lock on channel_members lets the prior-role read pass and holds the write before its statement snapshot; the agent's first owner record commits while it waits. The re-add must report the agent as already_member with one joined notice, and admin -> member and guest -> member in the same event as updated. With the exact comparison back at the call site, it fails. Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Logan Johnson <[email protected]>
1 parent d20e11d commit c1aabfa

1 file changed

Lines changed: 134 additions & 0 deletions

File tree

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

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,140 @@ async fn put_user_reports_role_changes_as_updated() {
249249
);
250250
}
251251

252+
/// `member` and `bot` are one role. The label is computed from the agent
253+
/// owner record, which does not take the membership lock, so an owner record
254+
/// can commit after `put_members` reads the prior roles and before its write
255+
/// returns the new ones. A re-add of that agent then reads `member` before
256+
/// and `bot` after, and is still reported as `already_member` with no second
257+
/// joined notice. Real role changes in the same event are still `updated`.
258+
///
259+
/// The test holds a SHARE lock on `channel_members`: the prior-role read
260+
/// passes it and the write waits for it before its statement snapshot. The
261+
/// owner record commits while the write waits.
262+
/// Mutation: compare the labels exactly in `persist_put_user` → the agent is
263+
/// `updated` and gets a second joined notice → RED.
264+
#[tokio::test]
265+
#[ignore = "requires Postgres"]
266+
async fn put_user_re_add_racing_the_first_owner_record_is_already_member() {
267+
let state = state().await;
268+
let tenant = community(&state, "put-user-owner-race").await;
269+
let (owner, agent, agent_owner, admin, guest) = (
270+
Keys::generate(),
271+
Keys::generate(),
272+
Keys::generate(),
273+
Keys::generate(),
274+
Keys::generate(),
275+
);
276+
let (agent_hex, admin_hex, guest_hex) = (
277+
agent.public_key().to_hex(),
278+
admin.public_key().to_hex(),
279+
guest.public_key().to_hex(),
280+
);
281+
let channel_id = create_channel(&state, &tenant, &owner, &[&["p", &agent_hex]]).await;
282+
let setup = ingest(
283+
&state,
284+
&tenant,
285+
put_user(
286+
&owner,
287+
channel_id,
288+
&[&[&admin_hex, "admin"], &[&guest_hex, "guest"]],
289+
&[],
290+
),
291+
&owner,
292+
)
293+
.await
294+
.expect("set up admin and guest");
295+
assert!(setup.accepted, "{}", setup.message);
296+
for key in [&agent, &agent_owner] {
297+
state
298+
.db
299+
.ensure_user(tenant.community(), &key.public_key().to_bytes())
300+
.await
301+
.expect("ensure user");
302+
}
303+
304+
let mut lock = state.db.pool().begin().await.expect("begin lock");
305+
sqlx::query("LOCK TABLE channel_members IN SHARE MODE")
306+
.execute(&mut *lock)
307+
.await
308+
.expect("lock channel_members");
309+
let re_add = tokio::spawn({
310+
let (state, tenant, owner) = (Arc::clone(&state), tenant.clone(), owner.clone());
311+
let event = put_user(
312+
&owner,
313+
channel_id,
314+
&[
315+
&[&agent_hex],
316+
&[&admin_hex, "member"],
317+
&[&guest_hex, "member"],
318+
],
319+
&[],
320+
);
321+
async move { ingest(&state, &tenant, event, &owner).await }
322+
});
323+
324+
// Wait until the transaction that holds this channel's membership lock
325+
// (`acquire_channel_membership_lock`) is waiting to write
326+
// `channel_members`: its prior-role read has run.
327+
let lock_name = format!(
328+
"buzz_channel_membership:{}:{channel_id}",
329+
tenant.community().as_uuid()
330+
);
331+
tokio::time::timeout(std::time::Duration::from_secs(10), async {
332+
loop {
333+
let waiting: bool = sqlx::query_scalar(
334+
"SELECT EXISTS ( \
335+
SELECT 1 FROM pg_locks held \
336+
JOIN pg_locks waiting ON waiting.pid = held.pid \
337+
WHERE held.locktype = 'advisory' AND held.granted AND held.objsubid = 1 \
338+
AND held.classid::bigint = (k.key >> 32) & 4294967295 \
339+
AND held.objid::bigint = k.key & 4294967295 \
340+
AND waiting.locktype = 'relation' AND NOT waiting.granted \
341+
AND waiting.relation = 'channel_members'::regclass \
342+
) FROM (SELECT hashtextextended($1, 0) AS key) k",
343+
)
344+
.bind(&lock_name)
345+
.fetch_one(state.db.pool())
346+
.await
347+
.expect("inspect the membership write");
348+
if waiting {
349+
break;
350+
}
351+
tokio::task::yield_now().await;
352+
}
353+
})
354+
.await
355+
.expect("the membership write reached the table lock");
356+
357+
assert!(state
358+
.db
359+
.set_agent_owner(
360+
tenant.community(),
361+
&agent.public_key().to_bytes(),
362+
&agent_owner.public_key().to_bytes(),
363+
)
364+
.await
365+
.expect("record owner"));
366+
lock.rollback().await.expect("release lock");
367+
368+
let result = re_add.await.expect("join re-add").expect("re-add");
369+
assert!(result.accepted, "{}", result.message);
370+
assert_eq!(
371+
report(&result.message),
372+
serde_json::json!({
373+
"updated": [admin_hex, guest_hex],
374+
"already_member": [agent_hex],
375+
})
376+
);
377+
assert_eq!(
378+
role_of(&state, &tenant, channel_id, &agent)
379+
.await
380+
.as_deref(),
381+
Some("bot")
382+
);
383+
assert_eq!(joined_notices(&state, &tenant, channel_id, &agent).await, 1);
384+
}
385+
252386
/// When nobody named may be added the event is refused: one person keeps the
253387
/// single-add error, several get the grouped report. A malformed `p` refuses
254388
/// the whole event.

0 commit comments

Comments
 (0)