use std::sync::Arc;
use std::sync::atomic::{AtomicI64, Ordering};
use shiny_application::{Module, TestApplication};
use shiny_application::controllers::Controllers;
use shiny_common::pointer_utils::ToArc;
use shiny_common::context::Context;
use shiny_common::error::ServiceError;
use shiny_jobs::Job;
#[tokio::test]
async fn test_application() {
let counter = AtomicI64::new(0).arc();
let application = TestApplication::new(counter.clone(), TestModule);
let mut context = Context::new();
application.run_job("hello_world", &mut context).await.unwrap();
application.run_job("hello_world", &mut context).await.unwrap();
let counter = counter.load(Ordering::SeqCst);
assert_eq!(2, counter)
}
#[tokio::test]
#[should_panic(expected = "job `hello_world_but_not_exists` does not exists")]
async fn test_panic() {
let counter = AtomicI64::new(0).arc();
let application = TestApplication::new(counter.clone(), TestModule);
let mut context = Context::new();
application.run_job("hello_world_but_not_exists", &mut context).await.unwrap();
}
struct TestModule;
impl Module for TestModule {
type Clients = Arc<AtomicI64>;
fn create(&self, client: Self::Clients) -> Controllers {
let mut controllers = Controllers::new();
controllers.jobs().add("hello_world", JobExample(client.clone()).arc());
controllers
}
}
struct JobExample(Arc<AtomicI64>);
#[async_trait::async_trait]
impl Job for JobExample {
async fn execute(&self, _: &mut Context) -> Result<(), ServiceError> {
self.0.fetch_add(1, Ordering::SeqCst);
Ok(())
}
}