tests for redis connection

This commit is contained in:
Aravinth Manivannan 2021-06-08 20:15:02 +05:30
parent 06df4d24d0
commit 7d80302fa2
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
6 changed files with 484 additions and 89 deletions

View file

@ -44,6 +44,9 @@ jobs:
command: check
args: --all --bins --examples --tests
- name: run mcaptcha cache redis instance
run: docker run -p 6379:6379 mcaptcha/cache
- name: tests
uses: actions-rs/cargo@v1
timeout-minutes: 40

470
Cargo.lock generated
View file

@ -19,8 +19,8 @@ dependencies = [
"parking_lot",
"pin-project",
"smallvec",
"tokio 0.2.25",
"tokio-util 0.3.1",
"tokio",
"tokio-util",
"trust-dns-proto",
"trust-dns-resolver",
]
@ -47,7 +47,7 @@ dependencies = [
"futures-channel",
"futures-util",
"smallvec",
"tokio 0.2.25",
"tokio",
]
[[package]]
@ -85,6 +85,118 @@ dependencies = [
"memchr",
]
[[package]]
name = "async-channel"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2114d64672151c0c5eaa5e131ec84a74f06e1e559830dabba01ca30605d66319"
dependencies = [
"concurrent-queue",
"event-listener",
"futures-core",
]
[[package]]
name = "async-executor"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "871f9bb5e0a22eeb7e8cf16641feb87c9dc67032ccf8ff49e772eb9941d3a965"
dependencies = [
"async-task",
"concurrent-queue",
"fastrand",
"futures-lite",
"once_cell",
"slab",
]
[[package]]
name = "async-global-executor"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9586ec52317f36de58453159d48351bc244bc24ced3effc1fce22f3d48664af6"
dependencies = [
"async-channel",
"async-executor",
"async-io",
"async-mutex",
"blocking",
"futures-lite",
"num_cpus",
"once_cell",
]
[[package]]
name = "async-io"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bbfd5cf2794b1e908ea8457e6c45f8f8f1f6ec5f74617bf4662623f47503c3b"
dependencies = [
"concurrent-queue",
"fastrand",
"futures-lite",
"libc",
"log",
"once_cell",
"parking",
"polling",
"slab",
"socket2 0.4.0",
"waker-fn",
"winapi 0.3.9",
]
[[package]]
name = "async-lock"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6a8ea61bf9947a1007c5cada31e647dbc77b103c679858150003ba697ea798b"
dependencies = [
"event-listener",
]
[[package]]
name = "async-mutex"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "479db852db25d9dbf6204e6cb6253698f175c15726470f78af0d918e99d6156e"
dependencies = [
"event-listener",
]
[[package]]
name = "async-std"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9f06685bad74e0570f5213741bea82158279a4103d988e57bfada11ad230341"
dependencies = [
"async-channel",
"async-global-executor",
"async-io",
"async-lock",
"crossbeam-utils 0.8.5",
"futures-channel",
"futures-core",
"futures-io",
"futures-lite",
"gloo-timers",
"kv-log-macro",
"log",
"memchr",
"num_cpus",
"once_cell",
"pin-project-lite 0.2.6",
"pin-utils",
"slab",
"wasm-bindgen-futures",
]
[[package]]
name = "async-task"
version = "4.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e91831deabf0d6d7ec49552e489aed63b7456a7a3c46cff62adad428110b0af0"
[[package]]
name = "async-trait"
version = "0.1.48"
@ -96,6 +208,12 @@ dependencies = [
"syn",
]
[[package]]
name = "atomic-waker"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a"
[[package]]
name = "atty"
version = "0.2.14"
@ -138,6 +256,26 @@ dependencies = [
"generic-array",
]
[[package]]
name = "blocking"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5e170dbede1f740736619b776d7251cb1b9095c435c34d8ca9f57fcd2f335e9"
dependencies = [
"async-channel",
"async-task",
"atomic-waker",
"fastrand",
"futures-lite",
"once_cell",
]
[[package]]
name = "bumpalo"
version = "3.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c59e7af012c713f529e7a3ee57ce9b31ddd858d4b512923602f74608b009631"
[[package]]
name = "byteorder"
version = "1.3.4"
@ -156,6 +294,18 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040"
[[package]]
name = "cache-padded"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "631ae5198c9be5e753e5cc215e1bd73c2b466a3565173db433f52bb9d3e66dba"
[[package]]
name = "cc"
version = "1.0.68"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a72c244c1ff497a746a7e1fb3d14bd08420ecda70c8f25c7112f2781652d787"
[[package]]
name = "cfg-if"
version = "0.1.10"
@ -174,11 +324,21 @@ version = "4.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc4369b5e4c0cddf64ad8981c0111e7df4f7078f4d6ba98fb31f2e17c4c57b7e"
dependencies = [
"bytes 0.5.6",
"bytes 1.0.1",
"futures-util",
"memchr",
"pin-project-lite 0.2.6",
"tokio 1.6.1",
"tokio",
]
[[package]]
name = "concurrent-queue"
version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30ed07550be01594c6026cff2a1d7fe9c8f683caa798e12b68694ac9e88286a3"
dependencies = [
"cache-padded",
]
[[package]]
@ -211,7 +371,7 @@ version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b153fe7cbef478c567df0f972e02e6d736db11affe43dfc9c56a9374d1adfb87"
dependencies = [
"crossbeam-utils",
"crossbeam-utils 0.7.2",
"maybe-uninit",
]
@ -226,6 +386,26 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d82cfc11ce7f2c3faef78d8a684447b40d503d9681acebed6cb728d45940c4db"
dependencies = [
"cfg-if 1.0.0",
"lazy_static",
]
[[package]]
name = "ctor"
version = "0.1.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e98e2ad1a782e33928b96fc3948e7c355e5af34ba4de7670fe8bac2a3b2006d"
dependencies = [
"quote",
"syn",
]
[[package]]
name = "darling"
version = "0.10.2"
@ -405,6 +585,21 @@ dependencies = [
"termcolor",
]
[[package]]
name = "event-listener"
version = "2.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7531096570974c3a9dcf9e4b8e1cede1ec26cf5046219fb3b9d897503b9be59"
[[package]]
name = "fastrand"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77b705829d1e87f762c2df6da140b26af5839e1033aa84aa5f56bb688e4e1bdb"
dependencies = [
"instant",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -473,6 +668,21 @@ version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d71c2c65c57704c32f5241c1223167c2c3294fd34ac020c807ddbe6db287ba59"
[[package]]
name = "futures-lite"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7694489acd39452c77daa48516b894c153f192c3578d5a839b62c58099fcbf48"
dependencies = [
"fastrand",
"futures-core",
"futures-io",
"memchr",
"parking",
"pin-project-lite 0.2.6",
"waker-fn",
]
[[package]]
name = "futures-sink"
version = "0.3.13"
@ -534,6 +744,19 @@ dependencies = [
"wasi 0.10.2+wasi-snapshot-preview1",
]
[[package]]
name = "gloo-timers"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47204a46aaff920a1ea58b11d03dec6f704287d27561724a4631e450654a891f"
dependencies = [
"futures-channel",
"futures-core",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "heck"
version = "0.3.2"
@ -613,7 +836,7 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7e2f18aece9709094573a9f24f483c4f65caa4298e2f7ae1b71cc65d853fad7"
dependencies = [
"socket2",
"socket2 0.3.19",
"widestring",
"winapi 0.3.9",
"winreg",
@ -625,6 +848,15 @@ version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"
[[package]]
name = "js-sys"
version = "0.3.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83bdfbace3a0e81a4253f73b49e960b053e396a11012cbd49b9b74d6a2b67062"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "kernel32-sys"
version = "0.2.2"
@ -635,6 +867,15 @@ dependencies = [
"winapi-build",
]
[[package]]
name = "kv-log-macro"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f"
dependencies = [
"log",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
@ -686,6 +927,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
dependencies = [
"cfg-if 1.0.0",
"value-bag",
]
[[package]]
@ -734,25 +976,12 @@ dependencies = [
"kernel32-sys",
"libc",
"log",
"miow 0.2.2",
"miow",
"net2",
"slab",
"winapi 0.2.8",
]
[[package]]
name = "mio"
version = "0.7.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf80d3e903b34e0bd7282b218398aec54e082c840d9baf8339e0080a0c542956"
dependencies = [
"libc",
"log",
"miow 0.3.7",
"ntapi",
"winapi 0.3.9",
]
[[package]]
name = "mio-uds"
version = "0.6.8"
@ -761,7 +990,7 @@ checksum = "afcb699eb26d4332647cc848492bbc15eafb26f08d0304550d5aa1f612e066f0"
dependencies = [
"iovec",
"libc",
"mio 0.6.23",
"mio",
]
[[package]]
@ -776,15 +1005,6 @@ dependencies = [
"ws2_32-sys",
]
[[package]]
name = "miow"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21"
dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "net2"
version = "0.2.37"
@ -796,15 +1016,6 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "ntapi"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44"
dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "num_cpus"
version = "1.13.0"
@ -827,6 +1038,12 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
[[package]]
name = "parking"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72"
[[package]]
name = "parking_lot"
version = "0.11.1"
@ -896,6 +1113,19 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "polling"
version = "2.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fc12d774e799ee9ebae13f4076ca003b40d18a11ac0f3641e6f899618580b7b"
dependencies = [
"cfg-if 1.0.0",
"libc",
"log",
"wepoll-sys",
"winapi 0.3.9",
]
[[package]]
name = "pow_sha256"
version = "0.2.1"
@ -1030,23 +1260,24 @@ dependencies = [
[[package]]
name = "redis"
version = "0.20.1"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a32cb439c4e89c1e6415e5b3b23d9d8cc6dc1bf5a8cade19adbd5418de803be"
checksum = "95357caf2640abc54651b93c98a8df4fe1ccbf44b8e601ccdf43d5c1451f29ac"
dependencies = [
"async-std",
"async-trait",
"bytes 1.0.1",
"bytes 0.5.6",
"combine",
"crc16",
"dtoa",
"futures-util",
"itoa",
"percent-encoding",
"pin-project-lite 0.2.6",
"rand 0.8.3",
"pin-project-lite 0.1.12",
"rand 0.7.3",
"sha1",
"tokio 1.6.1",
"tokio-util 0.6.7",
"tokio",
"tokio-util",
"url",
]
@ -1180,6 +1411,16 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "socket2"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e3dfc207c526015c632472a77be09cf1b6e46866581aecae5cc38fb4235dea2"
dependencies = [
"libc",
"winapi 0.3.9",
]
[[package]]
name = "strsim"
version = "0.9.3"
@ -1269,7 +1510,7 @@ dependencies = [
"lazy_static",
"libc",
"memchr",
"mio 0.6.23",
"mio",
"mio-uds",
"pin-project-lite 0.1.12",
"signal-hook-registry",
@ -1277,20 +1518,6 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "tokio"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a38d31d7831c6ed7aad00aa4c12d9375fd225a6dd77da1d25b707346319a975"
dependencies = [
"autocfg",
"bytes 1.0.1",
"libc",
"memchr",
"mio 0.7.11",
"pin-project-lite 0.2.6",
]
[[package]]
name = "tokio-util"
version = "0.3.1"
@ -1303,21 +1530,7 @@ dependencies = [
"futures-sink",
"log",
"pin-project-lite 0.1.12",
"tokio 0.2.25",
]
[[package]]
name = "tokio-util"
version = "0.6.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1caa0b0c8d94a049db56b5acf8cba99dc0623aab1b26d5b5f5e2d945846b3592"
dependencies = [
"bytes 1.0.1",
"futures-core",
"futures-sink",
"log",
"pin-project-lite 0.2.6",
"tokio 1.6.1",
"tokio",
]
[[package]]
@ -1336,7 +1549,7 @@ dependencies = [
"rand 0.7.3",
"smallvec",
"thiserror",
"tokio 0.2.25",
"tokio",
"url",
]
@ -1355,7 +1568,7 @@ dependencies = [
"resolv-conf",
"smallvec",
"thiserror",
"tokio 0.2.25",
"tokio",
"trust-dns-proto",
]
@ -1407,12 +1620,28 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "value-bag"
version = "1.0.0-alpha.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd320e1520f94261153e96f7534476ad869c14022aee1e59af7c778075d840ae"
dependencies = [
"ctor",
"version_check",
]
[[package]]
name = "version_check"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
[[package]]
name = "waker-fn"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca"
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
@ -1425,6 +1654,91 @@ version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
[[package]]
name = "wasm-bindgen"
version = "0.2.74"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d54ee1d4ed486f78874278e63e4069fc1ab9f6a18ca492076ffb90c5eb2997fd"
dependencies = [
"cfg-if 1.0.0",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.74"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b33f6a0694ccfea53d94db8b2ed1c3a8a4c86dd936b13b9f0a15ec4a451b900"
dependencies = [
"bumpalo",
"lazy_static",
"log",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fba7978c679d53ce2d0ac80c8c175840feb849a161664365d1287b41f2e67f1"
dependencies = [
"cfg-if 1.0.0",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.74"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "088169ca61430fe1e58b8096c24975251700e7b1f6fd91cc9d59b04fb9b18bd4"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.74"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be2241542ff3d9f241f5e2cb6dd09b37efe786df8851c54957683a49f0987a97"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.74"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7cff876b8f18eed75a66cf49b65e7f967cb354a7aa16003fb55dbfd25b44b4f"
[[package]]
name = "web-sys"
version = "0.3.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e828417b379f3df7111d3a2a9e5753706cae29c41f7c4029ee9fd77f3e09e582"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "wepoll-sys"
version = "3.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fcb14dea929042224824779fbc82d9fab8d2e6d3cbc0ac404de8edf489e77ff"
dependencies = [
"cc",
]
[[package]]
name = "widestring"
version = "0.4.3"

View file

@ -26,7 +26,8 @@ rand = {version = "0.8", optional = true }
pow_sha256 = { version = "0.2.1", git = "https://github.com/mcaptcha/pow_sha256", optional=true }
redis = { version = "0.20.1", features = ["tokio-comp","aio", "cluster"], optional=true }
#redis = { version = "0.20.1", features = ["tokio-comp","aio", "cluster"], optional=true }
redis = { version = "0.17.0", features = ["tokio-comp","aio", "cluster"], optional=true }
[dev-dependencies]
actix-rt = "1"

View file

@ -93,6 +93,24 @@ pub enum CaptchaError {
fmt = "Something weird happening with mCaptcha redis module. Please file bug report"
)]
MCaptchaRedisModuleError,
/// When libmcaptcha is ordered to connect to a Redis instance that doesn't have mCaptcha
/// Redis module loaded
#[display(
fmt = "You are trying to connect to a Redis instance that doesn't have mCaptcha redis module loaded.
Please see https://github.com/mCaptcha/cache for details on how to install mCaptcha redis module moudle"
)]
MCaptchaRedisModuleIsNotLoaded,
/// MCaptcha redis module is loaded but it doesn't have the necessary Redis commands.
/// Usually a version mismatch
#[display(
fmt = "The Redis instance that libmcaptcha is trying to connect to has mCaptcha Redis module loaded,
but it's probably outdated and as a result, we are not able to find all required commands to operate mCaptcha
Command {} is not found",
_0
)]
MCaptchaRediSModuleCommandNotFound(#[error(not(source))] String),
}
#[cfg(feature = "full")]

View file

@ -58,13 +58,13 @@ macro_rules! exec {
}
impl RedisConnection {
pub async fn is_module_loaded(&self) {
pub async fn is_module_loaded(&self) -> CaptchaResult<()> {
let modules: Vec<Vec<String>> = exec!(redis::cmd("MODULE").arg(&["LIST"]), &self).unwrap();
for list in modules.iter() {
match list.iter().find(|module| module.as_str() == MODULE_NAME) {
Some(_) => println!("module exists"),
None => println!("Module doesn't exist"),
Some(_) => (),
None => return Err(CaptchaError::MCaptchaRedisModuleIsNotLoaded),
}
}
@ -73,16 +73,21 @@ impl RedisConnection {
for cmd in commands.iter() {
match exec!(redis::cmd("COMMAND").arg(&["INFO", cmd]), &self).unwrap() {
Value::Bulk(mut val) => {
let x = val.pop();
match x {
Some(Value::Nil) => println!("Command: {} doesn't exist", &cmd),
_ => println!("commands {} exists", &cmd),
match val.pop() {
Some(Value::Nil) => {
return Err(CaptchaError::MCaptchaRediSModuleCommandNotFound(
cmd.to_string(),
))
}
_ => (),
};
}
_ => println!("commands exists"),
_ => (),
};
}
Ok(())
}
pub async fn add_visitor(&self, msg: AddVisitor) -> CaptchaResult<Option<AddVisitorResult>> {
@ -125,3 +130,57 @@ impl RedisConnection {
Ok(visitors)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::defense::{Level, LevelBuilder};
use crate::master::embedded::counter::tests::get_mcaptcha;
use crate::master::redis::master::{Master, Redis};
async fn connect(redis: &Redis) -> RedisConnection {
match &redis {
Redis::Single(c) => {
let con = c.get_async_connection().await.unwrap();
RedisConnection::Single(Rc::new(RefCell::new(con)))
}
Redis::Cluster(c) => {
let con = c.get_connection().unwrap();
RedisConnection::Cluster(Rc::new(RefCell::new(con)))
}
}
}
const CAPTCHA_NAME: &str = "REDIS_CAPTCHA_TEST";
const DURATION: usize = 10;
#[actix_rt::test]
async fn redis_master_works() {
let client = redis::Client::open("redis://127.0.0.1/").unwrap();
let r = connect(&Redis::Single(client)).await;
{
let _ = r.delete_captcha(CAPTCHA_NAME).await;
}
assert!(r.is_module_loaded().await.is_ok());
assert!(!r.check_captcha_exists(CAPTCHA_NAME).await.unwrap());
let add_mcaptcha_msg = AddSite {
id: CAPTCHA_NAME.into(),
mcaptcha: get_mcaptcha(),
};
assert!(r.add_mcaptcha(add_mcaptcha_msg).await.is_ok());
assert!(r.check_captcha_exists(CAPTCHA_NAME).await.unwrap());
let add_visitor_msg = AddVisitor(CAPTCHA_NAME.into());
assert!(r.add_visitor(add_visitor_msg).await.is_ok());
let visitors = r.get_visitors(CAPTCHA_NAME).await.unwrap();
assert_eq!(visitors, 1);
let add_visitor_msg = AddVisitor(CAPTCHA_NAME.into());
assert!(r.add_visitor(add_visitor_msg).await.is_ok());
let visitors = r.get_visitors(CAPTCHA_NAME).await.unwrap();
assert_eq!(visitors, 2);
assert!(r.delete_captcha(CAPTCHA_NAME).await.is_ok());
}
}

View file

@ -50,9 +50,9 @@ pub struct Master {
}
impl Master {
async fn new(redis: Redis) -> Self {
pub async fn new(redis: Redis) -> Self {
let con = Self::connect(&redis).await;
con.is_module_loaded().await;
con.is_module_loaded().await.unwrap();
let con = Rc::new(con);
let master = Self { redis, con };
master