diff --git a/crates/voelin-core/src/link/tests.rs b/crates/voelin-core/src/link/tests.rs index 711cce5..2c6d408 100644 --- a/crates/voelin-core/src/link/tests.rs +++ b/crates/voelin-core/src/link/tests.rs @@ -2184,12 +2184,46 @@ async fn event_of( .await } +/// Asks for the file list of the lobby until the answer is one `wanted` takes, +/// one question at a time: the next goes out only when the last is answered. +/// Asked every few milliseconds instead, the answers fell behind the 16 +/// events the bus holds (and the host's work), and the one that counted was +/// lost in the backlog. +async fn ask_files_until( + cmd_tx: &mpsc::UnboundedSender, + bus: &mut broadcast::Receiver, + request: &mut crate::files::RequestId, + wanted: impl Fn(&Result, String>) -> bool, +) { + within("the file list to be answered as wanted", async { + loop { + *request += 1; + let dir = remote(10, None, "/"); + cmd_tx.send(LinkCmd::ListFiles { request: *request, dir }).ok()?; + let result = loop { + match bus.recv().await { + Ok(Event::FileList { request: answered, result, .. }) if answered == *request => { + break result; + } + Ok(_) | Err(broadcast::error::RecvError::Lagged(_)) => {} + Err(broadcast::error::RecvError::Closed) => return None, + } + }; + if wanted(&result) { + return Some(()); + } + tokio::time::sleep(Duration::from_millis(20)).await; + } + }) + .await; +} + /// `Server.features` narrows what the host offers (protocol 1.3, /// `docs/session-seam.md` 4.7), and a `HostUpdate` changes what is narrowed. #[tokio::test(flavor = "multi_thread")] async fn the_features_of_a_server_are_the_hosts_narrowed_by_the_servers_list() { use v1::entity::Kind; - use voelin_proto::test_util::{FakeConnector, eventually}; + use voelin_proto::test_util::FakeConnector; let connector = FakeConnector::new(); let host = connector.host(); @@ -2241,55 +2275,27 @@ async fn the_features_of_a_server_are_the_hosts_narrowed_by_the_servers_list() { }) .count() }; - let ask = |request| { - let dir = remote(10, None, "/"); - cmd_tx.send(LinkCmd::ListFiles { request, dir }).unwrap(); - }; let refusal = "not offered by this server: files"; - let refused = - |event: &Event| matches!(event, Event::FileList { result: Err(why), .. } if why == refusal); + let refused = |result: &Result, String>| { + matches!(result, Err(why) if why == refusal) + }; + let mut request = 0; // Refused here: nothing reaches the host. - ask(1); - loop { - let event = within("the refusal", async { bus.recv().await.ok() }).await; - if refused(&event) { - break; - } - } + ask_files_until(&cmd_tx, &mut bus, &mut request, refused).await; assert_eq!(file_lists(), 0); // The server lifts its list (an empty one keeps all the host's): files work. put_server(&[]); - let mut request = 1; - eventually(WAIT, "a file list to reach the host", || { - request += 1; - ask(request); - file_lists() > 0 - }) - .await; + ask_files_until(&cmd_tx, &mut bus, &mut request, |result| !refused(result)).await; + assert!(file_lists() > 0, "the answer did not come from the host"); - // The host narrows what it offers this user. A refusal counts only for a - // request made after the update: the earlier ones were answered by the host's - // wider set (or, before the server lifted its list, refused for that). + // The host narrows what it offers this user. Every question is answered + // before the next is asked, so those asked from here on are the ones the + // update can refuse; the earlier ones were answered by the host's wider set. let update = v1::HostUpdate { features: vec!["voice".into(), "chat".into()], limits: None }; handle.push(v1::Push { body: Some(v1::push::Body::HostUpdate(update)) }).unwrap(); - let after = request; - within("the narrower set to apply", async { - loop { - request += 1; - ask(request); - tokio::time::sleep(Duration::from_millis(20)).await; - while let Ok(event) = bus.try_recv() { - if matches!(&event, Event::FileList { request, .. } if *request > after) - && refused(&event) - { - return Some(()); - } - } - } - }) - .await; + ask_files_until(&cmd_tx, &mut bus, &mut request, refused).await; } /// A port that is no port is refused in words, not dropped: the connection