From f0550859c9d7497c30be69cc8a1a2620df74a815 Mon Sep 17 00:00:00 2001 From: njs Date: Thu, 20 Aug 2026 23:40:49 +0000 Subject: [PATCH] Use tokio::time::Instant instead of std::time::Instant These are identical in all situations, except when using tokio's virtualized-wallclock testing mode, which makes time-based tests fast and deterministic, and is fantastic for things like fuzzing. This makes h2 behave correctly in any downstream codebases with this style of test. --- Cargo.toml | 2 +- src/proto/streams/recv.rs | 2 +- src/proto/streams/stream.rs | 3 ++- tests/h2-tests/Cargo.toml | 2 +- tests/h2-tests/tests/flow_control.rs | 2 +- tests/h2-tests/tests/stream_states.rs | 2 +- 6 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 26eb88bb2..2a66cd459 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,7 +43,7 @@ atomic-waker = "1.0.0" futures-core = { version = "0.3", default-features = false } futures-sink = { version = "0.3", default-features = false } tokio-util = { version = "0.7.1", features = ["codec", "io"] } -tokio = { version = "1", features = ["io-util"] } +tokio = { version = "1", features = ["io-util", "time"] } bytes = "1" http = "1.1" tracing = { version = "0.1.35", default-features = false, features = ["std"] } diff --git a/src/proto/streams/recv.rs b/src/proto/streams/recv.rs index c56b368fb..df56c68bc 100644 --- a/src/proto/streams/recv.rs +++ b/src/proto/streams/recv.rs @@ -4,11 +4,11 @@ use crate::frame::{PushPromiseHeaderError, Reason, DEFAULT_INITIAL_WINDOW_SIZE}; use crate::proto; use http::{HeaderMap, Request, Response}; +use tokio::time::Instant; use std::cmp::Ordering; use std::io; use std::task::{Context, Poll, Waker}; -use std::time::Instant; #[derive(Debug)] pub(super) struct Recv { diff --git a/src/proto/streams/stream.rs b/src/proto/streams/stream.rs index 3b0ddec76..bc21b851d 100644 --- a/src/proto/streams/stream.rs +++ b/src/proto/streams/stream.rs @@ -4,7 +4,8 @@ use super::*; use std::fmt; use std::task::{Context, Waker}; -use std::time::Instant; + +use tokio::time::Instant; /// Tracks Stream related state /// diff --git a/tests/h2-tests/Cargo.toml b/tests/h2-tests/Cargo.toml index 6afdf9053..e8f0eccc2 100644 --- a/tests/h2-tests/Cargo.toml +++ b/tests/h2-tests/Cargo.toml @@ -11,4 +11,4 @@ edition = "2018" h2-support = { path = "../h2-support" } tracing = "0.1.13" futures = { version = "0.3", default-features = false, features = ["alloc"] } -tokio = { version = "1", features = ["macros", "net", "rt", "io-util", "rt-multi-thread"] } +tokio = { version = "1", features = ["macros", "net", "rt", "io-util", "rt-multi-thread", "test-util"] } diff --git a/tests/h2-tests/tests/flow_control.rs b/tests/h2-tests/tests/flow_control.rs index 11d1207e1..668c8d15f 100644 --- a/tests/h2-tests/tests/flow_control.rs +++ b/tests/h2-tests/tests/flow_control.rs @@ -683,7 +683,7 @@ async fn padded_data_stream_error_releases_connection_capacity() { } // Regression test for TODO -#[tokio::test] +#[tokio::test(start_paused = true)] async fn padded_data_on_forgotten_stream_releases_connection_capacity() { h2_support::trace_init!(); let (io, mut srv) = mock::new(); diff --git a/tests/h2-tests/tests/stream_states.rs b/tests/h2-tests/tests/stream_states.rs index 531f39d0a..fe50c2825 100644 --- a/tests/h2-tests/tests/stream_states.rs +++ b/tests/h2-tests/tests/stream_states.rs @@ -982,7 +982,7 @@ async fn send_rst_stream_allows_recv_trailers() { join(srv, client).await; } -#[tokio::test] +#[tokio::test(start_paused = true)] async fn rst_stream_expires() { h2_support::trace_init!(); let (io, mut srv) = mock::new();