refactor(client): improve session initialization and state access

This commit is contained in:
Nils Andreas Svee committed 2026-05-02 00:01:31 +07:00
1 parent e63b176265
commit 851d143f86
1 file changed
+19 -6
+19 -6
View File
@@ -85,7 +85,7 @@ impl Client {
} }
} }
fn open_inner(&mut self, now: ClientTimestamp) -> Result<OpenOutcome, ClientError> { fn open_inner(&mut self, _now: ClientTimestamp) -> Result<OpenOutcome, ClientError> {
let request = OpenRequest { let request = OpenRequest {
params: self.requested.clone(), params: self.requested.clone(),
close: self.config.run_mode == RunMode::NoTest, close: self.config.run_mode == RunMode::NoTest,
@@ -103,7 +103,7 @@ impl Client {
&buf[..size], &buf[..size],
self.config.hmac_key.as_deref(), self.config.hmac_key.as_deref(),
)?; )?;
return self.accept_open_reply(reply, now); return self.accept_open_reply(reply, ClientTimestamp::now());
} }
Err(err) Err(err)
if matches!( if matches!(
@@ -150,6 +150,10 @@ impl Client {
Some(session.next_send_at) Some(session.next_send_at)
} }
pub fn probe_timeout(&self) -> Duration {
self.config.probe_timeout
}
pub fn send_probe(&mut self) -> Result<Vec<ClientEvent>, ClientError> { pub fn send_probe(&mut self) -> Result<Vec<ClientEvent>, ClientError> {
self.send_probe_inner(None) self.send_probe_inner(None)
} }
@@ -339,7 +343,7 @@ impl Client {
Ok(events) Ok(events)
} }
pub(crate) fn is_run_complete(&self) -> bool { pub fn is_run_complete(&self) -> bool {
let Some(session) = self.session.as_ref() else { let Some(session) = self.session.as_ref() else {
return matches!( return matches!(
self.phase, self.phase,
@@ -1617,8 +1621,17 @@ mod tests {
wall: SystemTime::now(), wall: SystemTime::now(),
}; };
assert_open_started(client.open(start).unwrap()); assert_open_started(client.open(start).unwrap());
let session_start = client.session.as_ref().unwrap().start_mono;
assert!(
session_start >= start.mono,
"probe schedule must start after open begins"
);
let first_probe_at = ClientTimestamp {
mono: session_start,
wall: SystemTime::now(),
};
let events = client.send_probe_at(start).unwrap(); let events = client.send_probe_at(first_probe_at).unwrap();
match &events[0] { match &events[0] {
ClientEvent::EchoSent { ClientEvent::EchoSent {
scheduled_at, scheduled_at,
@@ -1626,8 +1639,8 @@ mod tests {
timer_error, timer_error,
.. ..
} => { } => {
assert_eq!(*scheduled_at, start.mono); assert_eq!(*scheduled_at, session_start);
assert_eq!(*sent_at, start); assert_eq!(*sent_at, first_probe_at);
assert_eq!(*timer_error, Duration::ZERO); assert_eq!(*timer_error, Duration::ZERO);
} }
other => panic!("expected EchoSent, got {other:?}"), other => panic!("expected EchoSent, got {other:?}"),