Skip to content
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
137 changes: 137 additions & 0 deletions llvm_buildbot_monitor/src/calendar_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,4 +577,141 @@ mod test {
baseline_time + Duration::from_secs(9 * 60) - PRE_PING_STATE_REFRESH_TIME,
);
}

#[test]
fn test_calculate_ping_time_subtracts_lead() {
let baseline_time = arbitrary_time();
let make_event = |ping_duration_before_start_mins: u32| CommunityEvent {
start_time: baseline_time,
end_time: baseline_time + Duration::from_secs(3600),
id: "e".into(),
description_data: calendar_check::CommunityEventDescriptionData {
ping_duration_before_start_mins,
..Default::default()
},
..Default::default()
};

// Non-zero lead: ping time is shifted before start.
assert_eq!(
calculate_ping_time(&make_event(10)),
baseline_time - Duration::from_secs(10 * 60),
);

// Zero lead: ping time equals start time.
assert_eq!(calculate_ping_time(&make_event(0)), baseline_time);
}

#[test]
fn test_calculate_event_ping_data_empty_events() {
let baseline_time = arbitrary_time();
let state = State {
events: Vec::new(),
refreshed_at: baseline_time,
};
let (to_ping, next_ping) =
calculate_event_ping_data(&state, &baseline_time, &Default::default());
assert!(to_ping.is_empty(), "{to_ping:?}");
assert_eq!(next_ping, None);
}

#[test]
fn test_calculate_event_ping_data_all_due_none_remaining() {
let baseline_time = arbitrary_time();
// Events start 1s and 2s after `now`, but their ping windows (5 min lead)
// have already passed, so both should be in `ping_now_indices` and no
// future ping should be scheduled.
let now = baseline_time + Duration::from_secs(10 * 60);
let state = State {
events: vec![
CommunityEvent {
start_time: now + Duration::from_secs(1),
end_time: now + Duration::from_secs(3600),
id: "a".into(),
description_data: calendar_check::CommunityEventDescriptionData {
ping_duration_before_start_mins: 5,
..Default::default()
},
..Default::default()
},
CommunityEvent {
start_time: now + Duration::from_secs(2),
end_time: now + Duration::from_secs(3600),
id: "b".into(),
description_data: calendar_check::CommunityEventDescriptionData {
ping_duration_before_start_mins: 5,
..Default::default()
},
..Default::default()
},
],
refreshed_at: baseline_time,
};
let (to_ping, next_ping) = calculate_event_ping_data(&state, &now, &Default::default());
assert_eq!(&to_ping, &[0, 1]);
assert_eq!(next_ping, None);
}

#[test]
fn test_calculate_event_ping_data_nonzero_lead_shifts_nearest_ping() {
let baseline_time = arbitrary_time();
let state = State {
events: vec![
CommunityEvent {
// ping_time = baseline + 5min (20min start - 15min lead)
start_time: baseline_time + Duration::from_secs(20 * 60),
end_time: baseline_time + Duration::from_secs(3600),
id: "in-20-min".into(),
description_data: calendar_check::CommunityEventDescriptionData {
ping_duration_before_start_mins: 15,
..Default::default()
},
..Default::default()
},
CommunityEvent {
// ping_time = baseline + 25min (30min start - 5min lead)
start_time: baseline_time + Duration::from_secs(30 * 60),
end_time: baseline_time + Duration::from_secs(3600),
id: "in-30-min".into(),
description_data: calendar_check::CommunityEventDescriptionData {
ping_duration_before_start_mins: 5,
..Default::default()
},
..Default::default()
},
],
refreshed_at: baseline_time,
};

let now = baseline_time;
let (to_ping, next_ping) = calculate_event_ping_data(&state, &now, &Default::default());
assert!(to_ping.is_empty(), "{to_ping:?}");
// The nearest ping is the one shifted the most by a large lead time.
assert_eq!(next_ping, Some(baseline_time + Duration::from_secs(5 * 60)),);
}

