66 lines
1.7 KiB
Rust
66 lines
1.7 KiB
Rust
use bedrock_rpc::{CallError, Fault, Reply, Request, Transport};
|
|
use serde_json::json;
|
|
|
|
mod catalog {
|
|
bedrock_rpc::service! {
|
|
"bedrock.catalog.v1" {
|
|
3 => lookup(String) -> u64,
|
|
8 => ready(()) -> bool,
|
|
}
|
|
}
|
|
}
|
|
|
|
struct Catalog;
|
|
|
|
impl catalog::Service for Catalog {
|
|
async fn lookup(&self, key: String) -> Result<u64, Fault> {
|
|
if key == "documents" {
|
|
Ok(17)
|
|
} else {
|
|
Err(Fault::Unavailable)
|
|
}
|
|
}
|
|
|
|
async fn ready(&self, (): ()) -> Result<bool, Fault> {
|
|
Ok(true)
|
|
}
|
|
}
|
|
|
|
struct Local;
|
|
|
|
impl Transport for Local {
|
|
type Error = std::convert::Infallible;
|
|
async fn exchange(&self, request: Request) -> Result<Reply, Self::Error> {
|
|
Ok(catalog::dispatch(&Catalog, request).await)
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn typed_calls_and_remote_failures() {
|
|
let client = catalog::Client(Local);
|
|
assert_eq!(client.lookup("documents".into()).await.unwrap(), 17);
|
|
assert!(client.ready(()).await.unwrap());
|
|
assert!(matches!(
|
|
client.lookup("missing".into()).await,
|
|
Err(CallError::Remote(Fault::Unavailable))
|
|
));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn rejects_incompatible_requests() {
|
|
for (service, method, fault) in [
|
|
("bedrock.catalog.v2", 3, Fault::UnknownService),
|
|
(catalog::SERVICE, 7, Fault::UnknownMethod),
|
|
(catalog::SERVICE, 3, Fault::InvalidRequest),
|
|
] {
|
|
let request = Request {
|
|
service: service.into(),
|
|
method,
|
|
payload: json!(false),
|
|
};
|
|
assert!(
|
|
matches!(catalog::dispatch(&Catalog, request).await, Reply::Failure(actual) if actual == fault)
|
|
);
|
|
}
|
|
}
|