time: fix the loom test of the race between cancellation/insertion (#8302)

This commit is contained in:
ADD-SP
2026-07-20 19:10:31 +08:00
committed by GitHub
parent 91d3b4c0bc
commit eb4988dc2e
2 changed files with 31 additions and 27 deletions
+2
View File
@@ -15,6 +15,8 @@ R-loom-time-driver:
- any-glob-to-any-file: - any-glob-to-any-file:
- tokio/src/runtime/time/* - tokio/src/runtime/time/*
- tokio/src/runtime/time/**/* - tokio/src/runtime/time/**/*
- tokio/src/runtime/time_alt/*
- tokio/src/runtime/time_alt/**/*
R-loom-current-thread: R-loom-current-thread:
- changed-files: - changed-files:
+29 -27
View File
@@ -97,39 +97,41 @@ fn cancel_in_the_same_thread() {
#[test] #[test]
fn insert_of_already_cancelled_entry_does_not_enter_wheel() { fn insert_of_already_cancelled_entry_does_not_enter_wheel() {
let (cancel_tx, mut cancel_rx) = cancellation_queue::new(); model(|| {
let mut wheel = Wheel::new(); let (cancel_tx, mut cancel_rx) = cancellation_queue::new();
let mut wheel = Wheel::new();
// do not expire during the test // do not expire during the test
let far_future = 10_000_000; let far_future = 10_000_000;
let (hdl, awoken_count) = new_handle_with_deadline(far_future); let (hdl, awoken_count) = new_handle_with_deadline(far_future);
// cancel the timer before inserting it into the wheel // cancel the timer before inserting it into the wheel
hdl.cancel(); hdl.cancel();
assert!(hdl.is_cancelled()); assert!(hdl.is_cancelled());
// try to insert the cancelled entry into the wheel // try to insert the cancelled entry into the wheel
unsafe { unsafe {
wheel.insert(hdl, cancel_tx); wheel.insert(hdl, cancel_tx);
} }
// a cancelled entry should not be inserted into the wheel // a cancelled entry should not be inserted into the wheel
assert!( assert!(
wheel.next_expiration_time().is_none(), wheel.next_expiration_time().is_none(),
"an already-cancelled entry leaked into the wheel on insert" "an already-cancelled entry leaked into the wheel on insert"
); );
// It also must not have been queued for cancellation removal, since // It also must not have been queued for cancellation removal, since
// it was never actually placed in the wheel for `remove` to find. // it was never actually placed in the wheel for `remove` to find.
assert_eq!(cancel_rx.recv_all().count(), 0); assert_eq!(cancel_rx.recv_all().count(), 0);
assert_eq!(awoken_count.get(), 0); assert_eq!(awoken_count.get(), 0);
// drain the wheel unconditionally, otherwise loom will complain // drain the wheel unconditionally, otherwise loom will complain
// about the leaked entry, which confuses developers in case // about the leaked entry, which confuses developers in case
// this test fails for some other reason. // this test fails for some other reason.
let mut wake_queue = WakeQueue::new(); let mut wake_queue = WakeQueue::new();
wheel.take_expired(u64::MAX, &mut wake_queue); wheel.take_expired(u64::MAX, &mut wake_queue);
wake_queue.wake_all(); wake_queue.wake_all();
});
} }
#[test] #[test]