Read the portraits folders once and stop the card pass offline (#77)
The statistics list the portraits folder and its cards subfolder once instead of checking about 340 files. The tall-portrait pass is skipped when the catalog refresh failed and stops at the first network error; the core tells after each attempt whether a portrait was saved, so the worker's throttle can tell the page after any attempt, not only saves. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
485d7b9065
commit
18b06f8ea6
5 files changed
+183
-53
No files matched your search
@@ -170,6 +170,29 @@ fn portraits_are_downloaded_and_exposed_and_a_failed_one_is_left_out() {
|
||||
assert!(!portraits.join("Jinx.part").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_files_count_as_portraits() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let tracker = reopen(dir.path(), FakeChampions::new("1.0", &CATALOG));
|
||||
tracker.refresh_champions().unwrap();
|
||||
let portraits = dir.path().join("portraits");
|
||||
std::fs::remove_file(portraits.join("Jinx.png")).unwrap();
|
||||
std::fs::create_dir(portraits.join("Jinx.png")).unwrap();
|
||||
|
||||
let stats = statistics(&tracker);
|
||||
|
||||
let jinx = stats
|
||||
.rows
|
||||
.iter()
|
||||
.find(|r| r.champion.key == "Jinx")
|
||||
.unwrap();
|
||||
assert_eq!(jinx.portrait, None);
|
||||
assert_eq!(
|
||||
stats.rows.iter().filter(|r| r.portrait.is_some()).count(),
|
||||
5
|
||||
);
|
||||
}
|
||||
|
||||
fn cards_dir(dir: &std::path::Path) -> std::path::PathBuf {
|
||||
dir.join("portraits").join("cards")
|
||||
}
|
||||
@@ -198,7 +221,9 @@ fn a_missing_card_portrait_is_downloaded_once_then_read_from_disk() {
|
||||
);
|
||||
|
||||
let mut saved = 0;
|
||||
tracker.refresh_card_portraits(&mut || saved += 1).unwrap();
|
||||
tracker
|
||||
.refresh_card_portraits(&mut |ok| saved += usize::from(ok))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!((saved, calls.load(Ordering::SeqCst)), (6, 6));
|
||||
let stats = statistics(&tracker);
|
||||
@@ -212,7 +237,7 @@ fn a_missing_card_portrait_is_downloaded_once_then_read_from_disk() {
|
||||
|
||||
let mut saved_again = 0;
|
||||
tracker
|
||||
.refresh_card_portraits(&mut || saved_again += 1)
|
||||
.refresh_card_portraits(&mut |ok| saved_again += usize::from(ok))
|
||||
.unwrap();
|
||||
assert_eq!((saved_again, calls.load(Ordering::SeqCst)), (0, 6));
|
||||
}
|
||||
@@ -228,7 +253,9 @@ fn a_failed_card_download_leaves_no_file_and_the_next_pass_resumes() {
|
||||
failure.store(true, Ordering::SeqCst);
|
||||
|
||||
let mut saved = 0;
|
||||
tracker.refresh_card_portraits(&mut || saved += 1).unwrap();
|
||||
tracker
|
||||
.refresh_card_portraits(&mut |ok| saved += usize::from(ok))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(saved, 0);
|
||||
assert!(card_files(dir.path()).is_empty());
|
||||
@@ -240,11 +267,14 @@ fn a_failed_card_download_leaves_no_file_and_the_next_pass_resumes() {
|
||||
);
|
||||
|
||||
failure.store(false, Ordering::SeqCst);
|
||||
tracker.refresh_card_portraits(&mut || saved += 1).unwrap();
|
||||
tracker
|
||||
.refresh_card_portraits(&mut |ok| saved += usize::from(ok))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(saved, 6);
|
||||
assert_eq!(card_files(dir.path()).len(), 6);
|
||||
assert_eq!(calls.load(Ordering::SeqCst), 12);
|
||||
// One request failed the first pass; the second asked for the six.
|
||||
assert_eq!(calls.load(Ordering::SeqCst), 7);
|
||||
assert!(
|
||||
statistics(&tracker)
|
||||
.rows
|
||||
@@ -253,6 +283,43 @@ fn a_failed_card_download_leaves_no_file_and_the_next_pass_resumes() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_network_error_stops_the_card_pass_after_one_request() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let source = FakeChampions::new("1.0", &CATALOG);
|
||||
let failure = source.card_failure();
|
||||
let calls = source.card_counter();
|
||||
let tracker = reopen(dir.path(), source);
|
||||
tracker.refresh_champions().unwrap();
|
||||
failure.store(true, Ordering::SeqCst);
|
||||
|
||||
let mut attempts = Vec::new();
|
||||
tracker
|
||||
.refresh_card_portraits(&mut |ok| attempts.push(ok))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(calls.load(Ordering::SeqCst), 1);
|
||||
assert_eq!(attempts, [false]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn each_card_attempt_tells_whether_a_portrait_was_saved() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let tracker = reopen(
|
||||
dir.path(),
|
||||
FakeChampions::new("1.0", &CATALOG).without_portrait("Jinx"),
|
||||
);
|
||||
tracker.refresh_champions().unwrap();
|
||||
|
||||
let mut attempts = Vec::new();
|
||||
tracker
|
||||
.refresh_card_portraits(&mut |ok| attempts.push(ok))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(attempts.len(), 6);
|
||||
assert_eq!(attempts.iter().filter(|ok| **ok).count(), 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_card_portrait_that_cannot_be_read_is_skipped_without_a_file() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
@@ -263,7 +330,9 @@ fn a_card_portrait_that_cannot_be_read_is_skipped_without_a_file() {
|
||||
tracker.refresh_champions().unwrap();
|
||||
|
||||
let mut saved = 0;
|
||||
tracker.refresh_card_portraits(&mut || saved += 1).unwrap();
|
||||
tracker
|
||||
.refresh_card_portraits(&mut |ok| saved += usize::from(ok))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(saved, 0);
|
||||
assert!(card_files(dir.path()).is_empty());
|
||||
@@ -279,7 +348,9 @@ fn one_failed_card_portrait_does_not_stop_the_others() {
|
||||
tracker.refresh_champions().unwrap();
|
||||
|
||||
let mut saved = 0;
|
||||
tracker.refresh_card_portraits(&mut || saved += 1).unwrap();
|
||||
tracker
|
||||
.refresh_card_portraits(&mut |ok| saved += usize::from(ok))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(saved, 5);
|
||||
assert!(!card_files(dir.path()).contains(&"Jinx.jpg".to_string()));
|
||||
@@ -297,7 +368,7 @@ fn refreshing_card_portraits_without_a_champion_source_does_nothing() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let tracker = open_tracker(dir.path(), &[]);
|
||||
|
||||
tracker.refresh_card_portraits(&mut || {}).unwrap();
|
||||
tracker.refresh_card_portraits(&mut |_| {}).unwrap();
|
||||
|
||||
assert!(!dir.path().join("portraits").exists());
|
||||
}
|
||||
|
||||
@@ -407,7 +407,7 @@ pub struct FakeChampions {
|
||||
catalog_calls: Arc<AtomicUsize>,
|
||||
/// What every card portrait download delivers.
|
||||
card_art: Vec<u8>,
|
||||
/// The card portrait downloads fail while this is set.
|
||||
/// The card portrait downloads fail with a network error while this is set.
|
||||
card_failure: Arc<AtomicBool>,
|
||||
card_calls: Arc<AtomicUsize>,
|
||||
}
|
||||
@@ -511,9 +511,12 @@ impl ChampionSource for FakeChampions {
|
||||
fn card_portrait(&self, key: &str) -> Result<Vec<u8>, SourceError> {
|
||||
self.card_calls.fetch_add(1, Ordering::SeqCst);
|
||||
self.check()?;
|
||||
if self.card_failure.load(Ordering::SeqCst) || self.no_portrait.iter().any(|k| k == key) {
|
||||
if self.card_failure.load(Ordering::SeqCst) {
|
||||
return Err(SourceError::Network("coupé".into()));
|
||||
}
|
||||
if self.no_portrait.iter().any(|k| k == key) {
|
||||
return Err(SourceError::NotFound);
|
||||
}
|
||||
Ok(self.card_art.clone())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user