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

Tsabary/component runner #79

Closed
wants to merge 3 commits into from
Closed
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
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = ["crates/gateway", "crates/mempool", "crates/mempool_node"]
members = ["crates/gateway", "crates/mempool", "crates/mempool_node", "crates/mempool_infra"]

[workspace.package]
version = "0.0.0"
Expand All @@ -19,6 +19,7 @@ unused = "deny"
as_conversions = "deny"

[workspace.dependencies]
anyhow = "1.0.44"
assert_matches = "1.5.0"
async-trait = "0.1.79"
axum = "0.6.12"
Expand Down
27 changes: 27 additions & 0 deletions crates/mempool_infra/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "mempool_infra"
version.workspace = true
edition.workspace = true
repository.workspace = true
license.workspace = true

[package.metadata.cargo-udeps.ignore]
normal = ["tokio"]

[lints]
workspace = true

[dependencies]
async-trait.workspace = true
clap.workspace = true
serde.workspace = true
serde_json.workspace = true
papyrus_config.workspace = true
thiserror.workspace = true
tokio.workspace = true
validator.workspace = true

[dev-dependencies]
assert_matches.workspace = true
pretty_assertions.workspace = true

145 changes: 145 additions & 0 deletions crates/mempool_infra/src/infra_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
use assert_matches::assert_matches;
use async_trait::async_trait;
use pretty_assertions::assert_eq;
use serde::{Deserialize, Serialize};

use std::collections::BTreeMap;

use crate::{ComponentRunner, ComponentStartError};
use papyrus_config::dumping::{ser_param, SerializeConfig};

use papyrus_config::ParamPrivacyInput;
use papyrus_config::{ParamPath, SerializedParam};

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct TestConfig1 {
pub config1: bool,
}

impl SerializeConfig for TestConfig1 {
fn dump(&self) -> BTreeMap<ParamPath, SerializedParam> {
BTreeMap::from_iter([ser_param(
"test1",
&self.config1,
"...",
ParamPrivacyInput::Public,
)])
}
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct TestConfig2 {
pub config2: u32,
}

impl SerializeConfig for TestConfig2 {
fn dump(&self) -> BTreeMap<ParamPath, SerializedParam> {
BTreeMap::from_iter([ser_param(
"test2",
&self.config2,
"...",
ParamPrivacyInput::Public,
)])
}
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct TestConfig3 {
pub config3: f64,
}

impl SerializeConfig for TestConfig3 {
fn dump(&self) -> BTreeMap<ParamPath, SerializedParam> {
BTreeMap::from_iter([ser_param(
"test2",
&self.config3,
"...",
ParamPrivacyInput::Public,
)])
}
}

trait ExtendedConfigTrait: SerializeConfig + std::fmt::Debug + Send + Sync {}

impl ExtendedConfigTrait for TestConfig1 {}
impl ExtendedConfigTrait for TestConfig2 {}

#[async_trait]
impl ComponentRunner for TestComponent1 {
async fn start_component(&self) -> Result<(), ComponentStartError> {
println!("TestComponent1::start_component(): {:#?}", self.config);
Ok(())
}
}

#[async_trait]
impl ComponentRunner for TestComponent2 {
async fn start_component(&self) -> Result<(), ComponentStartError> {
println!("TestComponent2::start_component(): {:#?}", self.config);
if self.config.config2 == 42 {
return Err(ComponentStartError::InternalComponentError);
} else {
return Ok(());
};
}
}

pub struct TestComponent1 {
pub config: TestConfig1,
}

pub struct TestComponent2 {
pub config: TestConfig2,
}

#[tokio::test]
async fn test_testruner1() {
let test_config = TestConfig1 { config1: true };
let test_component = TestComponent1 {
config: test_config,
};
assert_matches!(test_component.start_component().await, Ok(()));
}

#[tokio::test]
async fn test_testruner2() {
let test_config = TestConfig2 { config2: 16 };
let test_component = TestComponent2 {
config: test_config,
};
assert_matches!(test_component.start_component().await, Ok(()));
}

#[tokio::test]
async fn test_run_from_vector() {
let test_config_1 = TestConfig1 { config1: true };
let test_config_2 = TestConfig2 { config2: 17 };
let erroneous_test_config_2 = TestConfig2 { config2: 42 };

let test_component_1 = TestComponent1 {
config: test_config_1,
};
let test_component_2 = TestComponent2 {
config: test_config_2,
};
let erroneous_test_component_2 = TestComponent2 {
config: erroneous_test_config_2,
};

let components: Vec<Box<dyn ComponentRunner>> = vec![
Box::new(test_component_1),
Box::new(test_component_2),
Box::new(erroneous_test_component_2),
];

let expected_results: Vec<Result<(), ComponentStartError>> = vec![
Ok(()),
Ok(()),
Err(ComponentStartError::InternalComponentError),
];

for (component, expected_result) in components.iter().zip(expected_results.iter()) {
let ret_val = component.start_component().await;
assert_eq!(ret_val, *expected_result);
}
}
21 changes: 21 additions & 0 deletions crates/mempool_infra/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use async_trait::async_trait;
// use papyrus_config::dumping::SerializeConfig;
use std::fmt::Debug;

#[cfg(test)]
mod infra_test;

#[derive(thiserror::Error, Debug, PartialEq)]
pub enum ComponentStartError {
#[error("Error in the component configuration.")]
ComponentConfigError,
#[error("An internal component error.")]
InternalComponentError,
}

/// Interface to start memepool components.
#[async_trait]
pub trait ComponentRunner {
/// Start the component. Normally this function should never return.
async fn start_component(&self) -> Result<(), ComponentStartError>;
}
Loading