Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/rust/cxx/kj-rs-tokio/port-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,23 @@ fn poll_never_sleeps() {
assert!(start.elapsed() < Duration::from_millis(100));
}

/// A zero timeout means a KJ timer is already due. Tokio would sleep until the end of its next
/// 1 ms tick however short the timeout, so `wait_timeout_ns(0)` polls instead of parking. A task
/// spawned before the call runs during it either way, and records whether the port was parked.
#[test]
fn zero_timeout_does_not_park() {
use std::cell::Cell;
let port = TokioPort::new();
let parked = Rc::new(Cell::new(None));
let task_parked = Rc::clone(&parked);
let state = Arc::clone(&port.state);
let _jh = spawn(async move {
task_parked.set(Some(state.in_wait.load(Ordering::Relaxed)));
});
let _ = port.wait_timeout_ns(0);
assert_eq!(parked.get(), Some(false));
}

/// `spawn` accepts `!Send` futures because it is backed by `LocalSet::spawn_local`. This
/// future holds an `Rc` — which is `!Send` — across an await point, exercising that path,
/// and proves such a task actually runs to completion on the loop thread.
Expand Down
23 changes: 0 additions & 23 deletions src/rust/cxx/kj-rs-tokio/tests/tokio-event-port-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <kj/thread.h>
#include <kj/vector.h>

#include <algorithm>
#include <atomic>
#include <chrono>
#include <thread>
Expand Down Expand Up @@ -196,28 +195,6 @@ KJ_TEST("timer fires while blocked waiting on a cross-thread event") {
// Timer precision: due timers fire without sleeping; pending ones fire on tokio's 1 ms timer
// wheel (see tokio-event-port.h). No high-resolution side channel.

KJ_TEST("an already-due timer fires without waiting for a tokio tick") {
// A zero-delay timer, like setTimeout(fn, 0), is due by the time the loop runs out of work, so
// the loop must fire it without sleeping. A tokio sleep lasts until its next 1 ms tick: almost
// always a full tick, but only a moment when a tick boundary happens to pass right before the
// loop parks. The median of a few waits sees past that, and past contention.
auto io = setupTokioAsyncIo();
auto &ws = io.getWaitScope();
auto &timer = io.getTimer();
auto &sysClock = kj::systemPreciseMonotonicClock();

constexpr size_t WAITS = 9;
kj::Vector<kj::Duration> waits(WAITS);
for (size_t i = 0; i < WAITS; i++) {
auto start = sysClock.now();
timer.afterDelay(0 * kj::MILLISECONDS).wait(ws);
waits.add(sysClock.now() - start);
}
std::sort(waits.begin(), waits.end());
auto median = waits[WAITS / 2];
KJ_EXPECT(median < 1 * kj::MILLISECONDS, median / kj::MICROSECONDS);
}

KJ_TEST("wake() from another thread interrupts a loop cycling through short timers promptly") {
// While the loop keeps re-arming a short timer (so wait() is always a short timed sleep), a
// cross-thread fulfill must still get through promptly: the timer wakeup and wake() share the
Expand Down
Loading