mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2026-02-11 10:05:41 +00:00
fix and chore: refactor tests to minimize initializing DB connections
SUMMARY
The test suite was spinning up way too many database connections that what's
strictly needed and so the test suite was failing with[0]:
code: "53300", message: "sorry, too many clients already"
EXPERIMENTS
Tried sharing database connection pool across all tests with
async_once[0] but faced:
- IO errors
The connections were probably getting dropped in between tests
- actix Actor errors
The actor was probably not getting initialized before a
a reference to the async_once initialized app
context(crate::data::Data) is retrieved and used
FIX
crate::tests was spinning up an App context
instance(crate::data::Data) for most utility functions, which was
unnecessarily excessive.
Each test now creates an instance of the application context at the
beginning and shared a reference with all test utility functions. So
number of database connections/app context instance = number of unit
tests.
[0]: permanently fixes #22
[1]: https://docs.rs/async_once/latest/async_once/
This commit is contained in:
@@ -216,7 +216,7 @@ async fn update(
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
pub mod tests {
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::test;
|
||||
use actix_web::web::Bytes;
|
||||
@@ -227,7 +227,6 @@ mod tests {
|
||||
use crate::tests::*;
|
||||
use crate::*;
|
||||
|
||||
#[cfg(test)]
|
||||
mod isoloated_test {
|
||||
use super::{LevelBuilder, TrafficPattern};
|
||||
|
||||
@@ -303,18 +302,17 @@ mod tests {
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn easy_works() {
|
||||
pub async fn easy_works() {
|
||||
const NAME: &str = "defaultuserconfgworks";
|
||||
const PASSWORD: &str = "longpassworddomain";
|
||||
const EMAIL: &str = "defaultuserconfgworks@a.com";
|
||||
let data = crate::data::Data::new().await;
|
||||
let data = &data;
|
||||
|
||||
{
|
||||
let data = Data::new().await;
|
||||
delete_user(NAME, &data).await;
|
||||
}
|
||||
delete_user(data, NAME).await;
|
||||
|
||||
let (data, _creds, signin_resp) =
|
||||
register_and_signin(NAME, EMAIL, PASSWORD).await;
|
||||
let (_creds, signin_resp) =
|
||||
register_and_signin(data, NAME, EMAIL, PASSWORD).await;
|
||||
let cookies = get_cookie!(signin_resp);
|
||||
let app = get_app!(data).await;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user