Skip to content

Commit f2f9517

Browse files
wpfleger96Hayt
andcommitted
test(credential): assert the Ask is emitted under the state lock
Replaces the sleep-based race test, which old code could pass when the competing thread ran late, with a deterministic check. Co-authored-by: Hayt <211b96e6a2b7f45fd4047988976c7bbbeeda0c15f3ae7b32eec20834b5a55118@buzz.block.builderlab.xyz>
1 parent 87d7bb5 commit f2f9517

2 files changed

Lines changed: 52 additions & 116 deletions

File tree

‎src-tauri/src/credential_service/pairing.rs‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,3 +690,55 @@ impl Drop for Closing<'_> {
690690
}
691691
}
692692
}
693+
694+
#[cfg(test)]
695+
mod tests {
696+
use super::*;
697+
use std::sync::{Arc, OnceLock, TryLockError};
698+
699+
/// A request is asked under the state lock that removal and admission
700+
/// take, so neither can come between its pending check and its Ask.
701+
#[tokio::test]
702+
async fn a_request_is_asked_under_the_state_lock() {
703+
let pairings: Arc<OnceLock<Arc<Pairings>>> = Arc::default();
704+
let (asked_send, asked) = std::sync::mpsc::channel();
705+
let notify: Notify = {
706+
let pairings = pairings.clone();
707+
Arc::new(move |notice| {
708+
if let Notice::Ask(request) = notice {
709+
let held = matches!(
710+
pairings.get().unwrap().state.try_lock(),
711+
Err(TryLockError::WouldBlock)
712+
);
713+
asked_send.send((request.id, held)).unwrap();
714+
}
715+
})
716+
};
717+
let keychain = Arc::new(super::super::keychain::Memory::default());
718+
let made = Arc::new(Pairings::new(Arc::new(keychain), notify));
719+
pairings.set(made.clone()).ok();
720+
let relay = "wss://relay.example";
721+
made.set_communities(vec![relay.into()]).await;
722+
let pairing = tokio::spawn({
723+
let made = made.clone();
724+
async move {
725+
let human = || async {
726+
Human::Ready(Approver {
727+
pubkey: "a".repeat(64),
728+
store_account: "account".into(),
729+
fi_account: None,
730+
})
731+
};
732+
made.pair("buzz-cli", relay, || async { None }, human)
733+
.await
734+
.map(|_| ())
735+
}
736+
});
737+
let (id, held) = tokio::task::spawn_blocking(move || asked.recv().unwrap())
738+
.await
739+
.unwrap();
740+
assert!(held, "Ask was emitted without the state lock");
741+
made.answer(&id, false);
742+
assert_eq!(pairing.await.unwrap().err().unwrap().code, "denied");
743+
}
744+
}

