Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: testing improvement #629

Merged
merged 2 commits into from
Feb 3, 2025
Merged
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ certs

node_modules

blueprints/**/dependencies/
blueprints/**/dependencies/
blueprint.lock
target
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ uuid = { version = "1.10.0", default-features = false }
blake3 = { version = "1.5.5", default-features = false }

# Development & Testing
auto_impl = { version = "1.2.0", default-features = false }
auto_impl = { version = "1.2.1", default-features = false }
cargo_toml = { version = "0.21.0", default-features = false }
escargot = { version = "0.5.12", default-features = false }
itertools = { version = "0.13.0", default-features = false }
Expand Down
16 changes: 15 additions & 1 deletion crates/event-listeners/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,21 @@ pub fn get_exponential_backoff<const N: usize>() -> Take<ExponentialBackoff> {
}

#[async_trait]
pub trait InitializableEventHandler {
pub trait CloneableEventHandler: Send {
fn clone_box(&self) -> Box<dyn InitializableEventHandler + Send>;
}

impl<T> CloneableEventHandler for T
where
T: InitializableEventHandler + Clone + 'static,
{
fn clone_box(&self) -> Box<dyn InitializableEventHandler + Send> {
Box::new(self.clone())
}
}

#[async_trait]
pub trait InitializableEventHandler: Send + CloneableEventHandler {
async fn init_event_handler(
&self,
) -> Option<tokio::sync::oneshot::Receiver<Result<(), Box<dyn core::error::Error + Send>>>>;
Expand Down
16 changes: 15 additions & 1 deletion crates/runners/core/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
use crate::error::RunnerError;
use gadget_config::GadgetConfiguration;

pub trait CloneableConfig: Send + Sync {
fn clone_box(&self) -> Box<dyn BlueprintConfig>;
}

impl<T> CloneableConfig for T
where
T: BlueprintConfig + Clone,
{
fn clone_box(&self) -> Box<dyn BlueprintConfig> {
Box::new(self.clone())
}
}

#[async_trait::async_trait]
pub trait BlueprintConfig: Send + Sync + 'static {
pub trait BlueprintConfig: Send + Sync + CloneableConfig + 'static {
async fn register(&self, _env: &GadgetConfiguration) -> Result<(), RunnerError> {
Ok(())
}

async fn requires_registration(&self, _env: &GadgetConfiguration) -> Result<bool, RunnerError> {
Ok(true)
}
Expand Down
31 changes: 30 additions & 1 deletion crates/runners/core/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,22 @@ use gadget_config::GadgetConfiguration;
use gadget_event_listeners::core::InitializableEventHandler;
use tokio::sync::oneshot;

// Clone traits with distinct names
pub trait CloneableService: Send {
fn clone_box(&self) -> Box<dyn BackgroundService>;
}

impl<T> CloneableService for T
where
T: BackgroundService + Clone,
{
fn clone_box(&self) -> Box<dyn BackgroundService> {
Box::new(self.clone())
}
}

#[async_trait::async_trait]
pub trait BackgroundService: Send + Sync + 'static {
pub trait BackgroundService: Send + Sync + CloneableService + 'static {
async fn start(&self) -> Result<oneshot::Receiver<Result<(), Error>>, Error>;
}

Expand Down Expand Up @@ -95,3 +109,18 @@ impl BlueprintRunner {
Ok(())
}
}

impl Clone for BlueprintRunner {
fn clone(&self) -> Self {
Self {
config: self.config.clone_box(),
jobs: self.jobs.iter().map(|job| job.clone_box()).collect(),
env: self.env.clone(),
background_services: self
.background_services
.iter()
.map(|service| service.clone_box())
.collect(),
}
}
}
2 changes: 2 additions & 0 deletions crates/runners/core/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::runner::{BackgroundService, BlueprintRunner};
use gadget_config::GadgetConfiguration;
use tokio::sync::oneshot;

#[derive(Clone)]
struct MockBlueprintConfig;

#[async_trait::async_trait]
Expand All @@ -17,6 +18,7 @@ impl BlueprintConfig for MockBlueprintConfig {
}
}

#[derive(Clone)]
struct MockBackgroundService;

#[async_trait::async_trait]
Expand Down
1 change: 1 addition & 0 deletions crates/testing-utils/core/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use gadget_runners::core::config::BlueprintConfig;
use gadget_runners::core::error::RunnerError as Error;
use gadget_runners::core::runner::{BackgroundService, BlueprintRunner};

#[derive(Clone)]
pub struct TestRunner {
inner: BlueprintRunner,
}
Expand Down
47 changes: 46 additions & 1 deletion crates/testing-utils/tangle/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ use gadget_event_listeners::core::InitializableEventHandler;
use gadget_runners::core::error::RunnerError as Error;
use gadget_runners::core::runner::BackgroundService;
use gadget_runners::tangle::tangle::TangleConfig;
use tokio::task::JoinHandle;

pub struct TangleTestEnv {
runner: TestRunner,
config: TangleConfig,
gadget_config: GadgetConfiguration,
runner_handle: Option<JoinHandle<Result<(), Error>>>,
}

impl TestEnv for TangleTestEnv {
Expand All @@ -22,6 +24,7 @@ impl TestEnv for TangleTestEnv {
runner,
config,
gadget_config: env,
runner_handle: None,
})
}

Expand All @@ -44,6 +47,48 @@ impl TestEnv for TangleTestEnv {
}

async fn run_runner(&mut self) -> Result<(), Error> {
self.runner.run().await
// Spawn the runner in a background task
let mut runner = self.runner.clone();
let handle = tokio::spawn(async move { runner.run().await });

self.runner_handle = Some(handle);

// Brief delay to allow for startup
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;

// Just check if it failed immediately
if let Some(handle) = self.runner_handle.take() {
if handle.is_finished() {
match handle.await {
Ok(Ok(())) => Ok(()),
Ok(Err(e)) => {
gadget_logging::error!("Runner failed during startup: {}", e);
Err(Error::Tangle(format!(
"Runner failed during startup: {}",
e
)))
}
Err(e) => {
gadget_logging::error!("Runner task panicked: {}", e);
Err(Error::Tangle(format!("Runner task panicked: {}", e)))
}
}
} else {
// Put the handle back since the runner is still running
self.runner_handle = Some(handle);
gadget_logging::info!("Runner started successfully");
Ok(())
}
} else {
Err(Error::Tangle("Failed to spawn runner task".to_string()))
}
}
}

impl Drop for TangleTestEnv {
fn drop(&mut self) {
if let Some(handle) = &self.runner_handle {
handle.abort();
}
}
}
Loading