Skip to content

Commit

Permalink
fix(tests): switch to test-log crate and use dedicated mock server …
Browse files Browse the repository at this point in the history
…paths for scheduler tests
  • Loading branch information
azasypkin committed Jan 25, 2025
1 parent 572f453 commit beabe98
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 51 deletions.
66 changes: 44 additions & 22 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ uuid = "1.12.1"
[dev-dependencies]
httpmock = "0.7.0"
insta = "1.42.0"
test-log = "0.2.17"
toml = "0.8.19"
tracing-test = "0.2.5"

[features]
default = [
Expand Down
60 changes: 32 additions & 28 deletions src/scheduler/scheduler_jobs/trackers_run_job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,8 @@ mod tests {
use serde_json::json;
use sqlx::PgPool;
use std::{ops::Add, sync::Arc, time::Duration};
use test_log::test;
use time::OffsetDateTime;
use tracing_test::traced_test;
use uuid::uuid;

#[sqlx::test]
Expand Down Expand Up @@ -827,7 +827,7 @@ mod tests {
Ok(())
}

#[sqlx::test]
#[test(sqlx::test)]
async fn when_run_removes_job_if_does_not_reference_tracker(
pool: PgPool,
) -> anyhow::Result<()> {
Expand All @@ -854,7 +854,7 @@ mod tests {
Ok(())
}

#[sqlx::test]
#[test(sqlx::test)]
async fn when_run_removes_job_if_tracker_does_not_support_tracking(
pool: PgPool,
) -> anyhow::Result<()> {
Expand Down Expand Up @@ -892,7 +892,7 @@ mod tests {
Ok(())
}

#[sqlx::test]
#[test(sqlx::test)]
async fn when_run_removes_retry_job_if_tracker_does_not_support_retries(
pool: PgPool,
) -> anyhow::Result<()> {
Expand Down Expand Up @@ -938,7 +938,7 @@ mod tests {
Ok(())
}

#[sqlx::test]
#[test(sqlx::test)]
async fn when_run_removes_job_if_tracker_does_not_support_revisions(
pool: PgPool,
) -> anyhow::Result<()> {
Expand Down Expand Up @@ -984,7 +984,7 @@ mod tests {
Ok(())
}

#[sqlx::test]
#[test(sqlx::test)]
async fn when_run_removes_job_if_tracker_is_disabled(pool: PgPool) -> anyhow::Result<()> {
let mut scheduler = mock_scheduler(&pool).await?;
let api = Arc::new(mock_api(pool).await?);
Expand Down Expand Up @@ -1024,7 +1024,7 @@ mod tests {
Ok(())
}

#[sqlx::test]
#[test(sqlx::test)]
async fn can_run(pool: PgPool) -> anyhow::Result<()> {
let mut scheduler = mock_scheduler(&pool).await?;
let api = Arc::new(mock_api(pool).await?);
Expand All @@ -1034,10 +1034,10 @@ mod tests {
let trackers = api.trackers();
let tracker = trackers
.create_tracker(
TrackerCreateParamsBuilder::new("tracker")
TrackerCreateParamsBuilder::new("tracker-normal-job")
.with_schedule("0 0 * * * *")
.with_target(TrackerTarget::Api(ApiTarget {
requests: vec![TargetRequest::new(server.url("/api").parse()?)],
requests: vec![TargetRequest::new(server.url("/api-normal-job").parse()?)],
configurator: None,
extractor: None,
}))
Expand All @@ -1058,7 +1058,7 @@ mod tests {
// Create server mock.
let content = TrackerDataValue::new(json!("some-content"));
let content_mock = server.mock(|when, then| {
when.method(httpmock::Method::GET).path("/api");
when.method(httpmock::Method::GET).path("/api-normal-job");
then.status(200)
.header("Content-Type", "application/json")
.json_body_obj(content.value())
Expand Down Expand Up @@ -1125,16 +1125,16 @@ mod tests {
Ok(())
}

#[sqlx::test]
#[test(sqlx::test)]
async fn can_run_retry_job(pool: PgPool) -> anyhow::Result<()> {
let mut scheduler = mock_scheduler(&pool).await?;
let api = Arc::new(mock_api(pool).await?);
let server = MockServer::start();

// Create tracker with retry strategy.
let mut create_params = TrackerCreateParamsBuilder::new("tracker")
let mut create_params = TrackerCreateParamsBuilder::new("tracker-retry-job")
.with_target(TrackerTarget::Api(ApiTarget {
requests: vec![TargetRequest::new(server.url("/api").parse()?)],
requests: vec![TargetRequest::new(server.url("/api-retry-job").parse()?)],
configurator: None,
extractor: None,
}))
Expand Down Expand Up @@ -1169,7 +1169,7 @@ mod tests {
// Create server mock.
let content = TrackerDataValue::new(json!("some-content"));
let content_mock = server.mock(|when, then| {
when.method(httpmock::Method::GET).path("/api");
when.method(httpmock::Method::GET).path("/api-retry-job");
then.status(200)
.header("Content-Type", "application/json")
.json_body_obj(content.value())
Expand Down Expand Up @@ -1219,7 +1219,7 @@ mod tests {
Ok(())
}

#[sqlx::test]
#[test(sqlx::test)]
async fn resets_job_and_report_error_if_job_fails(pool: PgPool) -> anyhow::Result<()> {
let mut config = mock_config()?;
config.smtp = config.smtp.map(|config| SmtpConfig {
Expand All @@ -1238,10 +1238,10 @@ mod tests {
let trackers = api.trackers();
let tracker = trackers
.create_tracker(
TrackerCreateParamsBuilder::new("tracker")
TrackerCreateParamsBuilder::new("tracker-failed-job")
.with_schedule("0 0 * * * *")
.with_target(TrackerTarget::Api(ApiTarget {
requests: vec![TargetRequest::new(server.url("/api").parse()?)],
requests: vec![TargetRequest::new(server.url("/api-failed-job").parse()?)],
configurator: None,
extractor: None,
}))
Expand All @@ -1261,10 +1261,10 @@ mod tests {

// Create server mock.
let content_mock = server.mock(|when, then| {
when.method(httpmock::Method::GET).path("/api");
when.method(httpmock::Method::GET).path("/api-failed-job");
then.status(400)
.header("Content-Type", "application/json")
.body("Uh oh!")
.body("Uh oh (failed-job)!")
.delay(Duration::from_secs(2));
});

Expand Down Expand Up @@ -1337,15 +1337,17 @@ mod tests {
content: EmailContent::Template(EmailTemplate::TrackerCheckResult {
tracker_id: tracker.id,
tracker_name: tracker.name,
result: Err("Failed to execute API target request (0): Uh oh!".to_string())
result: Err(
"Failed to execute API target request (0): Uh oh (failed-job)!".to_string()
)
})
})
);

Ok(())
}

#[sqlx::test]
#[test(sqlx::test)]
async fn removes_job_and_report_error_if_last_retry_fails(pool: PgPool) -> anyhow::Result<()> {
let mut config = mock_config()?;
config.smtp = config.smtp.map(|config| SmtpConfig {
Expand All @@ -1361,9 +1363,9 @@ mod tests {
let server = MockServer::start();

// Create tracker with retry strategy.
let mut create_params = TrackerCreateParamsBuilder::new("tracker")
let mut create_params = TrackerCreateParamsBuilder::new("tracker-failed-retry")
.with_target(TrackerTarget::Api(ApiTarget {
requests: vec![TargetRequest::new(server.url("/api").parse()?)],
requests: vec![TargetRequest::new(server.url("/api-failed-retry").parse()?)],
configurator: None,
extractor: None,
}))
Expand Down Expand Up @@ -1395,10 +1397,10 @@ mod tests {

// Create server mock.
let content_mock = server.mock(|when, then| {
when.method(httpmock::Method::GET).path("/api");
when.method(httpmock::Method::GET).path("/api-failed-retry");
then.status(400)
.header("Content-Type", "application/json")
.body("Uh oh!")
.body("Uh oh (failed retry)!")
.delay(Duration::from_secs(2));
});

Expand Down Expand Up @@ -1460,16 +1462,18 @@ mod tests {
content: EmailContent::Template(EmailTemplate::TrackerCheckResult {
tracker_id: tracker.id,
tracker_name: tracker.name,
result: Err("Failed to execute API target request (0): Uh oh!".to_string())
result: Err(
"Failed to execute API target request (0): Uh oh (failed retry)!"
.to_string()
)
})
})
);

Ok(())
}

#[sqlx::test]
#[traced_test]
#[test(sqlx::test)]
async fn can_schedule_retries_if_request_fails(pool: PgPool) -> anyhow::Result<()> {
let mut config = mock_config()?;
config.smtp = config.smtp.map(|config| SmtpConfig {
Expand Down

0 comments on commit beabe98

Please sign in to comment.