use super::InnerNetStatusClient;
use super::MonitorInitialization;
use crate::common::CommonEngine;
use crate::error::NetError;
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Weak};
use std::task::Poll;
use std::time::Duration;
use tokio::sync::{mpsc, oneshot, watch};
pub(super) type TestResult<T = ()> = Result<T, Box<dyn std::error::Error + Send + Sync>>;
pub(crate) type MonitorFactory = Arc<
dyn Fn() -> Pin<Box<dyn Future<Output = Result<netwatch::netmon::Monitor, NetError>> + Send>>
+ Send
+ Sync,
>;
#[path = "provider_barrier_tests.rs"]
mod provider_barrier_tests;
struct Attempt {
release: oneshot::Sender<Result<(), NetError>>,
}
struct Gate {
attempts: mpsc::UnboundedReceiver<Attempt>,
calls: Arc<AtomicUsize>,
}
#[track_caller]
pub(super) fn check(condition: bool, message: &str) -> TestResult {
if condition {
Ok(())
} else {
let location = std::panic::Location::caller();
Err(std::io::Error::other(format!("{location}: {message}")).into())
}
}
async fn automatic_cleanup_is_awaited(permanent: bool) -> TestResult {
let (client, _gate) = client_with_gate()?;
let installed = install_unpolled(&client)?;
let state = client.current_state()?.ok_or("monitor state missing")?;
let mut stop = Box::pin(async {
if permanent {
client.destroy().await
} else {
client.stop().await
}
});
{
let state_guard = state.lock().map_err(NetError::from_poison)?;
let worker = std::thread::Builder::new().spawn(move || drop(installed.task))?;
let deadline = std::time::Instant::now() + Duration::from_secs(5);
while client
.lifecycle
.lock()
.map_err(NetError::from_poison)?
.monitor
.is_some()
{
check(
std::time::Instant::now() < deadline,
"monitor slot did not retire",
)?;
std::thread::yield_now();
}
let mut context = std::task::Context::from_waker(std::task::Waker::noop());
let pending = stop.as_mut().poll(&mut context).is_pending();
drop(state_guard);
check(worker.join().is_ok(), "cleanup worker failed")?;
check(
pending,
"stop returned while automatic cleanup was still releasing resources",
)?;
}
bounded(stop).await??;
Ok(())
}
struct DestroyOnDrop {
client: Weak<InnerNetStatusClient>,
result: Option<oneshot::Sender<Result<(), String>>>,
}
impl Drop for DestroyOnDrop {
fn drop(&mut self) {
let result = match self.client.upgrade() {
Some(client) => {
let runtime = client.engine.runtime_handle();
let _entered = runtime.enter();
futures::executor::block_on(async {
bounded(client.destroy())
.await
.map_err(|error| error.to_string())?
.map_err(|error| format!("{error:?}"))
})
}
None => Err("client was dropped before its capture".to_owned()),
};
if let Some(sender) = self.result.take() {
let _ = sender.send(result);
}
}
}
#[tokio::test]
async fn automatic_cleanup_allows_capture_drop_to_await_destroy() -> TestResult {
let (client, _gate) = client_with_gate()?;
let installed = install_unpolled(&client)?;
let (result, received) = oneshot::channel();
let capture = DestroyOnDrop {
client: Arc::downgrade(&client),
result: Some(result),
};
let subscription = client.subscribe_state()?.into_callback(move |_, _| {
let _ = &capture;
})?;
let worker = std::thread::Builder::new().spawn(move || {
drop(installed.task);
drop(subscription);
})?;
let result = bounded(received).await??;
check(worker.join().is_ok(), "destroy capture worker failed")?;
result.map_err(Into::into)
}
#[tokio::test]
async fn destroy_waits_for_automatic_failure_cleanup() -> TestResult {
automatic_cleanup_is_awaited(true).await
}
#[tokio::test]
async fn shutdown_waits_for_automatic_failure_cleanup() -> TestResult {
automatic_cleanup_is_awaited(false).await
}
async fn bounded<T>(future: impl Future<Output = T>) -> TestResult<T> {
Ok(tokio::time::timeout(Duration::from_secs(5), future).await?)
}
async fn poll_once<F: Future>(future: Pin<&mut F>) -> Poll<F::Output> {
let mut future = future;
std::future::poll_fn(|context| Poll::Ready(future.as_mut().poll(context))).await
}
fn client_with_gate() -> TestResult<(Arc<InnerNetStatusClient>, Gate)> {
let client = Arc::new(InnerNetStatusClient::new(Arc::new(
CommonEngine::new_with_runtime_worker_threads(16, 16, Some(1)).map_err(NetError::from)?,
))?);
let (sender, attempts) = mpsc::unbounded_channel();
let calls = Arc::new(AtomicUsize::new(0));
let counted = Arc::clone(&calls);
let factory: MonitorFactory = Arc::new(move || {
counted.fetch_add(1, Ordering::SeqCst);
let sender = sender.clone();
Box::pin(async move {
let (release, wait) = oneshot::channel();
sender
.send(Attempt { release })
.map_err(|_| NetError::from(crate::error::ErrorKind::RuntimeUnavailable))?;
wait.await
.map_err(|_| NetError::from(crate::error::ErrorKind::RuntimeUnavailable))??;
netwatch::netmon::Monitor::new()
.await
.map_err(|_| NetError::from(crate::error::ErrorKind::RuntimeUnavailable))
})
});
*client
.monitor_factory
.lock()
.map_err(NetError::from_poison)? = Some(factory);
Ok((client, Gate { attempts, calls }))
}
impl Gate {
async fn next(&mut self) -> TestResult<Attempt> {
bounded(self.attempts.recv())
.await?
.ok_or_else(|| std::io::Error::other("monitor factory closed").into())
}
}
impl Attempt {
fn finish(self, result: Result<(), NetError>) -> TestResult {
self.release
.send(result)
.map_err(|_| std::io::Error::other("initialization was cancelled").into())
}
}
fn completion(client: &InnerNetStatusClient) -> TestResult<watch::Receiver<bool>> {
client
.lifecycle
.lock()
.map_err(NetError::from_poison)?
.monitor
.as_ref()
.map(|monitor| monitor.finished.clone())
.ok_or_else(|| std::io::Error::other("missing monitor generation").into())
}
async fn finished(mut receiver: watch::Receiver<bool>) -> TestResult {
bounded(async move {
while !*receiver.borrow_and_update() {
if receiver.changed().await.is_err() {
break;
}
}
})
.await
}
#[tokio::test]
async fn initialization_failure_is_returned_and_next_start_retries() -> TestResult {
let (client, mut gate) = client_with_gate()?;
let mut starting = Box::pin(client.start());
check(
matches!(poll_once(starting.as_mut()).await, Poll::Pending),
"start must wait",
)?;
gate.next().await?.finish(Err(NetError::from(
crate::error::ErrorKind::RuntimeUnavailable,
)))?;
check(
bounded(starting)
.await?
.as_ref()
.map(|_| ())
.map_err(|error| error.kind())
== Err(crate::error::ErrorKind::RuntimeUnavailable),
"initialization failure was reported as success",
)?;
check(
!client.is_started(),
"failed monitor was reported as started",
)?;
check(
client
.lifecycle
.lock()
.map_err(NetError::from_poison)?
.monitor
.is_none(),
"failed generation remained installed",
)?;
let mut retry = Box::pin(client.start());
check(
matches!(poll_once(retry.as_mut()).await, Poll::Pending),
"retry did not start a new constructor",
)?;
gate.next().await?.finish(Ok(()))?;
bounded(retry).await??;
check(client.is_started(), "successful retry is not running")?;
check(
gate.calls.load(Ordering::SeqCst) == 2,
"retry constructor count changed",
)?;
bounded(client.stop()).await??;
Ok(())
}
#[tokio::test]
async fn concurrent_starts_share_one_pending_failure() -> TestResult {
let (client, mut gate) = client_with_gate()?;
let mut starts = Vec::new();
for _ in 0..8 {
let mut start = Box::pin(client.start());
check(
matches!(poll_once(start.as_mut()).await, Poll::Pending),
"concurrent start did not wait",
)?;
starts.push(start);
}
let attempt = gate.next().await?;
check(
!client.is_started(),
"pending initialization was reported as running",
)?;
check(
gate.calls.load(Ordering::SeqCst) == 1,
"concurrent starts created multiple generations",
)?;
attempt.finish(Err(NetError::from(
crate::error::ErrorKind::RuntimeUnavailable,
)))?;
for start in starts {
check(
bounded(start)
.await?
.as_ref()
.map(|_| ())
.map_err(|error| error.kind())
== Err(crate::error::ErrorKind::RuntimeUnavailable),
"same generation did not share its failure",
)?;
}
check(!client.is_started(), "completed failure remained started")?;
Ok(())
}
#[tokio::test]
async fn cancelled_start_waiter_does_not_prevent_failure_cleanup() -> TestResult {
let (client, mut gate) = client_with_gate()?;
let mut starting = Box::pin(client.start());
check(
matches!(poll_once(starting.as_mut()).await, Poll::Pending),
"start must wait",
)?;
let attempt = gate.next().await?;
let done = completion(&client)?;
drop(starting);
attempt.finish(Err(NetError::from(
crate::error::ErrorKind::RuntimeUnavailable,
)))?;
finished(done).await?;
check(
!client.is_started(),
"cancelled waiter left a failed monitor running",
)?;
check(
client
.lifecycle
.lock()
.map_err(NetError::from_poison)?
.monitor
.is_none(),
"background failure did not retire the generation",
)?;
Ok(())
}
struct InstalledMonitor {
task: Pin<Box<dyn Future<Output = ()> + Send>>,
initial: watch::Receiver<MonitorInitialization>,
done: watch::Receiver<bool>,
active: Arc<AtomicBool>,
}
fn install_unpolled(client: &InnerNetStatusClient) -> TestResult<InstalledMonitor> {
let (monitor, task, publication) = client.prepare_monitor_task()?;
let installed = InstalledMonitor {
task: Box::pin(task),
initial: monitor.initial_state.clone(),
done: monitor.finished.clone(),
active: Arc::clone(&monitor.active),
};
let mut lifecycle = client.lifecycle.lock().map_err(NetError::from_poison)?;
check(
lifecycle.monitor.is_none(),
"test attempted to replace a live monitor",
)?;
lifecycle.monitor = Some(monitor);
drop(lifecycle);
publication.dispatch()?;
Ok(installed)
}
async fn initialized(
mut initial: watch::Receiver<MonitorInitialization>,
) -> TestResult<MonitorInitialization> {
bounded(async move {
while matches!(*initial.borrow_and_update(), MonitorInitialization::Pending) {
initial.changed().await?;
}
Ok::<_, Box<dyn std::error::Error + Send + Sync>>(
::tokio::sync::watch::Receiver::borrow(&initial).clone(),
)
})
.await?
}
#[tokio::test]
async fn concurrent_successful_starts_share_one_detector() -> TestResult {
let (client, mut gate) = client_with_gate()?;
let mut starts = Vec::new();
for _ in 0..8 {
let mut start = Box::pin(client.start());
check(
matches!(poll_once(start.as_mut()).await, Poll::Pending),
"start must share pending initialization",
)?;
starts.push(start);
}
gate.next().await?.finish(Ok(()))?;
for start in starts {
bounded(start).await??;
}
check(
client.is_started(),
"shared successful monitor was not running",
)?;
check(
gate.calls.load(Ordering::SeqCst) == 1,
"successful starts duplicated the detector",
)?;
bounded(client.stop()).await??;
Ok(())
}
#[tokio::test]
async fn old_failure_waiters_cannot_retire_a_successful_retry() -> TestResult {
let (client, mut gate) = client_with_gate()?;
let mut old = Box::pin(client.start());
check(
matches!(poll_once(old.as_mut()).await, Poll::Pending),
"old start must wait",
)?;
let old_done = completion(&client)?;
gate.next().await?.finish(Err(NetError::from(
crate::error::ErrorKind::RuntimeUnavailable,
)))?;
finished(old_done).await?;
let mut fresh = Box::pin(client.start());
check(
matches!(poll_once(fresh.as_mut()).await, Poll::Pending),
"new start did not retry",
)?;
gate.next().await?.finish(Ok(()))?;
bounded(fresh).await??;
let current = client
.current_state()?
.ok_or_else(|| std::io::Error::other("new state missing"))?;
check(
bounded(old)
.await?
.as_ref()
.map(|_| ())
.map_err(|error| error.kind())
== Err(crate::error::ErrorKind::RuntimeUnavailable),
"old failure was changed by retry",
)?;
let after = client
.current_state()?
.ok_or_else(|| std::io::Error::other("new state was removed"))?;
check(
Arc::ptr_eq(¤t, &after) && client.is_started(),
"old waiter changed new generation",
)?;
bounded(client.stop()).await??;
Ok(())
}
#[tokio::test]
async fn cancelling_start_waiter_preserves_successful_background_initialization() -> TestResult {
let (client, mut gate) = client_with_gate()?;
let mut start = Box::pin(client.start());
check(
matches!(poll_once(start.as_mut()).await, Poll::Pending),
"start must wait",
)?;
let initial = client
.lifecycle
.lock()
.map_err(NetError::from_poison)?
.monitor
.as_ref()
.map(|monitor| monitor.initial_state.clone())
.ok_or_else(|| std::io::Error::other("initialization channel missing"))?;
let attempt = gate.next().await?;
drop(start);
attempt.finish(Ok(()))?;
check(
matches!(initialized(initial).await?, MonitorInitialization::Ready),
"background initialization failed",
)?;
check(
client.is_started(),
"cancelled waiter stopped a successful monitor",
)?;
bounded(client.stop()).await??;
Ok(())
}
#[tokio::test]
async fn shutdown_cancels_pending_initialization_and_allows_restart() -> TestResult {
let (client, mut gate) = client_with_gate()?;
let mut start = Box::pin(client.start());
check(
matches!(poll_once(start.as_mut()).await, Poll::Pending),
"start must wait",
)?;
let attempt = gate.next().await?;
bounded(client.stop()).await??;
bounded(start).await??;
check(
attempt.release.send(Ok(())).is_err(),
"shutdown retained the old constructor",
)?;
check(!client.is_started(), "shutdown left monitor running")?;
let mut retry = Box::pin(client.start());
check(
matches!(poll_once(retry.as_mut()).await, Poll::Pending),
"shutdown disabled retry",
)?;
gate.next().await?.finish(Ok(()))?;
bounded(retry).await??;
bounded(client.stop()).await??;
Ok(())
}
#[tokio::test]
async fn destroy_cancels_pending_initialization_and_rejects_restart() -> TestResult {
let (client, mut gate) = client_with_gate()?;
let mut start = Box::pin(client.start());
check(
matches!(poll_once(start.as_mut()).await, Poll::Pending),
"start must wait",
)?;
let attempt = gate.next().await?;
bounded(client.destroy()).await??;
check(
bounded(start)
.await?
.as_ref()
.map(|_| ())
.map_err(|error| error.kind())
== Err(crate::error::ErrorKind::Closed),
"destroyed initialization returned success",
)?;
check(
bounded(client.start())
.await?
.as_ref()
.map(|_| ())
.map_err(|error| error.kind())
== Err(crate::error::ErrorKind::Closed),
"destroy permitted restart",
)?;
check(
attempt.release.send(Ok(())).is_err(),
"destroy retained the pending constructor",
)?;
check(
gate.calls.load(Ordering::SeqCst) == 1 && !client.is_started(),
"destroy created or retained a detector",
)?;
Ok(())
}
#[tokio::test]
async fn dropping_unpolled_task_retires_generation_and_reports_failure() -> TestResult {
let (client, gate) = client_with_gate()?;
let installed = install_unpolled(&client)?;
drop(installed.task);
check(
matches!(&*::tokio::sync::watch::Receiver::borrow(&installed.initial), MonitorInitialization::Failed(error) if error.kind() == crate::error::ErrorKind::RuntimeUnavailable),
"unpolled task did not report failure",
)?;
check(
*::tokio::sync::watch::Receiver::borrow(&installed.done)
&& !installed.active.load(Ordering::Acquire),
"unpolled task did not finish",
)?;
check(
client
.lifecycle
.lock()
.map_err(NetError::from_poison)?
.monitor
.is_none(),
"unpolled task retained its generation",
)?;
check(
gate.calls.load(Ordering::SeqCst) == 0,
"unpolled task unexpectedly invoked factory",
)?;
Ok(())
}
#[tokio::test]
async fn retired_task_exits_before_restart_and_late_facts_cannot_clear_new_generation() -> TestResult
{
let (client, mut gate) = client_with_gate()?;
let old = install_unpolled(&client)?;
let old_state = client.current_state()?.ok_or("old state missing")?;
let (_, error) = client.request_stop(false);
if let Some(error) = error {
return Err(error.into());
}
let mut fresh = Box::pin(client.start());
check(
matches!(poll_once(fresh.as_mut()).await, Poll::Pending),
"fresh start must wait",
)?;
check(
client.current_state()?.is_none(),
"restart skipped old task completion",
)?;
drop(old.task);
check(
matches!(
&*::tokio::sync::watch::Receiver::borrow(&old.initial),
MonitorInitialization::Stopped
) && *::tokio::sync::watch::Receiver::borrow(&old.done),
"old stopped task did not settle",
)?;
check(
poll_once(fresh.as_mut()).await.is_pending(),
"fresh constructor did not wait",
)?;
gate.next().await?.finish(Ok(()))?;
bounded(fresh).await??;
let current = client
.current_state()?
.ok_or_else(|| std::io::Error::other("fresh state missing"))?;
InnerNetStatusClient::update_state_inner(
&old_state,
crate::module::net_status::NetworkStatus::Unavailable,
crate::module::net_status::IpStack::V4Only,
)?;
let after = client
.current_state()?
.ok_or_else(|| std::io::Error::other("old cleanup removed new state"))?;
check(
Arc::ptr_eq(¤t, &after) && client.is_started(),
"old cleanup replaced the new generation",
)?;
check(
::tokio::sync::watch::Receiver::borrow(&client.subscribe())
.status
.is_some(),
"old cleanup cleared fresh observation",
)?;
bounded(client.stop()).await??;
Ok(())
}
#[tokio::test]
async fn initial_observation_failure_is_propagated_and_retired() -> TestResult {
let (client, mut gate) = client_with_gate()?;
let mut start = Box::pin(client.start());
check(
matches!(poll_once(start.as_mut()).await, Poll::Pending),
"start must wait",
)?;
let attempt = gate.next().await?;
client.observations.exhaust_revision_for_test()?;
attempt.finish(Ok(()))?;
check(
bounded(start)
.await?
.as_ref()
.map(|_| ())
.map_err(|error| error.kind())
== Err(crate::error::ErrorKind::Internal),
"initial publication failure was discarded",
)?;
check(
!client.is_started() && client.current_state()?.is_none(),
"failed publication retained a monitor",
)?;
check(
::tokio::sync::watch::Receiver::borrow(&client.subscribe())
.status
.is_none(),
"failed publication manufactured a network state",
)?;
Ok(())
}
#[tokio::test]
async fn aborted_running_task_is_retired_and_can_restart() -> TestResult {
let (client, mut gate) = client_with_gate()?;
let installed = install_unpolled(&client)?;
let task = client.engine.runtime_handle().spawn(installed.task);
gate.next().await?.finish(Ok(()))?;
bounded(client.start()).await??;
task.abort();
let joined = bounded(task).await?;
check(
matches!(joined, Err(error) if error.is_cancelled()),
"test did not cancel the running task",
)?;
finished(installed.done).await?;
check(
!client.is_started() && client.current_state()?.is_none(),
"aborted task retained active generation",
)?;
let mut retry = Box::pin(client.start());
check(
matches!(poll_once(retry.as_mut()).await, Poll::Pending),
"aborted task prevented retry",
)?;
gate.next().await?.finish(Ok(()))?;
bounded(retry).await??;
bounded(client.stop()).await??;
Ok(())
}
struct ReenterOnDrop {
client: Weak<InnerNetStatusClient>,
result: mpsc::UnboundedSender<bool>,
}
impl Drop for ReenterOnDrop {
fn drop(&mut self) {
let valid = self.client.upgrade().is_some_and(|client| {
!client.is_started()
&& matches!(
client.snapshot().map(|snapshot| snapshot.ip_stack),
Ok(None)
)
});
let _ = self.result.send(valid);
}
}
#[tokio::test]
async fn failed_task_releases_listener_captures_outside_locks() -> TestResult {
let (client, _) = client_with_gate()?;
let installed = install_unpolled(&client)?;
let (result, mut observed) = mpsc::unbounded_channel();
let capture = ReenterOnDrop {
client: Arc::downgrade(&client),
result,
};
let subscription = client.subscribe_state()?.into_callback(move |_, _| {
let _ = &capture;
})?;
let worker = std::thread::Builder::new().spawn(move || {
drop(installed.task);
drop(subscription);
})?;
let result = bounded(observed.recv())
.await?
.ok_or_else(|| std::io::Error::other("capture did not report cleanup"))?;
check(result, "capture observed uncleared state")?;
check(worker.join().is_ok(), "cleanup thread failed")?;
Ok(())
}
#[test]
fn closed_runtime_rejects_unpolled_monitor_without_retaining_it() -> TestResult {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()?;
let handle = runtime.handle().clone();
drop(runtime);
let (client, gate) = client_with_gate()?;
let installed = install_unpolled(&client)?;
drop(handle.spawn(installed.task));
check(
*::tokio::sync::watch::Receiver::borrow(&installed.done),
"closed runtime retained monitor task",
)?;
check(
matches!(&*::tokio::sync::watch::Receiver::borrow(&installed.initial), MonitorInitialization::Failed(error) if error.kind() == crate::error::ErrorKind::RuntimeUnavailable),
"runtime rejection was not propagated",
)?;
check(
client.current_state()?.is_none() && !client.is_started(),
"runtime rejection retained generation",
)?;
check(
gate.calls.load(Ordering::SeqCst) == 0,
"closed runtime ran the initializer",
)?;
Ok(())
}
#[tokio::test]
async fn failure_is_observable_and_stop_resets_it_without_ending_subscription() -> TestResult {
let (client, mut gate) = client_with_gate()?;
let mut observer = client.subscribe_state()?;
check(
matches!(
observer.recv().await?.map(|s| s.state),
Some(crate::net_status::MonitorState::Stopped)
),
"initial state missing",
)?;
let mut starting = Box::pin(client.start());
check(
poll_once(starting.as_mut()).await.is_pending(),
"start did not wait",
)?;
check(
matches!(
client.snapshot()?.state,
crate::net_status::MonitorState::Starting
),
"pending constructor was not Starting",
)?;
gate.next()
.await?
.finish(Err(crate::error::ErrorKind::RuntimeUnavailable.into()))?;
let outcome = bounded(starting).await?;
check(
matches!(outcome, Err(ref e) if e.kind() == crate::error::ErrorKind::RuntimeUnavailable),
"initialization failure lost",
)?;
check(
matches!(
client.snapshot()?.state,
crate::net_status::MonitorState::Failed(_)
),
"failed initialization not observable",
)?;
bounded(client.stop()).await??;
let stopped = bounded(observer.recv())
.await??
.ok_or("subscription ended on stop")?;
check(
matches!(stopped.state, crate::net_status::MonitorState::Stopped)
&& stopped.reachability.is_none(),
"stop did not clear failed monitor",
)?;
bounded(client.shutdown()).await??;
let closed = bounded(observer.recv())
.await??
.ok_or("final Closed missing")?;
check(
matches!(closed.state, crate::net_status::MonitorState::Closed),
"missing Closed",
)?;
check(
observer.recv().await?.is_none(),
"closed subscription not fused",
)
}
struct ShutdownOnWake {
client: Weak<InnerNetStatusClient>,
outcome: std::sync::Mutex<Option<oneshot::Sender<Result<(), String>>>>,
}
impl std::task::Wake for ShutdownOnWake {
fn wake(self: Arc<Self>) {
self.wake_by_ref();
}
fn wake_by_ref(self: &Arc<Self>) {
let sender = match self.outcome.lock() {
Ok(mut sender) => sender.take(),
Err(error) => {
eprintln!("shutdown wake result lock poisoned: {error}");
None
}
};
let Some(sender) = sender else {
return;
};
let outcome = match self.client.upgrade() {
Some(client) => {
futures::executor::block_on(client.shutdown()).map_err(|error| error.to_string())
}
None => Err("monitor vanished before wake".to_owned()),
};
let _ = sender.send(outcome);
}
}
#[tokio::test]
async fn starting_publication_can_reenter_shutdown_after_task_is_submitted() -> TestResult {
let (client, _gate) = client_with_gate()?;
let mut receiver = client.subscribe_state()?;
check(receiver.recv().await?.is_some(), "initial Stopped missing")?;
let (sent, received) = oneshot::channel();
let waker = std::task::Waker::from(Arc::new(ShutdownOnWake {
client: Arc::downgrade(&client),
outcome: std::sync::Mutex::new(Some(sent)),
}));
{
let mut pending = Box::pin(receiver.recv());
let mut context = std::task::Context::from_waker(&waker);
check(
pending.as_mut().poll(&mut context).is_pending(),
"receiver not pending",
)?;
}
let (done, started) = oneshot::channel();
let worker = std::thread::Builder::new().spawn(move || {
let outcome = client.engine.runtime_handle().block_on(client.start());
let _ = done.send(outcome);
})?;
bounded(received).await??.map_err(std::io::Error::other)?;
check(
matches!(bounded(started).await??, Err(error) if error.kind() == crate::error::ErrorKind::Closed),
"reentrant shutdown did not close the starting monitor",
)?;
check(worker.join().is_ok(), "starting thread failed")
}
#[tokio::test]
async fn failed_start_publication_retires_unpolled_task_without_constructing_detector() -> TestResult
{
let (client, gate) = client_with_gate()?;
let mut receiver = client.subscribe_state()?;
check(receiver.recv().await?.is_some(), "initial Stopped missing")?;
client.observations.exhaust_publication_revision_for_test();
let outcome = bounded(client.start()).await?;
check(
matches!(outcome, Err(error) if error.kind() == crate::error::ErrorKind::ResourceExhausted),
"publication preparation failure was hidden",
)?;
check(
gate.calls.load(Ordering::SeqCst) == 0 && client.current_state()?.is_none(),
"failed preparation constructed or retained a native detector",
)?;
let mut terminal = bounded(receiver.recv()).await?;
if let Ok(Some(snapshot)) = terminal {
check(
matches!(snapshot.state, crate::net_status::MonitorState::Stopped)
&& snapshot.revision == 0
&& snapshot.reachability.is_none(),
"failed preparation fabricated a new observation",
)?;
terminal = bounded(receiver.recv()).await?;
}
check(
matches!(terminal, Err(error) if error.kind() == crate::error::ErrorKind::ResourceExhausted),
"failed preparation did not notify the subscription",
)?;
check(
matches!(client.snapshot(), Err(error) if error.kind() == crate::error::ErrorKind::ResourceExhausted),
"broken publisher reported a valid monitor snapshot",
)?;
check(
receiver.recv().await?.is_none(),
"failed publication did not end the subscription",
)
}
#[tokio::test]
async fn concurrent_waiter_observes_the_same_preparation_failure() -> TestResult {
let (client, gate) = client_with_gate()?;
client.observations.exhaust_publication_revision_for_test();
let (monitor, task, publication) = client.prepare_monitor_task()?;
client
.lifecycle
.lock()
.map_err(NetError::from_poison)?
.monitor = Some(monitor);
let mut concurrent = Box::pin(client.start());
check(
poll_once(concurrent.as_mut()).await.is_pending(),
"concurrent caller did not share preparation",
)?;
drop(task);
check(
matches!(publication.dispatch(), Err(error) if error.kind() == crate::error::ErrorKind::ResourceExhausted),
"test did not reproduce preparation failure",
)?;
check(
matches!(bounded(concurrent).await?, Err(error) if error.kind() == crate::error::ErrorKind::ResourceExhausted),
"concurrent start received a different failure for the same generation",
)?;
check(
gate.calls.load(Ordering::SeqCst) == 0,
"failed preparation ran its detector",
)
}