#[test]
fn test_state_refresh_cadence_wins_over_distant_pre_ping_refresh() {
let baseline_time = arbitrary_time();
// Event is 90 minutes away with a 1-minute ping lead:
// ping_time = baseline + 89min
// pre-ping refresh_at = baseline + 87min (89min - PRE_PING_STATE_REFRESH_TIME=2min)
// 87min > 60min hourly cadence, so the cadence should win.
let state = State {
events: vec![CommunityEvent {
start_time: baseline_time + Duration::from_secs(90 * 60),
end_time: baseline_time + Duration::from_secs(120 * 60),
description_data: calendar_check::CommunityEventDescriptionData {
ping_duration_before_start_mins: 1,
..Default::default()
},
..Default::default()
}],
refreshed_at: baseline_time,
};
assert_eq!(
calculate_next_refresh_time(&state),
baseline_time + Duration::from_secs(60 * 60),
);
}
}
65 changes: 65 additions & 0 deletions llvm_buildbot_monitor/src/discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1876,6 +1876,71 @@ mod test {
);
}

#[test]
fn test_duration_to_shorthand() {
// Sub-minute
assert_eq!(
duration_to_shorthand(chrono::Duration::seconds(0)),
"<1 minute"
);
assert_eq!(
duration_to_shorthand(chrono::Duration::seconds(59)),
"<1 minute"
);

// Minutes - boundary
assert_eq!(
duration_to_shorthand(chrono::Duration::seconds(60)),
"1 minute"
);
// Minutes - singular (anything in [60s, 120s) rounds down to 1)
assert_eq!(
duration_to_shorthand(chrono::Duration::seconds(119)),
"1 minute"
);
// Minutes - plural
assert_eq!(
duration_to_shorthand(chrono::Duration::seconds(120)),
"2 minutes"
);
assert_eq!(
duration_to_shorthand(chrono::Duration::minutes(59)),
"59 minutes"
);

// Hours - boundary
assert_eq!(
duration_to_shorthand(chrono::Duration::minutes(60)),
"1 hour"
);
// Hours - singular (anything in [60m, 120m))
assert_eq!(
duration_to_shorthand(chrono::Duration::minutes(119)),
"1 hour"
);
// Hours - plural
assert_eq!(
duration_to_shorthand(chrono::Duration::minutes(120)),
"2 hours"
);
assert_eq!(
duration_to_shorthand(chrono::Duration::hours(23)),
"23 hours"
);

// Days - boundary
assert_eq!(duration_to_shorthand(chrono::Duration::hours(24)), "1 day");
// Days - singular
assert_eq!(duration_to_shorthand(chrono::Duration::hours(47)), "1 day");
// Days - plural
assert_eq!(duration_to_shorthand(chrono::Duration::hours(48)), "2 days");
assert_eq!(duration_to_shorthand(chrono::Duration::days(27)), "27 days");

// Weeks - boundary (28 days = 4 weeks)
assert_eq!(duration_to_shorthand(chrono::Duration::days(28)), "4 weeks");
assert_eq!(duration_to_shorthand(chrono::Duration::days(35)), "5 weeks");
}