‎src-tauri/src/credential_service/tests/pairing.rs‎

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,119 +1052,3 @@ async fn a_removal_during_the_process_lookup_asks_nothing() {
10521052
.await;
10531053
a_removal_left_nothing_waiting(&fixture, removed.err().unwrap().code).await;
10541054
}
1055-
1056-
/// The prompts the page still shows after `notices`, as `PairingPrompt` keeps
1057-
/// them: each Ask replaces the prompt, and a Closed ends only its own.
1058-
fn shown(notices: &[Notice]) -> Option<String> {
1059-
notices.iter().fold(None, |shown, notice| match notice {
1060-
Notice::Ask(request) => Some(request.id.clone()),
1061-
Notice::Closed(id) if shown.as_ref() == Some(id) => None,
1062-
_ => shown,
1063-
})
1064-
}
1065-
1066-
#[tokio::test]
1067-
async fn a_removal_and_a_new_request_while_one_is_asked_leave_the_new_one_shown() {
1068-
use crate::credential_service::pairing::{Notice, Pairings};
1069-
use std::sync::OnceLock;
1070-
let fixture = fixture();
1071-
let notices: Arc<Mutex<Vec<Notice>>> = Arc::default();
1072-
let pairings: Arc<OnceLock<Arc<Pairings>>> = Arc::default();
1073-
let (raced_send, raced) = std::sync::mpsc::channel();
1074-
let racer = {
1075-
let (notices, pairings, service) =
1076-
(notices.clone(), pairings.clone(), fixture.service.clone());
1077-
move |notice: Notice| {
1078-
if matches!(&notice, Notice::Ask(request) if request.relay == REMOVED) {
1079-
// While A is being asked: remove its community, and ask B.
1080-
let (pairings, service, raced_send) = (
1081-
pairings.get().unwrap().clone(),
1082-
service.clone(),
1083-
raced_send.clone(),
1084-
);
1085-
let race = std::thread::spawn(move || {
1086-
let runtime = tokio::runtime::Runtime::new().unwrap();
1087-
runtime.block_on(pairings.set_communities(vec![RELAY.into()]));
1088-
let b = runtime.spawn(async move {
1089-
let human = || service.human();
1090-
pairings
1091-
.pair(CLIENT, RELAY, || async { None }, human)
1092-
.await
1093-
.map(|_| ())
1094-
});
1095-
raced_send.send((runtime, b)).unwrap();
1096-
});
1097-
// Before the fix, the race finishes inside this window.
1098-
std::thread::sleep(Duration::from_millis(200));
1099-
drop(race);
1100-
}
1101-
notices.lock().unwrap().push(notice);
1102-
}
1103-
};
1104-
let made = Arc::new(Pairings::new(
1105-
Arc::new(fixture.keychain.clone()),
1106-
Arc::new(racer),
1107-
));
1108-
made.set_communities(vec![RELAY.into(), REMOVED.into()])
1109-
.await;
1110-
pairings.set(made.clone()).ok();
1111-
let a = made
1112-
.pair(
1113-
CLIENT,
1114-
REMOVED,
1115-
|| async { None },
1116-
|| fixture.service.human(),
1117-
)
1118-
.await;
1119-
assert_eq!(a.err().unwrap().code, "denied");
1120-
let (runtime, b) = tokio::task::spawn_blocking(move || raced.recv().unwrap())
1121-
.await
1122-
.unwrap();
1123-
// B is the prompt still shown once both settle.
1124-
let mut b_id = None;
1125-
for _ in 0..400 {
1126-
let settled = {
1127-
let notices = notices.lock().unwrap();
1128-
let b_asked = notices.iter().find_map(|notice| match notice {
1129-
Notice::Ask(request) if request.relay == RELAY => Some(request.id.clone()),
1130-
_ => None,
1131-
});
1132-
let a_closed = notices
1133-
.iter()
1134-
.any(|notice| matches!(notice, Notice::Closed(_)));
1135-
if b_asked.is_some() && a_closed {
1136-
assert_eq!(shown(&notices), b_asked, "{notices:?}");
1137-
}
1138-
b_asked.filter(|_| a_closed)
1139-
};
1140-
if settled.is_some() {
1141-
b_id = settled;
1142-
break;
1143-
}
1144-
tokio::time::sleep(Duration::from_millis(5)).await;
1145-
}
1146-
let b_id_copy = b_id.expect("B was asked");
1147-
made.answer(&b_id_copy, false);
1148-
let b = tokio::task::spawn_blocking(move || runtime.block_on(b).unwrap())
1149-
.await
1150-
.unwrap();
1151-
assert_eq!(b.err().unwrap().code, "denied");
1152-
// Nothing is left waiting: the next request is asked, not throttled.
1153-
let c = tokio::spawn({
1154-
let (made, service) = (made.clone(), fixture.service.clone());
1155-
async move {
1156-
let human = || service.human();
1157-
made.pair(CLIENT, RELAY, || async { None }, human)
1158-
.await
1159-
.map(|_| ())
1160-
}
1161-
});
1162-
let c_id = loop {
1163-
if let Some(id) = shown(&notices.lock().unwrap()).filter(|id| *id != b_id_copy) {
1164-
break id;
1165-
}
1166-
tokio::time::sleep(Duration::from_millis(5)).await;
1167-
};
1168-
made.answer(&c_id, false);
1169-
assert_eq!(c.await.unwrap().err().unwrap().code, "denied");
1170-
}

0 commit comments

Comments
 (0)