feat(managed): implement automatic cancellation on session drop

This commit is contained in:
Nils Andreas Svee committed 2026-05-10 20:56:43 +07:00
1 parent 4da8f962d4
commit a2d27b154b
1 file changed
+12 -5
+12 -5
View File
@@ -28,7 +28,7 @@ pub struct ManagedClient;
pub struct ManagedClientSession { pub struct ManagedClientSession {
hub: EventHub, hub: EventHub,
cancellation: CancellationToken, cancellation: CancellationToken,
worker: JoinHandle<Result<SessionOutcome, ClientError>>, worker: Option<JoinHandle<Result<SessionOutcome, ClientError>>>,
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@@ -92,7 +92,7 @@ impl ManagedClient {
ManagedClientSession { ManagedClientSession {
hub, hub,
cancellation, cancellation,
worker, worker: Some(worker),
}, },
initial_subscription, initial_subscription,
)) ))
@@ -108,8 +108,9 @@ impl ManagedClientSession {
self.cancellation.cancel(); self.cancellation.cancel();
} }
pub fn join(self) -> Result<SessionOutcome, ClientError> { pub fn join(mut self) -> Result<SessionOutcome, ClientError> {
match self.worker.join() { let worker = self.worker.take().expect("worker handle must be present");
match worker.join() {
Ok(outcome) => { Ok(outcome) => {
self.hub.disconnect_all(); self.hub.disconnect_all();
outcome outcome
@@ -122,6 +123,12 @@ impl ManagedClientSession {
} }
} }
impl Drop for ManagedClientSession {
fn drop(&mut self) {
self.cancellation.cancel();
}
}
fn publish_open_outcome(hub: &EventHub, outcome: &OpenOutcome) { fn publish_open_outcome(hub: &EventHub, outcome: &OpenOutcome) {
match outcome { match outcome {
OpenOutcome::Started { event, .. } | OpenOutcome::NoTestCompleted { event, .. } => { OpenOutcome::Started { event, .. } | OpenOutcome::NoTestCompleted { event, .. } => {
@@ -495,7 +502,7 @@ mod tests {
let session = ManagedClientSession { let session = ManagedClientSession {
hub, hub,
cancellation, cancellation,
worker, worker: Some(worker),
}; };
assert!(matches!(session.join(), Err(ClientError::WorkerPanicked))); assert!(matches!(session.join(), Err(ClientError::WorkerPanicked)));