eagle/tests/full.rs

101 lines
2.9 KiB
Rust
Raw Normal View History

2024-06-24 15:34:14 +02:00
/*
2024-06-24 18:26:19 +02:00
Eagle - A simple library for RPC in Rust
2024-06-24 15:34:14 +02:00
Copyright (c) 2024 KodiCraft
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use eagle::Protocol;
use env_logger::{Builder, Env};
use std::sync::Once;
2024-06-26 11:16:20 +02:00
use tokio::net::TcpStream;
2024-06-24 15:34:14 +02:00
2024-06-26 11:16:20 +02:00
static LOG_INIT: Once = Once::new();
static CONSOLE_INIT: Once = Once::new();
2024-06-24 15:34:14 +02:00
pub fn init_logger() {
2024-06-26 11:16:20 +02:00
LOG_INIT.call_once(|| {
2024-06-24 15:34:14 +02:00
let env = Env::default()
.filter_or("RUST_LOG", "info")
.write_style_or("LOG_STYLE", "always");
Builder::from_env(env).format_timestamp_nanos().init();
});
}
2024-06-26 11:16:20 +02:00
pub fn init_console() {
CONSOLE_INIT.call_once(|| {
console_subscriber::init();
});
}
2024-06-24 15:34:14 +02:00
#[derive(Protocol)]
enum TestProtocol {
Addition((i32, i32), i32),
SomeKindOfQuestion(String, i32),
ThisRespondsWithAString(i32, String),
Void((), ()),
}
#[derive(Clone)]
struct TrivialServer;
2024-06-24 16:58:14 +02:00
impl TestProtocolServerHandler for TrivialServer {
2024-06-24 15:34:14 +02:00
async fn addition(&mut self, a: i32, b: i32) -> i32 {
a + b
}
async fn some_kind_of_question(&mut self, s: String) -> i32 {
s.len() as i32
}
async fn this_responds_with_a_string(&mut self, i: i32) -> String {
format!("The number is {}", i)
}
async fn void(&mut self) {}
}
struct Cleanup {
address: String,
}
impl Drop for Cleanup {
fn drop(&mut self) {
std::fs::remove_file(&self.address).unwrap();
}
}
2024-06-24 15:34:14 +02:00
#[tokio::test]
async fn e2e() {
init_logger();
2024-06-26 11:16:20 +02:00
init_console();
2024-06-24 15:34:14 +02:00
#[cfg(feature = "unix")]
let address = format!("/tmp/eagle-test-{}.sock", rand::random::<u64>());
#[cfg(feature = "unix")]
let _cleanup = Cleanup {
address: address.clone(),
};
2024-06-24 15:34:14 +02:00
#[cfg(feature = "tcp")]
let address = format!("127.0.0.1:{}", 10000 + rand::random::<u64>() % 1000);
2024-06-26 11:16:20 +02:00
let _server = TestProtocolServer::bind(TrivialServer, address.clone()).await;
2024-06-25 11:39:07 +02:00
tokio::time::sleep(tokio::time::Duration::from_millis(10)).await; // Wait for the server to start
2024-06-24 15:34:14 +02:00
let client = TestProtocolClient::connect(address).await.unwrap();
assert_eq!(client.addition(2, 5).await.unwrap(), 7);
assert_eq!(
client
.some_kind_of_question("Hello, world!".to_string())
.await
.unwrap(),
"Hello, world!".len() as i32
);
assert_eq!(
client.this_responds_with_a_string(42).await.unwrap(),
"The number is 42"
);
client.void().await.unwrap();
2024-06-24 15:34:14 +02:00
}