cultnet-rs presence publisher tests: the lease wait clips a late ack to the timeout
A held sink makes the ack late by construction; the test releases it after the wait returns. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
17c1d6e758
commit
c8c02110fb
1 changed files with 52 additions and 1 deletions
|
|
@ -49,6 +49,9 @@ enum Sink {
|
|||
Accept,
|
||||
/// Accepts after this long, so the ack comes late.
|
||||
AcceptAfter(Duration),
|
||||
/// Accepts only once the test calls `Odin::release`, so the ack is late by
|
||||
/// construction, not by a sleep race.
|
||||
Hold,
|
||||
Refuse,
|
||||
}
|
||||
|
||||
|
|
@ -57,6 +60,7 @@ struct Odin {
|
|||
addr: SocketAddr,
|
||||
received: Arc<Mutex<Vec<Delivered>>>,
|
||||
stop: Arc<AtomicBool>,
|
||||
gate: Arc<AtomicBool>,
|
||||
thread: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
|
|
@ -70,16 +74,25 @@ impl Odin {
|
|||
let addr = socket.local_addr().unwrap();
|
||||
let received = Arc::new(Mutex::new(Vec::new()));
|
||||
let stop = Arc::new(AtomicBool::new(false));
|
||||
let gate = Arc::new(AtomicBool::new(false));
|
||||
let held = Arc::clone(&gate);
|
||||
let stopped = Arc::clone(&stop);
|
||||
let record = Arc::clone(&received);
|
||||
let mut server = CultMeshRudpDocumentServer::new(
|
||||
socket,
|
||||
move |receipt: CultMeshRudpRawDocumentReceipt| -> Result<()> {
|
||||
match sink {
|
||||
Sink::Refuse => bail!(SINK_CANARY),
|
||||
Sink::Accept | Sink::AcceptAfter(_) => {
|
||||
Sink::Accept | Sink::AcceptAfter(_) | Sink::Hold => {
|
||||
if let Sink::AcceptAfter(delay) = sink {
|
||||
std::thread::sleep(delay);
|
||||
}
|
||||
while matches!(sink, Sink::Hold)
|
||||
&& !held.load(Ordering::Acquire)
|
||||
&& !stopped.load(Ordering::Relaxed)
|
||||
{
|
||||
std::thread::sleep(Duration::from_millis(1));
|
||||
}
|
||||
record.lock().unwrap().push(Delivered {
|
||||
message_id: receipt.message_id,
|
||||
record_key: receipt.document.record_key,
|
||||
|
|
@ -111,10 +124,16 @@ impl Odin {
|
|||
addr,
|
||||
received,
|
||||
stop,
|
||||
gate,
|
||||
thread: Some(thread),
|
||||
}
|
||||
}
|
||||
|
||||
/// Lets a `Sink::Hold` sink accept, which sends the held ack.
|
||||
fn release(&self) {
|
||||
self.gate.store(true, Ordering::Release);
|
||||
}
|
||||
|
||||
fn deliveries(&self) -> Vec<Delivered> {
|
||||
self.received.lock().unwrap().clone()
|
||||
}
|
||||
|
|
@ -671,6 +690,38 @@ mod lease_wait {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// A reachable Odin whose sink holds the ack until the test releases it,
|
||||
/// after the wait returned. The 2 s ack wait is far longer than the
|
||||
/// timeout, so only the clip to the time remaining ends the heartbeat in
|
||||
/// time. The sink is held, not slept, so there is no race to lose: the ack
|
||||
/// cannot arrive before the wait returns.
|
||||
#[test]
|
||||
fn lease_wait_never_overruns_the_timeout_on_a_late_ack() -> Result<()> {
|
||||
let odin = Odin::start(Sink::Hold);
|
||||
let rig = odin_rig(true, &odin.rudp_endpoint())?;
|
||||
let mut signer = rig.signer()?;
|
||||
let cadence = Cadence {
|
||||
heartbeat: Duration::from_secs(5),
|
||||
connect: Duration::from_secs(2),
|
||||
ack: Duration::from_secs(2),
|
||||
..TEST_CADENCE
|
||||
};
|
||||
let publisher = publisher_for(&signer, cadence)?;
|
||||
let timeout = Duration::from_millis(400);
|
||||
let slack = Duration::from_millis(300);
|
||||
|
||||
let started = Instant::now();
|
||||
let refused = refusal(publisher.wait_for_write_lease(&mut signer, timeout));
|
||||
let elapsed = started.elapsed();
|
||||
odin.release();
|
||||
|
||||
assert!(elapsed >= timeout, "{elapsed:?}");
|
||||
assert!(elapsed < timeout + slack, "{elapsed:?}");
|
||||
assert!(refused.contains("1 failed deliveries"), "{refused}");
|
||||
assert!(refused.contains("ack_timeout"), "{refused}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lease_wait_timeout_counts_only_failed_deliveries() -> Result<()> {
|
||||
let odin = Odin::start(Sink::Accept);
|
||||
|
|
|
|||
Loading…
Reference in a new issue