diff --git a/crates/voelin-core/src/link/tests.rs b/crates/voelin-core/src/link/tests.rs index fe7efeb..f84761d 100644 --- a/crates/voelin-core/src/link/tests.rs +++ b/crates/voelin-core/src/link/tests.rs @@ -1961,3 +1961,88 @@ async fn a_scope_bigger_than_the_channel_is_sent_in_bigger_chunks() { // Two of the fixture's channels, and the ones added. assert_eq!(replica.channels(1).count() as u64, 2 + CHANNELS + 1, "with the server chat"); } + +// ---- moves in flight, transfers, nameless pokes, narrowed features -------------- + +fn join(channel: u64) -> v1::request::Op { + request(v1::request::Op::JoinVoice(v1::JoinVoice { + server_id: 1, + channel_id: channel, + ..Default::default() + })) +} + +/// The channel of the voice state an answer carries. +fn voice_channel(answer: v1::response::Result) -> u64 { + let v1::response::Result::Entities(entities) = answer else { panic!("a voice state") }; + let Some(v1::entity::Kind::Voice(voice)) = &entities.items[0].kind else { + panic!("a voice state") + }; + voice.channel_id +} + +/// The link reports a snapshot that has us in `channel`. +fn moved_to(inner: &Inner, channel: u64) { + let mut next = presence(); + next.clients.get_mut(&ME).unwrap().channel = channel; + inner.events.send(LinkEvent::Presence(Box::new(next))).unwrap(); +} + +/// A move made while another is on its way is a move: the channel the +/// snapshots show is where we were, not where the first move leaves us. A +/// move into the channel we are in, with none on its way, is answered at once. +#[tokio::test(flavor = "multi_thread")] +async fn a_move_made_while_another_is_on_its_way_reaches_the_link() { + let (session, _pushes, mut inner) = open(everything()).await; + // The seat, then a move into the channel we are in: nothing to ask the link. + assert_eq!(voice_channel(session.request(join(GAMES)).await.unwrap()), GAMES); + let same = within("the answer", async { session.request(join(GAMES)).await.ok() }).await; + assert_eq!(voice_channel(same), GAMES); + assert!(inner.cmds.try_recv().is_err(), "a move into the channel we are in went to the link"); + + // The link is slow to answer the first move ... + let first = session.request(join(LOBBY)); + let LinkCmd::Move(channel, _) = within("a move", inner.cmds.recv()).await else { + panic!("a move") + }; + assert_eq!(channel, LOBBY); + // ... and the user changes their mind: back into the channel the snapshots + // still show. The link is asked, or the user stays in the lobby. + let second = session.request(join(GAMES)); + let LinkCmd::Move(channel, _) = within("the move back", inner.cmds.recv()).await else { + panic!("the move back") + }; + assert_eq!(channel, GAMES); + moved_to(&inner, LOBBY); + let first = within("the first move", async { first.await.ok() }).await; + assert_eq!(voice_channel(first), LOBBY); + moved_to(&inner, GAMES); + let second = within("the move back", async { second.await.ok() }).await; + assert_eq!(voice_channel(second), GAMES); + + // Nothing is on its way any more. + let same = within("the answer", async { session.request(join(GAMES)).await.ok() }).await; + assert_eq!(voice_channel(same), GAMES); + assert!(inner.cmds.try_recv().is_err()); +} + +/// A move back into the channel we never left, after a refused move: the link +/// says nothing to a move into the channel it is in, and the refusal of the +/// first move answers the second. +#[tokio::test(flavor = "multi_thread")] +async fn a_move_back_after_a_refused_move_is_answered_though_the_link_says_nothing() { + let (session, _pushes, mut inner) = open(everything()).await; + session.request(join(GAMES)).await.unwrap(); + let refused = session.request(join(LOBBY)); + let LinkCmd::Move(..) = within("a move", inner.cmds.recv()).await else { panic!("a move") }; + let back = session.request(join(GAMES)); + let LinkCmd::Move(..) = within("the move back", inner.cmds.recv()).await else { + panic!("the move back") + }; + inner.ctx.emit(Event::JoinFailed { session: 0, channel: LOBBY, reason: JoinFailure::Password }); + let error = within("the refusal", async { Some(refused.await.unwrap_err()) }).await; + assert_eq!(error.code(), Some(v1::ErrorCode::Password)); + // Answered now, not after the ten seconds of a move that gets no answer. + let back = tokio::time::timeout(Duration::from_secs(2), back).await; + assert_eq!(voice_channel(back.expect("answered at once").unwrap()), GAMES); +} diff --git a/crates/voelin-core/src/ts_session/driver.rs b/crates/voelin-core/src/ts_session/driver.rs index 76b7c62..6d8dc7c 100644 --- a/crates/voelin-core/src/ts_session/driver.rs +++ b/crates/voelin-core/src/ts_session/driver.rs @@ -1053,6 +1053,10 @@ impl Driver { JoinFailure::Other(text) => rejected(ErrorCode::Forbidden, text), }; reply_to(pending.reply, Err(error)); + // A move that followed this one into the channel we never + // left gets no answer from the link (it swallows "already + // in that channel"): the snapshots' channel answers it. + self.settle_moves(); } } // What the stream task says (its own bus events) goes to the engine. @@ -1432,14 +1436,20 @@ impl Driver { } /// A move into a channel (`JoinVoice`). The first of a connection is the - /// seat the link gave us at connect: answered, nothing moved. + /// seat the link gave us at connect: answered, nothing moved. A move into + /// the channel the snapshots show is answered at once, unless another + /// move is on its way: the snapshot is where we were, not where that move + /// leaves us, so this request goes to the link like any other (the link + /// answers a move into the channel it is in with nothing, which is what + /// [`Driver::settle_moves`] makes up for). fn join(&mut self, join: v1::JoinVoice, reply: Reply) { if join.server_id != SERVER_ID { let _ = reply.send(Err(rejected(ErrorCode::NotFound, "no such server"))); return; } let first = !std::mem::replace(&mut self.seated, true); - if first || self.own_channel == Some(join.channel_id) { + let moving = self.pending.values().any(|p| matches!(p.waiting, Waiting::Move { .. })); + if first || (!moving && self.own_channel == Some(join.channel_id)) { let _ = reply.send(Ok(self.own_voice())); return; }