diff --git a/quest/m1/subscribe-ranges/README.md b/quest/m1/subscribe-ranges/README.md index b6399fc948e..9ec1b4db696 100644 --- a/quest/m1/subscribe-ranges/README.md +++ b/quest/m1/subscribe-ranges/README.md @@ -100,7 +100,6 @@ every sequence delivered or dropped. - [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - the lite-07 wire, Rust publisher and subscriber, and the draft - [moq-transport ranges](/quest/m1/subscribe-ranges/ietf.md) - range FETCH upstream per missing run, and non-blocking FETCH served downstream - [JS ranges](/quest/m1/subscribe-ranges/js.md) - `@moq/net` model and lite-07 parity -- [Frame bounds](/quest/m1/subscribe-ranges/frame-bounds.md) - a local subscriber reads only the frames its ranges cover ## Related diff --git a/quest/m1/subscribe-ranges/dynamic.md b/quest/m1/subscribe-ranges/dynamic.md index 2a96fd1bb92..4cd24f387ff 100644 --- a/quest/m1/subscribe-ranges/dynamic.md +++ b/quest/m1/subscribe-ranges/dynamic.md @@ -38,8 +38,9 @@ Decided by the maintainer on 2026-10-20, when the quest was first started - **Split.** This quest replaces `requested_group`; removing `fetch_group` is [fetch as a range](/quest/m1/subscribe-ranges/fetch-range.md), so each - PR breaks one API once. That half needs head fills, which - [frame bounds](/quest/m1/subscribe-ranges/frame-bounds.md) provides. + PR breaks one API once. That half needs head fills for a range starting + mid-group, which build on frame bounds + ([moq-dev/moq#6129](https://github.com/moq-dev/moq/pull/6129)). - **Handler shape.** `Dynamic::requested_fill` yields a `track::Fill` naming one bounded run: `start: Position`, exclusive end group, priority, the most tolerant budget among its readers, and order. Each reader's cursor still diff --git a/quest/m1/subscribe-ranges/fetch-range.md b/quest/m1/subscribe-ranges/fetch-range.md index f1caf9f25f7..c04f9f5ef45 100644 --- a/quest/m1/subscribe-ranges/fetch-range.md +++ b/quest/m1/subscribe-ranges/fetch-range.md @@ -29,10 +29,14 @@ breaks one API once. stream errors, and `Old` still aborts a cut group mid-read. Each affected package gets a `MIGRATE.md` entry. - A one-range subscription for a cached live group missing the frames its - range starts at needs a head fill, which is why this requires frame bounds. + range starts at needs a head fill. Frame bounds + ([moq-dev/moq#6129](https://github.com/moq-dev/moq/pull/6129)) hand such a + group out positioned at the asked frame, so reading the missing head returns + `Error::Lagged` rather than the group being skipped (maintainer, + 2026-10-20). Only `fetch_group` requests a head fill today, so the one-range + read has to request it instead of letting `Lagged` reach the caller. - Benchmark filling beside `track_range_serving`, which covers serving. ## Required - [Dynamic range requests](/quest/m1/subscribe-ranges/dynamic.md) - `requested_fill` and subscriptions filling their misses -- [Frame bounds](/quest/m1/subscribe-ranges/frame-bounds.md) - head fills for a range starting mid-group diff --git a/quest/m1/subscribe-ranges/frame-bounds.md b/quest/m1/subscribe-ranges/frame-bounds.md deleted file mode 100644 index 23c940af714..00000000000 --- a/quest/m1/subscribe-ranges/frame-bounds.md +++ /dev/null @@ -1,37 +0,0 @@ -# [M] A local subscriber reads only its frame bounds - -## Goal - -A `track::Subscriber` whose ranges start or end partway through a group reads only the -frames they cover: a start at group 4 frame 5 hands out group 4 from frame 5, and an -end mid-group stops there, whatever another subscriber widened the aggregate to. Today -the model filters whole groups and hands out every cached frame, so local readers -(moq-ffi, moq-mux, in-process apps) see frames they never asked for. - -## Plan - -Found by Codex on #5929 and reproduced on `main` (824179a3a) as well as on that PR: a -subscription starting at frame 5 of group 0 is handed the group from frame 0. #5929's -`Window` collapses each range to group sequences, but the leak predates it: `main`'s -cursor never positioned a delivered group either. Only the datagram path checks the -start frame (`mid_group` in `Cursor::poll_recv_datagram`). - -The wire is not affected: the lite publisher positions each served group per -subscription (`position_group` in `rs/moq-net/src/lite/publisher.rs`, and the same -shape in `js/net/src/lite/publisher.ts`). - -- Keep each range's absolute frame bounds in the `Window` beside its group runs. -- Position every group the cursor hands out (`group::Consumer::start_at`/`end_at`), on - the arrival, ordered, and re-offer paths, using the bounds of the range that covers it. - A group whose head is gone and that was asked for from frame 0 stays a miss, as - `position_group` does today. -- Then let the lite publisher rely on the model's positioning instead of its own, if - that is simpler. -- Check `js/net`'s `track` subscriber for the same shape and mirror the fix. - -Regression test: a start at frame 5 and an end at frame 3 of a cached group, each read -through `recv_group` and `ordered()`. - -## Related - -- [Dynamic range requests](/quest/m1/subscribe-ranges/dynamic.md) - the per-subscriber ranges this extends to frames diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index 6b9f12c909d..f182fab06f1 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -2454,6 +2454,12 @@ impl Consumer { self.cursor.state.read().wire().cloned() } + /// Whether the group was relayed from moq-transport, whose Object IDs may not be its frame + /// ordinals. + pub(super) fn relayed(&self) -> bool { + self.cursor.state.read().wire().is_some() + } + /// Read a compressed group's stored DEFLATE slices instead of plain frames, with each /// frame's inflated size beside it ([`frame::Object::inflated`], /// [`frame::Consumer::inflated`]), for egress that carries compression. A no-op on a diff --git a/rs/moq-net/src/model/subscription.rs b/rs/moq-net/src/model/subscription.rs index 98177f43798..ca15f330985 100644 --- a/rs/moq-net/src/model/subscription.rs +++ b/rs/moq-net/src/model/subscription.rs @@ -75,8 +75,8 @@ pub struct Subscription { /// for nothing. /// /// A request to the publisher, aggregated across every live subscriber as the union of - /// their ranges, and this subscriber's read filter: it reads only the groups its own - /// ranges cover, whatever another subscriber widens the union to. + /// their ranges, and this subscriber's read filter: it reads only the groups and frames + /// its own ranges cover, whatever another subscriber widens the union to. /// [`crate::track::Subscriber::set_groups`] narrows the reads further. See [Local cursor /// vs wire preference](crate::track::Subscriber#local-cursor-vs-wire-preference). pub ranges: Vec, @@ -305,6 +305,7 @@ impl Subscription { /// from the latest group (or none is asked for). /// /// The form a wire without ranges carries: a floor below which nothing is requested. + #[cfg(any(feature = "async", test))] // pending: /quest/m1/public-model/serve.md pub(crate) fn start(&self) -> Option { let mut floor: Option = None; diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index f52c06823a9..0867c7e561e 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -130,6 +130,23 @@ struct Span { floored: bool, } +impl Span { + /// The exclusive end of the groups read now, `None` for none: an unsettled time end + /// reads up to its cap. + fn limit(&self) -> Option { + match self.end { + End::Before(end) => Some(end), + End::Open => None, + End::Time(time) => time.limit, + } + } + + /// Whether group `sequence` is read now. + fn covers(&self, sequence: u64) -> bool { + sequence >= self.start && self.limit().is_none_or(|end| sequence < end) + } +} + #[derive(Clone, Copy, Debug)] enum End { /// Before this group. @@ -372,16 +389,25 @@ impl Window { } /// Whether a datagram is in range, judged on its own and never through a time end's - /// settling: its `timestamp` against each range's times, and its `sequence` against the - /// range's absolute groups and the relative ones resolved so far. - pub fn admits_datagram(&self, sequence: u64, timestamp: Option) -> bool { + /// settling: its `timestamp` against each range's times, its `sequence` against the + /// range's absolute groups and the relative ones resolved so far, and its `object` ID + /// against the frames that same range covers in its group. + pub fn admits_datagram(&self, sequence: u64, timestamp: Option, object: u64) -> bool { + let framed = |range: &Range| { + let (start, end) = frames(range, sequence); + object >= start && end.is_none_or(|end| object < end) + }; let Some(timestamp) = timestamp else { - return self.contains(sequence); + return self.pending + || self + .spans + .iter() + .any(|span| span.covers(sequence) && framed(&self.ranges[span.index])); }; self.ranges .iter() .enumerate() - .filter(|(_, range)| !range.is_empty()) + .filter(|(_, range)| !range.is_empty() && framed(range)) .any(|(index, range)| { // A relative anchor still waiting on a latest group bounds nothing yet. let span = self.spans.iter().find(|span| span.index == index); @@ -459,6 +485,28 @@ impl Window { all.into_iter().chain(runs.into_iter().flatten()) } + /// The frames of group `sequence` in range: from a start frame up to an exclusive end, + /// `None` through the group's end. + /// + /// Only an absolute anchor names a frame, so a group a range starts or ends inside reads + /// part of itself and every other group reads whole. Ranges that cover a group + /// differently read the hull of their frames, since a group is handed out once, as one + /// run of frames. Everything while a relative anchor waits. + pub fn frames(&self, sequence: u64) -> (u64, Option) { + if self.pending { + return (0, None); + } + let mut hull = None; + for span in self.spans.iter().filter(|span| span.covers(sequence)) { + let (start, end) = frames(&self.ranges[span.index], sequence); + hull = Some(match hull { + None => (start, end), + Some((low, high)) => (start.min(low), end.zip(high).map(|(end, high)| end.max(high))), + }); + } + hull.unwrap_or((0, None)) + } + /// The first run of `range` that is in range. pub fn clip(&self, range: ops::Range) -> Option> { let last = range.end.checked_sub(1).filter(|last| *last >= range.start)?; @@ -513,12 +561,7 @@ impl Window { self.settled = !self.pending && !self.spans.iter().any(|span| matches!(span.end, End::Time(_))); self.groups = RangeInclusiveSet::new(); for span in &self.spans { - let end = match span.end { - End::Before(end) => Some(end), - End::Open => None, - End::Time(time) => time.limit, - }; - let last = match end { + let last = match span.limit() { Some(end) => match end.checked_sub(1) { Some(last) if last >= span.start => last, _ => continue, @@ -530,6 +573,21 @@ impl Window { } } +/// The frames of group `sequence` that `range` names, if it covers that group: from a start +/// frame up to an exclusive end, `None` through the group's end. Only an absolute anchor +/// names a frame; a head-of-group end covers nothing of its group, so it never gets here. +fn frames(range: &Range, sequence: u64) -> (u64, Option) { + let start = match range.start { + Anchor::Absolute(start) if start.group == sequence => start.frame, + _ => 0, + }; + let end = match range.end { + Some(Anchor::Absolute(end)) if end.group == sequence => Some(end.frame), + _ => None, + }; + (start, end) +} + /// The first group a range starts at, and whether it names that group rather than /// reaching back to the oldest one. `None` while it waits on a latest group. fn start(anchor: Anchor, latest: Option, holdings: &impl Holdings) -> Option<(u64, bool)> { @@ -722,6 +780,44 @@ mod tests { assert_eq!((window.floor(), window.end()), (Some(0), Some(0))); } + /// Only a group an absolute anchor falls inside reads part of itself, and overlapping + /// ranges read the hull of their frames there. + #[test] + fn frames_bound_only_the_groups_an_anchor_falls_inside() { + let at = |group, frame| Anchor::Absolute(Position { group, frame }); + let window = Window::new(&[range(at(4, 5), at(6, 3))], None, &Untimed).unwrap(); + assert_eq!(window.frames(4), (5, None)); + assert_eq!(window.frames(5), (0, None)); + assert_eq!(window.frames(6), (0, Some(3))); + + // Within one group, both ends bound it. + let window = Window::new(&[range(at(4, 2), at(4, 7))], None, &Untimed).unwrap(); + assert_eq!(window.frames(4), (2, Some(7))); + + // A second range reaching further either way widens the group to cover both. + let window = Window::new(&[range(at(4, 5), at(4, 7)), range(at(4, 2), at(4, 6))], None, &Untimed).unwrap(); + assert_eq!(window.frames(4), (2, Some(7))); + let window = Window::new(&[range(at(4, 5), None), range(at(2, 0), at(4, 3))], None, &Untimed).unwrap(); + assert_eq!(window.frames(4), (0, None)); + + // Relative anchors cover whole groups, and a pending one bounds nothing yet. + let window = Window::new(&[range(Anchor::Relative(1), at(9, 2))], Some(9), &Untimed).unwrap(); + assert_eq!((window.frames(8), window.frames(9)), ((0, None), (0, Some(2)))); + let window = Window::new(&[range(at(4, 5), None), Range::live()], None, &Untimed).unwrap(); + assert_eq!(window.frames(4), (0, None)); + + // A time end covers whole groups, through its cap while it waits to settle. + let groups = Groups::new([Groups::started(4, 4000), Groups::started(5, 5000), (6, Held::Pending)]); + let window = Window::new( + &[range(at(6, 2), None), range(time(4500), time(5000))], + Some(6), + &groups, + ) + .unwrap(); + assert!(!window.is_settled()); + assert_eq!(window.frames(6), (0, None)); + } + /// Relative anchors count back from the latest group, both ends whole groups, and wait /// for a first group when there is none. #[test] @@ -895,8 +991,8 @@ mod tests { &Groups::default(), ) .unwrap(); - assert!(window.admits_datagram(9, Some(at(3500)))); - assert!(!window.admits_datagram(9, Some(at(3600)))); + assert!(window.admits_datagram(9, Some(at(3500)), 0)); + assert!(!window.admits_datagram(9, Some(at(3600)), 0)); } /// Groups whose `racing` one starts past the end after the first time it is asked @@ -1000,18 +1096,18 @@ mod tests { }; let window = Window::new(&[range(time(0), time(5000))], None, &groups).unwrap(); assert_eq!(window.resolved(), vec![Some(Resolved { start: 0, end: Some(2) })]); - assert!(window.admits_datagram(1, Some(at(4000)))); - assert!(!window.admits_datagram(1, Some(at(7000)))); + assert!(window.admits_datagram(1, Some(at(4000)), 0)); + assert!(!window.admits_datagram(1, Some(at(7000)), 0)); } #[test] fn a_waiting_window_judges_a_datagram_against_both_times() { let window = Window::new(&[range(time(3000), time(3500))], None, &Groups::default()).unwrap(); assert!(window.is_pending(), "no group has started"); - assert!(!window.admits_datagram(0, Some(at(2900)))); - assert!(window.admits_datagram(1, Some(at(3000)))); - assert!(window.admits_datagram(2, Some(at(3500)))); - assert!(!window.admits_datagram(3, Some(at(3600)))); + assert!(!window.admits_datagram(0, Some(at(2900)), 0)); + assert!(window.admits_datagram(1, Some(at(3000)), 0)); + assert!(window.admits_datagram(2, Some(at(3500)), 0)); + assert!(!window.admits_datagram(3, Some(at(3600)), 0)); } #[test] @@ -1023,9 +1119,30 @@ mod tests { ) .unwrap(); assert!(window.is_pending(), "no group has started"); - assert!(window.admits_datagram(9, Some(at(3500)))); - assert!(!window.admits_datagram(10, Some(at(3500)))); - assert!(!window.admits_datagram(9, Some(at(2900)))); + assert!(window.admits_datagram(9, Some(at(3500)), 0)); + assert!(!window.admits_datagram(10, Some(at(3500)), 0)); + assert!(!window.admits_datagram(9, Some(at(2900)), 0)); + } + + /// A datagram is in range only when one range admits its sequence, time, and Object ID + /// together, not when each passes a different range. + #[test] + fn a_datagram_is_judged_against_one_range_at_a_time() { + let pos = |group, frame| Anchor::Absolute(Position { group, frame }); + let groups = Groups::new([Groups::started(5, 5000)]); + // The time range covers group 5 whole, but only from 5500 on. + let window = Window::new(&[range(pos(5, 1), None), range(time(5500), None)], Some(5), &groups).unwrap(); + assert!(!window.admits_datagram(5, Some(at(5000)), 0)); + assert!(window.admits_datagram(5, Some(at(5000)), 1)); + assert!(window.admits_datagram(5, Some(at(5500)), 0)); + + // Untimed, two ranges splitting a group admit only the Object IDs each names. + let window = Window::new(&[range(pos(4, 1), pos(4, 2)), range(pos(4, 5), None)], None, &Untimed).unwrap(); + assert_eq!(window.frames(4), (1, None), "a group stream reads the hull"); + let admitted: Vec = (0..7) + .filter(|object| window.admits_datagram(4, None, *object)) + .collect(); + assert_eq!(admitted, vec![1, 5, 6]); } #[test] diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index f2b1c6c54a3..a9c23a418ea 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -5656,7 +5656,21 @@ impl Cursor { } } - fn with_expiry(&self, group: group::Consumer) -> group::Consumer { + /// Hand `group` out: bounded to the frames this cursor's ranges cover in it, and expiring + /// against its budget. + /// + /// Positioned without clamping, so a group missing the head asked for reads it as + /// [`Error::Lagged`], which a route copy recovers from another. A group relayed from + /// moq-transport is handed out whole, since its ranges may name Object IDs rather than + /// frame ordinals, so its reader filters by ID. + fn deliver(&self, mut group: group::Consumer) -> group::Consumer { + if !group.relayed() { + let (start, end) = self.filter.window.frames(group.sequence); + group.skip_to(start); + if let Some(end) = end { + group.end_at(Bound::Excluded(end)); + } + } let sequence = group.sequence; group.with_expiry(Arc::new(GroupExpiry { state: self.state.weak(), @@ -5668,9 +5682,12 @@ impl Cursor { fn poll_recv_group(&mut self, take: bool, waiter: &kio::Waiter) -> Poll>> { if let Some(peeked) = self.peeked.take().filter(|peeked| take && !peeked.ordered) { + // The reserved group is still the one handed out, but bounded by the ranges in + // force now, which a `Control` may have replaced since the peek. + self.sync(); self.advance_arrival(peeked.group.sequence, peeked.index); peeked.group.cache_refresh(); - return Poll::Ready(Ok(Some(self.with_expiry(peeked.group)))); + return Poll::Ready(Ok(Some(self.deliver(peeked.group)))); } self.sync(); if let Some(err) = &self.filter.refused { @@ -5768,7 +5785,7 @@ impl Cursor { continue; } return Poll::Ready(Ok(Some(if take { - self.with_expiry(consumer) + self.deliver(consumer) } else { self.peeked = Some(Box::new(Peeked { group: consumer.clone(), @@ -5791,7 +5808,8 @@ impl Cursor { /// The next datagram inside this subscriber's range, dropping the ones outside it. /// - /// The range is the one groups get: the floor, the cap, and the start Object ID. + /// The range is the one groups get: the floor, the cap, and the frames its group's + /// ranges cover, matched by Object ID. /// Plain datagrams have Object ID 0. A datagram is judged when the cursor reaches /// it, against the range in force then, and one outside it is gone for good: unlike a /// group past the cap, nothing holds it for a later raise. @@ -5811,14 +5829,11 @@ impl Cursor { self.sync(); let sequence = datagram.sequence; - let mid_group = self.subscription.read().start().is_some_and(|start| { - start.group == sequence - && start.frame > datagram.wire.as_ref().and_then(|wire| wire.object_id).unwrap_or(0) - }); // A datagram is a whole group, so its own timestamp places it against a time end. + let object = datagram.wire.as_ref().and_then(|wire| wire.object_id).unwrap_or(0); let admits = super::subscription::before_end(sequence, self.end_sequence) - && self.filter.window.admits_datagram(sequence, datagram.timestamp); - if sequence >= self.floor() && admits && !mid_group { + && self.filter.window.admits_datagram(sequence, datagram.timestamp, object); + if sequence >= self.floor() && admits { return Poll::Ready(Ok(Some(datagram))); } } @@ -5828,6 +5843,8 @@ impl Cursor { /// drift budget has convicted on the way. fn poll_next_group(&mut self, take: bool, waiter: &kio::Waiter) -> Poll>> { if let Some(peeked) = self.peeked.take().filter(|peeked| take && peeked.ordered) { + // Bounded by the ranges in force now, as on the arrival path. + self.sync(); return Poll::Ready(Ok(Some(self.advance_sequence(peeked.group)))); } let Some(next) = self.next_sequence else { @@ -5919,7 +5936,7 @@ impl Cursor { // The delivery commits everything the seek stepped over to reach this group. self.commit_seek_stale(self.next_sequence.unwrap_or(u64::MAX)); group.cache_refresh(); - self.with_expiry(group) + self.deliver(group) } /// Count the sequence path's convictions below `committed` into [`Self::stale`], @@ -6111,6 +6128,10 @@ impl Subscriber { /// that the cache still holds, lowest sequence first. Once the track finishes, a /// reader with nothing left in range ends. /// + /// A group one of [`Subscription::ranges`] starts or ends inside reads only the frames + /// they cover there. One missing the frames asked for reads [`Error::Lagged`] rather + /// than starting short. + /// /// Returns `Poll::Ready(Ok(Some(group)))` when a group is available, /// `Poll::Ready(Ok(None))` when the track is finished, /// `Poll::Ready(Err(e))` when the track has been aborted, or @@ -7446,6 +7467,115 @@ mod test { group } + /// Every group a reader hands out with the frame indices it reads, until one would block. + fn read_groups( + mut next: impl FnMut(&kio::Waiter) -> Poll>>, + ) -> Vec<(u64, Vec)> { + let noop = kio::Waiter::noop(); + let mut groups = Vec::new(); + while let Poll::Ready(group) = next(&noop) { + let Some(mut group) = group.unwrap() else { break }; + let mut frames = Vec::new(); + loop { + let index = group.index(); + match group.poll_read_frame(&noop) { + Poll::Ready(Ok(Some(_))) => frames.push(index), + Poll::Ready(Ok(None)) => break, + res => panic!("group {} read {res:?}", group.sequence), + } + } + groups.push((group.sequence, frames)); + } + groups + } + + /// A range starting and ending inside cached groups reads only the frames it covers, + /// in arrival and sequence order alike, however wide another subscriber made the + /// aggregate. + #[test] + fn a_range_reads_only_its_frames() { + let producer = track_producer("test", Info::default()); + let _groups: Vec<_> = (3..8) + .map(|sequence| { + let mut group = framed(&producer, sequence, 8); + group.finish().unwrap(); + group + }) + .collect(); + let _wide = producer.subscribe(replay()); + let range = Range { + start: Anchor::Absolute(Position { group: 4, frame: 5 }), + end: Some(Anchor::Absolute(Position { group: 6, frame: 3 })), + }; + let bounded = || producer.subscribe(replay().with_ranges([range])); + + let want = vec![(4, vec![5, 6, 7]), (5, (0..8).collect()), (6, vec![0, 1, 2])]; + let mut arrival = bounded(); + assert_eq!(read_groups(|waiter| arrival.poll_recv_group(waiter)), want); + let mut ordered = bounded().ordered(); + assert_eq!(read_groups(|waiter| ordered.poll_next_group(waiter)), want); + } + + /// A group missing the frames a range asks for is never served short: reading them is + /// [`Error::Lagged`], which a route copy recovers. One holding them from where the range + /// starts is served from there. + #[test] + fn a_group_missing_its_asked_head_lags() { + let producer = track_producer("test", Info::default()); + let mut headless = producer.create_group(group::Info { sequence: 4 }).unwrap(); + headless.start_at(2).unwrap(); + for _ in 0..3 { + headless.write_frame(None, &b"x"[..]).unwrap(); + } + headless.finish().unwrap(); + + let noop = kio::Waiter::noop(); + for frame in [0, 1] { + let start = Anchor::Absolute(Position { group: 4, frame }); + let mut sub = producer.subscribe(replay().with_ranges([Range { start, end: None }])); + let Poll::Ready(Ok(Some(mut group))) = sub.poll_recv_group(&noop) else { + panic!("from frame {frame}: no group"); + }; + assert_eq!(group.index(), frame); + assert!( + matches!(group.poll_read_frame(&noop), Poll::Ready(Err(Error::Lagged))), + "from frame {frame}" + ); + } + + let start = Anchor::Absolute(Position { group: 4, frame: 2 }); + let mut sub = producer.subscribe(replay().with_ranges([Range { start, end: None }])); + assert_eq!( + read_groups(|waiter| sub.poll_recv_group(waiter)), + vec![(4, vec![2, 3, 4])] + ); + } + + /// A range replaced between a peek and the take re-bounds the reserved group: it is + /// still the group handed out, positioned by the ranges in force when it is taken. + #[test] + fn a_peeked_group_is_bounded_by_the_ranges_at_handout() { + for ordered in [false, true] { + let producer = track_producer("test", Info::default()); + framed(&producer, 4, 8).finish().unwrap(); + let mut subscriber = producer.subscribe(replay()); + let waiter = kio::Waiter::noop(); + assert_eq!( + subscriber.poll_peek_group(ordered, &waiter).map(Result::unwrap), + Poll::Ready(Some(4)) + ); + let start = Anchor::Absolute(Position { group: 4, frame: 5 }); + subscriber + .control() + .update(replay().with_ranges([Range { start, end: None }])) + .unwrap(); + let Poll::Ready(Ok(Some(group))) = subscriber.poll_group(ordered, &waiter) else { + panic!("ordered={ordered}: the reserved group must be returned"); + }; + assert_eq!((group.sequence, group.index()), (4, 5), "ordered={ordered}"); + } + } + /// The largest reports only while it is within the bounds: a cap below it or a floor /// above it reports nothing, rather than a cached group standing in for it. #[test] @@ -8128,12 +8258,19 @@ mod test { } /// A start past the start group's first frame excludes that group's datagram, its only - /// frame; a start at frame 0 keeps it. + /// frame, even behind an earlier range; a start at frame 0 keeps it. #[test] #[cfg(feature = "async")] fn a_mid_group_start_drops_the_start_groups_datagram() { let mut producer = track_producer("test", Info::timed()); let mut mid = producer.subscribe(Subscription::default().with_start(Position { group: 5, frame: 1 })); + let mut split = producer.subscribe(Subscription::default().with_ranges([ + Range::groups(2..4), + Range { + start: Anchor::Absolute(Position { group: 5, frame: 1 }), + end: None, + }, + ])); let mut whole = producer.subscribe(Subscription::default().with_start(Position::group(5))); for sequence in [5, 6] { producer @@ -8142,6 +8279,7 @@ mod test { } assert_eq!(recv_datagram(&mut mid).sequence, 6); + assert_eq!(recv_datagram(&mut split).sequence, 6); assert_eq!(recv_datagram(&mut whole).sequence, 5); assert_eq!(recv_datagram(&mut whole).sequence, 6); }