Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tests): add 10ms delay after we are done with stubr #408

Merged
merged 3 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,4 @@ sha2 = "0.10"
stubr = "0.6"
stubr-attributes = "0.6"
assert-json-diff = "2"
tokio = { version = "1", features = ["io-util"] }
7 changes: 2 additions & 5 deletions tests/api/ai_explain.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::helpers::app::test_app_with_login;
use crate::helpers::app::{drop_stubr, test_app_with_login};
use crate::helpers::db::{get_pool, reset};
use crate::helpers::wait_for_stubr;
use actix_web::test;
use anyhow::Error;
use diesel::{QueryDsl, RunQueryDsl};
Expand Down Expand Up @@ -49,7 +48,6 @@ fn add_explain_cache() -> Result<(), Error> {
async fn test_explain() -> Result<(), Error> {
let pool = reset()?;
add_explain_cache()?;
wait_for_stubr().await?;
let app = test_app_with_login(&pool).await.unwrap();
let service = test::init_service(app).await;
let request = test::TestRequest::post()
Expand Down Expand Up @@ -123,7 +121,6 @@ async fn test_explain() -> Result<(), Error> {
.first(&mut conn)?;
assert_eq!(row.thumbs_up, 1);
assert_eq!(row.thumbs_down, 1);

drop(stubr);
drop_stubr(stubr).await;
Ok(())
}
6 changes: 3 additions & 3 deletions tests/api/ai_help.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::time::Duration;

use crate::helpers::api_assertions::assert_ok_with_json_containing;
use crate::helpers::app::init_test;
use crate::helpers::app::{drop_stubr, init_test};
use actix_http::StatusCode;
use actix_rt::time::sleep;
use anyhow::Error;
Expand Down Expand Up @@ -58,7 +58,7 @@ async fn test_quota() -> Result<(), Error> {
)
.await;
assert_eq!(ai_help.status(), StatusCode::PAYMENT_REQUIRED);
drop(stubr);
drop_stubr(stubr).await;
Ok(())
}

Expand Down Expand Up @@ -142,6 +142,6 @@ async fn test_quota_rest() -> Result<(), Error> {
assert_eq!(ai_help.status(), StatusCode::NOT_IMPLEMENTED);
let quota = client.get("/api/v1/plus/ai/ask/quota", None).await;
assert_ok_with_json_containing(quota, json!({"quota": { "count": 1, "limit": 5}})).await;
drop(stubr);
drop_stubr(stubr).await;
Ok(())
}
7 changes: 2 additions & 5 deletions tests/api/ai_help_history.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::helpers::app::test_app_with_login;
use crate::helpers::app::{drop_stubr, test_app_with_login};
use crate::helpers::db::{get_pool, reset};
use crate::helpers::http_client::TestHttpClient;
use crate::helpers::wait_for_stubr;
use actix_web::test;
use anyhow::Error;
use async_openai::types::ChatCompletionRequestMessage;
Expand Down Expand Up @@ -64,7 +63,6 @@ fn add_history_log() -> Result<(), Error> {
#[stubr::mock(port = 4321)]
async fn test_history() -> Result<(), Error> {
let pool = reset()?;
wait_for_stubr().await?;
let app = test_app_with_login(&pool).await.unwrap();
let service = test::init_service(app).await;
let mut logged_in_client = TestHttpClient::new(service).await;
Expand Down Expand Up @@ -93,8 +91,7 @@ async fn test_history() -> Result<(), Error> {
test::read_body(history).await.as_ref()
))
);

drop(stubr);
drop_stubr(stubr).await;
Ok(())
}

Expand Down
8 changes: 5 additions & 3 deletions tests/api/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use actix_web::test;
use anyhow::Error;
use url::Url;

use crate::helpers::{app::test_app_with_login, db::reset, http_client::check_stubr_initialized};
use crate::helpers::{
app::{drop_stubr, test_app_with_login},
db::reset,
};

#[actix_rt::test]
#[stubr::mock(port = 4321)]
async fn test_next() -> Result<(), Error> {
let pool = reset()?;
let _stubr_ok = check_stubr_initialized().await;

let app = test_app_with_login(&pool).await?;
let service = test::init_service(app).await;

Expand Down Expand Up @@ -53,5 +54,6 @@ async fn test_next() -> Result<(), Error> {
.and_then(|l| l.to_str().ok()),
Some("http://localhost:8000/foo")
);
drop_stubr(stubr).await;
Ok(())
}
51 changes: 25 additions & 26 deletions tests/api/fxa_webhooks.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::thread;
use std::time::Duration;

use crate::helpers::app::drop_stubr;
use crate::helpers::app::test_app_with_login;
use crate::helpers::db::reset;
use crate::helpers::http_client::PostPayload;
use crate::helpers::http_client::TestHttpClient;
use crate::helpers::read_json;
use crate::helpers::wait_for_stubr;
use actix_http::StatusCode;
use actix_web::test;
use anyhow::anyhow;
Expand All @@ -19,6 +19,7 @@ use rumba::db::types::FxaEventStatus;
use rumba::db::Pool;
use serde_json::json;
use stubr::{Config, Stubr};
use tokio::time::sleep;

const TEN_MS: std::time::Duration = Duration::from_millis(10);

Expand Down Expand Up @@ -68,7 +69,6 @@ async fn subscription_state_change_to_10m_test() -> Result<(), Error> {
let set_token =
include_str!("../data/set_tokens/set_token_subscription_state_change_to_10m.txt");
let pool = reset()?;
wait_for_stubr().await?;
let app = test_app_with_login(&pool).await?;
let service = test::init_service(app).await;
let mut logged_in_client = TestHttpClient::new(service).await;
Expand Down Expand Up @@ -111,8 +111,7 @@ async fn subscription_state_change_to_10m_test() -> Result<(), Error> {
FxaEvent::SubscriptionStateChange,
FxaEventStatus::Ignored,
)?;

drop(stubr);
drop_stubr(stubr).await;
Ok(())
}

