Files
zed/crates/gpui_web/examples/hello_web/http_client_test.rs
T
Lukas Wirth 2c2f737ab2 gpui: Add sendable foreground execution for web HTTP clients (#66165)
## Summary

This cleans up the gpui_platform code a bit, and in combination the way
we set up the http client for the web. This does introduce a
`ForegroundExecutor` variant that it itself is Send-able, allowing to
dispatch foreground futures from background workers (by requiring the
future to be Send, unlike `ForegroundExecutor`), which is a requirement
for web fetch.

a31c7b54c60738b82524e4e4c12aec2f6caf101d fixes a bunch of warnings with
our binary names

Release Notes:

- [gpui] N/A
2026-10-29 09:12:10 +00:00

51 lines
1.9 KiB
Rust

//! Exercises Fetch response-body contention between the browser and real workers.
//!
//! The browser harness supplies chunked Fetch streams while the production HTTP
//! client pumps them on the main thread and background workers consume them.
use futures::AsyncReadExt as _;
use gpui::Platform as _;
use gpui_web::{FetchHttpClient, WebPlatform};
use http_client::HttpClient as _;
use std::sync::Arc;
use wasm_bindgen::prelude::*;
fn main() {}
#[wasm_bindgen]
pub async fn test_worker_fetch(url: String) -> Result<(), JsValue> {
let platform = WebPlatform::new(true);
let client = Arc::new(FetchHttpClient::new(
platform.foreground_executor().to_send(),
));
let background = platform.background_executor();
let main_thread = std::thread::current().id();
let readers = (0..8).map(|_| {
let client = client.clone();
let url = url.clone();
background.spawn(async move {
anyhow::ensure!(
std::thread::current().id() != main_thread,
"response body must be read on a real worker"
);
let mut response = client.get(&url, Default::default(), true).await?;
let mut bytes = Vec::new();
response.body_mut().read_to_end(&mut bytes).await?;
anyhow::ensure!(bytes.len() == 32768 * 3, "response did not reach EOF");
for (index, chunk) in bytes.chunks_exact(3).enumerate() {
let expected = [
(index % 251) as u8,
(index % 239) as u8,
(index % 227) as u8,
];
anyhow::ensure!(chunk == expected, "response chunks must remain ordered");
}
anyhow::Ok(())
})
});
futures::future::try_join_all(readers)
.await
.map(|_| ())
.map_err(|error| JsValue::from_str(&error.to_string()))
}