Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 0 additions & 77 deletions crates/arty_io_host/src/driver_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,80 +374,3 @@ impl Drop for DriverHost {
self.shutdown_drivers();
}
}

#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
use std::task::Waker;

use arty_io_core::{Cycle, Driver, DriverError, DriverOptions, IoContext};
use observed::Sink;
use thread_aware::{Thread, ThreadAware, ThreadBuilder};

use crate::IoRuntime;

#[derive(Clone)]
struct TestContext;

impl ThreadAware for TestContext {
fn relocate(&mut self, _source: Option<&Thread>, _destination: &Thread) {}
}

impl IoContext for TestContext {
type ContextOptions = ();
type Driver = TestDriver;

fn new((): Self::ContextOptions) -> Self {
Self
}

fn driver(&mut self, _options: DriverOptions) -> Result<Self::Driver, DriverError> {
Ok(TestDriver)
}
}

struct TestDriver;

impl Driver for TestDriver {
fn waker(&self) -> Waker {
Waker::noop().clone()
}

fn execute_cycle(&mut self, _cycle: &mut Cycle) -> Result<(), DriverError> {
Ok(())
}

fn shutdown(self) -> Result<(), DriverError> {
Ok(())
}
}

#[test]
fn nested_context_retrieval_is_rejected_during_initialization() {
let thread = ThreadBuilder::default().build(std::thread::current().id());
let runtime = IoRuntime::new(Sink::noop());
let host = runtime.driver_host(thread, Waker::noop().clone()).unwrap();
host.initializing.set(true);

let Err(error) = host.context::<TestContext>(()) else {
panic!("nested context retrieval should fail");
};

assert_eq!(error.to_string(), "I/O context retrieval cannot be nested during initialization");
}

#[test]
fn context_retrieval_is_rejected_while_drivers_are_borrowed() {
let thread = ThreadBuilder::default().build(std::thread::current().id());
let runtime = IoRuntime::new(Sink::noop());
let host = runtime.driver_host(thread, Waker::noop().clone()).unwrap();
let drivers = host.drivers.borrow_mut();

let Err(error) = host.context::<TestContext>(()) else {
panic!("context retrieval should fail while drivers are borrowed");
};
assert_eq!(error.to_string(), "I/O context retrieval cannot overlap driver cycle execution");
drop(drivers);
host.context::<TestContext>(()).unwrap();
}
}
1 change: 1 addition & 0 deletions crates/arty_io_host/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ impl IoRuntime {
#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
// The public API cannot pause both resolutions between slot lookup and initialization.
use std::any::TypeId;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Barrier};
Expand Down
1 change: 1 addition & 0 deletions crates/arty_io_host/src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub(crate) struct IoDriverShutdownFailed {
#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
// These assertions inspect private classified values before an event emits them.
use arty_io_core::{DriverError, DriverRole};

use super::{IoDriverErrorMessage, IoDriverRoleLabel};
Expand Down
106 changes: 106 additions & 0 deletions crates/arty_io_host/tests/context_reentrancy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#![cfg(test)]

//! Context retrieval during provider callbacks.

use std::cell::RefCell;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use std::task::Waker;
use std::time::Duration;

use arty_io_core::{Cycle, Driver, DriverError, DriverOptions, IoContext};
use arty_io_host::{DriverHost, IoRuntime};
use observed::Sink;
use thread_aware::{Thread, ThreadAware, ThreadBuilder};

thread_local! {
static HOST: RefCell<Option<Rc<DriverHost>>> = const { RefCell::new(None) };
}

#[derive(Clone)]
struct TestContext {
during_initialization: bool,
errors: Arc<Mutex<Vec<String>>>,
}

impl ThreadAware for TestContext {
fn relocate(&mut self, _source: Option<&Thread>, _destination: &Thread) {}
}

impl TestContext {
fn retrieve_again(&self) {
let host = HOST.with_borrow(|host| Rc::clone(host.as_ref().unwrap()));
let error = host.context::<Self>(self.clone()).err().unwrap();
self.errors.lock().unwrap().push(error.to_string());
}
}

impl IoContext for TestContext {
type ContextOptions = Self;
type Driver = Self;

fn new(options: Self::ContextOptions) -> Self {
options
}

fn driver(&mut self, _options: DriverOptions) -> Result<Self::Driver, DriverError> {
if self.during_initialization {
self.retrieve_again();
}
Ok(self.clone())
}
}

impl Driver for TestContext {
fn waker(&self) -> Waker {
Waker::noop().clone()
}

fn execute_cycle(&mut self, _cycle: &mut Cycle) -> Result<(), DriverError> {
self.retrieve_again();
Ok(())
}

fn shutdown(self) -> Result<(), DriverError> {
Ok(())
}
}

fn exercise_retrieval(during_initialization: bool) -> Vec<String> {
let thread = ThreadBuilder::default().build(std::thread::current().id());
let runtime = IoRuntime::new(Sink::noop());
let host = Rc::new(runtime.driver_host(thread, Waker::noop().clone()).unwrap());
HOST.with_borrow_mut(|installed| *installed = Some(Rc::clone(&host)));
let errors = Arc::new(Mutex::new(Vec::new()));
let context = TestContext {
during_initialization,
errors: Arc::clone(&errors),
};

host.context::<TestContext>(context.clone()).unwrap();
if !during_initialization {
host.execute_cycle(Duration::ZERO).unwrap();
}
host.context::<TestContext>(context).unwrap();
HOST.with_borrow_mut(Option::take);
errors.lock().unwrap().clone()
}

#[test]
fn nested_context_retrieval_is_rejected_during_initialization() {
assert_eq!(
exercise_retrieval(true),
["I/O context retrieval cannot be nested during initialization"]
);
}

#[test]
fn context_retrieval_is_rejected_while_drivers_are_borrowed() {
assert_eq!(
exercise_retrieval(false),
["I/O context retrieval cannot overlap driver cycle execution"]
);
}
Loading
Loading