-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(state-operator): Add try_load for State (#42)
- Loading branch information
1 parent
b18eebd
commit 1b83775
Showing
7 changed files
with
171 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"pong_count":12} | ||
{"pong_count":5} |
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
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
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,113 @@ | ||
use std::thread; | ||
use std::time::Duration; | ||
// Crates | ||
use async_trait::async_trait; | ||
use overwatch_derive::Services; | ||
use overwatch_rs::overwatch::OverwatchRunner; | ||
use overwatch_rs::services::handle::{ServiceHandle, ServiceStateHandle}; | ||
use overwatch_rs::services::relay::NoMessage; | ||
use overwatch_rs::services::state::{ServiceState, StateOperator}; | ||
use overwatch_rs::services::{ServiceCore, ServiceData, ServiceId}; | ||
use overwatch_rs::DynError; | ||
use tokio::sync::broadcast; | ||
use tokio::sync::broadcast::error::SendError; | ||
|
||
#[derive(Clone)] | ||
struct TryLoadState; | ||
|
||
impl ServiceState for TryLoadState { | ||
type Settings = TryLoadSettings; | ||
type Error = DynError; | ||
fn from_settings(settings: &Self::Settings) -> Result<Self, DynError> { | ||
settings | ||
.origin_sender | ||
.send(String::from("ServiceState::from_settings"))?; | ||
Ok(Self {}) | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
struct TryLoadOperator; | ||
|
||
#[async_trait] | ||
impl StateOperator for TryLoadOperator { | ||
type StateInput = TryLoadState; | ||
type LoadError = SendError<String>; | ||
|
||
fn try_load( | ||
settings: &<Self::StateInput as ServiceState>::Settings, | ||
) -> Result<Option<Self::StateInput>, Self::LoadError> { | ||
settings | ||
.origin_sender | ||
.send(String::from("StateOperator::try_load"))?; | ||
Ok(Some(Self::StateInput {})) | ||
} | ||
|
||
fn from_settings(_settings: <Self::StateInput as ServiceState>::Settings) -> Self { | ||
Self {} | ||
} | ||
|
||
async fn run(&mut self, _state: Self::StateInput) {} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
struct TryLoadSettings { | ||
origin_sender: broadcast::Sender<String>, | ||
} | ||
|
||
struct TryLoad { | ||
service_state_handle: ServiceStateHandle<Self>, | ||
} | ||
|
||
impl ServiceData for TryLoad { | ||
const SERVICE_ID: ServiceId = "try_load"; | ||
type Settings = TryLoadSettings; | ||
type State = TryLoadState; | ||
type StateOperator = TryLoadOperator; | ||
type Message = NoMessage; | ||
} | ||
|
||
#[async_trait] | ||
impl ServiceCore for TryLoad { | ||
fn init( | ||
service_state: ServiceStateHandle<Self>, | ||
_initial_state: Self::State, | ||
) -> Result<Self, DynError> { | ||
Ok(Self { | ||
service_state_handle: service_state, | ||
}) | ||
} | ||
|
||
async fn run(self) -> Result<(), DynError> { | ||
let Self { | ||
service_state_handle, | ||
.. | ||
} = self; | ||
|
||
service_state_handle.overwatch_handle.shutdown().await; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Services)] | ||
struct TryLoadApp { | ||
try_load: ServiceHandle<TryLoad>, | ||
} | ||
|
||
#[test] | ||
fn load_state_from_operator() { | ||
// Create a sender that will be called wherever the state is loaded | ||
let (origin_sender, mut origin_receiver) = broadcast::channel(1); | ||
let settings = TryLoadAppServiceSettings { | ||
try_load: TryLoadSettings { origin_sender }, | ||
}; | ||
|
||
// Run the app | ||
let app = OverwatchRunner::<TryLoadApp>::run(settings, None).unwrap(); | ||
app.wait_finished(); | ||
|
||
// Check if the origin was called | ||
thread::sleep(Duration::from_secs(1)); | ||
let origin = origin_receiver.try_recv().expect("Value was not sent"); | ||
assert_eq!(origin, "StateOperator::try_load"); | ||
} |