#[test]
fn test_announcement_message_generation_adds_extra_info() {
use chrono::DateTime;
Expand Down
9 changes: 1 addition & 8 deletions llvm_buildbot_monitor/src/greendragon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,14 @@ struct RawStatusBuild {
number: BuildNumber,
}

fn is_sorted_by<T>(container: &[T], mut cmp: impl FnMut(&T, &T) -> bool) -> bool {
container
.iter()
.zip(container.iter().skip(1))
.all(|(a, b)| cmp(a, b))
}

async fn find_first_failing_build(
client: &reqwest::Client,
bot_name: &str,
build_list: &[RawStatusBuild],
last_successful: Option<BuildNumber>,
last_failure: BuildNumber,
) -> Result<CompletedBuild> {
debug_assert!(is_sorted_by(build_list, |x, y| x.number < y.number));
debug_assert!(build_list.is_sorted_by(|x, y| x.number < y.number));

let search_start: usize = if let Some(s) = last_successful {
assert!(last_failure > s, "{last_failure} should be > {s}");
Expand Down
120 changes: 114 additions & 6 deletions llvm_buildbot_monitor/src/lab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1032,15 +1032,123 @@ pub(crate) async fn fetch_new_status_snapshot(
mod test {
use super::*;

// Helper to reduce boilerplate in category tests.
fn builder(name: &str, tags: &[&str]) -> BuilderInfo {
BuilderInfo {
id: 0,
name: name.to_string(),
tags: tags.iter().map(|s| s.to_string()).collect(),
}
}

// --- remove_name_from_email ---

#[test]
fn test_category_determinations() {
fn test_remove_name_from_email_with_display_name() {
assert_eq!(
"foo@bar.com",
remove_name_from_email("Foo Bar <foo@bar.com>")
);
}

#[test]
fn test_remove_name_from_email_plain_address_unchanged() {
assert_eq!("foo@bar.com", remove_name_from_email("foo@bar.com"));
}

#[test]
fn test_remove_name_from_email_angle_brackets_only() {
assert_eq!("foo@bar.com", remove_name_from_email("<foo@bar.com>"));
}

#[test]
fn test_remove_name_from_email_only_close_bracket() {
// No opening '<', so the string should pass through unchanged.
assert_eq!("foo@bar.com>", remove_name_from_email("foo@bar.com>"));
}

#[test]
fn test_remove_name_from_email_embedded_gt_before_angle() {
// The comment in the source mentions "foo bar <x at y dot com>" style inputs, but here
// we verify that an embedded '>' before the final '<...>' pair is handled correctly by
// rfind picking the last '<'.
assert_eq!("x@y.com", remove_name_from_email("foo > bar <x@y.com>"));
}

#[test]
fn test_remove_name_from_email_empty_string() {
assert_eq!("", remove_name_from_email(""));
}

// --- determine_bot_category ---

#[test]
fn test_category_single_tag_wins_directly() {
assert_eq!(
Some("clang"),
determine_bot_category(&builder("clang-x86_64-ubuntu", &["clang"]))
);
}

#[test]
fn test_category_toolchain_tag_beats_others() {
assert_eq!(
Some("toolchain"),
determine_bot_category(&builder("clang-toolchain-x86", &["clang", "toolchain"]))
);
}

#[test]
fn test_category_clang_tools_tag_beats_others() {
assert_eq!(
Some("clang-tools"),
determine_bot_category(&builder("clang-tools-extra-x86", &["clang", "clang-tools"]))
);
}

#[test]
fn test_category_name_scan_clang_beats_llvm() {
// "clang" is earlier in the importance list than "llvm", so it should win.
assert_eq!(
Some("clang"),
determine_bot_category(&builder("llvm-clang-foo-bar", &["llvm", "docs"]))
);
}

#[test]
fn test_category_name_scan_existing_test_case() {
// Preserves the original test: no 'clang' token in the name, so 'llvm' wins.
assert_eq!(
Some("llvm"),
determine_bot_category(&BuilderInfo {
id: 123,
name: "llvm-sphinx-docs".to_string(),
tags: vec!["llvm".into(), "docs".into()],
})
determine_bot_category(&builder("llvm-sphinx-docs", &["llvm", "docs"]))
);
}

#[test]
fn test_category_name_scan_single_match() {
assert_eq!(
Some("lld"),
determine_bot_category(&builder("ppc64le-lld-foo-bar", &["lld", "other"]))
);
}

#[test]
fn test_category_name_scan_polly() {
// Example from the source comment.
assert_eq!(
Some("polly"),
determine_bot_category(&builder(
"aosp-O3-polly-before-vectorizer-unprofitable",
&["unrelated", "tags"]
))
);
}

#[test]
fn test_category_no_match_returns_none() {
assert_eq!(
None,
determine_bot_category(&builder("windows-msvc-amd64", &["tag1", "tag2"]))
);
}
}
Loading