#![cfg(not(target_arch = "wasm32"))]
use repe::server::Router;
use repe::{AsyncClient, AsyncServer, Client, Server};
use std::time::Duration;
async fn spawn(router: Router) -> String {
let listener = AsyncServer::listen("127.0.0.1:0").await.expect("bind");
let addr = listener.local_addr().expect("addr").to_string();
tokio::spawn(async move { AsyncServer::new(router).serve(listener).await });
addr
}
fn spawn_sync(router: Router) -> String {
let server = Server::new(router);
let listener = server.listen("127.0.0.1:0").expect("bind");
let addr = listener.local_addr().expect("addr").to_string();
std::thread::spawn(move || server.serve(listener));
addr
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn call_typed_slice_roundtrips_f64_and_f32() {
let router = Router::new()
.with_typed_slice::<f64, f64, _>("/scale2", |xs| Ok(xs.iter().map(|x| x * 2.0).collect()))
.with_typed_slice::<f32, f32, _>("/negate", |xs| Ok(xs.iter().map(|x| -x).collect()));
let addr = spawn(router).await;
let client = AsyncClient::connect(&addr).await.expect("connect");
let xs: Vec<f64> = (0..4096).map(|i| i as f64 * 0.25).collect();
let out: Vec<f64> = client
.call_typed_slice("/scale2", &xs)
.await
.expect("call f64");
assert_eq!(out, xs.iter().map(|x| x * 2.0).collect::<Vec<_>>());
let fs: Vec<f32> = (0..1024).map(|i| i as f32 - 7.5).collect();
let neg: Vec<f32> = client
.call_typed_slice("/negate", &fs)
.await
.expect("call f32");
assert_eq!(neg, fs.iter().map(|x| -x).collect::<Vec<_>>());
let empty: Vec<f64> = Vec::new();
let back: Vec<f64> = client
.call_typed_slice("/scale2", &empty)
.await
.expect("empty");
assert!(back.is_empty());
let xs2: Vec<f64> = vec![1.0, 2.0, 3.0];
let out2: Vec<f64> = client
.call_typed_slice_with_timeout("/scale2", &xs2, Duration::from_secs(5))
.await
.expect("call_typed_slice_with_timeout");
assert_eq!(out2, vec![2.0, 4.0, 6.0]);
}
#[test]
fn sync_client_call_typed_slice_roundtrips() {
let router = Router::new()
.with_typed_slice::<f64, f64, _>("/scale2", |xs| Ok(xs.iter().map(|x| x * 2.0).collect()));
let addr = spawn_sync(router);
let client = Client::connect(&addr).expect("connect");
let xs: Vec<f64> = (0..2048).map(|i| i as f64 * 0.5).collect();
let out: Vec<f64> = client
.call_typed_slice("/scale2", &xs)
.expect("sync call_typed_slice");
assert_eq!(out, xs.iter().map(|x| x * 2.0).collect::<Vec<_>>());
let out2: Vec<f64> = client
.call_typed_slice_with_timeout("/scale2", &xs, Duration::from_secs(5))
.expect("sync call_typed_slice_with_timeout");
assert_eq!(out2, xs.iter().map(|x| x * 2.0).collect::<Vec<_>>());
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn slice_route_interops_with_serde_client() {
let router = Router::new().with_typed_slice::<f64, f64, _>("/echo", Ok);
let addr = spawn(router).await;
let client = AsyncClient::connect(&addr).await.expect("connect");
let xs: Vec<f64> = vec![1.0, -2.5, 3.25, 4.0e9];
let out: Vec<f64> = client
.call_typed_beve("/echo", &xs)
.await
.expect("serde->slice");
assert_eq!(out, xs);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn serde_route_interops_with_typed_slice_client() {
let router = Router::new().with_typed::<Vec<f64>, Vec<f64>, _>("/echo", |xs: Vec<f64>| {
Ok(repe::server::TypedResponse::beve(xs))
});
let addr = spawn(router).await;
let client = AsyncClient::connect(&addr).await.expect("connect");
let xs: Vec<f64> = (0..256).map(|i| i as f64 * 1.5).collect();
let out: Vec<f64> = client
.call_typed_slice("/echo", &xs)
.await
.expect("slice->serde");
assert_eq!(out, xs);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn wrong_element_type_is_an_error() {
let router = Router::new().with_typed_slice::<f64, f64, _>("/echo", Ok);
let addr = spawn(router).await;
let client = AsyncClient::connect(&addr).await.expect("connect");
let fs: Vec<f32> = vec![1.0, 2.0, 3.0];
let res: Result<Vec<f32>, _> = client.call_typed_slice("/echo", &fs).await;
assert!(res.is_err(), "f32 into an f64 route must error");
}