Implement basic test for the client interface

This commit is contained in:
Kodi Craft 2024-06-21 11:28:46 +02:00
parent e6e9610d1f
commit b4f1e1b092
Signed by: kodi
GPG Key ID: 69D9EED60B242822
4 changed files with 91 additions and 19 deletions

29
Cargo.lock generated
View File

@ -61,6 +61,12 @@ version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd"
[[package]]
name = "hermit-abi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.155" version = "0.2.155"
@ -82,6 +88,16 @@ dependencies = [
"adler", "adler",
] ]
[[package]]
name = "num_cpus"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43"
dependencies = [
"hermit-abi",
"libc",
]
[[package]] [[package]]
name = "object" name = "object"
version = "0.36.0" version = "0.36.0"
@ -159,7 +175,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a" checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a"
dependencies = [ dependencies = [
"backtrace", "backtrace",
"num_cpus",
"pin-project-lite", "pin-project-lite",
"tokio-macros",
]
[[package]]
name = "tokio-macros"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a"
dependencies = [
"proc-macro2",
"quote",
"syn",
] ]
[[package]] [[package]]

View File

@ -10,7 +10,8 @@ proc-macro2 = "1.0.85"
quote = "1.0.36" quote = "1.0.36"
serde = { version = "1.0.203", features = ["serde_derive"] } serde = { version = "1.0.203", features = ["serde_derive"] }
syn = "2.0.66" syn = "2.0.66"
tokio = { version = "1.38.0", features = ["sync"] } # TODO: rt and macros should be removed unless we do tests
tokio = { version = "1.38.0", features = ["sync", "rt-multi-thread", "macros"] }
[lib] [lib]
proc-macro = true proc-macro = true

View File

@ -123,6 +123,7 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
// Create an error and result type for sending messages // Create an error and result type for sending messages
let error_enum = quote! { let error_enum = quote! {
#[derive(Debug)]
#vis enum #error_enum_name { #vis enum #error_enum_name {
SendError(tokio::sync::mpsc::error::SendError<(u64, #question_enum_name)>), SendError(tokio::sync::mpsc::error::SendError<(u64, #question_enum_name)>),
Closed, Closed,

View File

@ -16,6 +16,7 @@ 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/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
use eagle::Protocol; use eagle::Protocol;
use tokio::sync::mpsc::{self, Receiver, Sender};
#[derive(Protocol)] #[derive(Protocol)]
enum TestProtocol { enum TestProtocol {
@ -25,23 +26,63 @@ enum TestProtocol {
Void((), ()), Void((), ()),
} }
struct DummyServer; #[tokio::test]
impl TestProtocolServer for DummyServer { async fn main() {
fn some_kind_of_question(&mut self, question: String) -> i32 { let (qtx, qrx) = mpsc::channel(16);
question.len() as i32 let (atx, arx) = mpsc::channel(16);
} let mut client = TestProtocolClient::new(qtx, arx);
let server = tokio::spawn(server_loop(qrx, atx));
fn addition(&mut self, a: i32, b: i32) -> i32 { let result = client.addition(2, 5).await.unwrap();
a + b assert_eq!(result, 7);
} let result = client
.some_kind_of_question("Hello, world!".to_string())
fn this_responds_with_a_string(&mut self, arg: i32) -> String { .await
format!("The number is {}", arg) .unwrap();
} assert_eq!(result, "Hello, world!".len() as i32);
let result = client.this_responds_with_a_string(42).await.unwrap();
fn void(&mut self) { assert_eq!(result, "The number is 42");
println!("Void function called!") client.void().await.unwrap();
} server.abort();
} }
fn main() {} async fn server_loop(
mut qrx: Receiver<(u64, __TestProtocolQuestion)>,
atx: Sender<(u64, __TestProtocolAnswer)>,
) {
loop {
if let Some((nonce, q)) = qrx.recv().await {
match q {
__TestProtocolQuestion::addition((a, b)) => {
atx.send((nonce, __TestProtocolAnswer::addition(a + b)))
.await
.unwrap();
}
__TestProtocolQuestion::some_kind_of_question(s) => {
atx.send((
nonce,
__TestProtocolAnswer::some_kind_of_question(s.len() as i32),
))
.await
.unwrap();
}
__TestProtocolQuestion::this_responds_with_a_string(i) => {
atx.send((
nonce,
__TestProtocolAnswer::this_responds_with_a_string(format!(
"The number is {}",
i
)),
))
.await
.unwrap();
}
__TestProtocolQuestion::void(()) => {
println!("Received void question");
atx.send((nonce, __TestProtocolAnswer::void(())))
.await
.unwrap();
}
}
}
}
}