Files
leleka/crates/runtime-client/tests/grpc_backend/scripted_service.rs
T

147 lines
5.0 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 out = scripted_frames(request.request_id, epoch, frames);
// 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");
let stream = scripted_stream(out, hang);
let mut response = Response::new(stream);
response
.metadata_mut()
.insert("x-leleka-accepted", "true".parse().unwrap());
Ok(response)
}
}
/// The accepted frame, then the scripted payloads in sequence; an error item stays an error.
fn scripted_frames(
id: String,
epoch: String,
frames: Frames,
) -> Vec<Result<proto::GenerationFrame, Status>> {
let frame = |sequence, payload| proto::GenerationFrame {
request_id: id.clone(),
epoch: epoch.clone(),
sequence,
payload: Some(payload),
};
let accepted = Payload::Accepted(proto::Accepted { accepted: true });
let mut out: Vec<Result<proto::GenerationFrame, Status>> = vec![Ok(frame(1, accepted))];
for (sequence, item) in (2..).zip(frames) {
out.push(item.map(|payload| frame(sequence, payload)));
}
out
}
/// The frames as a stream; with `hang` the trailing marker is dropped and the stream stays open after the rest.
fn scripted_stream(
mut out: Vec<Result<proto::GenerationFrame, Status>>,
hang: bool,
) -> Pin<Box<dyn Stream<Item = Result<proto::GenerationFrame, Status>> + Send>> {
if hang {
out.pop();
Box::pin(tokio_stream::StreamExt::chain(
tokio_stream::iter(out),
futures_util::stream::pending(),
))
} else {
Box::pin(tokio_stream::iter(out))
}
}