Files
leleka/crates/runtime-client/tests/grpc_backend/scripted_service.rs
T
Pavlo Ratushnyi 0ad203f7d7 STRA-338 size-rest: tokenizer, gateway-text, gateway-domain, runtime-client, cpu-worker and experimental-ref within 400 lines per file
Oversize files before/after: tokenizer 1 -> 0 (lib.rs 840 into construction, encoding, decoding, bpe_merge, vocabulary),
gateway-text 1 -> 0, runtime-client 1 -> 0, cpu-worker 1 -> 0, experimental-ref 1 -> 0 (test files split by topic),
gateway-domain 1 -> 0: render_request_with (385 lines, complexity 93) became an entry function plus one function per
system message and one method per input item kind; messages, check order and rendered text unchanged (gateway-domain 17
tests and strata-control responses 72 tests pass).

Census prod/test (fn>30 / cx>10 / nesting>4 / wide lines): gateway-domain 16/3, 13/0, 6/0, 53/1 -> 16/3, 12/0, 5/0, 45/1;
runtime-client test 9 -> 6 long functions; experimental-ref test wide lines 58 -> 37; tokenizer, gateway-text and
cpu-worker unchanged. Test ids and outcomes unchanged.
2026-11-06 16:28:25 +01:00

110 lines
4.4 KiB
Rust

//! The scripted `leleka.runtime.v1` service the tests run against.
#[allow(unused_imports)]
use super::*;
#[tonic::async_trait]
impl proto::runtime_server::Runtime for Fake {
async fn handshake(&self, _: Request<proto::Hello>) -> Result<Response<proto::Snapshot>, Status> {
Ok(Response::new(self.snapshot()))
}
async fn snapshot(&self, _: Request<proto::Empty>) -> Result<Response<proto::Snapshot>, Status> {
Ok(Response::new(self.snapshot()))
}
type WatchStream = Pin<Box<dyn Stream<Item = Result<proto::Snapshot, Status>> + Send>>;
async fn watch(&self, _: Request<proto::Cursor>) -> Result<Response<Self::WatchStream>, Status> {
Err(Status::unimplemented("fixture"))
}
async fn load(&self, _: Request<proto::LoadRequest>) -> Result<Response<proto::OperationResult>, Status> {
panic!("an attach-only backend must never send Load");
}
async fn unload(&self, _: Request<proto::UnloadRequest>) -> Result<Response<proto::OperationResult>, Status> {
panic!("an attach-only backend must never send Unload");
}
async fn operation(&self, _: Request<proto::OperationQuery>) -> Result<Response<proto::OperationResult>, Status> {
Err(Status::unimplemented("fixture"))
}
async fn reconfigure(
&self,
request: Request<proto::ReconfigureRequest>,
) -> Result<Response<proto::ReconfigureReply>, Status> {
let request = request.into_inner();
let refuse = request.settings_json.contains("bad");
Ok(Response::new(proto::ReconfigureReply {
operation_id: request.operation_id,
epoch: request.expected_epoch,
ok: !refuse,
effective_json: request.settings_json,
error_code: if refuse { "invalid".into() } else { String::new() },
error: if refuse {
"context_tokens must be 1..=262144".into()
} else {
String::new()
},
retryable: false,
}))
}
async fn cancel(&self, request: Request<proto::CancelRequest>) -> Result<Response<proto::CancelAck>, Status> {
self.cancels.fetch_add(1, Ordering::SeqCst);
let request = request.into_inner();
let ack = proto::CancelAck {
request_id: request.request_id.clone(),
epoch: request.expected_epoch.clone(),
acknowledged: true,
};
self.state.lock().unwrap().cancel_requests.push(request);
Ok(Response::new(ack))
}
type GenerateStream = Pin<Box<dyn Stream<Item = Result<proto::GenerationFrame, Status>> + Send>>;
async fn generate(
&self,
request: Request<proto::GenerateRequest>,
) -> Result<Response<Self::GenerateStream>, Status> {
let request = request.into_inner();
let (epoch, frames) = {
let mut state = self.state.lock().unwrap();
state.requests.push(request.clone());
let frames = if state.scripts.is_empty() {
Vec::new()
} else {
state.scripts.remove(0)
};
(state.epoch.clone(), frames)
};
let id = request.request_id;
let mut out: Vec<Result<proto::GenerationFrame, Status>> = Vec::new();
let sequence = 1;
let frame = |sequence, payload| proto::GenerationFrame {
request_id: id.clone(),
epoch: epoch.clone(),
sequence,
payload: Some(payload),
};
out.push(Ok(frame(
sequence,
Payload::Accepted(proto::Accepted { accepted: true }),
)));
for (sequence, item) in (2..).zip(frames) {
out.push(item.map(|payload| frame(sequence, payload)));
}
// A trailing `Aborted("hang")` means: stay open and silent after the scripted frames.
let hang = matches!(out.last(), Some(Err(s)) if s.code() == tonic::Code::Aborted && s.message() == "hang");
if hang {
out.pop();
}
let stream: Self::GenerateStream = if hang {
Box::pin(tokio_stream::StreamExt::chain(
tokio_stream::iter(out),
futures_util::stream::pending(),
))
} else {
Box::pin(tokio_stream::iter(out))
};
let mut response = Response::new(stream);
response
.metadata_mut()
.insert("x-leleka-accepted", "true".parse().unwrap());
Ok(response)
}
}