From 0ce1b938889b4a7ff9f62ab9bf00acdac80a6ac4 Mon Sep 17 00:00:00 2001 From: Heap-Hop Date: Thu, 28 Nov 2024 22:58:22 +0900 Subject: [PATCH] add test_tcp_input_stream_should_not_wake_on_empty_data --- .../src/bin/preview2_tcp_streams.rs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/crates/test-programs/src/bin/preview2_tcp_streams.rs b/crates/test-programs/src/bin/preview2_tcp_streams.rs index c756958968d4..712b234bbfaf 100644 --- a/crates/test-programs/src/bin/preview2_tcp_streams.rs +++ b/crates/test-programs/src/bin/preview2_tcp_streams.rs @@ -109,6 +109,29 @@ fn test_tcp_shutdown_should_not_lose_data(net: &Network, family: IpAddressFamily }); } +/// InputStream::subscribe should not wake up if there is no data to read. +fn test_tcp_input_stream_should_not_wake_on_empty_data(net: &Network, family: IpAddressFamily) { + setup(net, family, |server, client| { + use test_programs::wasi::clocks::monotonic_clock::subscribe_duration; + let timeout_100ms = 100_000_000; + + // Send some data to the server + client.output.blocking_write_and_flush(b"Hi!").unwrap(); + + server.input.subscribe().block(); + let res = server.input.read(512).unwrap(); + assert_eq!(res, b"Hi!", "Expected to receive data"); + + // Don't send any data + + let res = server + .input + .subscribe() + .block_until(&subscribe_duration(timeout_100ms)); + assert!(res.is_err(), "Expected to time out cause no data was sent"); + }); +} + fn main() { let net = Network::default(); @@ -123,6 +146,9 @@ fn main() { test_tcp_shutdown_should_not_lose_data(&net, IpAddressFamily::Ipv4); test_tcp_shutdown_should_not_lose_data(&net, IpAddressFamily::Ipv6); + + test_tcp_input_stream_should_not_wake_on_empty_data(&net, IpAddressFamily::Ipv4); + test_tcp_input_stream_should_not_wake_on_empty_data(&net, IpAddressFamily::Ipv6); } struct Connection {