Skip to content

Commit

Permalink
Remove alert testing pending resolution of #97
Browse files Browse the repository at this point in the history
  • Loading branch information
deciduously committed Feb 10, 2022
1 parent 37de848 commit 48d1507
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 748 deletions.
237 changes: 0 additions & 237 deletions crates/app/core/alert_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,240 +412,3 @@ impl App {
Ok(())
}
}

#[cfg(test)]
mod test {
use super::*;
use crate::{
alert::{AlertMethod, AlertMethodWebhook, AlertMetric},
monitor::{MonitorCadence, MonitorThreshold, MonitorThresholdMode},
monitor_checker::MonitorConfig,
test_common::*,
};
use tracing_test::traced_test;

async fn get_total_sends_with_status(
txn: &mut sqlx::Transaction<'_, sqlx::Any>,
status: AlertSendStatus,
) -> Result<i64> {
let result = sqlx::query(
"
select
count (*)
from
alert_sends
where
status = $1
",
)
.bind(u8::from(status) as i64)
.fetch_one(txn.borrow_mut())
.await?
.get(0);
Ok(result)
}

#[tokio::test]
#[traced_test]
async fn test_alert_email_send() {
let mut ctx = TestContext::new("email_send").await;
// Seed app, generate an alert, assert the email is sent
let app = init_test_app(init_test_options_postgres("email_send"))
.await
.unwrap();
app.clock().resume();
let model_id = init_heart_disease_model(&app).await.unwrap();
seed_monitor_event_pair(&app, model_id, true).await.unwrap();
let test_monitor = MonitorConfig {
cadence: MonitorCadence::Hourly,
threshold: MonitorThreshold {
metric: AlertMetric::Accuracy,
mode: MonitorThresholdMode::Absolute,
difference_lower: Some(0.05),
difference_upper: Some(0.05),
},
title: None,
methods: vec![AlertMethod::Email("ben@tangram.dev".to_owned().into())],
};
seed_single_monitor(&app, &test_monitor, model_id)
.await
.unwrap();
seed_events(&app, 100, model_id).await.unwrap();
// Scroll to allow monitor_checker to write alert
app.clock().pause();
app.clock()
.advance(std::time::Duration::from_secs(60 * 60))
.await;
app.clock().resume();
app.sync_tasks().await.unwrap();

// Scroll to allow alert_sender to pick up alert
app.clock().pause();
app.clock()
.advance(std::time::Duration::from_secs(10))
.await;
app.clock().resume();
app.sync_tasks().await.unwrap();

// Assert exactly one success has been logged.
let mut txn = app.begin_transaction().await.unwrap();
let num_successes =
get_total_sends_with_status(txn.borrow_mut(), AlertSendStatus::Succeeded)
.await
.unwrap();
assert_eq!(num_successes, 1);

// Assert there are no unset, sending, or failed
let num_unsent = get_total_sends_with_status(txn.borrow_mut(), AlertSendStatus::Unsent)
.await
.unwrap();
assert_eq!(num_unsent, 0);
let num_sending = get_total_sends_with_status(txn.borrow_mut(), AlertSendStatus::Sending)
.await
.unwrap();
assert_eq!(num_sending, 0);
let num_failed = get_total_sends_with_status(txn.borrow_mut(), AlertSendStatus::Failed)
.await
.unwrap();
assert_eq!(num_failed, 0);
app.commit_transaction(txn).await.unwrap();
ctx.drop().await;
}

#[tokio::test]
#[traced_test]
async fn test_alert_webhook_send() {
let mut ctx = TestContext::new("webhook_send").await;
// Seed app, generate an alert, assert the email is sent
let app = init_test_app(init_test_options_postgres("webhook_send"))
.await
.unwrap();
app.clock().resume();
let model_id = init_heart_disease_model(&app).await.unwrap();
seed_monitor_event_pair(&app, model_id, true).await.unwrap();
let test_monitor = MonitorConfig {
cadence: MonitorCadence::Hourly,
threshold: MonitorThreshold {
metric: AlertMetric::Accuracy,
mode: MonitorThresholdMode::Absolute,
difference_lower: Some(0.05),
difference_upper: Some(0.05),
},
title: None,
methods: vec![AlertMethod::Webhook(
AlertMethodWebhook::try_from("http://0.0.0.0:8085/webhook".to_owned()).unwrap(),
)],
};
seed_single_monitor(&app, &test_monitor, model_id)
.await
.unwrap();
seed_events(&app, 100, model_id).await.unwrap();
app.clock().pause();
app.clock()
.advance(std::time::Duration::from_secs(60 * 60))
.await;
app.clock().resume();
app.sync_tasks().await.unwrap();
// Scroll to allow alert_sender to pick up alert
app.clock().pause();
app.clock()
.advance(std::time::Duration::from_secs(10))
.await;
app.clock().resume();
app.sync_tasks().await.unwrap();

// Assert exactly one success has been logged.
let mut txn = app.begin_transaction().await.unwrap();
let num_successes =
get_total_sends_with_status(txn.borrow_mut(), AlertSendStatus::Succeeded)
.await
.unwrap();
assert_eq!(num_successes, 1);

// Assert there are no unset, sending, or failed
let num_unsent = get_total_sends_with_status(txn.borrow_mut(), AlertSendStatus::Unsent)
.await
.unwrap();
assert_eq!(num_unsent, 0);
let num_sending = get_total_sends_with_status(txn.borrow_mut(), AlertSendStatus::Sending)
.await
.unwrap();
assert_eq!(num_sending, 0);
let num_failed = get_total_sends_with_status(txn.borrow_mut(), AlertSendStatus::Failed)
.await
.unwrap();
assert_eq!(num_failed, 0);
app.commit_transaction(txn).await.unwrap();
ctx.drop().await;
}

#[tokio::test]
#[traced_test]
async fn test_alert_multiple_methods_send() {
let mut ctx = TestContext::new("multiple_methods_send").await;
// Seed app, generate an alert, assert the email is sent
let app = init_test_app(init_test_options_postgres("multiple_methods_send"))
.await
.unwrap();
app.clock().resume();
let model_id = init_heart_disease_model(&app).await.unwrap();
seed_monitor_event_pair(&app, model_id, true).await.unwrap();
let test_monitor = MonitorConfig {
cadence: MonitorCadence::Hourly,
threshold: MonitorThreshold {
metric: AlertMetric::Accuracy,
mode: MonitorThresholdMode::Absolute,
difference_lower: Some(0.05),
difference_upper: Some(0.05),
},
title: None,
methods: vec![
AlertMethod::Email("ben@tangram.dev".to_owned().into()),
AlertMethod::Webhook(
AlertMethodWebhook::try_from("http://0.0.0.0:8085/webhook".to_owned()).unwrap(),
),
],
};
seed_single_monitor(&app, &test_monitor, model_id)
.await
.unwrap();
seed_events(&app, 100, model_id).await.unwrap();
app.clock().pause();
app.clock()
.advance(std::time::Duration::from_secs(60 * 60))
.await;
app.clock().resume();
app.sync_tasks().await.unwrap();
// Scroll to allow alert_sender to pick up alert
app.clock().pause();
app.clock()
.advance(std::time::Duration::from_secs(10))
.await;
app.clock().resume();
app.sync_tasks().await.unwrap();

// Assert exactly two successes have been logged.
let mut txn = app.begin_transaction().await.unwrap();
let num_successes =
get_total_sends_with_status(txn.borrow_mut(), AlertSendStatus::Succeeded)
.await
.unwrap();
assert_eq!(num_successes, 2);

// Assert there are no unset, sending, or failed
let num_unsent = get_total_sends_with_status(txn.borrow_mut(), AlertSendStatus::Unsent)
.await
.unwrap();
assert_eq!(num_unsent, 0);
let num_sending = get_total_sends_with_status(txn.borrow_mut(), AlertSendStatus::Sending)
.await
.unwrap();
assert_eq!(num_sending, 0);
let num_failed = get_total_sends_with_status(txn.borrow_mut(), AlertSendStatus::Failed)
.await
.unwrap();
assert_eq!(num_failed, 0);
app.commit_transaction(txn).await.unwrap();
ctx.drop().await;
}
}
Loading

0 comments on commit 48d1507

Please sign in to comment.