Skip to content

Commit d50c219

Browse files
committed
Fix checking and checking tests
1 parent 672980e commit d50c219

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

src/checking.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use tokio::io::AsyncReadExt;
1414
use axum::http::{Uri, uri::Scheme};
1515
use chrono::Utc;
1616
use futures::TryStreamExt;
17-
use log::{error, info};
17+
use log::info;
1818
use reqwest::{Client, StatusCode};
1919
use sarlacc::Intern;
2020
use tokio::sync::RwLock;
@@ -84,30 +84,32 @@ async fn is_online() -> bool {
8484

8585
/// Checks whether a given URL passes the given check level.
8686
///
87-
/// Returns `Some` with the failure details if the site fails the check, or `None` if it passes (or
88-
/// if the check cannot be performed, e.g., due to the server being offline).
87+
/// Returns `Ok(Some(...))` with the failure details if the site fails the check, or `Ok(None)` if it passes.
88+
///
89+
/// Returns `Err` if the check cannot be performed (e.g., due to the server being offline).
8990
pub async fn check(
9091
website: &Uri,
9192
check_level: CheckLevel,
9293
base_address: Intern<Uri>,
93-
) -> Option<CheckFailure> {
94+
) -> Result<Option<CheckFailure>, ()> {
9495
match check_impl(website, check_level, base_address).await {
95-
None => None,
96+
None => Ok(None),
9697
Some(failure) => {
9798
// If the issue is not a connection issue, or if it is a connection issue and the
9899
// server is online, return it. Otherwise, it's a connection issue on our end, so log
99100
// and count the check as successful.
101+
#[cfg(not(test))]
100102
if let CheckFailure::Connection(connection_error) = &failure {
101103
if !is_online().await {
102-
error!(
104+
log::error!(
103105
"Server-side connectivity issue detected: Could not reach {website}: {connection_error}"
104106
);
105-
return None;
107+
return Err(());
106108
}
107109
}
108110

109111
info!("{website} failed a check: {failure}");
110-
Some(failure)
112+
Ok(Some(failure))
111113
}
112114
}
113115
}
@@ -141,6 +143,7 @@ async fn check_impl(
141143
Ok(response) => response,
142144
Err(err) => return Some(CheckFailure::Connection(err)),
143145
};
146+
println!("{response:?}");
144147
mark_server_as_online().await;
145148
let successful_response = match response.error_for_status() {
146149
Ok(r) => r,
@@ -604,7 +607,7 @@ mod tests {
604607
#[expect(clippy::type_complexity)]
605608
let sites: Vec<(Uri, Vec<CheckLevel>, fn(CheckFailure) -> bool)> = vec![
606609
(
607-
Uri::from_static("http://127.0.0.10:0/connection"),
610+
Uri::from_static("http://127.0.0.10:60000/connection"),
608611
vec![CheckLevel::None],
609612
|failure| matches!(failure, CheckFailure::Connection(_)),
610613
),
@@ -638,7 +641,7 @@ mod tests {
638641
];
639642
for level in levels {
640643
// FIXME: Collect CheckFailure and check type
641-
let maybe_failure = super::check(&site, level, base).await;
644+
let maybe_failure = super::check(&site, level, base).await.unwrap();
642645
eprintln!("Checking {} at level {:?}", &site, level);
643646
let was_successful = maybe_failure.is_none();
644647
assert_eq!(expect_passing.contains(&level), was_successful);
@@ -662,6 +665,7 @@ mod tests {
662665
assert!(
663666
check(&Uri::from_static("https://kasad.com"), level, base)
664667
.await
668+
.unwrap()
665669
.is_none()
666670
);
667671
}

src/config.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,11 @@ mod tests {
279279
let actual: Config = toml::from_str(config).unwrap();
280280
assert_eq!(
281281
LoggingTable {
282-
verbosity: LevelFilter::Debug,
282+
verbosity: if cfg!(debug_assertions) {
283+
LevelFilter::Debug
284+
} else {
285+
LevelFilter::Info
286+
},
283287
log_file: None
284288
},
285289
actual.logging

src/webring.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use serde::Deserialize;
2121
use thiserror::Error;
2222

2323
use crate::{
24-
checking::{CheckFailure, check},
24+
checking::check,
2525
config::{Config, MemberSpec},
2626
discord::{DiscordNotifier, Snowflake},
2727
homepage::{Homepage, MemberForHomepage},
@@ -74,15 +74,19 @@ impl Member {
7474
&self,
7575
base_address: Intern<Uri>,
7676
notifier: Option<Arc<DiscordNotifier>>,
77-
) -> impl Future<Output = Option<CheckFailure>> + Send + 'static {
77+
) -> impl Future<Output = ()> + Send + 'static {
7878
let website = self.website;
7979
let check_level = self.check_level;
8080
let successful = Arc::clone(&self.check_successful);
8181

8282
let discord_id_for_block = self.discord_id;
8383
let base_address_for_block = base_address;
8484
async move {
85-
let check_result = check(&website, check_level, base_address_for_block).await;
85+
let Ok(check_result) = check(&website, check_level, base_address_for_block).await
86+
else {
87+
return;
88+
};
89+
8690
if let Some(failure) = &check_result {
8791
successful.store(false, Ordering::Relaxed);
8892
if let (Some(notifier), Some(user_id)) = (notifier, discord_id_for_block) {
@@ -96,7 +100,6 @@ impl Member {
96100
} else {
97101
successful.store(true, Ordering::Relaxed);
98102
}
99-
check_result
100103
}
101104
}
102105
}

0 commit comments

Comments
 (0)