use std::sync::{Arc, Mutex};
use std::time::Duration;
use serde::de::DeserializeOwned;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
use crate::error::{Error, Result};
use crate::task::context::HeartbeatMessage;
use crate::TaskContext;
#[derive(Debug, Clone)]
#[must_use = "call `build` to get the context"]
pub struct TestTaskContext {
workflow_id: String,
run_id: String,
task_id: String,
attempt: i32,
heartbeat_timeout: Option<Duration>,
}
impl Default for TestTaskContext {
fn default() -> Self {
Self {
workflow_id: "test-workflow".to_string(),
run_id: "test-run".to_string(),
task_id: "test-task".to_string(),
attempt: 1,
heartbeat_timeout: None,
}
}
}
impl TestTaskContext {
pub fn new() -> Self {
Self::default()
}
pub fn workflow_id(mut self, workflow_id: impl Into<String>) -> Self {
self.workflow_id = workflow_id.into();
self
}
pub fn run_id(mut self, run_id: impl Into<String>) -> Self {
self.run_id = run_id.into();
self
}
pub fn task_id(mut self, task_id: impl Into<String>) -> Self {
self.task_id = task_id.into();
self
}
pub fn attempt(mut self, attempt: i32) -> Self {
self.attempt = attempt;
self
}
pub fn heartbeat_timeout(mut self, timeout: Duration) -> Self {
self.heartbeat_timeout = Some(timeout);
self
}
pub fn build(self) -> (TaskContext, TaskProbe) {
let (heartbeat_tx, heartbeat_rx) = mpsc::unbounded_channel();
let cancellation = Arc::new(CancellationToken::new());
let ctx = TaskContext::with_heartbeat_channel(
self.workflow_id,
self.run_id,
self.task_id,
self.attempt,
self.heartbeat_timeout,
heartbeat_tx,
cancellation.clone(),
);
let probe = TaskProbe {
cancellation,
heartbeat_rx: Mutex::new(heartbeat_rx),
received: Mutex::new(Vec::new()),
};
(ctx, probe)
}
}
#[derive(Debug)]
pub struct TaskProbe {
cancellation: Arc<CancellationToken>,
heartbeat_rx: Mutex<mpsc::UnboundedReceiver<HeartbeatMessage>>,
received: Mutex<Vec<Option<Vec<u8>>>>,
}
impl TaskProbe {
pub fn cancel(&self) {
self.cancellation.cancel();
}
pub fn is_cancelled(&self) -> bool {
self.cancellation.is_cancelled()
}
pub fn heartbeat_count(&self) -> usize {
self.drain();
self.received.lock().unwrap().len()
}
pub fn heartbeat_details<T: DeserializeOwned>(&self) -> Result<Vec<T>> {
self.drain();
self.received
.lock()
.unwrap()
.iter()
.flatten()
.map(|bytes| {
serde_json::from_slice(bytes).map_err(|e| {
Error::Serialization(format!("heartbeat details do not decode: {e}"))
})
})
.collect()
}
fn drain(&self) {
let mut rx = self.heartbeat_rx.lock().unwrap();
let mut received = self.received.lock().unwrap();
while let Ok(message) = rx.try_recv() {
received.push(message.details);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn defaults_describe_a_first_attempt() {
let (ctx, probe) = TestTaskContext::new().build();
assert_eq!(ctx.workflow_id(), "test-workflow");
assert_eq!(ctx.run_id(), "test-run");
assert_eq!(ctx.task_id(), "test-task");
assert_eq!(ctx.attempt(), 1);
assert!(!ctx.is_retry());
assert_eq!(ctx.heartbeat_timeout(), None);
assert_eq!(probe.heartbeat_count(), 0);
}
#[test]
fn metadata_is_what_the_test_set() {
let (ctx, _probe) = TestTaskContext::new()
.workflow_id("wf-1")
.run_id("run-1")
.task_id("task-1")
.attempt(3)
.heartbeat_timeout(Duration::from_secs(5))
.build();
assert_eq!(ctx.workflow_id(), "wf-1");
assert_eq!(ctx.run_id(), "run-1");
assert_eq!(ctx.task_id(), "task-1");
assert_eq!(ctx.attempt(), 3);
assert!(ctx.is_retry());
assert_eq!(ctx.heartbeat_timeout(), Some(Duration::from_secs(5)));
}
#[tokio::test]
async fn the_probe_reads_heartbeats_after_the_context_is_gone() {
let (ctx, probe) = TestTaskContext::new().build();
ctx.heartbeat().await.unwrap();
ctx.heartbeat_with_details(Some(7u32)).await.unwrap();
ctx.heartbeat_with_details(Some(9u32)).await.unwrap();
drop(ctx);
assert_eq!(probe.heartbeat_count(), 3);
assert_eq!(probe.heartbeat_details::<u32>().unwrap(), vec![7, 9]);
assert_eq!(probe.heartbeat_details::<u32>().unwrap(), vec![7, 9]);
}
#[tokio::test]
async fn cancelling_through_the_probe_reaches_the_task() {
let (ctx, probe) = TestTaskContext::new().build();
assert!(!ctx.is_cancelled());
probe.cancel();
assert!(probe.is_cancelled());
assert!(ctx.is_cancelled());
assert!(ctx.cancellation_token().is_cancelled());
assert!(ctx.heartbeat().await.is_err());
}
#[test]
fn details_of_the_wrong_type_are_reported() {
let (ctx, probe) = TestTaskContext::new().build();
tokio_test::block_on(ctx.heartbeat_with_details(Some("text"))).unwrap();
assert!(matches!(
probe.heartbeat_details::<u32>(),
Err(Error::Serialization(_))
));
}
}