From a169806b32d0cd580f093ef6ad3cbf25b2d5b428 Mon Sep 17 00:00:00 2001 From: Kodi Craft Date: Fri, 21 Jun 2024 11:54:29 +0200 Subject: [PATCH] Add small random delay in tests --- Cargo.lock | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 ++- tests/mod.rs | 7 +++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index a718723..0e60d72 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,11 +50,23 @@ version = "0.2.0" dependencies = [ "proc-macro2", "quote", + "rand", "serde", "syn", "tokio", ] +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "gimli" version = "0.29.0" @@ -113,6 +125,12 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + [[package]] name = "proc-macro2" version = "1.0.85" @@ -131,6 +149,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "rustc-demangle" version = "0.1.24" @@ -196,3 +244,9 @@ name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" diff --git a/Cargo.toml b/Cargo.toml index 5c29ab7..6091876 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,10 +8,11 @@ publish = ["gitea"] [dependencies] proc-macro2 = "1.0.85" quote = "1.0.36" +rand = "0.8.5" serde = { version = "1.0.203", features = ["serde_derive"] } syn = "2.0.66" # TODO: rt and macros should be removed unless we do tests -tokio = { version = "1.38.0", features = ["sync", "rt-multi-thread", "macros"] } +tokio = { version = "1.38.0", features = ["sync", "rt-multi-thread", "macros", "time"] } [lib] proc-macro = true diff --git a/tests/mod.rs b/tests/mod.rs index d99e968..2406993 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -45,12 +45,19 @@ async fn main() { server.abort(); } +// Simulates a network delay or some other kind of slow-down in the communication. +// We don't want it to be too big not to slow down tests unnecessarily. +async fn random_sleep() { + tokio::time::sleep(std::time::Duration::from_millis(rand::random::() % 20)).await; +} + async fn server_loop( mut qrx: Receiver<(u64, __TestProtocolQuestion)>, atx: Sender<(u64, __TestProtocolAnswer)>, ) { loop { if let Some((nonce, q)) = qrx.recv().await { + random_sleep().await; match q { __TestProtocolQuestion::addition((a, b)) => { atx.send((nonce, __TestProtocolAnswer::addition(a + b)))