diff --git a/libs/runtime/core/src/application.rs b/libs/runtime/core/src/application.rs index 547cb3bf..e23030bb 100644 --- a/libs/runtime/core/src/application.rs +++ b/libs/runtime/core/src/application.rs @@ -86,6 +86,7 @@ use crate::{ types::ApiAppState, utils::get_epoch_seconds, websocket::{self, WebSocketMessageHandler}, + websocket_dedupe::{InMemoryMessageIdStore, DEFAULT_MESSAGE_ID_TTL_MS}, }; /// Shutdown signal for a consumer — either oneshot (SQS) or broadcast (Redis). @@ -122,6 +123,10 @@ pub struct Application { // through, so that `run` can start the worker that waits on clients. // Taken when it does, since it is only started once. ws_conn_registry: Option>, + // Kept for `run` to start its sweep, for the same reason as the registry + // above. The store itself is built in setup, since building it spawns + // nothing. + ws_seen_messages: Option>, ws_app_routes: Arc>>>, custom_auth_guards: Arc>>>, server_shutdown_signal: Option>, @@ -166,6 +171,7 @@ impl Application { ipc_server_shutdown_signal: None, ws_connections: None, ws_conn_registry: None, + ws_seen_messages: None, ws_app_routes: Arc::new(AsyncMutex::new(HashMap::new())), custom_auth_guards: Arc::new(AsyncMutex::new(HashMap::new())), resource_store: None, @@ -350,6 +356,9 @@ impl Application { // mean spawning, and this runs outside the async runtime when in FFI mode. self.ws_conn_registry = Some(conn_registry.clone()); + let seen_messages = Arc::new(InMemoryMessageIdStore::new(DEFAULT_MESSAGE_ID_TTL_MS)); + self.ws_seen_messages = Some(seen_messages.clone()); + // As with HTTP routes, the FFI call mode has the SDK register these // as it binds each in-process handler. In the IPC call mode the // runtime registers one per blueprint handler, so that messages @@ -382,6 +391,7 @@ impl Application { websocket_base_path, get(websocket::handler).with_state(websocket::WebSocketAppState { connections: conn_registry, + seen_messages: seen_messages.clone(), routes: self.ws_app_routes.clone(), route_key: websocket_config.route_key.clone(), api_auth: api_config.auth.clone(), @@ -708,6 +718,10 @@ impl Application { conn_registry.start_client_ack_worker(); } + if let Some(seen_messages) = self.ws_seen_messages.take() { + seen_messages.start_eviction(); + } + self.run_ipc_stream_server().await?; let mut server_task = None; diff --git a/libs/runtime/core/src/lib.rs b/libs/runtime/core/src/lib.rs index 274f2c39..72daa83c 100644 --- a/libs/runtime/core/src/lib.rs +++ b/libs/runtime/core/src/lib.rs @@ -35,3 +35,4 @@ pub mod types; pub(crate) mod utils; mod value_sources; pub mod websocket; +pub mod websocket_dedupe; diff --git a/libs/runtime/core/src/websocket.rs b/libs/runtime/core/src/websocket.rs index 6ec5ca8b..e9c688dd 100644 --- a/libs/runtime/core/src/websocket.rs +++ b/libs/runtime/core/src/websocket.rs @@ -7,6 +7,7 @@ use std::{ time::{Duration, Instant}, }; +use crate::websocket_dedupe::MessageIdStore; use async_trait::async_trait; use axum::{ extract::{ @@ -78,6 +79,9 @@ pub(crate) struct WebSocketAppState { pub connection_auth_guards: HashMap>, pub cors: Option, pub resource_store: Arc, + // What the node has already seen from its clients, so a message resent + // because an acknowledgement went missing is not acted on twice. + pub seen_messages: Arc, } #[derive(Clone, Debug, Serialize, Deserialize)] @@ -1055,6 +1059,39 @@ async fn validate_auth_message_token_with_guards( Ok(serde_json::Value::Object(accumulated_claims)) } +/// Whether a message has already been acted on, so a resend of it is not acted +/// on again. +/// +/// Only asked of a message the client gave an id to. An id the runtime made up +/// for one that carried none is unique by construction, so it would never match +/// anything, and remembering it would fill the store with entries nothing can +/// look up. +/// +/// This runs after the acknowledgement has gone out, which is the order it has +/// to be in. A client resends because it did not hear one, and the thing that +/// went missing may have been the acknowledgement rather than the message. +/// Staying silent about a duplicate would leave it resending the message the +/// runtime is refusing to answer for. +async fn already_acted_on( + message_id: &Option, + connection_id: &str, + state: &WebSocketAppState, +) -> bool { + let Some(message_id) = message_id else { + return false; + }; + + if state.seen_messages.record_and_check_seen(message_id).await { + debug!( + "client {connection_id} sent message {message_id} again, which has already been \ + handled, so it is not handled a second time" + ); + return true; + } + + false +} + async fn process_message( msg: Message, connection_id: String, @@ -1069,6 +1106,9 @@ async fn process_message( state.route_key.clone(), )?; if let Some((route, message_id, _requires_ack, data)) = resolved { + if already_acted_on(&message_id, &connection_id, state).await { + return ControlFlow::Continue(()); + } if let Some(handler) = get_message_route_handler(&route, state).await { handle_json_message( handler.clone(), @@ -1090,6 +1130,9 @@ async fn process_message( Message::Binary(bytes) => { let resolved = resolve_binary_route(&bytes, connection_id.clone())?; if let Some((route, message_id, _requires_ack, bytes_stripped)) = resolved { + if already_acted_on(&message_id, &connection_id, state).await { + return ControlFlow::Continue(()); + } if let Some(handler) = get_message_route_handler(&route, state).await { handle_binary_message( handler.clone(), diff --git a/libs/runtime/core/src/websocket_dedupe.rs b/libs/runtime/core/src/websocket_dedupe.rs new file mode 100644 index 00000000..94feb313 --- /dev/null +++ b/libs/runtime/core/src/websocket_dedupe.rs @@ -0,0 +1,153 @@ +use std::{ + collections::HashMap, + sync::{Arc, RwLock}, + time::{Duration, Instant}, +}; + +use async_trait::async_trait; +use tracing::debug; + +/// How long a message id is remembered for, which is how far apart two copies +/// of a message can arrive and still be recognised as the same one. +pub const DEFAULT_MESSAGE_ID_TTL_MS: u64 = 300_000; + +/// How often expired ids are cleaned up. Expiry is decided by the timestamp +/// rather than by the clean up, so this only bounds how long a dead entry keeps +/// its memory, not how long it counts as seen. +const EVICTION_INTERVAL_MS: u64 = 60_000; + +/// Remembers the messages a client has already sent, so the same one is not +/// acted on twice. +/// +/// A client resends when it does not receive an acknowledgement in time, and the +/// message that went missing may have been the acknowledgement rather than the +/// message. The second copy is a delivery the runtime already has. +#[async_trait] +pub trait MessageIdStore: Send + Sync + std::fmt::Debug { + /// Records a message id and says whether it was already there. + /// + /// One call rather than a check and a write, so two copies of a message + /// arriving together cannot both find it absent and both be processed. + async fn record_and_check_seen(&self, message_id: &str) -> bool; +} + +/// The store for a single node, which is every deployment that is not a +/// cluster. +/// +/// A cluster needs a shared one, since a client that reconnects may land on a +/// different node and resend there, and what this node holds in-memory says nothing about +/// what another one has seen. +#[derive(Debug)] +pub struct InMemoryMessageIdStore { + seen: Arc>>, + ttl: Duration, +} + +impl InMemoryMessageIdStore { + pub fn new(ttl_ms: u64) -> Self { + Self { + seen: Arc::new(RwLock::new(HashMap::new())), + ttl: Duration::from_millis(ttl_ms), + } + } + + /// Starts the sweep that keeps the store from growing without limit. + /// + /// Spawns, so it must be called with a runtime running. Nothing depends on + /// it for correctness, since an entry past its time is treated as absent + /// whether or not it has been swept. + pub fn start_eviction(self: Arc) { + tokio::spawn(async move { + let mut interval = tokio::time::interval(Duration::from_millis(EVICTION_INTERVAL_MS)); + loop { + interval.tick().await; + self.evict_expired(); + } + }); + } + + fn evict_expired(&self) { + let now = Instant::now(); + let mut seen = match self.seen.write() { + Ok(seen) => seen, + Err(poisoned) => poisoned.into_inner(), + }; + let before = seen.len(); + seen.retain(|_, recorded_at| now.duration_since(*recorded_at) < self.ttl); + let removed = before - seen.len(); + if removed > 0 { + debug!("swept {removed} expired message ids, {} remain", seen.len()); + } + } +} + +#[async_trait] +impl MessageIdStore for InMemoryMessageIdStore { + async fn record_and_check_seen(&self, message_id: &str) -> bool { + let now = Instant::now(); + let mut seen = match self.seen.write() { + Ok(seen) => seen, + Err(poisoned) => poisoned.into_inner(), + }; + + match seen.insert(message_id.to_string(), now) { + // Past its time, so this is a new message that happens to reuse an + // id the store had not got around to sweeping. + Some(recorded_at) => now.duration_since(recorded_at) < self.ttl, + None => false, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test_log::test(tokio::test)] + async fn test_a_message_is_new_the_first_time_and_seen_the_second() { + let store = InMemoryMessageIdStore::new(DEFAULT_MESSAGE_ID_TTL_MS); + + assert!(!store.record_and_check_seen("m-1").await); + assert!(store.record_and_check_seen("m-1").await); + assert!(store.record_and_check_seen("m-1").await); + } + + #[test_log::test(tokio::test)] + async fn test_messages_are_told_apart_by_their_id() { + let store = InMemoryMessageIdStore::new(DEFAULT_MESSAGE_ID_TTL_MS); + + assert!(!store.record_and_check_seen("m-1").await); + assert!(!store.record_and_check_seen("m-2").await); + } + + /// An id past its time is a new message again, which is what makes the + /// store bounded rather than a record of everything a client ever sent. + #[test_log::test(tokio::test)] + async fn test_an_id_is_forgotten_once_its_time_is_up() { + let store = InMemoryMessageIdStore::new(50); + + assert!(!store.record_and_check_seen("m-1").await); + tokio::time::sleep(Duration::from_millis(80)).await; + assert!( + !store.record_and_check_seen("m-1").await, + "an id past its time should be treated as one that was never seen" + ); + } + + /// Expiry is decided by the entry's own timestamp, so it holds whether or + /// not the sweep has run. + #[test_log::test(tokio::test)] + async fn test_sweeping_removes_what_has_expired_and_keeps_what_has_not() { + let store = InMemoryMessageIdStore::new(50); + + store.record_and_check_seen("old").await; + tokio::time::sleep(Duration::from_millis(80)).await; + store.record_and_check_seen("new").await; + + store.evict_expired(); + + let seen = store.seen.read().unwrap(); + assert!(!seen.contains_key("old"), "an expired id should be swept"); + assert!(seen.contains_key("new"), "a live id should be kept"); + } +} diff --git a/libs/runtime/core/tests/common/ipc.rs b/libs/runtime/core/tests/common/ipc.rs new file mode 100644 index 00000000..a68da4d9 --- /dev/null +++ b/libs/runtime/core/tests/common/ipc.rs @@ -0,0 +1,270 @@ +//! The shared harness for the IPC end to end tests. +//! +//! Each of those files compiles as its own crate, so this is included by each +//! rather than linked once, which is why unused items are allowed here. A file +//! covering one application type will not touch every helper. +#![allow(dead_code)] + +use std::{collections::HashMap, time::Duration}; + +use axum::body::Body; +use celerity_runtime_core::{ + application::Application, + config::RuntimeConfig, + ipc_proto::{ + self as proto, handler_message, + handler_runtime_service_client::HandlerRuntimeServiceClient, runtime_message, + }, +}; +use tokio::{net::UnixStream, sync::mpsc}; +use tokio_stream::StreamExt; +use tonic::transport::{Endpoint, Uri}; + +pub fn ipc_env( + service_name: &str, + fixture: &str, + socket: &str, + overrides: &[(&'static str, &str)], +) -> super::MockEnvVars<'static> { + let mut vars: std::collections::HashMap<&'static str, String> = vec![ + ("CELERITY_BLUEPRINT", fixture.to_string()), + ("CELERITY_SERVICE_NAME", service_name.to_string()), + ("CELERITY_RUNTIME_PLATFORM", "local".to_string()), + ("CELERITY_RUNTIME_CALL_MODE", "ipc".to_string()), + ("CELERITY_SERVER_PORT", "0".to_string()), + ("CELERITY_RUNTIME_SOCKET_FALLBACK_PORT", "0".to_string()), + ("CELERITY_RUNTIME_SOCKET", socket.to_string()), + ("CELERITY_SERVER_LOOPBACK_ONLY", "true".to_string()), + ("CELERITY_TEST_MODE", "true".to_string()), + ("CELERITY_ENABLE_LOCAL_INVOKE", "true".to_string()), + ("CELERITY_VARIABLE_logLevel", "DEBUG".to_string()), + ("CELERITY_CLIENT_IP_SOURCE", "ConnectInfo".to_string()), + ] + .into_iter() + .collect(); + for (key, value) in overrides { + vars.insert(key, value.to_string()); + } + super::MockEnvVars::new(Some(vars)) +} + +/// A socket path unique to this test, so tests can run alongside each other. +pub fn socket_path(name: &str) -> String { + std::env::temp_dir() + .join(format!("celerity-ipc-{}-{name}.sock", std::process::id())) + .to_string_lossy() + .into_owned() +} + +/// Starts a runtime serving the given blueprint, returning its public address +/// and the socket its handler stream is on. +pub async fn start_runtime( + name: &str, + fixture: &str, +) -> (Application, std::net::SocketAddr, String) { + start_runtime_with(name, fixture, &[]).await +} + +/// Starts a runtime with environment overrides applied on top of the defaults. +pub async fn start_runtime_with( + name: &str, + fixture: &str, + overrides: &[(&'static str, &str)], +) -> (Application, std::net::SocketAddr, String) { + let socket = socket_path(name); + let env_vars = ipc_env(name, fixture, &socket, overrides); + let runtime_config = RuntimeConfig::from_env(&env_vars); + let mut app = Application::new(runtime_config, Box::new(env_vars)); + app.setup().unwrap(); + let app_info = app.run(false).await.unwrap(); + let addr = app_info.http_server_address.unwrap(); + (app, addr, socket) +} + +/// Stands in for a handlers executable. +pub struct HandlerStub { + /// The events the handler was asked to process. + dispatches: mpsc::Receiver, + /// The events the handler was told to stop working on. + pub cancels: mpsc::Receiver, + /// Frames the handler sends of its own accord, rather than in answer to a + /// dispatch. + outbound: mpsc::Sender, + /// What the runtime made of each batch of websocket sends. + ws_acks: mpsc::Receiver, +} + +impl HandlerStub { + /// Connects, completes the handshake by declaring every tag the runtime + /// asked for, then answers dispatches with whatever `respond` returns. + /// + /// Returning `None` withholds a result, which is how a handler that has + /// stopped answering is simulated. + pub async fn attach( + socket: &str, + respond: impl Fn(&proto::Dispatch) -> Option + Send + 'static, + ) -> Self { + let channel = Endpoint::try_from("http://[::]:50051") + .unwrap() + .connect_with_connector(tower_tonic::service_fn({ + let socket = socket.to_string(); + move |_: Uri| { + let socket = socket.clone(); + async move { + Ok::<_, std::io::Error>(hyper_util::rt::TokioIo::new( + UnixStream::connect(socket).await?, + )) + } + } + })) + .await + .expect("the runtime should be serving the handler stream"); + + let mut client = HandlerRuntimeServiceClient::new(channel); + let (handler_tx, handler_rx) = mpsc::channel::(16); + let mut frames = client + .event_stream(tokio_stream::wrappers::ReceiverStream::new(handler_rx)) + .await + .expect("the stream should be accepted") + .into_inner(); + + let Some(Ok(proto::RuntimeMessage { + frame: Some(runtime_message::Frame::Config(config)), + })) = frames.next().await + else { + panic!("expected configuration to arrive before anything else"); + }; + + handler_tx + .send(proto::HandlerMessage { + frame: Some(handler_message::Frame::Ready(proto::Ready { + handler_tags: config + .handlers + .iter() + .map(|handler| handler.handler_tag.clone()) + .collect(), + initial_credit: 8, + sdk_version: "test/0.1".to_string(), + limits: vec![], + })), + }) + .await + .unwrap(); + + let Some(Ok(proto::RuntimeMessage { + frame: Some(runtime_message::Frame::ReadyAck(ack)), + })) = frames.next().await + else { + panic!("expected a ready acknowledgement"); + }; + assert!( + ack.accepted, + "the handshake should be accepted, unknown={:?} unhandled={:?}", + ack.unknown_tags, ack.unhandled_tags + ); + + let (dispatch_tx, dispatches) = mpsc::channel(16); + let (cancel_tx, cancels) = mpsc::channel(16); + let (ws_ack_tx, ws_acks) = mpsc::channel(16); + let outbound = handler_tx.clone(); + tokio::spawn(async move { + while let Some(Ok(message)) = frames.next().await { + let dispatch = match message.frame { + Some(runtime_message::Frame::Dispatch(dispatch)) => dispatch, + Some(runtime_message::Frame::Cancel(cancel)) => { + cancel_tx.send(cancel).await.ok(); + continue; + } + Some(runtime_message::Frame::WsAck(ack)) => { + ws_ack_tx.send(ack).await.ok(); + continue; + } + _ => continue, + }; + let outcome = respond(&dispatch); + let id = dispatch.id.clone(); + dispatch_tx.send(dispatch).await.ok(); + + if let Some(outcome) = outcome { + handler_tx + .send(proto::HandlerMessage { + frame: Some(handler_message::Frame::Result(proto::Result { + id, + credit_grant: 1, + outcome: Some(outcome), + })), + }) + .await + .ok(); + } + } + }); + + HandlerStub { + dispatches, + cancels, + outbound, + ws_acks, + } + } + + /// Sends a batch of websocket messages the way a handler does, of its own + /// accord rather than as the result of an event. + pub async fn send_ws(&self, correlation_id: &str, messages: Vec) { + self.outbound + .send(proto::HandlerMessage { + frame: Some(handler_message::Frame::WsSend(proto::WsSend { + correlation_id: correlation_id.to_string(), + messages, + })), + }) + .await + .expect("the runtime should still be taking frames"); + } + + pub async fn next_ws_ack(&mut self) -> Option { + tokio::time::timeout(Duration::from_secs(10), self.ws_acks.recv()) + .await + .ok() + .flatten() + } + + pub async fn next_dispatch(&mut self) -> Option { + tokio::time::timeout(Duration::from_secs(10), self.dispatches.recv()) + .await + .ok() + .flatten() + } + + pub async fn next_cancel(&mut self) -> Option { + tokio::time::timeout(Duration::from_secs(10), self.cancels.recv()) + .await + .ok() + .flatten() + } +} + +pub fn json_response(body: &'static str) -> proto::result::Outcome { + proto::result::Outcome::Http(proto::HttpResponse { + status: 200, + headers: HashMap::from([( + "content-type".to_string(), + proto::Values { + values: vec!["application/json".to_string()], + }, + )]), + body: body.as_bytes().to_vec(), + }) +} + +pub fn websocket_ack() -> proto::result::Outcome { + proto::result::Outcome::Websocket(proto::Ack { + success: true, + error_message: String::new(), + }) +} + +pub fn http_client( +) -> hyper_util::client::legacy::Client { + hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::new()).build_http() +} diff --git a/libs/runtime/core/tests/common/mod.rs b/libs/runtime/core/tests/common/mod.rs index a893ea7b..5a98d551 100644 --- a/libs/runtime/core/tests/common/mod.rs +++ b/libs/runtime/core/tests/common/mod.rs @@ -1,3 +1,5 @@ +pub mod ipc; + use std::{collections::HashMap, env::VarError, sync::Arc}; use celerity_helpers::env::EnvVars; diff --git a/libs/runtime/core/tests/ipc_end_to_end_test.rs b/libs/runtime/core/tests/ipc_end_to_end_test.rs deleted file mode 100644 index 2a0b8ec0..00000000 --- a/libs/runtime/core/tests/ipc_end_to_end_test.rs +++ /dev/null @@ -1,1985 +0,0 @@ -//! Covers the IPC path end to end: requests and WebSocket messages reach a -//! handler over a real gRPC stream on a real Unix socket, and their results -//! come back. -//! -//! Every piece has unit coverage of its own. What these cover is the assembly, -//! and they do it the way a handlers executable would rather than by reaching -//! into the runtime's internals, so nothing here can pass while the transport -//! is broken. - -use std::{collections::HashMap, time::Duration}; - -use axum::{body::Body, http::Request}; -use base64::{engine::general_purpose::STANDARD as BASE64, Engine as _}; -use celerity_runtime_core::{ - application::Application, - config::RuntimeConfig, - consts::CELERITY_WS_CAPABILITIES_SIGNAL, - ipc_proto::{ - self as proto, handler_message, - handler_runtime_service_client::HandlerRuntimeServiceClient, runtime_message, - }, -}; -use futures::SinkExt; -use http_body_util::BodyExt; -use serde_json::json; -use tokio::{net::UnixStream, sync::mpsc}; -use tokio_stream::StreamExt; -use tonic::transport::{Endpoint, Uri}; - -mod common; - -fn ipc_env( - service_name: &str, - fixture: &str, - socket: &str, - overrides: &[(&'static str, &str)], -) -> common::MockEnvVars<'static> { - let mut vars: std::collections::HashMap<&'static str, String> = vec![ - ("CELERITY_BLUEPRINT", fixture.to_string()), - ("CELERITY_SERVICE_NAME", service_name.to_string()), - ("CELERITY_RUNTIME_PLATFORM", "local".to_string()), - ("CELERITY_RUNTIME_CALL_MODE", "ipc".to_string()), - ("CELERITY_SERVER_PORT", "0".to_string()), - ("CELERITY_RUNTIME_SOCKET_FALLBACK_PORT", "0".to_string()), - ("CELERITY_RUNTIME_SOCKET", socket.to_string()), - ("CELERITY_SERVER_LOOPBACK_ONLY", "true".to_string()), - ("CELERITY_TEST_MODE", "true".to_string()), - ("CELERITY_ENABLE_LOCAL_INVOKE", "true".to_string()), - ("CELERITY_VARIABLE_logLevel", "DEBUG".to_string()), - ("CELERITY_CLIENT_IP_SOURCE", "ConnectInfo".to_string()), - ] - .into_iter() - .collect(); - for (key, value) in overrides { - vars.insert(key, value.to_string()); - } - common::MockEnvVars::new(Some(vars)) -} - -/// A socket path unique to this test, so tests can run alongside each other. -fn socket_path(name: &str) -> String { - std::env::temp_dir() - .join(format!("celerity-ipc-{}-{name}.sock", std::process::id())) - .to_string_lossy() - .into_owned() -} - -/// Starts a runtime serving the given blueprint, returning its public address -/// and the socket its handler stream is on. -async fn start_runtime(name: &str, fixture: &str) -> (Application, std::net::SocketAddr, String) { - start_runtime_with(name, fixture, &[]).await -} - -/// Starts a runtime with environment overrides applied on top of the defaults. -async fn start_runtime_with( - name: &str, - fixture: &str, - overrides: &[(&'static str, &str)], -) -> (Application, std::net::SocketAddr, String) { - let socket = socket_path(name); - let env_vars = ipc_env(name, fixture, &socket, overrides); - let runtime_config = RuntimeConfig::from_env(&env_vars); - let mut app = Application::new(runtime_config, Box::new(env_vars)); - app.setup().unwrap(); - let app_info = app.run(false).await.unwrap(); - let addr = app_info.http_server_address.unwrap(); - (app, addr, socket) -} - -/// Setting an application up must not need a runtime to be running. -/// -/// Deliberately not a tokio test, which is the whole point. An SDK calls setup -/// from wherever the host language happens to be, with no runtime on that -/// thread, so anything here that spawns takes the process down rather than -/// returning an error. That is what a websocket API did until the worker that -/// waits on clients moved to `run`, and a tokio test cannot show it because a -/// tokio test always has the runtime that is missing. -#[test] -fn sets_up_a_websocket_api_without_a_runtime_to_spawn_into() { - let socket = socket_path("ipc-ws-setup-sync"); - let env_vars = ipc_env( - "ipc-ws-setup-sync", - "tests/data/fixtures/ipc-websocket-default-route.blueprint.yaml", - &socket, - &[], - ); - let runtime_config = RuntimeConfig::from_env(&env_vars); - let mut app = Application::new(runtime_config, Box::new(env_vars)); - - app.setup() - .expect("setting up a websocket api should not need a runtime"); -} - -/// Stands in for a handlers executable. -struct HandlerStub { - /// The events the handler was asked to process. - dispatches: mpsc::Receiver, - /// The events the handler was told to stop working on. - cancels: mpsc::Receiver, - /// Frames the handler sends of its own accord, rather than in answer to a - /// dispatch. - outbound: mpsc::Sender, - /// What the runtime made of each batch of websocket sends. - ws_acks: mpsc::Receiver, -} - -impl HandlerStub { - /// Connects, completes the handshake by declaring every tag the runtime - /// asked for, then answers dispatches with whatever `respond` returns. - /// - /// Returning `None` withholds a result, which is how a handler that has - /// stopped answering is simulated. - async fn attach( - socket: &str, - respond: impl Fn(&proto::Dispatch) -> Option + Send + 'static, - ) -> Self { - let channel = Endpoint::try_from("http://[::]:50051") - .unwrap() - .connect_with_connector(tower_tonic::service_fn({ - let socket = socket.to_string(); - move |_: Uri| { - let socket = socket.clone(); - async move { - Ok::<_, std::io::Error>(hyper_util::rt::TokioIo::new( - UnixStream::connect(socket).await?, - )) - } - } - })) - .await - .expect("the runtime should be serving the handler stream"); - - let mut client = HandlerRuntimeServiceClient::new(channel); - let (handler_tx, handler_rx) = mpsc::channel::(16); - let mut frames = client - .event_stream(tokio_stream::wrappers::ReceiverStream::new(handler_rx)) - .await - .expect("the stream should be accepted") - .into_inner(); - - let Some(Ok(proto::RuntimeMessage { - frame: Some(runtime_message::Frame::Config(config)), - })) = frames.next().await - else { - panic!("expected configuration to arrive before anything else"); - }; - - handler_tx - .send(proto::HandlerMessage { - frame: Some(handler_message::Frame::Ready(proto::Ready { - handler_tags: config - .handlers - .iter() - .map(|handler| handler.handler_tag.clone()) - .collect(), - initial_credit: 8, - sdk_version: "test/0.1".to_string(), - limits: vec![], - })), - }) - .await - .unwrap(); - - let Some(Ok(proto::RuntimeMessage { - frame: Some(runtime_message::Frame::ReadyAck(ack)), - })) = frames.next().await - else { - panic!("expected a ready acknowledgement"); - }; - assert!( - ack.accepted, - "the handshake should be accepted, unknown={:?} unhandled={:?}", - ack.unknown_tags, ack.unhandled_tags - ); - - let (dispatch_tx, dispatches) = mpsc::channel(16); - let (cancel_tx, cancels) = mpsc::channel(16); - let (ws_ack_tx, ws_acks) = mpsc::channel(16); - let outbound = handler_tx.clone(); - tokio::spawn(async move { - while let Some(Ok(message)) = frames.next().await { - let dispatch = match message.frame { - Some(runtime_message::Frame::Dispatch(dispatch)) => dispatch, - Some(runtime_message::Frame::Cancel(cancel)) => { - cancel_tx.send(cancel).await.ok(); - continue; - } - Some(runtime_message::Frame::WsAck(ack)) => { - ws_ack_tx.send(ack).await.ok(); - continue; - } - _ => continue, - }; - let outcome = respond(&dispatch); - let id = dispatch.id.clone(); - dispatch_tx.send(dispatch).await.ok(); - - if let Some(outcome) = outcome { - handler_tx - .send(proto::HandlerMessage { - frame: Some(handler_message::Frame::Result(proto::Result { - id, - credit_grant: 1, - outcome: Some(outcome), - })), - }) - .await - .ok(); - } - } - }); - - HandlerStub { - dispatches, - cancels, - outbound, - ws_acks, - } - } - - /// Sends a batch of websocket messages the way a handler does, of its own - /// accord rather than as the result of an event. - async fn send_ws(&self, correlation_id: &str, messages: Vec) { - self.outbound - .send(proto::HandlerMessage { - frame: Some(handler_message::Frame::WsSend(proto::WsSend { - correlation_id: correlation_id.to_string(), - messages, - })), - }) - .await - .expect("the runtime should still be taking frames"); - } - - async fn next_ws_ack(&mut self) -> Option { - tokio::time::timeout(Duration::from_secs(10), self.ws_acks.recv()) - .await - .ok() - .flatten() - } - - async fn next_dispatch(&mut self) -> Option { - tokio::time::timeout(Duration::from_secs(10), self.dispatches.recv()) - .await - .ok() - .flatten() - } - - async fn next_cancel(&mut self) -> Option { - tokio::time::timeout(Duration::from_secs(10), self.cancels.recv()) - .await - .ok() - .flatten() - } -} - -fn json_response(body: &'static str) -> proto::result::Outcome { - proto::result::Outcome::Http(proto::HttpResponse { - status: 200, - headers: HashMap::from([( - "content-type".to_string(), - proto::Values { - values: vec!["application/json".to_string()], - }, - )]), - body: body.as_bytes().to_vec(), - }) -} - -fn websocket_ack() -> proto::result::Outcome { - proto::result::Outcome::Websocket(proto::Ack { - success: true, - error_message: String::new(), - }) -} - -fn http_client( -) -> hyper_util::client::legacy::Client { - hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::new()).build_http() -} - -#[test_log::test(tokio::test)] -async fn serves_an_http_request_through_a_handler_over_the_stream() { - let (_app, addr, socket) = start_runtime( - "ipc-http", - "tests/data/fixtures/ipc-http-api.blueprint.yaml", - ) - .await; - let mut handler = - HandlerStub::attach(&socket, |_| Some(json_response(r#"{"id":"order-1"}"#))).await; - - let response = tokio::time::timeout( - Duration::from_secs(10), - http_client().request( - Request::builder() - .uri(format!( - "http://{addr}/orders/order-1?expand=items&expand=totals" - )) - .header("Host", "localhost") - .header("X-Trace", "abc") - .body(Body::empty()) - .unwrap(), - ), - ) - .await - .expect("the request should be served rather than time out") - .unwrap(); - - let status = response.status(); - let content_type = response - .headers() - .get("content-type") - .map(|value| value.to_str().unwrap().to_string()); - let body = response.into_body().collect().await.unwrap().to_bytes(); - - assert_eq!(status, 200); - assert_eq!(content_type, Some("application/json".to_string())); - assert_eq!(&body[..], br#"{"id":"order-1"}"#); - - // The frame the handler received should describe the request faithfully. - let dispatch = handler - .next_dispatch() - .await - .expect("the handler should have been given the request"); - assert_eq!(dispatch.handler_tag, "GET::/orders/{orderId}"); - assert!(dispatch.deadline_unix_ms > 0); - - let Some(proto::dispatch::Source::Http(request)) = dispatch.source else { - panic!("expected an HTTP source"); - }; - assert_eq!(request.method, "GET"); - assert_eq!(request.path, "/orders/order-1"); - assert_eq!(request.route, "/orders/{orderId}"); - assert_eq!( - request.path_params.get("orderId").map(|v| v.values.clone()), - Some(vec!["order-1".to_string()]) - ); - // Repeated query parameters survive as multiple values. - assert_eq!( - request.query_params.get("expand").map(|v| v.values.clone()), - Some(vec!["items".to_string(), "totals".to_string()]) - ); - // Header names reach the handler lowercased however they were sent. - assert_eq!( - request.headers.get("x-trace").map(|v| v.values.clone()), - Some(vec!["abc".to_string()]) - ); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn splits_a_catch_all_path_into_segments_without_losing_encoded_separators() { - let (_app, addr, socket) = start_runtime( - "ipc-catch-all", - "tests/data/fixtures/ipc-http-api.blueprint.yaml", - ) - .await; - let mut handler = HandlerStub::attach(&socket, |_| Some(json_response(r#"{"ok":true}"#))).await; - - // The middle segment carries an encoded separator, which must stay inside - // that segment rather than splitting it in two. - let response = tokio::time::timeout( - Duration::from_secs(10), - http_client().request( - Request::builder() - .uri(format!("http://{addr}/files/docs/a%2Fb/report%20final.pdf")) - .header("Host", "localhost") - .body(Body::empty()) - .unwrap(), - ), - ) - .await - .expect("the request should be served rather than time out") - .unwrap(); - assert_eq!(response.status(), 200); - - let dispatch = handler - .next_dispatch() - .await - .expect("the handler should have been given the request"); - let Some(proto::dispatch::Source::Http(request)) = dispatch.source else { - panic!("expected an HTTP source"); - }; - assert_eq!(request.route, "/files/{*filePath}"); - assert_eq!( - request - .path_params - .get("filePath") - .map(|v| v.values.clone()), - Some(vec![ - "docs".to_string(), - "a/b".to_string(), - "report final.pdf".to_string(), - ]) - ); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn restricts_the_handler_socket_to_the_runtime_user() { - use std::os::unix::fs::PermissionsExt; - - let (_app, _addr, socket) = start_runtime( - "ipc-socket-mode", - "tests/data/fixtures/ipc-http-api.blueprint.yaml", - ) - .await; - - // Anything able to connect can register as a handler and be given events, - // so the permissions on this socket are the whole of the access control. - let mode = tokio::fs::metadata(&socket) - .await - .expect("the socket should exist") - .permissions() - .mode() - & 0o777; - assert_eq!(mode, 0o600, "socket mode was {mode:o}"); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn restricts_a_socket_directory_it_creates_itself() { - use std::os::unix::fs::PermissionsExt; - - let dir = std::env::temp_dir().join(format!("celerity-ipc-dir-{}", std::process::id())); - let _ = tokio::fs::remove_dir_all(&dir).await; - let socket = dir.join("runtime.sock").to_string_lossy().into_owned(); - - let (_app, _addr, _) = start_runtime_with( - "ipc-socket-dir", - "tests/data/fixtures/ipc-http-api.blueprint.yaml", - &[("CELERITY_RUNTIME_SOCKET", &socket)], - ) - .await; - - // The socket's own mode is not the whole story. Enforcing it on connect is - // a Linux behaviour rather than a portable one, and a permissive umask - // would leave a directory another user could replace the socket in. - let mode = tokio::fs::metadata(&dir) - .await - .expect("the runtime should have created the directory") - .permissions() - .mode() - & 0o777; - assert_eq!(mode, 0o700, "directory mode was {mode:o}"); - - let _ = tokio::fs::remove_dir_all(&dir).await; -} - -#[test_log::test(tokio::test)] -async fn refuses_to_take_over_a_socket_another_runtime_is_listening_on() { - let (_app, _addr, socket) = start_runtime( - "ipc-socket-contended", - "tests/data/fixtures/ipc-http-api.blueprint.yaml", - ) - .await; - - // A second runtime pointed at the same socket must not unlink it. Doing so - // would leave the first serving a socket nothing can reach any more. - let env_vars = ipc_env( - "ipc-socket-contended-second", - "tests/data/fixtures/ipc-http-api.blueprint.yaml", - &socket, - &[("CELERITY_SERVER_PORT", "0")], - ); - let runtime_config = RuntimeConfig::from_env(&env_vars); - let mut second = Application::new(runtime_config, Box::new(env_vars)); - second.setup().unwrap(); - - let started = second.run(false).await; - assert!( - started.is_err(), - "the second runtime should refuse to start rather than take the socket" - ); - - // The first is still reachable, which is the point of refusing. - let _handler = HandlerStub::attach(&socket, |_| None).await; - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn returns_504_when_the_handler_never_answers() { - let (_app, addr, socket) = start_runtime( - "ipc-timeout", - "tests/data/fixtures/ipc-http-api.blueprint.yaml", - ) - .await; - // Withhold every result, as a handler that has stopped responding would. - let _handler = HandlerStub::attach(&socket, |_| None).await; - - // The POST route's timeout is deliberately short in this fixture. - let response = tokio::time::timeout( - Duration::from_secs(20), - http_client().request( - Request::builder() - .method("POST") - .uri(format!("http://{addr}/orders")) - .header("Host", "localhost") - .body(Body::from(r#"{"sku":"abc"}"#)) - .unwrap(), - ), - ) - .await - .expect("the runtime should time the request out rather than hang") - .unwrap(); - - assert_eq!(response.status(), 504); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn invokes_a_custom_handler_by_name_over_the_stream() { - let (_app, addr, socket) = start_runtime( - "ipc-invoke", - "tests/data/fixtures/ipc-http-api.blueprint.yaml", - ) - .await; - let mut handler = HandlerStub::attach(&socket, |_| { - Some(proto::result::Outcome::Custom(proto::CustomInvokeResult { - output: br#"{"reindexed":12}"#.to_vec(), - error_message: String::new(), - })) - }) - .await; - - let response = tokio::time::timeout( - Duration::from_secs(10), - http_client().request( - Request::builder() - .method("POST") - .uri(format!("http://{addr}/runtime/handlers/invoke")) - .header("Host", "localhost") - .header("content-type", "application/json") - .body(Body::from( - r#"{"handlerName":"reindexHandler","invocationType":"requestResponse","payload":{"full":true}}"#, - )) - .unwrap(), - ), - ) - .await - .expect("the invocation should be served rather than time out") - .unwrap(); - - let status = response.status(); - let body = response.into_body().collect().await.unwrap().to_bytes(); - assert_eq!(status, 200, "body: {}", String::from_utf8_lossy(&body)); - let body: serde_json::Value = serde_json::from_slice(&body).unwrap(); - assert_eq!(body["data"], r#"{"reindexed":12}"#); - - // The invocation reached the handlers executable as an ordinary event, on - // the custom tag, carrying the payload the caller sent. - let dispatch = handler - .next_dispatch() - .await - .expect("the handler should have been given the invocation"); - assert_eq!(dispatch.handler_tag, "custom::reindexHandler"); - let Some(proto::dispatch::Source::Custom(invoke)) = dispatch.source else { - panic!("expected a custom invocation source"); - }; - assert_eq!(invoke.handler_name, "reindexHandler"); - assert_eq!(invoke.input, br#"{"full":true}"#); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn invokes_an_http_handler_by_name_without_shaping_a_request() { - let (_app, addr, socket) = start_runtime( - "ipc-invoke-http", - "tests/data/fixtures/ipc-http-api.blueprint.yaml", - ) - .await; - let mut handler = HandlerStub::attach(&socket, |_| { - Some(proto::result::Outcome::Custom(proto::CustomInvokeResult { - output: br#"{"id":"order-1"}"#.to_vec(), - error_message: String::new(), - })) - }) - .await; - - let response = tokio::time::timeout( - Duration::from_secs(10), - http_client().request( - Request::builder() - .method("POST") - .uri(format!("http://{addr}/runtime/handlers/invoke")) - .header("Host", "localhost") - .header("content-type", "application/json") - .body(Body::from( - r#"{"handlerName":"getOrderHandler","invocationType":"requestResponse","payload":{"pathParams":{"orderId":"order-1"}}}"#, - )) - .unwrap(), - ), - ) - .await - .expect("the invocation should be served rather than time out") - .unwrap(); - - let status = response.status(); - let body = response.into_body().collect().await.unwrap().to_bytes(); - assert_eq!(status, 200, "body: {}", String::from_utf8_lossy(&body)); - - // Routed on the HTTP handler's own tag, since that is the tag the handler - // stream serves, and carrying the payload straight through rather than - // being dressed up as a request. - let dispatch = handler - .next_dispatch() - .await - .expect("the handler should have been given the invocation"); - assert_eq!(dispatch.handler_tag, "GET::/orders/{orderId}"); - let Some(proto::dispatch::Source::Custom(invoke)) = dispatch.source else { - panic!("expected a direct invocation source"); - }; - assert_eq!(invoke.handler_name, "getOrderHandler"); - assert_eq!(invoke.input, br#"{"pathParams":{"orderId":"order-1"}}"#); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn serves_the_invoke_endpoint_for_an_application_with_no_http_api() { - // The case the endpoint matters most in: nothing here can be triggered from - // outside, so without this there is no way to run these handlers by hand. - let (_app, addr, socket) = start_runtime( - "ipc-invoke-no-api", - "tests/data/fixtures/ipc-handlers-only.blueprint.yaml", - ) - .await; - let mut handler = HandlerStub::attach(&socket, |_| { - Some(proto::result::Outcome::Custom(proto::CustomInvokeResult { - output: br#"{"reconciled":3}"#.to_vec(), - error_message: String::new(), - })) - }) - .await; - - let response = tokio::time::timeout( - Duration::from_secs(10), - http_client().request( - Request::builder() - .method("POST") - .uri(format!("http://{addr}/runtime/handlers/invoke")) - .header("Host", "localhost") - .header("content-type", "application/json") - .body(Body::from( - r#"{"handlerName":"reconcileHandler","invocationType":"requestResponse"}"#, - )) - .unwrap(), - ), - ) - .await - .expect("the invocation should be served rather than time out") - .unwrap(); - - let status = response.status(); - let body = response.into_body().collect().await.unwrap().to_bytes(); - assert_eq!(status, 200, "body: {}", String::from_utf8_lossy(&body)); - let body: serde_json::Value = serde_json::from_slice(&body).unwrap(); - assert_eq!(body["data"], r#"{"reconciled":3}"#); - - let dispatch = handler - .next_dispatch() - .await - .expect("the handler should have been given the invocation"); - assert_eq!(dispatch.handler_tag, "custom::reconcileHandler"); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn does_not_serve_the_invoke_endpoint_unless_it_is_enabled() { - // Test mode and a local platform are both still on, as they are for every - // other test here, so this pins the switch as a condition in its own right. - let (_app, addr, socket) = start_runtime_with( - "ipc-invoke-disabled", - "tests/data/fixtures/ipc-http-api.blueprint.yaml", - &[("CELERITY_ENABLE_LOCAL_INVOKE", "false")], - ) - .await; - let _handler = HandlerStub::attach(&socket, |_| None).await; - - let response = tokio::time::timeout( - Duration::from_secs(10), - http_client().request( - Request::builder() - .method("POST") - .uri(format!("http://{addr}/runtime/handlers/invoke")) - .header("Host", "localhost") - .header("content-type", "application/json") - .body(Body::from( - r#"{"handlerName":"reindexHandler","invocationType":"requestResponse"}"#, - )) - .unwrap(), - ), - ) - .await - .expect("the request should be answered rather than hang") - .unwrap(); - - // Not routed at all, as opposed to routed and refused. - assert_eq!(response.status(), 404); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn invokes_a_handler_by_the_name_the_blueprint_publishes_it_under() { - let (_app, addr, socket) = start_runtime( - "ipc-invoke-published", - "tests/data/fixtures/ipc-http-api.blueprint.yaml", - ) - .await; - let mut handler = HandlerStub::attach(&socket, |_| { - Some(proto::result::Outcome::Custom(proto::CustomInvokeResult { - output: br#"{"ok":true}"#.to_vec(), - error_message: String::new(), - })) - }) - .await; - - // `spec.handlerName`, which is what a deployment addresses this handler by - // and what the invoke API documents, rather than the blueprint resource it - // is declared as. - let response = tokio::time::timeout( - Duration::from_secs(10), - http_client().request( - Request::builder() - .method("POST") - .uri(format!("http://{addr}/runtime/handlers/invoke")) - .header("Host", "localhost") - .header("content-type", "application/json") - .body(Body::from( - r#"{"handlerName":"Orders-GetOrderHandler-v1","invocationType":"requestResponse"}"#, - )) - .unwrap(), - ), - ) - .await - .expect("the invocation should be served rather than time out") - .unwrap(); - - let status = response.status(); - let body = response.into_body().collect().await.unwrap().to_bytes(); - assert_eq!(status, 200, "body: {}", String::from_utf8_lossy(&body)); - - // It reaches the same handler the resource name would. - let dispatch = handler - .next_dispatch() - .await - .expect("the handler should have been given the invocation"); - assert_eq!(dispatch.handler_tag, "GET::/orders/{orderId}"); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn returns_binary_output_from_an_invoked_handler_without_corrupting_it() { - let raw: Vec = vec![0xff, 0xfe, 0x00, 0x80, 0x01]; - let (_app, addr, socket) = start_runtime( - "ipc-invoke-binary", - "tests/data/fixtures/ipc-http-api.blueprint.yaml", - ) - .await; - let _handler = HandlerStub::attach(&socket, |_| { - Some(proto::result::Outcome::Custom(proto::CustomInvokeResult { - output: vec![0xff, 0xfe, 0x00, 0x80, 0x01], - error_message: String::new(), - })) - }) - .await; - - let response = tokio::time::timeout( - Duration::from_secs(10), - http_client().request( - Request::builder() - .method("POST") - .uri(format!("http://{addr}/runtime/handlers/invoke")) - .header("Host", "localhost") - .header("content-type", "application/json") - .body(Body::from( - r#"{"handlerName":"reindexHandler","invocationType":"requestResponse"}"#, - )) - .unwrap(), - ), - ) - .await - .expect("the invocation should be served rather than time out") - .unwrap(); - - let status = response.status(); - let body = response.into_body().collect().await.unwrap().to_bytes(); - assert_eq!(status, 200, "body: {}", String::from_utf8_lossy(&body)); - - // A handler returning an image or a protobuf can still be exercised here. - // The bytes come back encoded, and the response says they are encoded, so - // they cannot be mistaken for text. - let body: serde_json::Value = serde_json::from_slice(&body).unwrap(); - assert_eq!(body["dataEncoding"], "base64"); - let decoded = BASE64 - .decode(body["data"].as_str().expect("output should be carried")) - .expect("the output should be base64"); - assert_eq!(decoded, raw); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn serves_a_binary_response_body_from_an_http_handler() { - let raw: Vec = vec![0x89, 0x50, 0x4e, 0x47, 0x00, 0xff]; - let (_app, addr, socket) = start_runtime( - "ipc-http-binary-response", - "tests/data/fixtures/ipc-http-api.blueprint.yaml", - ) - .await; - let _handler = HandlerStub::attach(&socket, |_| { - Some(proto::result::Outcome::Http(proto::HttpResponse { - status: 200, - headers: HashMap::from([( - "content-type".to_string(), - proto::Values { - values: vec!["image/png".to_string()], - }, - )]), - body: vec![0x89, 0x50, 0x4e, 0x47, 0x00, 0xff], - })) - }) - .await; - - let response = tokio::time::timeout( - Duration::from_secs(10), - http_client().request( - Request::builder() - .uri(format!("http://{addr}/orders/order-1")) - .header("Host", "localhost") - .body(Body::empty()) - .unwrap(), - ), - ) - .await - .expect("the request should be served rather than time out") - .unwrap(); - - assert_eq!(response.status(), 200); - let body = response.into_body().collect().await.unwrap().to_bytes(); - - // The production response path carries bytes end to end, so a handler can - // answer with an image or a protobuf and nothing touches it. - assert_eq!(&body[..], &raw[..]); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn lets_an_async_invocation_run_on_after_the_caller_has_been_answered() { - let (_app, addr, socket) = start_runtime( - "ipc-invoke-async", - "tests/data/fixtures/ipc-http-api.blueprint.yaml", - ) - .await; - // Withhold the result, so the handler is still working when the caller has - // already been answered and its receiver dropped. - let mut handler = HandlerStub::attach(&socket, |_| None).await; - - let response = tokio::time::timeout( - Duration::from_secs(10), - http_client().request( - Request::builder() - .method("POST") - .uri(format!("http://{addr}/runtime/handlers/invoke")) - .header("Host", "localhost") - .header("content-type", "application/json") - .body(Body::from( - r#"{"handlerName":"reindexHandler","invocationType":"async"}"#, - )) - .unwrap(), - ), - ) - .await - .expect("an async invocation should be answered without waiting for the handler") - .unwrap(); - assert_eq!(response.status(), 200); - - let dispatch = handler - .next_dispatch() - .await - .expect("the handler should have been given the invocation"); - assert_eq!(dispatch.handler_tag, "custom::reindexHandler"); - - // The distinguishing property of an async invocation. Nobody is waiting for - // the result, but the work was asked for and should run to completion, so - // dropping the caller's receiver must not read as the caller going away. - let cancel = tokio::time::timeout(Duration::from_millis(500), handler.cancels.recv()).await; - assert!( - cancel.is_err(), - "an async invocation should not be cancelled once its caller has been answered" - ); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn refuses_to_invoke_a_handler_the_blueprint_does_not_declare() { - let (_app, addr, socket) = start_runtime( - "ipc-invoke-unknown", - "tests/data/fixtures/ipc-http-api.blueprint.yaml", - ) - .await; - let _handler = HandlerStub::attach(&socket, |_| None).await; - - let started = tokio::time::Instant::now(); - let response = tokio::time::timeout( - Duration::from_secs(10), - http_client().request( - Request::builder() - .method("POST") - .uri(format!("http://{addr}/runtime/handlers/invoke")) - .header("Host", "localhost") - .header("content-type", "application/json") - .body(Body::from( - r#"{"handlerName":"noSuchHandler","invocationType":"requestResponse"}"#, - )) - .unwrap(), - ), - ) - .await - .expect("the invocation should be refused rather than time out") - .unwrap(); - - assert_eq!(response.status(), 404); - // Refused on the name, so the caller is not left waiting for a stream to - // claim a tag that is not in the blueprint at all. - assert!(started.elapsed() < Duration::from_secs(2)); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn sheds_with_503_when_no_handler_stream_is_attached() { - let (_app, addr, socket) = start_runtime( - "ipc-no-handler", - "tests/data/fixtures/ipc-http-api.blueprint.yaml", - ) - .await; - // No handler stub connects, which is what an application whose handlers - // executable has not started, or has crashed, looks like. - - let started = tokio::time::Instant::now(); - let response = tokio::time::timeout( - Duration::from_secs(20), - http_client().request( - Request::builder() - .uri(format!("http://{addr}/orders/order-1")) - .header("Host", "localhost") - .body(Body::empty()) - .unwrap(), - ), - ) - .await - .expect("the runtime should answer rather than hang") - .unwrap(); - - // A capacity signal rather than a fault, and retryable. - assert_eq!(response.status(), 503); - assert!(response.headers().get("retry-after").is_some()); - // This route's handler timeout is the sixty second default, so answering - // in seconds is what distinguishes shedding from waiting it out. - assert!( - started.elapsed() < Duration::from_secs(15), - "the request should have been shed after the grace window, took {:?}", - started.elapsed() - ); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn tells_the_handler_to_stop_work_whose_deadline_has_passed() { - let (_app, addr, socket) = start_runtime( - "ipc-cancel-deadline", - "tests/data/fixtures/ipc-http-api.blueprint.yaml", - ) - .await; - // Withhold every result, so the event outlives its deadline. - let mut handler = HandlerStub::attach(&socket, |_| None).await; - - // The POST route's timeout is deliberately short in this fixture. - let response = tokio::time::timeout( - Duration::from_secs(20), - http_client().request( - Request::builder() - .method("POST") - .uri(format!("http://{addr}/orders")) - .header("Host", "localhost") - .body(Body::from(r#"{"sku":"abc"}"#)) - .unwrap(), - ), - ) - .await - .expect("the runtime should time the request out rather than hang") - .unwrap(); - assert_eq!(response.status(), 504); - - let dispatch = handler - .next_dispatch() - .await - .expect("the handler should have been given the request"); - let cancel = handler - .next_cancel() - .await - .expect("the handler should be told to stop work nobody is waiting for"); - assert_eq!(cancel.id, dispatch.id); - assert_eq!(cancel.reason(), proto::cancel::Reason::DeadlineExceeded); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn carries_a_request_body_that_is_not_text_without_corrupting_it() { - let (_app, addr, socket) = start_runtime( - "ipc-binary-body", - "tests/data/fixtures/ipc-http-api.blueprint.yaml", - ) - .await; - let mut handler = HandlerStub::attach(&socket, |_| Some(json_response("{}"))).await; - - // Bytes a lossy UTF-8 conversion would replace with the replacement - // character, destroying them. - let payload: Vec = vec![0xff, 0xfe, 0x00, 0x80, 0x01, 0x02]; - let response = tokio::time::timeout( - Duration::from_secs(10), - http_client().request( - Request::builder() - .method("POST") - .uri(format!("http://{addr}/orders")) - .header("Host", "localhost") - .header("content-type", "application/octet-stream") - .body(Body::from(payload.clone())) - .unwrap(), - ), - ) - .await - .expect("the request should be served") - .unwrap(); - assert_eq!(response.status(), 200); - - let dispatch = handler - .next_dispatch() - .await - .expect("the handler should have been given the request"); - let Some(proto::dispatch::Source::Http(request)) = dispatch.source else { - panic!("expected an HTTP source"); - }; - // The handler receives the bytes exactly, not a base64 string and not a - // string of replacement characters. - assert_eq!(request.body, payload); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn routes_a_websocket_message_to_a_handler_over_the_stream() { - let (_app, addr, socket) = start_runtime( - "ipc-ws", - "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", - ) - .await; - let mut handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; - - let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) - .await - .expect("the WebSocket server should accept the connection"); - socket_conn - .send(tokio_tungstenite::tungstenite::Message::Text( - json!({ "event": "sendMessage", "data": { "text": "hello" } }).to_string(), - )) - .await - .unwrap(); - - let dispatch = handler - .next_dispatch() - .await - .expect("the message should reach the handler"); - assert_eq!(dispatch.handler_tag, "event::sendMessage"); - - let Some(proto::dispatch::Source::Websocket(message)) = dispatch.source else { - panic!("expected a WebSocket source"); - }; - assert_eq!(message.route, "sendMessage"); - assert!(!message.is_binary); - assert!(!message.connection_id.is_empty()); - let body: serde_json::Value = serde_json::from_slice(&message.message).unwrap(); - assert_eq!(body["data"]["text"], "hello"); - - let _ = socket_conn.close(None).await; - let _ = tokio::fs::remove_file(&socket).await; -} - -/// A handler sending binary is held to the format its client can read, and is -/// told which of its messages was refused. -/// -/// Failures are per message rather than per batch, so a batch carrying one bad -/// message still delivers the rest and names the one that did not go. -#[test_log::test(tokio::test)] -async fn refuses_a_binary_websocket_message_that_is_not_framed() { - let (_app, addr, socket) = start_runtime( - "ipc-ws-binary-framing", - "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", - ) - .await; - let mut handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; - - let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) - .await - .expect("the WebSocket server should accept the connection"); - socket_conn - .send(tokio_tungstenite::tungstenite::Message::Text( - json!({ "event": "sendMessage", "data": { "text": "hello" } }).to_string(), - )) - .await - .unwrap(); - - let dispatch = handler - .next_dispatch() - .await - .expect("the message should reach the handler"); - let Some(proto::dispatch::Source::Websocket(message)) = dispatch.source else { - panic!("expected a WebSocket source"); - }; - let connection_id = message.connection_id; - - // [routeLength][route][requireAck][messageIdLength][messageId][payload] - let mut framed = vec![7u8]; - framed.extend_from_slice(b"updates"); - framed.push(0x0); - framed.push(0x0); - framed.extend_from_slice(&[0xde, 0xad, 0xbe, 0xef]); - - handler - .send_ws( - "batch-1", - vec![ - // Raw bytes, which is what an application reaches for when it - // does not know the format is required. A protobuf payload - // here, field one as a length delimited string. - proto::WsOutbound { - connection_id: connection_id.clone(), - message: vec![0x0a, 0x05, b'p', b'r', b'i', b'c', b'e'], - is_binary: true, - ..Default::default() - }, - proto::WsOutbound { - connection_id: connection_id.clone(), - message: framed.clone(), - is_binary: true, - ..Default::default() - }, - ], - ) - .await; - - let ack = handler - .next_ws_ack() - .await - .expect("the runtime should report what became of the batch"); - assert_eq!(ack.correlation_id, "batch-1"); - assert!( - !ack.success, - "a batch with a refused message is not a success" - ); - assert_eq!( - ack.failures.len(), - 1, - "only the unframed message should fail" - ); - assert_eq!( - ack.failures[0].index, 0, - "the failure should name the message that was refused" - ); - assert_eq!(ack.failures[0].connection_id, connection_id); - - // The framed one still arrives, exactly as the handler composed it, and it - // is the only binary frame after the capabilities signal. - let delivered = tokio::time::timeout(Duration::from_secs(5), async { - while let Some(Ok(message)) = socket_conn.next().await { - if let tokio_tungstenite::tungstenite::Message::Binary(bytes) = message { - if bytes[..] == CELERITY_WS_CAPABILITIES_SIGNAL[..] { - continue; - } - return Some(bytes.to_vec()); - } - } - None - }) - .await - .expect("the framed message should reach the client"); - - assert_eq!(delivered, Some(framed)); - - let _ = socket_conn.close(None).await; - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn answers_heartbeats_while_a_handler_is_still_running() { - let (_app, addr, socket) = start_runtime( - "ipc-ws-heartbeat", - "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", - ) - .await; - // Withhold every result, standing in for a handler that takes a while. - let mut handler = HandlerStub::attach(&socket, |_| None).await; - - let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) - .await - .expect("the WebSocket server should accept the connection"); - socket_conn - .send(tokio_tungstenite::tungstenite::Message::Text( - json!({ "event": "sendMessage", "data": { "text": "hello" } }).to_string(), - )) - .await - .unwrap(); - - // Once this arrives the handler is running and has not answered. - handler - .next_dispatch() - .await - .expect("the message should reach the handler"); - - socket_conn - .send(tokio_tungstenite::tungstenite::Message::Text( - json!({ "ping": true }).to_string(), - )) - .await - .unwrap(); - - // A client whose heartbeat goes unanswered concludes the connection is - // dead and reconnects, tearing down work that is still in progress. The - // handler's own timeout is far longer than any heartbeat interval, so the - // pong cannot wait on it. - let pong = tokio::time::timeout(Duration::from_secs(3), async { - while let Some(Ok(message)) = socket_conn.next().await { - if let tokio_tungstenite::tungstenite::Message::Text(text) = message { - if let Ok(value) = serde_json::from_str::(&text) { - if value.get("pong") == Some(&serde_json::Value::Bool(true)) { - return true; - } - } - } - } - false - }) - .await; - - assert_eq!( - pong, - Ok(true), - "the heartbeat should be answered while the handler is still running" - ); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn sheds_a_connection_that_outruns_its_handlers_with_a_retry_hint() { - let (_app, addr, socket) = start_runtime( - "ipc-ws-saturate", - "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", - ) - .await; - // Answer nothing, so the worker is stuck on the first message and every - // message after it piles up behind it. - let _handler = HandlerStub::attach(&socket, |_| None).await; - - let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) - .await - .expect("the WebSocket server should accept the connection"); - - // Comfortably more than the buffer holds, so the read loop reaches the - // point where it would otherwise wait in silence. - for index in 0..256 { - if socket_conn - .send(tokio_tungstenite::tungstenite::Message::Text( - json!({ "event": "sendMessage", "data": { "index": index } }).to_string(), - )) - .await - .is_err() - { - // The runtime shed the connection part way through, which is the - // behaviour under test. - break; - } - } - - // Stalling in silence is what makes a client give up on a connection it - // could have kept. Closing says the same thing in a way the client can act - // on, and the hint stops it returning straight into the same saturation. - let close = tokio::time::timeout(Duration::from_secs(10), async { - while let Some(Ok(message)) = socket_conn.next().await { - if let tokio_tungstenite::tungstenite::Message::Close(frame) = message { - return frame; - } - } - None - }) - .await - .expect("the runtime should close the connection rather than stall in silence"); - - let frame = close.expect("the close should carry a frame rather than be bare"); - assert_eq!( - u16::from(frame.code), - 1013, - "expected a try again later code" - ); - let reason: serde_json::Value = - serde_json::from_str(&frame.reason).expect("the reason should carry the retry hint"); - assert!( - reason["retryAfter"].as_u64().is_some_and(|ms| ms > 0), - "expected a retryAfter hint, got {reason}" - ); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn reads_faster_than_one_message_every_ten_milliseconds() { - let (_app, addr, socket) = start_runtime( - "ipc-ws-throughput", - "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", - ) - .await; - let mut handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; - - let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) - .await - .expect("the WebSocket server should accept the connection"); - - // The read loop used to sleep 10ms per message to yield a lock it held - // across every read, capping one connection at about 100 messages a - // second whatever the handlers did. Reading no longer takes that lock, so - // 50 messages must not take the 500ms that cap would have cost. - let started = tokio::time::Instant::now(); - for index in 0..50 { - socket_conn - .send(tokio_tungstenite::tungstenite::Message::Text( - json!({ "event": "sendMessage", "data": { "index": index } }).to_string(), - )) - .await - .unwrap(); - } - for _ in 0..50 { - handler - .next_dispatch() - .await - .expect("every message should reach the handler"); - } - let elapsed = started.elapsed(); - - // Below the 500ms the old cap would have cost, with enough room above the - // handful of milliseconds this actually takes that a loaded machine does - // not fail it for being busy. - assert!( - elapsed < Duration::from_millis(450), - "50 messages took {elapsed:?}, which suggests the read loop is still rate limited" - ); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn tells_an_unauthenticated_client_to_authenticate_first() { - let (_app, addr, socket) = start_runtime( - "ipc-ws-auth-message", - "tests/data/fixtures/ipc-websocket-auth-message.blueprint.yaml", - ) - .await; - let _handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; - - let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) - .await - .expect("the authMessage strategy should let the connection upgrade"); - - // Anything that is not an authenticate message, sent before the connection - // has authenticated. The runtime answers on the same socket it just read - // from, so this is the path where taking the lock twice would wedge the - // connection rather than refuse the message. - socket_conn - .send(tokio_tungstenite::tungstenite::Message::Text( - json!({ "event": "sendMessage", "data": { "text": "hello" } }).to_string(), - )) - .await - .unwrap(); - - let rejected = tokio::time::timeout(Duration::from_secs(5), async { - while let Some(Ok(message)) = socket_conn.next().await { - if let tokio_tungstenite::tungstenite::Message::Text(text) = message { - if let Ok(value) = serde_json::from_str::(&text) { - if value["event"] == "error" { - return true; - } - } - } - } - false - }) - .await; - - assert_eq!( - rejected, - Ok(true), - "an unauthenticated message should be refused rather than leave the connection hanging" - ); - - let _ = tokio::fs::remove_file(&socket).await; -} - -/// An acknowledgement is a message like any other until the client has proved -/// who it is. -/// -/// Acknowledgements are taken out of the way before routing, and that used to -/// happen ahead of the authentication gate, so an unauthenticated client could -/// still settle a message and call off the resend and loss event that follow -/// it. The refusal below is the gate doing its job. -#[test_log::test(tokio::test)] -async fn refuses_an_acknowledgement_from_a_client_that_has_not_authenticated() { - let (_app, addr, socket) = start_runtime( - "ipc-ws-auth-message-ack", - "tests/data/fixtures/ipc-websocket-auth-message.blueprint.yaml", - ) - .await; - let _handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; - - let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) - .await - .expect("the authMessage strategy should let the connection upgrade"); - - socket_conn - .send(tokio_tungstenite::tungstenite::Message::Text( - json!({ - "event": "ack", - "data": { "messageId": "m-1", "timestamp": "2026-01-01T00:00:00.000Z" } - }) - .to_string(), - )) - .await - .unwrap(); - - let rejected = tokio::time::timeout(Duration::from_secs(5), async { - while let Some(Ok(message)) = socket_conn.next().await { - if let tokio_tungstenite::tungstenite::Message::Text(text) = message { - if let Ok(value) = serde_json::from_str::(&text) { - if value["event"] == "error" { - return true; - } - } - } - } - false - }) - .await; - - assert_eq!( - rejected, - Ok(true), - "an acknowledgement before authentication should be refused rather than settle a message" - ); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn closes_a_connection_out_without_waiting_for_all_the_work_it_queued() { - let (_app, addr, socket) = start_runtime( - "ipc-ws-drain", - "tests/data/fixtures/ipc-websocket-disconnect.blueprint.yaml", - ) - .await; - // Answer nothing, so every queued message runs its timeout out in turn. - let mut handler = HandlerStub::attach(&socket, |_| None).await; - - let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) - .await - .expect("the WebSocket server should accept the connection"); - - // Eight messages against a one second handler timeout, so draining every - // one of them would take longer than the connection is given to close. - for index in 0..8 { - socket_conn - .send(tokio_tungstenite::tungstenite::Message::Text( - json!({ "event": "sendMessage", "data": { "index": index } }).to_string(), - )) - .await - .unwrap(); - } - handler - .next_dispatch() - .await - .expect("the first message should reach the handler"); - - let closed_at = tokio::time::Instant::now(); - socket_conn - .close(None) - .await - .expect("the client should be able to close"); - - // The disconnect handler running is what says teardown finished, and until - // it does the connection is still in the registry and still counted. It - // must not wait for messages the client is no longer there to hear about. - // - // That it runs exactly once is covered separately, by a client that sends - // a close frame outright. Closing the way this one does drives the whole - // handshake and never surfaces a frame to the application, so the second - // path that could fire a disconnect is not reached from here. - let disconnected = tokio::time::timeout(Duration::from_secs(7), async { - while let Some(dispatch) = handler.next_dispatch().await { - if let Some(proto::dispatch::Source::Websocket(message)) = dispatch.source { - if message.route == "$disconnect" { - return true; - } - } - } - false - }) - .await; - - assert_eq!( - disconnected, - Ok(true), - "the disconnect handler should run once the drain window passes" - ); - assert!( - closed_at.elapsed() < Duration::from_secs(7), - "teardown took {:?}, which suggests it waited for the whole queue", - closed_at.elapsed() - ); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn does_not_reorder_messages_when_handling_them_off_the_read_loop() { - let (_app, addr, socket) = start_runtime( - "ipc-ws-order", - "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", - ) - .await; - let mut handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; - - let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) - .await - .expect("the WebSocket server should accept the connection"); - - for index in 0..5 { - socket_conn - .send(tokio_tungstenite::tungstenite::Message::Text( - json!({ "event": "sendMessage", "data": { "index": index } }).to_string(), - )) - .await - .unwrap(); - } - - // Moving handling off the read loop must not reorder what was already in - // order, which is what pins the worker to one message at a time. - // - // This is the behaviour of this runtime rather than a promise the platform - // makes. The same API deployed to a serverless target invokes a function - // per message with no ordering between them, and a client resending after - // a lost acknowledgement reorders its own messages anyway. An application - // that needs ordering has to carry its own sequence. So this test exists to - // keep the runtime's behaviour deliberate, not to stop it ever changing. - let mut seen = Vec::new(); - for _ in 0..5 { - let dispatch = handler - .next_dispatch() - .await - .expect("every message should reach the handler"); - let Some(proto::dispatch::Source::Websocket(message)) = dispatch.source else { - panic!("expected a WebSocket source"); - }; - let body: serde_json::Value = serde_json::from_slice(&message.message).unwrap(); - seen.push(body["data"]["index"].as_i64().unwrap()); - } - assert_eq!(seen, vec![0, 1, 2, 3, 4]); - - let _ = socket_conn.close(None).await; - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn acknowledges_a_binary_message_that_asks_to_be_acknowledged() { - let (_app, addr, socket) = start_runtime( - "ipc-ws-ack-binary", - "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", - ) - .await; - let mut handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; - - let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) - .await - .expect("the WebSocket server should accept the connection"); - - // [routeLength][route][requireAck][messageIdLength][messageId][message], - // built the way the spec defines. - // The message id is what the acknowledgement has to name. - let route = b"sendMessage"; - let message_id = b"msg-1"; - let body: Vec = vec![0xde, 0xad, 0xbe, 0xef]; - let mut payload = vec![route.len() as u8]; - payload.extend_from_slice(route); - payload.push(0x1); - payload.push(message_id.len() as u8); - payload.extend_from_slice(message_id); - payload.extend_from_slice(&body); - - socket_conn - .send(tokio_tungstenite::tungstenite::Message::Binary(payload)) - .await - .unwrap(); - - let ack = tokio::time::timeout(Duration::from_secs(5), async { - while let Some(Ok(message)) = socket_conn.next().await { - if let tokio_tungstenite::tungstenite::Message::Binary(bytes) = message { - if bytes.starts_with(&[0x1, 0x4, 0x0, 0x0]) { - return Some(bytes[4..].to_vec()); - } - } - } - None - }) - .await - .expect("the acknowledgement should not take five seconds"); - - let ack = ack.expect("a message that asks to be acknowledged should be acknowledged"); - let ack: serde_json::Value = serde_json::from_slice(&ack).unwrap(); - assert_eq!(ack["messageId"], "msg-1"); - assert!( - ack["timestamp"].is_string(), - "the acknowledgement should carry a timestamp, got {ack}" - ); - - let dispatch = handler - .next_dispatch() - .await - .expect("the message should still reach the handler"); - let Some(proto::dispatch::Source::Websocket(message)) = dispatch.source else { - panic!("expected a WebSocket source"); - }; - assert_eq!(&message.message[..], &body[..]); - // The handler is told the id the client used, so instrumentation on its - // side can be tied to what the client saw acknowledged. - assert_eq!(message.message_id, "msg-1"); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn acknowledges_a_json_message_that_asks_to_be_acknowledged() { - let (_app, addr, socket) = start_runtime( - "ipc-ws-ack-json", - "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", - ) - .await; - let _handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; - - let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) - .await - .expect("the WebSocket server should accept the connection"); - - socket_conn - .send(tokio_tungstenite::tungstenite::Message::Text( - json!({ - "event": "sendMessage", - "messageId": "msg-json-1", - "ack": true, - "data": { "text": "hello" }, - }) - .to_string(), - )) - .await - .unwrap(); - - // Answered in the encoding it arrived in, so a client that only has text - // because its environment gave it no choice can still read the reply. - let ack = tokio::time::timeout(Duration::from_secs(5), async { - while let Some(Ok(message)) = socket_conn.next().await { - if let tokio_tungstenite::tungstenite::Message::Text(text) = message { - if let Ok(value) = serde_json::from_str::(&text) { - if value["event"] == "ack" { - return Some(value); - } - } - } - } - None - }) - .await - .expect("the acknowledgement should not take five seconds"); - - let ack = ack.expect("a message that asks to be acknowledged should be acknowledged"); - assert_eq!(ack["data"]["messageId"], "msg-json-1"); - assert!(ack["data"]["timestamp"].is_string()); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn acknowledges_a_message_queued_behind_a_handler_that_is_still_running() { - let (_app, addr, socket) = start_runtime( - "ipc-ws-ack-queued", - "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", - ) - .await; - // Answers nothing, so the first message is still being handled, for the - // whole of the ten second timeout this fixture sets, while the second - // arrives. - let _handler = HandlerStub::attach(&socket, |_| None).await; - - let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) - .await - .expect("the WebSocket server should accept the connection"); - - socket_conn - .send(tokio_tungstenite::tungstenite::Message::Text( - json!({ "event": "sendMessage", "data": { "first": true } }).to_string(), - )) - .await - .unwrap(); - socket_conn - .send(tokio_tungstenite::tungstenite::Message::Text( - json!({ - "event": "sendMessage", - "messageId": "queued-1", - "ack": true, - "data": { "second": true }, - }) - .to_string(), - )) - .await - .unwrap(); - - // Acknowledgement follows the message being taken in, not what happens to - // it after. Tying it to handling would leave this one waiting on the - // handler ahead of it, and a client that hears nothing sends it again for a - // message the runtime was holding the entire time. - let ack = tokio::time::timeout(Duration::from_secs(3), async { - while let Some(Ok(message)) = socket_conn.next().await { - if let tokio_tungstenite::tungstenite::Message::Text(text) = message { - if let Ok(value) = serde_json::from_str::(&text) { - if value["event"] == "ack" { - return Some(value); - } - } - } - } - None - }) - .await - .expect("the acknowledgement should not wait for the handler ahead of it"); - - let ack = ack.expect("the queued message should be acknowledged"); - assert_eq!(ack["data"]["messageId"], "queued-1"); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn does_not_route_a_client_acknowledgement_to_a_handler() { - let (_app, addr, socket) = start_runtime( - "ipc-ws-inbound-ack", - "tests/data/fixtures/ipc-websocket-default-route.blueprint.yaml", - ) - .await; - let mut handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; - - let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) - .await - .expect("the WebSocket server should accept the connection"); - - // What a client sends to acknowledge something the runtime sent it. A - // reserved message names its route with `event`, and this API routes on - // `action`, so left alone it carries no route the runtime knows and lands - // on the default handler as though the application had been sent it. - socket_conn - .send(tokio_tungstenite::tungstenite::Message::Text( - json!({ - "event": "ack", - "data": { "messageId": "m-1", "timestamp": "1" }, - }) - .to_string(), - )) - .await - .unwrap(); - let mut binary = vec![0x1, 0x4, 0x0, 0x0]; - binary.extend_from_slice(br#"{"messageId":"m-2","timestamp":"1"}"#); - socket_conn - .send(tokio_tungstenite::tungstenite::Message::Binary(binary)) - .await - .unwrap(); - - // Sent last and expected first, since anything the runtime failed to take - // out of the way would have reached the handler ahead of it. - socket_conn - .send(tokio_tungstenite::tungstenite::Message::Text( - json!({ "action": "sendMessage", "data": { "text": "hello" } }).to_string(), - )) - .await - .unwrap(); - - let dispatch = handler - .next_dispatch() - .await - .expect("the ordinary message should reach the handler"); - let Some(proto::dispatch::Source::Websocket(message)) = dispatch.source else { - panic!("expected a WebSocket source"); - }; - let body: serde_json::Value = serde_json::from_slice(&message.message).unwrap(); - assert_eq!( - body["data"]["text"], "hello", - "an acknowledgement reached the handler ahead of the message that followed it" - ); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn does_not_acknowledge_a_message_that_did_not_ask() { - let (_app, addr, socket) = start_runtime( - "ipc-ws-ack-absent", - "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", - ) - .await; - let _handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; - - let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) - .await - .expect("the WebSocket server should accept the connection"); - - // Carries an id but does not opt in, and opting in without an id has - // nothing to name. Neither should be answered. - for message in [ - json!({ "event": "sendMessage", "messageId": "msg-2", "data": {} }), - json!({ "event": "sendMessage", "ack": true, "data": {} }), - ] { - socket_conn - .send(tokio_tungstenite::tungstenite::Message::Text( - message.to_string(), - )) - .await - .unwrap(); - } - - let unexpected = tokio::time::timeout(Duration::from_secs(2), async { - while let Some(Ok(message)) = socket_conn.next().await { - if let tokio_tungstenite::tungstenite::Message::Text(text) = message { - if let Ok(value) = serde_json::from_str::(&text) { - if value["event"] == "ack" { - return true; - } - } - } - } - false - }) - .await; - - assert_ne!( - unexpected, - Ok(true), - "acknowledgement is opt in, and an opt in without a message id is ignored" - ); - - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn carries_a_binary_websocket_frame_without_corrupting_it() { - let (_app, addr, socket) = start_runtime( - "ipc-ws-binary", - "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", - ) - .await; - let mut handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; - - let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) - .await - .expect("the WebSocket server should accept the connection"); - - // [routeLength][route][requireAck][messageIdLength][messageId][message], - // here with no acknowledgement asked for and no message id. - let route = b"sendMessage"; - let body: Vec = vec![0xff, 0xfe, 0x00, 0x80, 0x01, 0x02]; - let mut payload = vec![route.len() as u8]; - payload.extend_from_slice(route); - payload.push(0); - payload.push(0); - payload.extend_from_slice(&body); - - socket_conn - .send(tokio_tungstenite::tungstenite::Message::Binary(payload)) - .await - .unwrap(); - - let dispatch = handler - .next_dispatch() - .await - .expect("the message should reach the handler"); - let Some(proto::dispatch::Source::Websocket(message)) = dispatch.source else { - panic!("expected a WebSocket source"); - }; - - assert!(message.is_binary); - // The bytes arrive exactly, with no base64 and no replacement characters. - assert_eq!(message.message, body); - // Guard against the encoding leaking onto the wire. - assert_ne!(message.message, BASE64.encode(&body).into_bytes()); - - let _ = socket_conn.close(None).await; - let _ = tokio::fs::remove_file(&socket).await; -} - -#[test_log::test(tokio::test)] -async fn fires_the_disconnect_handler_once_for_a_client_that_sends_a_close_frame() { - let (_app, addr, socket) = start_runtime( - "ipc-ws-close-once", - "tests/data/fixtures/ipc-websocket-disconnect.blueprint.yaml", - ) - .await; - let mut handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; - - let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) - .await - .expect("the WebSocket server should accept the connection"); - - // Sent as an ordinary frame rather than through close(), which drives the - // whole handshake and never surfaces the frame to the application. Sent - // this way it reaches message processing, which is the path that could - // once fire a disconnect of its own alongside the one teardown fires. - socket_conn - .send(tokio_tungstenite::tungstenite::Message::Close(None)) - .await - .unwrap(); - - let mut disconnects = 0; - let _ = tokio::time::timeout(Duration::from_secs(4), async { - while let Some(dispatch) = handler.next_dispatch().await { - if let Some(proto::dispatch::Source::Websocket(message)) = dispatch.source { - if message.route == "$disconnect" { - disconnects += 1; - } - } - } - }) - .await; - - assert_eq!( - disconnects, 1, - "the disconnect handler should run once, not once per path that can end a connection" - ); - - let _ = tokio::fs::remove_file(&socket).await; -} diff --git a/libs/runtime/core/tests/ipc_http_test.rs b/libs/runtime/core/tests/ipc_http_test.rs new file mode 100644 index 00000000..46454c63 --- /dev/null +++ b/libs/runtime/core/tests/ipc_http_test.rs @@ -0,0 +1,331 @@ +//! The IPC path for an HTTP API: a request reaches a handler over a real gRPC +//! stream on a real Unix socket and its response comes back, including what +//! happens when the handler is slow, absent, or answering with bytes. + +mod common; + +use std::{collections::HashMap, time::Duration}; + +use axum::{body::Body, http::Request}; +use celerity_runtime_core::ipc_proto::{self as proto}; +use common::ipc::{http_client, json_response, start_runtime, HandlerStub}; +use http_body_util::BodyExt; + +#[test_log::test(tokio::test)] +async fn serves_an_http_request_through_a_handler_over_the_stream() { + let (_app, addr, socket) = start_runtime( + "ipc-http", + "tests/data/fixtures/ipc-http-api.blueprint.yaml", + ) + .await; + let mut handler = + HandlerStub::attach(&socket, |_| Some(json_response(r#"{"id":"order-1"}"#))).await; + + let response = tokio::time::timeout( + Duration::from_secs(10), + http_client().request( + Request::builder() + .uri(format!( + "http://{addr}/orders/order-1?expand=items&expand=totals" + )) + .header("Host", "localhost") + .header("X-Trace", "abc") + .body(Body::empty()) + .unwrap(), + ), + ) + .await + .expect("the request should be served rather than time out") + .unwrap(); + + let status = response.status(); + let content_type = response + .headers() + .get("content-type") + .map(|value| value.to_str().unwrap().to_string()); + let body = response.into_body().collect().await.unwrap().to_bytes(); + + assert_eq!(status, 200); + assert_eq!(content_type, Some("application/json".to_string())); + assert_eq!(&body[..], br#"{"id":"order-1"}"#); + + // The frame the handler received should describe the request faithfully. + let dispatch = handler + .next_dispatch() + .await + .expect("the handler should have been given the request"); + assert_eq!(dispatch.handler_tag, "GET::/orders/{orderId}"); + assert!(dispatch.deadline_unix_ms > 0); + + let Some(proto::dispatch::Source::Http(request)) = dispatch.source else { + panic!("expected an HTTP source"); + }; + assert_eq!(request.method, "GET"); + assert_eq!(request.path, "/orders/order-1"); + assert_eq!(request.route, "/orders/{orderId}"); + assert_eq!( + request.path_params.get("orderId").map(|v| v.values.clone()), + Some(vec!["order-1".to_string()]) + ); + // Repeated query parameters survive as multiple values. + assert_eq!( + request.query_params.get("expand").map(|v| v.values.clone()), + Some(vec!["items".to_string(), "totals".to_string()]) + ); + // Header names reach the handler lowercased however they were sent. + assert_eq!( + request.headers.get("x-trace").map(|v| v.values.clone()), + Some(vec!["abc".to_string()]) + ); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn splits_a_catch_all_path_into_segments_without_losing_encoded_separators() { + let (_app, addr, socket) = start_runtime( + "ipc-catch-all", + "tests/data/fixtures/ipc-http-api.blueprint.yaml", + ) + .await; + let mut handler = HandlerStub::attach(&socket, |_| Some(json_response(r#"{"ok":true}"#))).await; + + // The middle segment carries an encoded separator, which must stay inside + // that segment rather than splitting it in two. + let response = tokio::time::timeout( + Duration::from_secs(10), + http_client().request( + Request::builder() + .uri(format!("http://{addr}/files/docs/a%2Fb/report%20final.pdf")) + .header("Host", "localhost") + .body(Body::empty()) + .unwrap(), + ), + ) + .await + .expect("the request should be served rather than time out") + .unwrap(); + assert_eq!(response.status(), 200); + + let dispatch = handler + .next_dispatch() + .await + .expect("the handler should have been given the request"); + let Some(proto::dispatch::Source::Http(request)) = dispatch.source else { + panic!("expected an HTTP source"); + }; + assert_eq!(request.route, "/files/{*filePath}"); + assert_eq!( + request + .path_params + .get("filePath") + .map(|v| v.values.clone()), + Some(vec![ + "docs".to_string(), + "a/b".to_string(), + "report final.pdf".to_string(), + ]) + ); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn carries_a_request_body_that_is_not_text_without_corrupting_it() { + let (_app, addr, socket) = start_runtime( + "ipc-binary-body", + "tests/data/fixtures/ipc-http-api.blueprint.yaml", + ) + .await; + let mut handler = HandlerStub::attach(&socket, |_| Some(json_response("{}"))).await; + + // Bytes a lossy UTF-8 conversion would replace with the replacement + // character, destroying them. + let payload: Vec = vec![0xff, 0xfe, 0x00, 0x80, 0x01, 0x02]; + let response = tokio::time::timeout( + Duration::from_secs(10), + http_client().request( + Request::builder() + .method("POST") + .uri(format!("http://{addr}/orders")) + .header("Host", "localhost") + .header("content-type", "application/octet-stream") + .body(Body::from(payload.clone())) + .unwrap(), + ), + ) + .await + .expect("the request should be served") + .unwrap(); + assert_eq!(response.status(), 200); + + let dispatch = handler + .next_dispatch() + .await + .expect("the handler should have been given the request"); + let Some(proto::dispatch::Source::Http(request)) = dispatch.source else { + panic!("expected an HTTP source"); + }; + // The handler receives the bytes exactly, not a base64 string and not a + // string of replacement characters. + assert_eq!(request.body, payload); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn serves_a_binary_response_body_from_an_http_handler() { + let raw: Vec = vec![0x89, 0x50, 0x4e, 0x47, 0x00, 0xff]; + let (_app, addr, socket) = start_runtime( + "ipc-http-binary-response", + "tests/data/fixtures/ipc-http-api.blueprint.yaml", + ) + .await; + let _handler = HandlerStub::attach(&socket, |_| { + Some(proto::result::Outcome::Http(proto::HttpResponse { + status: 200, + headers: HashMap::from([( + "content-type".to_string(), + proto::Values { + values: vec!["image/png".to_string()], + }, + )]), + body: vec![0x89, 0x50, 0x4e, 0x47, 0x00, 0xff], + })) + }) + .await; + + let response = tokio::time::timeout( + Duration::from_secs(10), + http_client().request( + Request::builder() + .uri(format!("http://{addr}/orders/order-1")) + .header("Host", "localhost") + .body(Body::empty()) + .unwrap(), + ), + ) + .await + .expect("the request should be served rather than time out") + .unwrap(); + + assert_eq!(response.status(), 200); + let body = response.into_body().collect().await.unwrap().to_bytes(); + + // The production response path carries bytes end to end, so a handler can + // answer with an image or a protobuf and nothing touches it. + assert_eq!(&body[..], &raw[..]); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn returns_504_when_the_handler_never_answers() { + let (_app, addr, socket) = start_runtime( + "ipc-timeout", + "tests/data/fixtures/ipc-http-api.blueprint.yaml", + ) + .await; + // Withhold every result, as a handler that has stopped responding would. + let _handler = HandlerStub::attach(&socket, |_| None).await; + + // The POST route's timeout is deliberately short in this fixture. + let response = tokio::time::timeout( + Duration::from_secs(20), + http_client().request( + Request::builder() + .method("POST") + .uri(format!("http://{addr}/orders")) + .header("Host", "localhost") + .body(Body::from(r#"{"sku":"abc"}"#)) + .unwrap(), + ), + ) + .await + .expect("the runtime should time the request out rather than hang") + .unwrap(); + + assert_eq!(response.status(), 504); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn sheds_with_503_when_no_handler_stream_is_attached() { + let (_app, addr, socket) = start_runtime( + "ipc-no-handler", + "tests/data/fixtures/ipc-http-api.blueprint.yaml", + ) + .await; + // No handler stub connects, which is what an application whose handlers + // executable has not started, or has crashed, looks like. + + let started = tokio::time::Instant::now(); + let response = tokio::time::timeout( + Duration::from_secs(20), + http_client().request( + Request::builder() + .uri(format!("http://{addr}/orders/order-1")) + .header("Host", "localhost") + .body(Body::empty()) + .unwrap(), + ), + ) + .await + .expect("the runtime should answer rather than hang") + .unwrap(); + + // A capacity signal rather than a fault, and retryable. + assert_eq!(response.status(), 503); + assert!(response.headers().get("retry-after").is_some()); + // This route's handler timeout is the sixty second default, so answering + // in seconds is what distinguishes shedding from waiting it out. + assert!( + started.elapsed() < Duration::from_secs(15), + "the request should have been shed after the grace window, took {:?}", + started.elapsed() + ); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn tells_the_handler_to_stop_work_whose_deadline_has_passed() { + let (_app, addr, socket) = start_runtime( + "ipc-cancel-deadline", + "tests/data/fixtures/ipc-http-api.blueprint.yaml", + ) + .await; + // Withhold every result, so the event outlives its deadline. + let mut handler = HandlerStub::attach(&socket, |_| None).await; + + // The POST route's timeout is deliberately short in this fixture. + let response = tokio::time::timeout( + Duration::from_secs(20), + http_client().request( + Request::builder() + .method("POST") + .uri(format!("http://{addr}/orders")) + .header("Host", "localhost") + .body(Body::from(r#"{"sku":"abc"}"#)) + .unwrap(), + ), + ) + .await + .expect("the runtime should time the request out rather than hang") + .unwrap(); + assert_eq!(response.status(), 504); + + let dispatch = handler + .next_dispatch() + .await + .expect("the handler should have been given the request"); + let cancel = handler + .next_cancel() + .await + .expect("the handler should be told to stop work nobody is waiting for"); + assert_eq!(cancel.id, dispatch.id); + assert_eq!(cancel.reason(), proto::cancel::Reason::DeadlineExceeded); + + let _ = tokio::fs::remove_file(&socket).await; +} diff --git a/libs/runtime/core/tests/ipc_invoke_test.rs b/libs/runtime/core/tests/ipc_invoke_test.rs new file mode 100644 index 00000000..6d99a5e3 --- /dev/null +++ b/libs/runtime/core/tests/ipc_invoke_test.rs @@ -0,0 +1,393 @@ +//! The IPC path for handlers invoked by name, rather than by a request or a +//! message arriving. Covers the invoke endpoint, what it will and will not +//! address, and what comes back. + +mod common; + +use std::time::Duration; + +use axum::{body::Body, http::Request}; +use base64::{engine::general_purpose::STANDARD as BASE64, Engine as _}; +use celerity_runtime_core::ipc_proto::{self as proto}; +use common::ipc::{http_client, start_runtime, start_runtime_with, HandlerStub}; +use http_body_util::BodyExt; + +#[test_log::test(tokio::test)] +async fn invokes_a_custom_handler_by_name_over_the_stream() { + let (_app, addr, socket) = start_runtime( + "ipc-invoke", + "tests/data/fixtures/ipc-http-api.blueprint.yaml", + ) + .await; + let mut handler = HandlerStub::attach(&socket, |_| { + Some(proto::result::Outcome::Custom(proto::CustomInvokeResult { + output: br#"{"reindexed":12}"#.to_vec(), + error_message: String::new(), + })) + }) + .await; + + let response = tokio::time::timeout( + Duration::from_secs(10), + http_client().request( + Request::builder() + .method("POST") + .uri(format!("http://{addr}/runtime/handlers/invoke")) + .header("Host", "localhost") + .header("content-type", "application/json") + .body(Body::from( + r#"{"handlerName":"reindexHandler","invocationType":"requestResponse","payload":{"full":true}}"#, + )) + .unwrap(), + ), + ) + .await + .expect("the invocation should be served rather than time out") + .unwrap(); + + let status = response.status(); + let body = response.into_body().collect().await.unwrap().to_bytes(); + assert_eq!(status, 200, "body: {}", String::from_utf8_lossy(&body)); + let body: serde_json::Value = serde_json::from_slice(&body).unwrap(); + assert_eq!(body["data"], r#"{"reindexed":12}"#); + + // The invocation reached the handlers executable as an ordinary event, on + // the custom tag, carrying the payload the caller sent. + let dispatch = handler + .next_dispatch() + .await + .expect("the handler should have been given the invocation"); + assert_eq!(dispatch.handler_tag, "custom::reindexHandler"); + let Some(proto::dispatch::Source::Custom(invoke)) = dispatch.source else { + panic!("expected a custom invocation source"); + }; + assert_eq!(invoke.handler_name, "reindexHandler"); + assert_eq!(invoke.input, br#"{"full":true}"#); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn invokes_an_http_handler_by_name_without_shaping_a_request() { + let (_app, addr, socket) = start_runtime( + "ipc-invoke-http", + "tests/data/fixtures/ipc-http-api.blueprint.yaml", + ) + .await; + let mut handler = HandlerStub::attach(&socket, |_| { + Some(proto::result::Outcome::Custom(proto::CustomInvokeResult { + output: br#"{"id":"order-1"}"#.to_vec(), + error_message: String::new(), + })) + }) + .await; + + let response = tokio::time::timeout( + Duration::from_secs(10), + http_client().request( + Request::builder() + .method("POST") + .uri(format!("http://{addr}/runtime/handlers/invoke")) + .header("Host", "localhost") + .header("content-type", "application/json") + .body(Body::from( + r#"{"handlerName":"getOrderHandler","invocationType":"requestResponse","payload":{"pathParams":{"orderId":"order-1"}}}"#, + )) + .unwrap(), + ), + ) + .await + .expect("the invocation should be served rather than time out") + .unwrap(); + + let status = response.status(); + let body = response.into_body().collect().await.unwrap().to_bytes(); + assert_eq!(status, 200, "body: {}", String::from_utf8_lossy(&body)); + + // Routed on the HTTP handler's own tag, since that is the tag the handler + // stream serves, and carrying the payload straight through rather than + // being dressed up as a request. + let dispatch = handler + .next_dispatch() + .await + .expect("the handler should have been given the invocation"); + assert_eq!(dispatch.handler_tag, "GET::/orders/{orderId}"); + let Some(proto::dispatch::Source::Custom(invoke)) = dispatch.source else { + panic!("expected a direct invocation source"); + }; + assert_eq!(invoke.handler_name, "getOrderHandler"); + assert_eq!(invoke.input, br#"{"pathParams":{"orderId":"order-1"}}"#); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn invokes_a_handler_by_the_name_the_blueprint_publishes_it_under() { + let (_app, addr, socket) = start_runtime( + "ipc-invoke-published", + "tests/data/fixtures/ipc-http-api.blueprint.yaml", + ) + .await; + let mut handler = HandlerStub::attach(&socket, |_| { + Some(proto::result::Outcome::Custom(proto::CustomInvokeResult { + output: br#"{"ok":true}"#.to_vec(), + error_message: String::new(), + })) + }) + .await; + + // `spec.handlerName`, which is what a deployment addresses this handler by + // and what the invoke API documents, rather than the blueprint resource it + // is declared as. + let response = tokio::time::timeout( + Duration::from_secs(10), + http_client().request( + Request::builder() + .method("POST") + .uri(format!("http://{addr}/runtime/handlers/invoke")) + .header("Host", "localhost") + .header("content-type", "application/json") + .body(Body::from( + r#"{"handlerName":"Orders-GetOrderHandler-v1","invocationType":"requestResponse"}"#, + )) + .unwrap(), + ), + ) + .await + .expect("the invocation should be served rather than time out") + .unwrap(); + + let status = response.status(); + let body = response.into_body().collect().await.unwrap().to_bytes(); + assert_eq!(status, 200, "body: {}", String::from_utf8_lossy(&body)); + + // It reaches the same handler the resource name would. + let dispatch = handler + .next_dispatch() + .await + .expect("the handler should have been given the invocation"); + assert_eq!(dispatch.handler_tag, "GET::/orders/{orderId}"); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn refuses_to_invoke_a_handler_the_blueprint_does_not_declare() { + let (_app, addr, socket) = start_runtime( + "ipc-invoke-unknown", + "tests/data/fixtures/ipc-http-api.blueprint.yaml", + ) + .await; + let _handler = HandlerStub::attach(&socket, |_| None).await; + + let started = tokio::time::Instant::now(); + let response = tokio::time::timeout( + Duration::from_secs(10), + http_client().request( + Request::builder() + .method("POST") + .uri(format!("http://{addr}/runtime/handlers/invoke")) + .header("Host", "localhost") + .header("content-type", "application/json") + .body(Body::from( + r#"{"handlerName":"noSuchHandler","invocationType":"requestResponse"}"#, + )) + .unwrap(), + ), + ) + .await + .expect("the invocation should be refused rather than time out") + .unwrap(); + + assert_eq!(response.status(), 404); + // Refused on the name, so the caller is not left waiting for a stream to + // claim a tag that is not in the blueprint at all. + assert!(started.elapsed() < Duration::from_secs(2)); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn serves_the_invoke_endpoint_for_an_application_with_no_http_api() { + // The case the endpoint matters most in: nothing here can be triggered from + // outside, so without this there is no way to run these handlers by hand. + let (_app, addr, socket) = start_runtime( + "ipc-invoke-no-api", + "tests/data/fixtures/ipc-handlers-only.blueprint.yaml", + ) + .await; + let mut handler = HandlerStub::attach(&socket, |_| { + Some(proto::result::Outcome::Custom(proto::CustomInvokeResult { + output: br#"{"reconciled":3}"#.to_vec(), + error_message: String::new(), + })) + }) + .await; + + let response = tokio::time::timeout( + Duration::from_secs(10), + http_client().request( + Request::builder() + .method("POST") + .uri(format!("http://{addr}/runtime/handlers/invoke")) + .header("Host", "localhost") + .header("content-type", "application/json") + .body(Body::from( + r#"{"handlerName":"reconcileHandler","invocationType":"requestResponse"}"#, + )) + .unwrap(), + ), + ) + .await + .expect("the invocation should be served rather than time out") + .unwrap(); + + let status = response.status(); + let body = response.into_body().collect().await.unwrap().to_bytes(); + assert_eq!(status, 200, "body: {}", String::from_utf8_lossy(&body)); + let body: serde_json::Value = serde_json::from_slice(&body).unwrap(); + assert_eq!(body["data"], r#"{"reconciled":3}"#); + + let dispatch = handler + .next_dispatch() + .await + .expect("the handler should have been given the invocation"); + assert_eq!(dispatch.handler_tag, "custom::reconcileHandler"); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn does_not_serve_the_invoke_endpoint_unless_it_is_enabled() { + // Test mode and a local platform are both still on, as they are for every + // other test here, so this pins the switch as a condition in its own right. + let (_app, addr, socket) = start_runtime_with( + "ipc-invoke-disabled", + "tests/data/fixtures/ipc-http-api.blueprint.yaml", + &[("CELERITY_ENABLE_LOCAL_INVOKE", "false")], + ) + .await; + let _handler = HandlerStub::attach(&socket, |_| None).await; + + let response = tokio::time::timeout( + Duration::from_secs(10), + http_client().request( + Request::builder() + .method("POST") + .uri(format!("http://{addr}/runtime/handlers/invoke")) + .header("Host", "localhost") + .header("content-type", "application/json") + .body(Body::from( + r#"{"handlerName":"reindexHandler","invocationType":"requestResponse"}"#, + )) + .unwrap(), + ), + ) + .await + .expect("the request should be answered rather than hang") + .unwrap(); + + // Not routed at all, as opposed to routed and refused. + assert_eq!(response.status(), 404); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn returns_binary_output_from_an_invoked_handler_without_corrupting_it() { + let raw: Vec = vec![0xff, 0xfe, 0x00, 0x80, 0x01]; + let (_app, addr, socket) = start_runtime( + "ipc-invoke-binary", + "tests/data/fixtures/ipc-http-api.blueprint.yaml", + ) + .await; + let _handler = HandlerStub::attach(&socket, |_| { + Some(proto::result::Outcome::Custom(proto::CustomInvokeResult { + output: vec![0xff, 0xfe, 0x00, 0x80, 0x01], + error_message: String::new(), + })) + }) + .await; + + let response = tokio::time::timeout( + Duration::from_secs(10), + http_client().request( + Request::builder() + .method("POST") + .uri(format!("http://{addr}/runtime/handlers/invoke")) + .header("Host", "localhost") + .header("content-type", "application/json") + .body(Body::from( + r#"{"handlerName":"reindexHandler","invocationType":"requestResponse"}"#, + )) + .unwrap(), + ), + ) + .await + .expect("the invocation should be served rather than time out") + .unwrap(); + + let status = response.status(); + let body = response.into_body().collect().await.unwrap().to_bytes(); + assert_eq!(status, 200, "body: {}", String::from_utf8_lossy(&body)); + + // A handler returning an image or a protobuf can still be exercised here. + // The bytes come back encoded, and the response says they are encoded, so + // they cannot be mistaken for text. + let body: serde_json::Value = serde_json::from_slice(&body).unwrap(); + assert_eq!(body["dataEncoding"], "base64"); + let decoded = BASE64 + .decode(body["data"].as_str().expect("output should be carried")) + .expect("the output should be base64"); + assert_eq!(decoded, raw); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn lets_an_async_invocation_run_on_after_the_caller_has_been_answered() { + let (_app, addr, socket) = start_runtime( + "ipc-invoke-async", + "tests/data/fixtures/ipc-http-api.blueprint.yaml", + ) + .await; + // Withhold the result, so the handler is still working when the caller has + // already been answered and its receiver dropped. + let mut handler = HandlerStub::attach(&socket, |_| None).await; + + let response = tokio::time::timeout( + Duration::from_secs(10), + http_client().request( + Request::builder() + .method("POST") + .uri(format!("http://{addr}/runtime/handlers/invoke")) + .header("Host", "localhost") + .header("content-type", "application/json") + .body(Body::from( + r#"{"handlerName":"reindexHandler","invocationType":"async"}"#, + )) + .unwrap(), + ), + ) + .await + .expect("an async invocation should be answered without waiting for the handler") + .unwrap(); + assert_eq!(response.status(), 200); + + let dispatch = handler + .next_dispatch() + .await + .expect("the handler should have been given the invocation"); + assert_eq!(dispatch.handler_tag, "custom::reindexHandler"); + + // The distinguishing property of an async invocation. Nobody is waiting for + // the result, but the work was asked for and should run to completion, so + // dropping the caller's receiver must not read as the caller going away. + let cancel = tokio::time::timeout(Duration::from_millis(500), handler.cancels.recv()).await; + assert!( + cancel.is_err(), + "an async invocation should not be cancelled once its caller has been answered" + ); + + let _ = tokio::fs::remove_file(&socket).await; +} diff --git a/libs/runtime/core/tests/ipc_stream_test.rs b/libs/runtime/core/tests/ipc_stream_test.rs new file mode 100644 index 00000000..959ccf12 --- /dev/null +++ b/libs/runtime/core/tests/ipc_stream_test.rs @@ -0,0 +1,92 @@ +//! The handler stream itself rather than any application served over it: the +//! socket the runtime listens on, who is allowed to reach it, and what happens +//! when one is already there. + +mod common; + +use celerity_runtime_core::{application::Application, config::RuntimeConfig}; +use common::ipc::{ipc_env, start_runtime, start_runtime_with, HandlerStub}; + +#[test_log::test(tokio::test)] +async fn restricts_the_handler_socket_to_the_runtime_user() { + use std::os::unix::fs::PermissionsExt; + + let (_app, _addr, socket) = start_runtime( + "ipc-socket-mode", + "tests/data/fixtures/ipc-http-api.blueprint.yaml", + ) + .await; + + // Anything able to connect can register as a handler and be given events, + // so the permissions on this socket are the whole of the access control. + let mode = tokio::fs::metadata(&socket) + .await + .expect("the socket should exist") + .permissions() + .mode() + & 0o777; + assert_eq!(mode, 0o600, "socket mode was {mode:o}"); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn restricts_a_socket_directory_it_creates_itself() { + use std::os::unix::fs::PermissionsExt; + + let dir = std::env::temp_dir().join(format!("celerity-ipc-dir-{}", std::process::id())); + let _ = tokio::fs::remove_dir_all(&dir).await; + let socket = dir.join("runtime.sock").to_string_lossy().into_owned(); + + let (_app, _addr, _) = start_runtime_with( + "ipc-socket-dir", + "tests/data/fixtures/ipc-http-api.blueprint.yaml", + &[("CELERITY_RUNTIME_SOCKET", &socket)], + ) + .await; + + // The socket's own mode is not the whole story. Enforcing it on connect is + // a Linux behaviour rather than a portable one, and a permissive umask + // would leave a directory another user could replace the socket in. + let mode = tokio::fs::metadata(&dir) + .await + .expect("the runtime should have created the directory") + .permissions() + .mode() + & 0o777; + assert_eq!(mode, 0o700, "directory mode was {mode:o}"); + + let _ = tokio::fs::remove_dir_all(&dir).await; +} + +#[test_log::test(tokio::test)] +async fn refuses_to_take_over_a_socket_another_runtime_is_listening_on() { + let (_app, _addr, socket) = start_runtime( + "ipc-socket-contended", + "tests/data/fixtures/ipc-http-api.blueprint.yaml", + ) + .await; + + // A second runtime pointed at the same socket must not unlink it. Doing so + // would leave the first serving a socket nothing can reach any more. + let env_vars = ipc_env( + "ipc-socket-contended-second", + "tests/data/fixtures/ipc-http-api.blueprint.yaml", + &socket, + &[("CELERITY_SERVER_PORT", "0")], + ); + let runtime_config = RuntimeConfig::from_env(&env_vars); + let mut second = Application::new(runtime_config, Box::new(env_vars)); + second.setup().unwrap(); + + let started = second.run(false).await; + assert!( + started.is_err(), + "the second runtime should refuse to start rather than take the socket" + ); + + // The first is still reachable, which is the point of refusing. + let _handler = HandlerStub::attach(&socket, |_| None).await; + + let _ = tokio::fs::remove_file(&socket).await; +} diff --git a/libs/runtime/core/tests/ipc_websocket_test.rs b/libs/runtime/core/tests/ipc_websocket_test.rs new file mode 100644 index 00000000..35cf84bf --- /dev/null +++ b/libs/runtime/core/tests/ipc_websocket_test.rs @@ -0,0 +1,1020 @@ +//! The IPC path for a WebSocket API: messages reach a handler over a real gRPC +//! stream on a real Unix socket, acknowledgements and duplicates are dealt with +//! on the way, and connections come and go. +//! +//! Every piece has unit coverage of its own. What these cover is the assembly, +//! and they do it the way a handlers executable and a real client would rather +//! than by reaching into the runtime's internals. + +mod common; + +use std::time::Duration; + +use base64::{engine::general_purpose::STANDARD as BASE64, Engine as _}; +use celerity_runtime_core::{ + application::Application, + config::RuntimeConfig, + consts::CELERITY_WS_CAPABILITIES_SIGNAL, + ipc_proto::{self as proto}, +}; +use common::ipc::{ipc_env, socket_path, start_runtime, websocket_ack, HandlerStub}; +use futures::SinkExt; +use serde_json::json; +use tokio_stream::StreamExt; + +/// Setting an application up must not need a runtime to be running. +/// +/// Deliberately not a tokio test, which is the whole point. An SDK calls setup +/// from wherever the host language happens to be, with no runtime on that +/// thread, so anything here that spawns takes the process down rather than +/// returning an error. That is what a websocket API did until the worker that +/// waits on clients moved to `run`, and a tokio test cannot show it because a +/// tokio test always has the runtime that is missing. +#[test] +fn sets_up_a_websocket_api_without_a_runtime_to_spawn_into() { + let socket = socket_path("ipc-ws-setup-sync"); + let env_vars = ipc_env( + "ipc-ws-setup-sync", + "tests/data/fixtures/ipc-websocket-default-route.blueprint.yaml", + &socket, + &[], + ); + let runtime_config = RuntimeConfig::from_env(&env_vars); + let mut app = Application::new(runtime_config, Box::new(env_vars)); + + app.setup() + .expect("setting up a websocket api should not need a runtime"); +} + +#[test_log::test(tokio::test)] +async fn routes_a_websocket_message_to_a_handler_over_the_stream() { + let (_app, addr, socket) = start_runtime( + "ipc-ws", + "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", + ) + .await; + let mut handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; + + let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) + .await + .expect("the WebSocket server should accept the connection"); + socket_conn + .send(tokio_tungstenite::tungstenite::Message::Text( + json!({ "event": "sendMessage", "data": { "text": "hello" } }).to_string(), + )) + .await + .unwrap(); + + let dispatch = handler + .next_dispatch() + .await + .expect("the message should reach the handler"); + assert_eq!(dispatch.handler_tag, "event::sendMessage"); + + let Some(proto::dispatch::Source::Websocket(message)) = dispatch.source else { + panic!("expected a WebSocket source"); + }; + assert_eq!(message.route, "sendMessage"); + assert!(!message.is_binary); + assert!(!message.connection_id.is_empty()); + let body: serde_json::Value = serde_json::from_slice(&message.message).unwrap(); + assert_eq!(body["data"]["text"], "hello"); + + let _ = socket_conn.close(None).await; + let _ = tokio::fs::remove_file(&socket).await; +} + +/// A handler sending binary is held to the format its client can read, and is +/// told which of its messages was refused. +/// + +#[test_log::test(tokio::test)] +async fn carries_a_binary_websocket_frame_without_corrupting_it() { + let (_app, addr, socket) = start_runtime( + "ipc-ws-binary", + "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", + ) + .await; + let mut handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; + + let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) + .await + .expect("the WebSocket server should accept the connection"); + + // [routeLength][route][requireAck][messageIdLength][messageId][message], + // here with no acknowledgement asked for and no message id. + let route = b"sendMessage"; + let body: Vec = vec![0xff, 0xfe, 0x00, 0x80, 0x01, 0x02]; + let mut payload = vec![route.len() as u8]; + payload.extend_from_slice(route); + payload.push(0); + payload.push(0); + payload.extend_from_slice(&body); + + socket_conn + .send(tokio_tungstenite::tungstenite::Message::Binary(payload)) + .await + .unwrap(); + + let dispatch = handler + .next_dispatch() + .await + .expect("the message should reach the handler"); + let Some(proto::dispatch::Source::Websocket(message)) = dispatch.source else { + panic!("expected a WebSocket source"); + }; + + assert!(message.is_binary); + // The bytes arrive exactly, with no base64 and no replacement characters. + assert_eq!(message.message, body); + // Guard against the encoding leaking onto the wire. + assert_ne!(message.message, BASE64.encode(&body).into_bytes()); + + let _ = socket_conn.close(None).await; + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn refuses_a_binary_websocket_message_that_is_not_framed() { + let (_app, addr, socket) = start_runtime( + "ipc-ws-binary-framing", + "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", + ) + .await; + let mut handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; + + let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) + .await + .expect("the WebSocket server should accept the connection"); + socket_conn + .send(tokio_tungstenite::tungstenite::Message::Text( + json!({ "event": "sendMessage", "data": { "text": "hello" } }).to_string(), + )) + .await + .unwrap(); + + let dispatch = handler + .next_dispatch() + .await + .expect("the message should reach the handler"); + let Some(proto::dispatch::Source::Websocket(message)) = dispatch.source else { + panic!("expected a WebSocket source"); + }; + let connection_id = message.connection_id; + + // [routeLength][route][requireAck][messageIdLength][messageId][payload] + let mut framed = vec![7u8]; + framed.extend_from_slice(b"updates"); + framed.push(0x0); + framed.push(0x0); + framed.extend_from_slice(&[0xde, 0xad, 0xbe, 0xef]); + + handler + .send_ws( + "batch-1", + vec![ + // Raw bytes, which is what an application reaches for when it + // does not know the format is required. A protobuf payload + // here, field one as a length delimited string. + proto::WsOutbound { + connection_id: connection_id.clone(), + message: vec![0x0a, 0x05, b'p', b'r', b'i', b'c', b'e'], + is_binary: true, + ..Default::default() + }, + proto::WsOutbound { + connection_id: connection_id.clone(), + message: framed.clone(), + is_binary: true, + ..Default::default() + }, + ], + ) + .await; + + let ack = handler + .next_ws_ack() + .await + .expect("the runtime should report what became of the batch"); + assert_eq!(ack.correlation_id, "batch-1"); + assert!( + !ack.success, + "a batch with a refused message is not a success" + ); + assert_eq!( + ack.failures.len(), + 1, + "only the unframed message should fail" + ); + assert_eq!( + ack.failures[0].index, 0, + "the failure should name the message that was refused" + ); + assert_eq!(ack.failures[0].connection_id, connection_id); + + // The framed one still arrives, exactly as the handler composed it, and it + // is the only binary frame after the capabilities signal. + let delivered = tokio::time::timeout(Duration::from_secs(5), async { + while let Some(Ok(message)) = socket_conn.next().await { + if let tokio_tungstenite::tungstenite::Message::Binary(bytes) = message { + if bytes[..] == CELERITY_WS_CAPABILITIES_SIGNAL[..] { + continue; + } + return Some(bytes.to_vec()); + } + } + None + }) + .await + .expect("the framed message should reach the client"); + + assert_eq!(delivered, Some(framed)); + + let _ = socket_conn.close(None).await; + let _ = tokio::fs::remove_file(&socket).await; +} + +/// A message the client sends twice is acted on once, and answered both times. +/// + +#[test_log::test(tokio::test)] +async fn acts_once_on_a_message_the_client_sent_twice_but_answers_both() { + let (_app, addr, socket) = start_runtime( + "ipc-ws-dedupe", + "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", + ) + .await; + let mut handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; + + let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) + .await + .expect("the WebSocket server should accept the connection"); + + let message = json!({ + "event": "sendMessage", + "messageId": "m-1", + "ack": true, + "data": { "text": "hello" } + }) + .to_string(); + + for _ in 0..2 { + socket_conn + .send(tokio_tungstenite::tungstenite::Message::Text( + message.clone(), + )) + .await + .unwrap(); + } + + let dispatch = handler + .next_dispatch() + .await + .expect("the first copy should reach the handler"); + let Some(proto::dispatch::Source::Websocket(delivered)) = dispatch.source else { + panic!("expected a WebSocket source"); + }; + assert_eq!(delivered.message_id, "m-1"); + + // Both copies are answered, so the client stops resending. + let mut acknowledgements = 0; + let _ = tokio::time::timeout(Duration::from_millis(600), async { + while let Some(Ok(frame)) = socket_conn.next().await { + if let tokio_tungstenite::tungstenite::Message::Text(text) = frame { + let value: serde_json::Value = serde_json::from_str(&text).unwrap(); + if value["event"] == "ack" && value["data"]["messageId"] == "m-1" { + acknowledgements += 1; + if acknowledgements == 2 { + return; + } + } + } + } + }) + .await; + assert_eq!( + acknowledgements, 2, + "both copies should be acknowledged, or the client keeps resending" + ); + + // Only the first copy was acted on. Asked for after the acknowledgements, + // so the second copy has had every chance to arrive at the handler. + let second = tokio::time::timeout(Duration::from_millis(400), handler.next_dispatch()).await; + assert!( + second.is_err() || second.unwrap().is_none(), + "the same message should not reach the handler twice" + ); + + let _ = socket_conn.close(None).await; + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn acknowledges_a_binary_message_that_asks_to_be_acknowledged() { + let (_app, addr, socket) = start_runtime( + "ipc-ws-ack-binary", + "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", + ) + .await; + let mut handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; + + let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) + .await + .expect("the WebSocket server should accept the connection"); + + // [routeLength][route][requireAck][messageIdLength][messageId][message], + // built the way the spec defines. + // The message id is what the acknowledgement has to name. + let route = b"sendMessage"; + let message_id = b"msg-1"; + let body: Vec = vec![0xde, 0xad, 0xbe, 0xef]; + let mut payload = vec![route.len() as u8]; + payload.extend_from_slice(route); + payload.push(0x1); + payload.push(message_id.len() as u8); + payload.extend_from_slice(message_id); + payload.extend_from_slice(&body); + + socket_conn + .send(tokio_tungstenite::tungstenite::Message::Binary(payload)) + .await + .unwrap(); + + let ack = tokio::time::timeout(Duration::from_secs(5), async { + while let Some(Ok(message)) = socket_conn.next().await { + if let tokio_tungstenite::tungstenite::Message::Binary(bytes) = message { + if bytes.starts_with(&[0x1, 0x4, 0x0, 0x0]) { + return Some(bytes[4..].to_vec()); + } + } + } + None + }) + .await + .expect("the acknowledgement should not take five seconds"); + + let ack = ack.expect("a message that asks to be acknowledged should be acknowledged"); + let ack: serde_json::Value = serde_json::from_slice(&ack).unwrap(); + assert_eq!(ack["messageId"], "msg-1"); + assert!( + ack["timestamp"].is_string(), + "the acknowledgement should carry a timestamp, got {ack}" + ); + + let dispatch = handler + .next_dispatch() + .await + .expect("the message should still reach the handler"); + let Some(proto::dispatch::Source::Websocket(message)) = dispatch.source else { + panic!("expected a WebSocket source"); + }; + assert_eq!(&message.message[..], &body[..]); + // The handler is told the id the client used, so instrumentation on its + // side can be tied to what the client saw acknowledged. + assert_eq!(message.message_id, "msg-1"); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn acknowledges_a_json_message_that_asks_to_be_acknowledged() { + let (_app, addr, socket) = start_runtime( + "ipc-ws-ack-json", + "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", + ) + .await; + let _handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; + + let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) + .await + .expect("the WebSocket server should accept the connection"); + + socket_conn + .send(tokio_tungstenite::tungstenite::Message::Text( + json!({ + "event": "sendMessage", + "messageId": "msg-json-1", + "ack": true, + "data": { "text": "hello" }, + }) + .to_string(), + )) + .await + .unwrap(); + + // Answered in the encoding it arrived in, so a client that only has text + // because its environment gave it no choice can still read the reply. + let ack = tokio::time::timeout(Duration::from_secs(5), async { + while let Some(Ok(message)) = socket_conn.next().await { + if let tokio_tungstenite::tungstenite::Message::Text(text) = message { + if let Ok(value) = serde_json::from_str::(&text) { + if value["event"] == "ack" { + return Some(value); + } + } + } + } + None + }) + .await + .expect("the acknowledgement should not take five seconds"); + + let ack = ack.expect("a message that asks to be acknowledged should be acknowledged"); + assert_eq!(ack["data"]["messageId"], "msg-json-1"); + assert!(ack["data"]["timestamp"].is_string()); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn acknowledges_a_message_queued_behind_a_handler_that_is_still_running() { + let (_app, addr, socket) = start_runtime( + "ipc-ws-ack-queued", + "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", + ) + .await; + // Answers nothing, so the first message is still being handled, for the + // whole of the ten second timeout this fixture sets, while the second + // arrives. + let _handler = HandlerStub::attach(&socket, |_| None).await; + + let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) + .await + .expect("the WebSocket server should accept the connection"); + + socket_conn + .send(tokio_tungstenite::tungstenite::Message::Text( + json!({ "event": "sendMessage", "data": { "first": true } }).to_string(), + )) + .await + .unwrap(); + socket_conn + .send(tokio_tungstenite::tungstenite::Message::Text( + json!({ + "event": "sendMessage", + "messageId": "queued-1", + "ack": true, + "data": { "second": true }, + }) + .to_string(), + )) + .await + .unwrap(); + + // Acknowledgement follows the message being taken in, not what happens to + // it after. Tying it to handling would leave this one waiting on the + // handler ahead of it, and a client that hears nothing sends it again for a + // message the runtime was holding the entire time. + let ack = tokio::time::timeout(Duration::from_secs(3), async { + while let Some(Ok(message)) = socket_conn.next().await { + if let tokio_tungstenite::tungstenite::Message::Text(text) = message { + if let Ok(value) = serde_json::from_str::(&text) { + if value["event"] == "ack" { + return Some(value); + } + } + } + } + None + }) + .await + .expect("the acknowledgement should not wait for the handler ahead of it"); + + let ack = ack.expect("the queued message should be acknowledged"); + assert_eq!(ack["data"]["messageId"], "queued-1"); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn does_not_acknowledge_a_message_that_did_not_ask() { + let (_app, addr, socket) = start_runtime( + "ipc-ws-ack-absent", + "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", + ) + .await; + let _handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; + + let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) + .await + .expect("the WebSocket server should accept the connection"); + + // Carries an id but does not opt in, and opting in without an id has + // nothing to name. Neither should be answered. + for message in [ + json!({ "event": "sendMessage", "messageId": "msg-2", "data": {} }), + json!({ "event": "sendMessage", "ack": true, "data": {} }), + ] { + socket_conn + .send(tokio_tungstenite::tungstenite::Message::Text( + message.to_string(), + )) + .await + .unwrap(); + } + + let unexpected = tokio::time::timeout(Duration::from_secs(2), async { + while let Some(Ok(message)) = socket_conn.next().await { + if let tokio_tungstenite::tungstenite::Message::Text(text) = message { + if let Ok(value) = serde_json::from_str::(&text) { + if value["event"] == "ack" { + return true; + } + } + } + } + false + }) + .await; + + assert_ne!( + unexpected, + Ok(true), + "acknowledgement is opt in, and an opt in without a message id is ignored" + ); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn does_not_route_a_client_acknowledgement_to_a_handler() { + let (_app, addr, socket) = start_runtime( + "ipc-ws-inbound-ack", + "tests/data/fixtures/ipc-websocket-default-route.blueprint.yaml", + ) + .await; + let mut handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; + + let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) + .await + .expect("the WebSocket server should accept the connection"); + + // What a client sends to acknowledge something the runtime sent it. A + // reserved message names its route with `event`, and this API routes on + // `action`, so left alone it carries no route the runtime knows and lands + // on the default handler as though the application had been sent it. + socket_conn + .send(tokio_tungstenite::tungstenite::Message::Text( + json!({ + "event": "ack", + "data": { "messageId": "m-1", "timestamp": "1" }, + }) + .to_string(), + )) + .await + .unwrap(); + let mut binary = vec![0x1, 0x4, 0x0, 0x0]; + binary.extend_from_slice(br#"{"messageId":"m-2","timestamp":"1"}"#); + socket_conn + .send(tokio_tungstenite::tungstenite::Message::Binary(binary)) + .await + .unwrap(); + + // Sent last and expected first, since anything the runtime failed to take + // out of the way would have reached the handler ahead of it. + socket_conn + .send(tokio_tungstenite::tungstenite::Message::Text( + json!({ "action": "sendMessage", "data": { "text": "hello" } }).to_string(), + )) + .await + .unwrap(); + + let dispatch = handler + .next_dispatch() + .await + .expect("the ordinary message should reach the handler"); + let Some(proto::dispatch::Source::Websocket(message)) = dispatch.source else { + panic!("expected a WebSocket source"); + }; + let body: serde_json::Value = serde_json::from_slice(&message.message).unwrap(); + assert_eq!( + body["data"]["text"], "hello", + "an acknowledgement reached the handler ahead of the message that followed it" + ); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn refuses_an_acknowledgement_from_a_client_that_has_not_authenticated() { + let (_app, addr, socket) = start_runtime( + "ipc-ws-auth-message-ack", + "tests/data/fixtures/ipc-websocket-auth-message.blueprint.yaml", + ) + .await; + let _handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; + + let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) + .await + .expect("the authMessage strategy should let the connection upgrade"); + + socket_conn + .send(tokio_tungstenite::tungstenite::Message::Text( + json!({ + "event": "ack", + "data": { "messageId": "m-1", "timestamp": "2026-01-01T00:00:00.000Z" } + }) + .to_string(), + )) + .await + .unwrap(); + + let rejected = tokio::time::timeout(Duration::from_secs(5), async { + while let Some(Ok(message)) = socket_conn.next().await { + if let tokio_tungstenite::tungstenite::Message::Text(text) = message { + if let Ok(value) = serde_json::from_str::(&text) { + if value["event"] == "error" { + return true; + } + } + } + } + false + }) + .await; + + assert_eq!( + rejected, + Ok(true), + "an acknowledgement before authentication should be refused rather than settle a message" + ); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn tells_an_unauthenticated_client_to_authenticate_first() { + let (_app, addr, socket) = start_runtime( + "ipc-ws-auth-message", + "tests/data/fixtures/ipc-websocket-auth-message.blueprint.yaml", + ) + .await; + let _handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; + + let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) + .await + .expect("the authMessage strategy should let the connection upgrade"); + + // Anything that is not an authenticate message, sent before the connection + // has authenticated. The runtime answers on the same socket it just read + // from, so this is the path where taking the lock twice would wedge the + // connection rather than refuse the message. + socket_conn + .send(tokio_tungstenite::tungstenite::Message::Text( + json!({ "event": "sendMessage", "data": { "text": "hello" } }).to_string(), + )) + .await + .unwrap(); + + let rejected = tokio::time::timeout(Duration::from_secs(5), async { + while let Some(Ok(message)) = socket_conn.next().await { + if let tokio_tungstenite::tungstenite::Message::Text(text) = message { + if let Ok(value) = serde_json::from_str::(&text) { + if value["event"] == "error" { + return true; + } + } + } + } + false + }) + .await; + + assert_eq!( + rejected, + Ok(true), + "an unauthenticated message should be refused rather than leave the connection hanging" + ); + + let _ = tokio::fs::remove_file(&socket).await; +} + +/// An acknowledgement is a message like any other until the client has proved +/// who it is. +/// + +#[test_log::test(tokio::test)] +async fn answers_heartbeats_while_a_handler_is_still_running() { + let (_app, addr, socket) = start_runtime( + "ipc-ws-heartbeat", + "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", + ) + .await; + // Withhold every result, standing in for a handler that takes a while. + let mut handler = HandlerStub::attach(&socket, |_| None).await; + + let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) + .await + .expect("the WebSocket server should accept the connection"); + socket_conn + .send(tokio_tungstenite::tungstenite::Message::Text( + json!({ "event": "sendMessage", "data": { "text": "hello" } }).to_string(), + )) + .await + .unwrap(); + + // Once this arrives the handler is running and has not answered. + handler + .next_dispatch() + .await + .expect("the message should reach the handler"); + + socket_conn + .send(tokio_tungstenite::tungstenite::Message::Text( + json!({ "ping": true }).to_string(), + )) + .await + .unwrap(); + + // A client whose heartbeat goes unanswered concludes the connection is + // dead and reconnects, tearing down work that is still in progress. The + // handler's own timeout is far longer than any heartbeat interval, so the + // pong cannot wait on it. + let pong = tokio::time::timeout(Duration::from_secs(3), async { + while let Some(Ok(message)) = socket_conn.next().await { + if let tokio_tungstenite::tungstenite::Message::Text(text) = message { + if let Ok(value) = serde_json::from_str::(&text) { + if value.get("pong") == Some(&serde_json::Value::Bool(true)) { + return true; + } + } + } + } + false + }) + .await; + + assert_eq!( + pong, + Ok(true), + "the heartbeat should be answered while the handler is still running" + ); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn reads_faster_than_one_message_every_ten_milliseconds() { + let (_app, addr, socket) = start_runtime( + "ipc-ws-throughput", + "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", + ) + .await; + let mut handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; + + let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) + .await + .expect("the WebSocket server should accept the connection"); + + // The read loop used to sleep 10ms per message to yield a lock it held + // across every read, capping one connection at about 100 messages a + // second whatever the handlers did. Reading no longer takes that lock, so + // 50 messages must not take the 500ms that cap would have cost. + let started = tokio::time::Instant::now(); + for index in 0..50 { + socket_conn + .send(tokio_tungstenite::tungstenite::Message::Text( + json!({ "event": "sendMessage", "data": { "index": index } }).to_string(), + )) + .await + .unwrap(); + } + for _ in 0..50 { + handler + .next_dispatch() + .await + .expect("every message should reach the handler"); + } + let elapsed = started.elapsed(); + + // Below the 500ms the old cap would have cost, with enough room above the + // handful of milliseconds this actually takes that a loaded machine does + // not fail it for being busy. + assert!( + elapsed < Duration::from_millis(450), + "50 messages took {elapsed:?}, which suggests the read loop is still rate limited" + ); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn does_not_reorder_messages_when_handling_them_off_the_read_loop() { + let (_app, addr, socket) = start_runtime( + "ipc-ws-order", + "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", + ) + .await; + let mut handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; + + let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) + .await + .expect("the WebSocket server should accept the connection"); + + for index in 0..5 { + socket_conn + .send(tokio_tungstenite::tungstenite::Message::Text( + json!({ "event": "sendMessage", "data": { "index": index } }).to_string(), + )) + .await + .unwrap(); + } + + // Moving handling off the read loop must not reorder what was already in + // order, which is what pins the worker to one message at a time. + // + // This is the behaviour of this runtime rather than a promise the platform + // makes. The same API deployed to a serverless target invokes a function + // per message with no ordering between them, and a client resending after + // a lost acknowledgement reorders its own messages anyway. An application + // that needs ordering has to carry its own sequence. So this test exists to + // keep the runtime's behaviour deliberate, not to stop it ever changing. + let mut seen = Vec::new(); + for _ in 0..5 { + let dispatch = handler + .next_dispatch() + .await + .expect("every message should reach the handler"); + let Some(proto::dispatch::Source::Websocket(message)) = dispatch.source else { + panic!("expected a WebSocket source"); + }; + let body: serde_json::Value = serde_json::from_slice(&message.message).unwrap(); + seen.push(body["data"]["index"].as_i64().unwrap()); + } + assert_eq!(seen, vec![0, 1, 2, 3, 4]); + + let _ = socket_conn.close(None).await; + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn sheds_a_connection_that_outruns_its_handlers_with_a_retry_hint() { + let (_app, addr, socket) = start_runtime( + "ipc-ws-saturate", + "tests/data/fixtures/ipc-websocket-api.blueprint.yaml", + ) + .await; + // Answer nothing, so the worker is stuck on the first message and every + // message after it piles up behind it. + let _handler = HandlerStub::attach(&socket, |_| None).await; + + let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) + .await + .expect("the WebSocket server should accept the connection"); + + // Comfortably more than the buffer holds, so the read loop reaches the + // point where it would otherwise wait in silence. + for index in 0..256 { + if socket_conn + .send(tokio_tungstenite::tungstenite::Message::Text( + json!({ "event": "sendMessage", "data": { "index": index } }).to_string(), + )) + .await + .is_err() + { + // The runtime shed the connection part way through, which is the + // behaviour under test. + break; + } + } + + // Stalling in silence is what makes a client give up on a connection it + // could have kept. Closing says the same thing in a way the client can act + // on, and the hint stops it returning straight into the same saturation. + let close = tokio::time::timeout(Duration::from_secs(10), async { + while let Some(Ok(message)) = socket_conn.next().await { + if let tokio_tungstenite::tungstenite::Message::Close(frame) = message { + return frame; + } + } + None + }) + .await + .expect("the runtime should close the connection rather than stall in silence"); + + let frame = close.expect("the close should carry a frame rather than be bare"); + assert_eq!( + u16::from(frame.code), + 1013, + "expected a try again later code" + ); + let reason: serde_json::Value = + serde_json::from_str(&frame.reason).expect("the reason should carry the retry hint"); + assert!( + reason["retryAfter"].as_u64().is_some_and(|ms| ms > 0), + "expected a retryAfter hint, got {reason}" + ); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn closes_a_connection_out_without_waiting_for_all_the_work_it_queued() { + let (_app, addr, socket) = start_runtime( + "ipc-ws-drain", + "tests/data/fixtures/ipc-websocket-disconnect.blueprint.yaml", + ) + .await; + // Answer nothing, so every queued message runs its timeout out in turn. + let mut handler = HandlerStub::attach(&socket, |_| None).await; + + let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) + .await + .expect("the WebSocket server should accept the connection"); + + // Eight messages against a one second handler timeout, so draining every + // one of them would take longer than the connection is given to close. + for index in 0..8 { + socket_conn + .send(tokio_tungstenite::tungstenite::Message::Text( + json!({ "event": "sendMessage", "data": { "index": index } }).to_string(), + )) + .await + .unwrap(); + } + handler + .next_dispatch() + .await + .expect("the first message should reach the handler"); + + let closed_at = tokio::time::Instant::now(); + socket_conn + .close(None) + .await + .expect("the client should be able to close"); + + // The disconnect handler running is what says teardown finished, and until + // it does the connection is still in the registry and still counted. It + // must not wait for messages the client is no longer there to hear about. + // + // That it runs exactly once is covered separately, by a client that sends + // a close frame outright. Closing the way this one does drives the whole + // handshake and never surfaces a frame to the application, so the second + // path that could fire a disconnect is not reached from here. + let disconnected = tokio::time::timeout(Duration::from_secs(7), async { + while let Some(dispatch) = handler.next_dispatch().await { + if let Some(proto::dispatch::Source::Websocket(message)) = dispatch.source { + if message.route == "$disconnect" { + return true; + } + } + } + false + }) + .await; + + assert_eq!( + disconnected, + Ok(true), + "the disconnect handler should run once the drain window passes" + ); + assert!( + closed_at.elapsed() < Duration::from_secs(7), + "teardown took {:?}, which suggests it waited for the whole queue", + closed_at.elapsed() + ); + + let _ = tokio::fs::remove_file(&socket).await; +} + +#[test_log::test(tokio::test)] +async fn fires_the_disconnect_handler_once_for_a_client_that_sends_a_close_frame() { + let (_app, addr, socket) = start_runtime( + "ipc-ws-close-once", + "tests/data/fixtures/ipc-websocket-disconnect.blueprint.yaml", + ) + .await; + let mut handler = HandlerStub::attach(&socket, |_| Some(websocket_ack())).await; + + let (mut socket_conn, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/")) + .await + .expect("the WebSocket server should accept the connection"); + + // Sent as an ordinary frame rather than through close(), which drives the + // whole handshake and never surfaces the frame to the application. Sent + // this way it reaches message processing, which is the path that could + // once fire a disconnect of its own alongside the one teardown fires. + socket_conn + .send(tokio_tungstenite::tungstenite::Message::Close(None)) + .await + .unwrap(); + + let mut disconnects = 0; + let _ = tokio::time::timeout(Duration::from_secs(4), async { + while let Some(dispatch) = handler.next_dispatch().await { + if let Some(proto::dispatch::Source::Websocket(message)) = dispatch.source { + if message.route == "$disconnect" { + disconnects += 1; + } + } + } + }) + .await; + + assert_eq!( + disconnects, 1, + "the disconnect handler should run once, not once per path that can end a connection" + ); + + let _ = tokio::fs::remove_file(&socket).await; +}