folk-api 0.1.2

Plugin contract for the Folk PHP application server
Documentation
use std::sync::Arc;
use std::time::Duration;

use anyhow::Result;
use async_trait::async_trait;
use folk_api::{Executor, Plugin, PluginContext, ServerPlugin, ServerPluginWrapper};
use tokio::sync::watch;

struct EchoPlugin;

#[async_trait]
impl ServerPlugin for EchoPlugin {
    fn name(&self) -> &'static str {
        "echo"
    }

    async fn run(&self, mut ctx: PluginContext) -> Result<()> {
        ctx.shutdown.changed().await.ok();
        Ok(())
    }
}

struct NoopExecutor;

#[async_trait]
impl Executor for NoopExecutor {
    async fn execute_method(&self, _: &str, _: bytes::Bytes) -> Result<bytes::Bytes> {
        unimplemented!("not used in this test")
    }
}

#[tokio::test]
async fn server_plugin_wrapper_boots_and_shuts_down() {
    let mut wrapper = ServerPluginWrapper::new(EchoPlugin);

    let (tx, rx) = watch::channel(false);
    let ctx = PluginContext {
        executor: Arc::new(NoopExecutor),
        shutdown: rx,
        rpc_registrar: None,
        health_registry: None,
        metrics_registry: None,
    };

    wrapper.boot(ctx).await.unwrap();
    tokio::time::sleep(Duration::from_millis(50)).await;
    tx.send(true).unwrap();

    wrapper.shutdown().await.unwrap();
}