Files
nym/common/test-utils/src/traits.rs
T
Jędrzej Stuczyński 0ee387d983 Feature/cancellation migration (#6014)
* squashing work on using cancellation in nym crates

making nym-task wasm compilable

removed sending of status messages

replaced TaskManager with ShutdownManager in the validator rewarder

additional helpers for ShutdownManager

simplified ShutdownToken by removing the name field

TaskClient => ShutdownToken within all client tasks

wip: remove TaskHandle

* track all long-living client tasks

* add task tracking for most top level tasks within nym-node

* improved default builder

* split up cancellation module

* module documentation and unit tests

* nym node fixes and naming consistency

* wasm fixes

* assert_eq => assert

* wasm fixes and made 'run_until_shutdown' take reference instead of ownership

* linux-specific fixes to IpPacketRouter

* post rebasing fixes for signing monitor

* add ShutdownManager constructor to build it from an external token

* applying PR review suggestions
2025-09-10 13:56:39 +01:00

68 lines
1.8 KiB
Rust

// Copyright 2025 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use crate::helpers::{leak, spawn_timeboxed};
use std::future::{Future, IntoFuture};
use std::time::Duration;
use tokio::task::JoinHandle;
use tokio::time::error::Elapsed;
// a helper trait for use in tests to easily convert `T` into `&'static mut T`
pub trait Leak {
fn leak(self) -> &'static mut Self;
}
impl<T> Leak for T {
fn leak(self) -> &'static mut T {
leak(self)
}
}
// those are internal testing traits so we're not concerned about auto traits
#[allow(async_fn_in_trait)]
pub trait Timeboxed: IntoFuture + Sized {
async fn timeboxed(self) -> Result<Self::Output, Elapsed> {
self.execute_with_deadline(Duration::from_millis(200)).await
}
async fn execute_with_deadline(self, timeout: Duration) -> Result<Self::Output, Elapsed> {
tokio::time::timeout(timeout, self).await
}
}
impl<T> Timeboxed for T where T: IntoFuture + Sized {}
pub trait ElapsedExt {
fn has_elapsed(&self) -> bool;
}
impl<T> ElapsedExt for Result<T, Elapsed> {
fn has_elapsed(&self) -> bool {
self.is_err()
}
}
// those are internal testing traits so we're not concerned about auto traits
#[allow(async_fn_in_trait)]
pub trait Spawnable: Future + Sized + Send + 'static {
fn spawn(self) -> JoinHandle<Self::Output>
where
<Self as Future>::Output: Send + 'static,
{
tokio::spawn(self)
}
}
impl<T> Spawnable for T where T: Future + Sized + Send + 'static {}
pub trait TimeboxedSpawnable: Timeboxed + Spawnable {
fn spawn_timeboxed(self) -> JoinHandle<Result<<Self as Future>::Output, Elapsed>>
where
<Self as Future>::Output: Send,
{
spawn_timeboxed(self)
}
}
impl<T> TimeboxedSpawnable for T where T: Spawnable + Future + Send {}