Add small random delay in tests

This commit is contained in:
Kodi Craft 2024-06-21 11:54:29 +02:00
parent b9128a465c
commit a169806b32
Signed by: kodi
GPG Key ID: 69D9EED60B242822
3 changed files with 63 additions and 1 deletions

54
Cargo.lock generated
View File

@ -50,11 +50,23 @@ version = "0.2.0"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"rand",
"serde", "serde",
"syn", "syn",
"tokio", "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]] [[package]]
name = "gimli" name = "gimli"
version = "0.29.0" version = "0.29.0"
@ -113,6 +125,12 @@ version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.85" version = "1.0.85"
@ -131,6 +149,36 @@ dependencies = [
"proc-macro2", "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]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.24" version = "0.1.24"
@ -196,3 +244,9 @@ name = "unicode-ident"
version = "1.0.12" version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"

View File

@ -8,10 +8,11 @@ publish = ["gitea"]
[dependencies] [dependencies]
proc-macro2 = "1.0.85" proc-macro2 = "1.0.85"
quote = "1.0.36" quote = "1.0.36"
rand = "0.8.5"
serde = { version = "1.0.203", features = ["serde_derive"] } serde = { version = "1.0.203", features = ["serde_derive"] }
syn = "2.0.66" syn = "2.0.66"
# TODO: rt and macros should be removed unless we do tests # 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] [lib]
proc-macro = true proc-macro = true

View File

@ -45,12 +45,19 @@ async fn main() {
server.abort(); 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::<u64>() % 20)).await;
}
async fn server_loop( async fn server_loop(
mut qrx: Receiver<(u64, __TestProtocolQuestion)>, mut qrx: Receiver<(u64, __TestProtocolQuestion)>,
atx: Sender<(u64, __TestProtocolAnswer)>, atx: Sender<(u64, __TestProtocolAnswer)>,
) { ) {
loop { loop {
if let Some((nonce, q)) = qrx.recv().await { if let Some((nonce, q)) = qrx.recv().await {
random_sleep().await;
match q { match q {
__TestProtocolQuestion::addition((a, b)) => { __TestProtocolQuestion::addition((a, b)) => {
atx.send((nonce, __TestProtocolAnswer::addition(a + b))) atx.send((nonce, __TestProtocolAnswer::addition(a + b)))