Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

test(openhuman-backend-model): add test for connection reuse across managed calls

Steven Enamakeled76e6c702c6
diff --git a/crates/openhuman-core/src/inference/provider/openhuman_backend_model_tests.rs b/crates/openhuman-core/src/inference/provider/openhuman_backend_model_tests.rs
index dedd68f65a..82a05a6d26 100644
--- a/crates/openhuman-core/src/inference/provider/openhuman_backend_model_tests.rs
+++ b/crates/openhuman-core/src/inference/provider/openhuman_backend_model_tests.rs
@@ -408,6 +408,72 @@ async fn probe_readiness_fails_open_on_timeout_or_5xx() {
);
}
+// ── shared pooled client (time to first token) ──────────────────────────
+
+/// A TCP relay in front of `upstream` that counts the connections it accepts.
+async fn spawn_counting_relay(
+ upstream: String,
+) -> (String, std::sync::Arc<std::sync::atomic::AtomicUsize>) {
+ let accepted = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
+ .await
+ .expect("bind relay");
+ let addr = listener.local_addr().expect("relay addr").to_string();
+ let counter = accepted.clone();
+ tokio::spawn(async move {
+ while let Ok((mut inbound, _)) = listener.accept().await {
+ counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
+ let upstream = upstream.clone();
+ tokio::spawn(async move {
+ if let Ok(mut outbound) = tokio::net::TcpStream::connect(&upstream).await {
+ let _ = tokio::io::copy_bidirectional(&mut inbound, &mut outbound).await;
+ }
+ });
+ }
+ });
+ (addr, accepted)
+}
+
+/// Every managed call used to build its own `reqwest::Client`, so each call
+/// opened a new connection (a fresh TCP + TLS handshake against the real
+/// backend) before its first token. Consecutive calls must now reuse one.
+#[tokio::test]
+async fn consecutive_managed_calls_reuse_one_connection() {
+ let tmp = tempfile::TempDir::new().unwrap();
+ seed_app_session(tmp.path());
+ let upstream = spawn_static_chat_server(
+ axum::http::StatusCode::OK,
+ serde_json::json!({
+ "id": "chatcmpl-pool",
+ "object": "chat.completion",
+ "created": 1,
+ "model": "reasoning-v1",
+ "choices": [{
+ "index": 0,
+ "message": { "role": "assistant", "content": "ok" },
+ "finish_reason": "stop"
+ }],
+ "usage": { "prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2 }
+ }),
+ )
+ .await;
+ let (relay, accepted) = spawn_counting_relay(upstream).await;
+ let backend = backend_pointed_at(&relay, tmp.path());
+
+ for call in 0..3 {
+ backend
+ .probe_readiness()
+ .await
+ .unwrap_or_else(|error| panic!("managed call {call} failed: {error}"));
+ }
+
+ assert_eq!(
+ accepted.load(std::sync::atomic::Ordering::SeqCst),
+ 1,
+ "three consecutive managed calls must share one pooled connection"
+ );
+}
+
// ── resolve_bearer local-expiry precheck (#5503, part e) ───────────────
#[test]