-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: clean component server client tests
- Loading branch information
1 parent
8090c8b
commit b513b8c
Showing
7 changed files
with
140 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use std::net::IpAddr; | ||
|
||
fn construct_url(ip_address: IpAddr, port: u16) -> String { | ||
match ip_address { | ||
IpAddr::V4(ip_address) => format!("http://{}:{}/", ip_address, port), | ||
IpAddr::V6(ip_address) => format!("http://[{}]:{}/", ip_address, port), | ||
} | ||
} | ||
|
||
pub struct ComponentClientRpc<Component> { | ||
pub dst: String, | ||
_component: std::marker::PhantomData<Component>, | ||
} | ||
|
||
impl<Component> ComponentClientRpc<Component> { | ||
pub fn new(ip_address: IpAddr, port: u16) -> Self { | ||
Self { dst: construct_url(ip_address, port), _component: Default::default() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use std::net::{IpAddr, SocketAddr}; | ||
|
||
use async_trait::async_trait; | ||
|
||
#[async_trait] | ||
pub trait ServerStart { | ||
async fn start_server(self, address: SocketAddr); | ||
} | ||
|
||
pub struct ComponentServerRpc<Component> { | ||
component: Option<Component>, | ||
address: SocketAddr, | ||
} | ||
|
||
impl<Component: ServerStart> ComponentServerRpc<Component> { | ||
pub fn new(component: Component, ip_address: IpAddr, port: u16) -> Self { | ||
Self { component: Some(component), address: SocketAddr::new(ip_address, port) } | ||
} | ||
|
||
pub async fn start(&mut self) { | ||
self.component.take().unwrap().start_server(self.address).await; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
pub mod component_client; | ||
pub mod component_client_rpc; | ||
pub mod component_definitions; | ||
pub mod component_runner; | ||
pub mod component_server; | ||
pub mod component_server_rpc; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,48 @@ | ||
use async_trait::async_trait; | ||
use starknet_mempool_infra::component_client::ClientError; | ||
|
||
pub(crate) type ValueA = u32; | ||
pub(crate) type ValueB = u8; | ||
|
||
// TODO(Tsabary): add more messages / functions to the components. | ||
|
||
pub type ClientResult<T> = Result<T, ClientError>; | ||
|
||
#[async_trait] | ||
pub(crate) trait ComponentATrait: Send + Sync { | ||
async fn a_get_value(&self) -> ValueA; | ||
pub(crate) trait AClientTrait: Send + Sync { | ||
async fn a_get_value(&self) -> ClientResult<ValueA>; | ||
} | ||
|
||
#[async_trait] | ||
pub(crate) trait ComponentBTrait: Send + Sync { | ||
async fn b_get_value(&self) -> ValueB; | ||
pub(crate) trait BClientTrait: Send + Sync { | ||
async fn b_get_value(&self) -> ClientResult<ValueB>; | ||
} | ||
|
||
pub(crate) struct ComponentA { | ||
b: Box<dyn ComponentBTrait>, | ||
} | ||
|
||
#[async_trait] | ||
impl ComponentATrait for ComponentA { | ||
async fn a_get_value(&self) -> ValueA { | ||
let b_value = self.b.b_get_value().await; | ||
b_value.into() | ||
} | ||
b: Box<dyn BClientTrait>, | ||
} | ||
|
||
impl ComponentA { | ||
pub fn new(b: Box<dyn ComponentBTrait>) -> Self { | ||
pub fn new(b: Box<dyn BClientTrait>) -> Self { | ||
Self { b } | ||
} | ||
|
||
pub async fn a_get_value(&self) -> ValueA { | ||
let b_value = self.b.b_get_value().await.unwrap(); | ||
b_value.into() | ||
} | ||
} | ||
|
||
pub(crate) struct ComponentB { | ||
value: ValueB, | ||
_a: Box<dyn ComponentATrait>, | ||
} | ||
|
||
#[async_trait] | ||
impl ComponentBTrait for ComponentB { | ||
async fn b_get_value(&self) -> ValueB { | ||
self.value | ||
} | ||
_a: Box<dyn AClientTrait>, | ||
} | ||
|
||
impl ComponentB { | ||
pub fn new(value: ValueB, a: Box<dyn ComponentATrait>) -> Self { | ||
pub fn new(value: ValueB, a: Box<dyn AClientTrait>) -> Self { | ||
Self { value, _a: a } | ||
} | ||
pub async fn b_get_value(&self) -> ValueB { | ||
self.value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.