#![cfg(feature = "test-util")]
#![expect(clippy::expect_used)]
use std::pin::Pin;
use std::task::{Context, Waker};
use std::time::Duration;
use aws_durable_execution_sdk as durable;
use durable::test_util::{LocalRunner, TestResult};
use durable::{BoxError, DurableContext, OperationStatus};
fn status<O>(result: &TestResult<O>, name: &str) -> Option<OperationStatus> {
result
.operation(name)
.map(durable::test_util::TestOperation::status)
}
fn in_a_dropped_runtime<T>(run_for: Duration, f: impl FnOnce() -> T) -> Result<T, BoxError> {
tokio::task::block_in_place(|| {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()?;
let out = runtime.block_on(async {
let out = f();
tokio::time::sleep(run_for).await;
out
});
drop(runtime);
Ok(out)
})
}
fn poll_once<F: Future + Unpin>(fut: &mut F) -> bool {
Pin::new(fut)
.poll(&mut Context::from_waker(Waker::noop()))
.is_pending()
}
async fn run_within_10s<O, F, Fut>(runner: LocalRunner, handler: F) -> TestResult<O>
where
O: serde::Serialize + serde::de::DeserializeOwned + Send + 'static,
F: Fn((), DurableContext) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<O, BoxError>> + Send + 'static,
{
tokio::time::timeout(Duration::from_secs(10), runner.run(handler, ()))
.await
.expect("the run finishes within 10 s")
.expect("the run finishes")
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn spawned_step_started_in_a_dropped_second_runtime_runs() {
async fn handler(_event: (), ctx: DurableContext) -> Result<u32, BoxError> {
let spawned = in_a_dropped_runtime(Duration::ZERO, || {
ctx.step(|_| async {
tokio::time::sleep(Duration::from_millis(50)).await;
Ok(1_u32)
})
.name("spawned")
.spawn()
})?;
Ok(spawned.await?)
}
let result = run_within_10s(LocalRunner::new(), handler).await;
assert_eq!(result.output(), Some(&1), "{result}");
assert_eq!(
status(&result, "spawned"),
Some(OperationStatus::Succeeded),
"{result}"
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn map_first_polled_in_a_dropped_second_runtime_finishes() {
async fn handler(_event: (), ctx: DurableContext) -> Result<Vec<u32>, BoxError> {
let mut map = Box::pin(
ctx.map(vec![1_u32, 2], |child, item, _index| async move {
Ok(child
.step(move |_| async move {
tokio::time::sleep(Duration::from_millis(50)).await;
Ok(item * 10)
})
.name("scale")
.await?)
})
.name("scale-all")
.into_future(),
);
let pending = in_a_dropped_runtime(Duration::ZERO, || poll_once(&mut map))?;
assert!(
pending,
"the first poll starts the items and waits for them"
);
Ok(map.await?)
}
let result = run_within_10s(LocalRunner::new(), handler).await;
assert_eq!(result.output(), Some(&vec![10, 20]), "{result}");
assert_eq!(
status(&result, "scale-all"),
Some(OperationStatus::Succeeded),
"{result}"
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn combinator_first_polled_in_a_dropped_second_runtime_finishes() {
async fn handler(_event: (), ctx: DurableContext) -> Result<Vec<u32>, BoxError> {
let a = ctx
.step(|_| async {
tokio::time::sleep(Duration::from_millis(50)).await;
Ok(1_u32)
})
.name("a")
.future();
let b = ctx
.step(|_| async {
tokio::time::sleep(Duration::from_millis(50)).await;
Ok(2_u32)
})
.name("b")
.future();
let mut all = Box::pin(ctx.try_join_all([a, b]).name("all").into_future());
let pending = in_a_dropped_runtime(Duration::ZERO, || poll_once(&mut all))?;
assert!(
pending,
"the first poll starts the inputs and waits for them"
);
Ok(all.await?)
}
let result = run_within_10s(LocalRunner::new(), handler).await;
assert_eq!(result.output(), Some(&vec![1, 2]), "{result}");
assert_eq!(
status(&result, "all"),
Some(OperationStatus::Succeeded),
"{result}"
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn checkpoint_write_in_flight_when_a_second_runtime_drops_is_applied() {
async fn handler(_event: (), ctx: DurableContext) -> Result<u32, BoxError> {
let mut step = Box::pin(
ctx.step(|_| async { Ok(1_u32) })
.name("written")
.into_future(),
);
let pending = in_a_dropped_runtime(Duration::from_millis(50), || poll_once(&mut step))?;
assert!(pending, "the step waits for its START response");
Ok(step.await?)
}
let runner = LocalRunner::new().checkpoint_latency(Duration::from_millis(500));
let result = run_within_10s(runner, handler).await;
assert_eq!(result.output(), Some(&1), "{result}");
assert_eq!(result.invocations().len(), 1, "{result}");
assert_eq!(
status(&result, "written"),
Some(OperationStatus::Succeeded),
"{result}"
);
}