use crate::error::Result;
use crate::{ActorId, Message};
#[derive(Debug)]
pub enum ExecutionResult {
Success {
fuel_consumed: u64,
response: Option<Vec<u8>>,
},
FuelExhausted {
requested: u64,
},
Failed {
error: String,
},
NotReady,
}
pub trait ActorExecutor: Send + Sync {
fn execute(&self, actor_id: &ActorId, message: &Message) -> ExecutionResult;
fn is_ready(&self, actor_id: &ActorId) -> bool;
fn get_fuel(&self, actor_id: &ActorId) -> Option<u64>;
fn reset(&self, actor_id: &ActorId) -> Result<()>;
}
pub struct NullExecutor {
call_count: std::sync::atomic::AtomicU64,
}
impl NullExecutor {
pub fn new() -> Self {
Self {
call_count: std::sync::atomic::AtomicU64::new(0),
}
}
pub fn call_count(&self) -> u64 {
self.call_count.load(std::sync::atomic::Ordering::Relaxed)
}
}
impl Default for NullExecutor {
fn default() -> Self {
Self::new()
}
}
impl ActorExecutor for NullExecutor {
fn execute(&self, _actor_id: &ActorId, _message: &Message) -> ExecutionResult {
self.call_count
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
ExecutionResult::Success {
fuel_consumed: 0,
response: None,
}
}
fn is_ready(&self, _actor_id: &ActorId) -> bool {
true
}
fn get_fuel(&self, _actor_id: &ActorId) -> Option<u64> {
Some(1_000_000)
}
fn reset(&self, _actor_id: &ActorId) -> Result<()> {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{MessagePayload, Priority};
#[test]
fn test_null_executor() {
let executor = NullExecutor::new();
let actor_id = ActorId::new();
let message = Message {
sender: None,
payload: MessagePayload::Start,
priority: Priority::Normal,
};
let result = executor.execute(&actor_id, &message);
assert!(matches!(result, ExecutionResult::Success { .. }));
assert_eq!(executor.call_count(), 1);
}
#[test]
fn test_null_executor_is_ready() {
let executor = NullExecutor::new();
let actor_id = ActorId::new();
assert!(executor.is_ready(&actor_id));
}
#[test]
fn test_execution_result_debug() {
let result = ExecutionResult::Success {
fuel_consumed: 100,
response: Some(vec![1, 2, 3]),
};
let debug_str = format!("{:?}", result);
assert!(debug_str.contains("Success"));
}
}