Expand All @@ -121,20 +120,23 @@ async fn subscription_state_change_to_10m_test() -> Result<(), Error> {
async fn subscription_state_change_to_core_test_empty_subscription() -> Result<(), Error> {
let set_token =
include_str!("../data/set_tokens/set_token_subscription_state_change_to_core.txt");
subscription_state_change_to_core_test(set_token).await
subscription_state_change_to_core_test(set_token).await?;
drop_stubr(stubr).await;
Ok(())
}

#[actix_rt::test]
#[stubr::mock(port = 4321)]
async fn subscription_state_change_to_core_test_inactive() -> Result<(), Error> {
let set_token =
include_str!("../data/set_tokens/set_token_subscription_state_change_to_core_inactive.txt");
subscription_state_change_to_core_test(set_token).await
subscription_state_change_to_core_test(set_token).await?;
drop_stubr(stubr).await;
Ok(())
}

async fn subscription_state_change_to_core_test(set_token: &str) -> Result<(), Error> {
let pool = reset()?;
wait_for_stubr().await?;
let app = test_app_with_login(&pool).await?;
let service = test::init_service(app).await;
let mut logged_in_client = TestHttpClient::new(service).await;
Expand Down Expand Up @@ -167,14 +169,11 @@ async fn subscription_state_change_to_core_test(set_token: &str) -> Result<(), E
FxaEvent::SubscriptionStateChange,
FxaEventStatus::Processed,
)?;

Ok(())
}

#[actix_rt::test]
async fn delete_user_test() -> Result<(), Error> {
let set_token = include_str!("../data/set_tokens/set_token_delete_user.txt");
let pool = reset()?;
let stubr = Stubr::start_blocking_with(
vec!["tests/stubs", "tests/test_specific_stubs/collections"],
Config {
Expand All @@ -185,7 +184,8 @@ async fn delete_user_test() -> Result<(), Error> {
verify: false,
},
);
wait_for_stubr().await?;
let set_token = include_str!("../data/set_tokens/set_token_delete_user.txt");
let pool = reset()?;

let app = test_app_with_login(&pool).await?;
let service = test::init_service(app).await;
Expand Down Expand Up @@ -248,8 +248,7 @@ async fn delete_user_test() -> Result<(), Error> {
FxaEvent::DeleteUser,
FxaEventStatus::Processed,
)?;

drop(stubr);
drop_stubr(stubr).await;
Ok(())
}

Expand All @@ -258,7 +257,6 @@ async fn delete_user_test() -> Result<(), Error> {
async fn invalid_set_test() -> Result<(), Error> {
let set_token = include_str!("../data/set_tokens/set_token_delete_user_invalid.txt");
let pool = reset()?;
wait_for_stubr().await?;
let app = test_app_with_login(&pool).await?;
let service = test::init_service(app).await;
let mut logged_in_client = TestHttpClient::new(service).await;
Expand All @@ -280,12 +278,12 @@ async fn invalid_set_test() -> Result<(), Error> {
.select(schema::raw_webhook_events_tokens::token)
.first::<String>(&mut conn)?;
assert_eq!(failed_token, set_token);
drop(stubr);
drop_stubr(stubr).await;
Ok(())
}

#[actix_rt::test]
async fn change_profile_test() -> Result<(), Error> {
async fn whoami_test() -> Result<(), Error> {
let stubr = Stubr::start_blocking_with(
vec!["tests/stubs"],
Config {
Expand All @@ -296,22 +294,22 @@ async fn change_profile_test() -> Result<(), Error> {
verify: false,
},
);
wait_for_stubr().await?;

let set_token = include_str!("../data/set_tokens/set_token_profile_change.txt");
let pool = reset()?;
let app = test_app_with_login(&pool).await?;
let service = test::init_service(app).await;
let mut logged_in_client = TestHttpClient::new(service).await;
let res = logged_in_client.trigger_webhook(set_token).await;
assert!(res.response().status().is_success());

let whoami = logged_in_client
.get("/api/v1/whoami", Some(vec![("X-Appengine-Country", "IS")]))
.await;
assert!(whoami.response().status().is_success());
let json = read_json(whoami).await;
assert_eq!(json["username"], "TEST_SUB");
assert_eq!(json["email"], "[email protected]");

drop(stubr);
drop_stubr(stubr).await;

let stubr = Stubr::start_blocking_with(
vec!["tests/stubs", "tests/test_specific_stubs/fxa_webhooks"],
Expand All @@ -323,9 +321,11 @@ async fn change_profile_test() -> Result<(), Error> {
verify: false,
},
);
wait_for_stubr().await?;

thread::sleep(TEN_MS);
let set_token = include_str!("../data/set_tokens/set_token_profile_change.txt");
let pool = reset()?;
let app = test_app_with_login(&pool).await?;
let service = test::init_service(app).await;
let mut logged_in_client = TestHttpClient::new(service).await;

let res = logged_in_client.trigger_webhook(set_token).await;
assert!(res.response().status().is_success());
Expand All @@ -341,7 +341,7 @@ async fn change_profile_test() -> Result<(), Error> {
if json["email"] == "[email protected]" {
break;
}
thread::sleep(TEN_MS);
sleep(TEN_MS).await;
tries -= 1;
}

Expand All @@ -355,7 +355,6 @@ async fn change_profile_test() -> Result<(), Error> {
FxaEvent::ProfileChange,
FxaEventStatus::Processed,
)?;

drop(stubr);
drop_stubr(stubr).await;
Ok(())
}
Loading
Loading