Skip to main content

dactor_test_harness/
handler.rs

1//! Trait for handling actor management commands in test nodes.
2//!
3//! Adapters (e.g. ractor, kameo) implement [`CommandHandler`] so that a
4//! [`TestNode`](crate::TestNode) can spawn, tell, ask, and stop actors
5//! through the gRPC control channel without depending on a specific runtime.
6
7/// Handler for actor management commands dispatched by the test node gRPC server.
8///
9/// Each method maps to an RPC in the `TestNodeService` proto definition.
10/// Implementations should be thread-safe and maintain their own actor registry.
11#[async_trait::async_trait]
12pub trait CommandHandler: Send + Sync + 'static {
13    /// Human-readable adapter name (e.g. "ractor", "kameo", "coerce").
14    fn adapter_name(&self) -> &str;
15
16    /// Spawn an actor of the given type with the given name.
17    /// Returns the actor ID on success.
18    async fn spawn_actor(
19        &self,
20        actor_type: &str,
21        actor_name: &str,
22        args: &[u8],
23    ) -> Result<String, String>;
24
25    /// Fire-and-forget message to an actor.
26    async fn tell_actor(
27        &self,
28        actor_name: &str,
29        message_type: &str,
30        payload: &[u8],
31    ) -> Result<(), String>;
32
33    /// Request-reply message to an actor. Returns the serialized reply.
34    /// If `timeout_ms > 0`, the ask should be cancelled after that many milliseconds.
35    async fn ask_actor(
36        &self,
37        actor_name: &str,
38        message_type: &str,
39        payload: &[u8],
40        timeout_ms: u64,
41    ) -> Result<Vec<u8>, String>;
42
43    /// Stop an actor by name.
44    async fn stop_actor(&self, actor_name: &str) -> Result<(), String>;
45
46    /// Register a watch: when `target_name` stops, notify `watcher_name`.
47    async fn watch_actor(&self, watcher_name: &str, target_name: &str) -> Result<(), String> {
48        let _ = (watcher_name, target_name);
49        Err("watch not supported".into())
50    }
51
52    /// Return the number of live actors managed by this handler.
53    fn actor_count(&self) -> u32 {
54        0
55    }
56}