Dever v0.0.2

This commit is contained in:
Codex committed 2026-11-27 06:03:56 +00:00
commit e6e175ebcf
816 files changed
+120349

No files matched your search

+210
View File
@@ -0,0 +1,210 @@
use dever_runtime::task::{self, Error, Group, RunError, Selection, Timed};
use std::{
future::pending,
sync::{
Arc,
atomic::{AtomicUsize, Ordering},
},
};
struct Released(Arc<AtomicUsize>);
impl Drop for Released {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::SeqCst);
}
}
#[test]
fn timeout_releases_descendants_before_returning_and_preserves_ready_failures() {
let released = Arc::new(AtomicUsize::new(0));
task::run(true, 2, async |scope| {
let held = Released(released.clone());
let task = scope
.spawn(move |child| async move {
let task = child
.spawn_leaf(async move {
let _held = held;
pending::<()>().await
})
.unwrap();
task.wait().await.unwrap();
})
.unwrap();
assert!(matches!(task.timeout(5).await, Ok(Timed::Expired(None))));
assert_eq!(released.load(Ordering::SeqCst), 1);
let task = scope
.spawn_leaf(async { Err::<(), _>("original failure") })
.unwrap();
task::sleep(1).await.unwrap();
assert!(matches!(
task.timeout(1).await,
Ok(Timed::Ready(Err("original failure")))
));
for millis in [0, -1, i64::MIN] {
let task = scope.spawn_leaf(pending::<()>()).unwrap();
assert!(matches!(
task.timeout(millis).await,
Err(Error::InvalidTimeout)
));
task::sleep(1).await.unwrap();
}
Ok::<_, ()>(())
})
.unwrap()
.unwrap();
}
#[test]
fn group_limits_real_admission_and_waits_for_every_member() {
let completed = Arc::new(AtomicUsize::new(0));
task::run(true, 1, async |scope| {
Group::<()>::new(scope, 1)
.unwrap()
.wait()
.await
.unwrap()
.unwrap();
Group::<()>::new(scope, 1)
.unwrap()
.stop()
.await
.unwrap()
.unwrap();
for limit in [0, -1, 65537] {
assert!(matches!(
Group::<()>::new(scope, limit),
Err(Error::InvalidGroupLimit)
));
}
let mut group = Group::new(scope, 1).unwrap();
for index in 0..8 {
let completed = completed.clone();
group
.run(|| {
// 工厂启动时前一成员已经完成;容量只有 1,也不能提前派生。
assert_eq!(completed.load(Ordering::SeqCst), index);
scope.spawn_leaf(async move {
task::sleep(1).await.unwrap();
completed.fetch_add(1, Ordering::SeqCst);
Ok::<_, ()>(())
})
})
.await
.unwrap()
.unwrap();
}
group.wait().await.unwrap().unwrap();
assert_eq!(completed.load(Ordering::SeqCst), 8);
Ok::<_, ()>(())
})
.unwrap()
.unwrap();
}
#[test]
fn group_failure_closes_admission_and_drains_before_capture_continues() {
let released = Arc::new(AtomicUsize::new(0));
task::run(true, 3, async |scope| {
let mut group = Group::new(scope, 3).unwrap();
let held = Released(released.clone());
group
.run(|| {
scope.spawn_leaf(async move {
let _held = held;
pending::<Result<(), &str>>().await
})
})
.await
.unwrap()
.unwrap();
group
.run(|| scope.spawn_leaf(async { Err("member failed") }))
.await
.unwrap()
.unwrap();
task::sleep(1).await.unwrap();
let result = group
.run(|| panic!("must not start after member failure"))
.await;
assert_eq!(result, Ok(Err("member failed")));
assert_eq!(released.load(Ordering::SeqCst), 1);
assert_eq!(
group.run(|| panic!("closed group")).await,
Err(Error::GroupClosed)
);
group.stop().await.unwrap().unwrap();
Ok::<_, ()>(())
})
.unwrap()
.unwrap();
}
#[test]
fn group_stop_preserves_completed_business_failure() {
task::run(true, 1, async |scope| {
let mut group = Group::new(scope, 1).unwrap();
group
.run(|| scope.spawn_leaf(async { Err("completed before stop") }))
.await
.unwrap()
.unwrap();
task::sleep(1).await.unwrap();
assert_eq!(group.stop().await, Ok(Err("completed before stop")));
Ok::<_, ()>(())
})
.unwrap()
.unwrap();
}
#[test]
fn selection_keeps_primary_business_error_and_original_loser_fault_once() {
let result = task::run(true, 2, async |scope| {
let mut first = scope.spawn_leaf(async { Err::<(), _>("primary") }).unwrap();
let mut second = scope
.spawn_leaf(async { std::panic::panic_any(73usize) })
.unwrap();
task::sleep(1).await.unwrap();
let result = task::select! {
biased;
value = &mut first => value.unwrap(),
_ = &mut second => panic!("first ready argument must win"),
};
let mut selection = Selection::new(scope);
second.cancel();
selection.join(second).await;
selection.finish(result.is_ok());
result
})
.unwrap();
let Err(RunError::Business { error, additional }) = result else {
panic!("business primary")
};
assert_eq!(error, "primary");
assert_eq!(additional.len(), 1);
assert_eq!(*additional[0].downcast_ref::<usize>().unwrap(), 73);
}
#[test]
fn selection_cannot_turn_loser_fault_into_success() {
let result = task::run(true, 1, async |scope| {
let task = scope
.spawn_leaf(async { std::panic::panic_any(91usize) })
.unwrap();
task::sleep(1).await.unwrap();
let mut selection = Selection::new(scope);
task.cancel();
selection.join(task).await;
selection.finish(true);
Ok::<_, ()>(())
})
.unwrap();
let Err(RunError::Fault {
primary,
additional,
}) = result
else {
panic!("must retain fault")
};
assert_eq!(*primary.downcast_ref::<usize>().unwrap(), 91);
assert!(additional.is_empty());
}