Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

wasi-http: test between-bytes-timeout on p3 (#14886)

Xia Chao19ac91fffc66
diff --git a/crates/test-programs/src/bin/p3_http_outbound_request_between_bytes_timeout.rs b/crates/test-programs/src/bin/p3_http_outbound_request_between_bytes_timeout.rs
new file mode 100644
index 0000000000..15022d8fe4
--- /dev/null
+++ b/crates/test-programs/src/bin/p3_http_outbound_request_between_bytes_timeout.rs
@@ -0,0 +1,47 @@
+//! `between-bytes-timeout` bounds the wait for each chunk of the body: the server
+//! sends one chunk then stalls, so reading the second one must fail.
+use std::time::Duration;
+
+use test_programs::p3::wasi::http::types::{ErrorCode, Method, Scheme};
+
+struct Component;
+
+test_programs::p3::export!(Component);
+
+impl test_programs::p3::exports::wasi::cli::run::Guest for Component {
+ async fn run() -> Result<(), ()> {
+ let addr = test_programs::p3::wasi::cli::environment::get_environment()
+ .into_iter()
+ .find_map(|(k, v)| k.eq("HTTP_SERVER").then_some(v))
+ .unwrap();
+ let between_bytes = Duration::from_millis(100).as_nanos() as u64;
+
+ let res = test_programs::p3::http::request(
+ Method::Get,
+ Scheme::Http,
+ &addr,
+ "/between-bytes-timeout",
+ None,
+ None,
+ None, // connect_timeout
+ None, // first_byte_timeout
+ Some(between_bytes), // between_bytes_timeout
+ )
+ .await;
+
+ let err = match res {
+ Ok(_) => panic!("the stalled second chunk should have timed out"),
+ Err(err) => err,
+ };
+ assert!(
+ matches!(
+ err.downcast_ref::<ErrorCode>(),
+ Some(ErrorCode::ConnectionReadTimeout)
+ ),
+ "expected a connection read timeout: {err:?}"
+ );
+ Ok(())
+ }
+}
+
+fn main() {}
diff --git a/crates/wasi-http/tests/all/p3/mod.rs b/crates/wasi-http/tests/all/p3/mod.rs
index 9e2507dc29..bea271dd79 100644
--- a/crates/wasi-http/tests/all/p3/mod.rs
+++ b/crates/wasi-http/tests/all/p3/mod.rs
@@ -247,6 +247,16 @@ async fn p3_http_outbound_request_get() -> wasmtime::Result<()> {
run_cli(P3_HTTP_OUTBOUND_REQUEST_GET_COMPONENT, &server).await
}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p3_http_outbound_request_between_bytes_timeout() -> wasmtime::Result<()> {
+ let server = Server::http1_stalled_body(1, std::time::Duration::from_secs(5))?;
+ run_cli(
+ P3_HTTP_OUTBOUND_REQUEST_BETWEEN_BYTES_TIMEOUT_COMPONENT,
+ &server,
+ )
+ .await
+}
+
#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p3_http_outbound_request_first_byte_timeout() -> wasmtime::Result<()> {
let server = Server::http1_delayed(1, std::time::Duration::from_secs(5))?;