Skip to content
Open
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
53 changes: 35 additions & 18 deletions crates/app/src/engine/drm_free/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl Engine {
let api = self.shared.ports.games.clone();
self.shared.runtime.spawn(async move {
loop {
let (fingerprint, candidate, cached_to_persist) = {
let (candidate, cached_to_persist) = {
let mut state = engine.lock();
let valid = state.local_generation == generation
&& state
Expand Down Expand Up @@ -196,30 +196,47 @@ impl Engine {
.candidates
.iter()
.find(|candidate| candidate.fingerprint == fingerprint)
.cloned();
(fingerprint, candidate, cached)
.cloned()
.filter(|candidate| !candidate.ignored);
// Claim the title in the same critical section that pops it. Marking it in
// flight later let a concurrent request queue the same title again and search
// it twice.
let candidate = match candidate {
Some(candidate)
if candidate.suggestion.is_some()
|| candidate.likely_non_game
|| state.local_suggestion_searched.contains(&fingerprint) =>
{
state.local_suggestion_searched.insert(fingerprint.clone());
None
}
Some(candidate) => {
let title_key = normalize_title(&candidate.title);
if title_key.is_empty()
|| state.local_suggestion_in_flight.contains(&title_key)
|| state.local_suggestion_titles.contains_key(&title_key)
{
// Another step owns or already answered this title; the next
// queue fill applies its cached answer.
None
} else {
state.local_suggestion_in_flight.insert(title_key.clone());
Some((candidate, title_key))
}
}
None => None,
};
(candidate, cached)
};
if !cached_to_persist.is_empty() {
persist_suggestions(&engine, cached_to_persist);
}
let Some(candidate) = candidate else {
// The view may have been replaced while the request was queued; a later
// refresh re-queues the candidate if it is still there.
let Some((candidate, title_key)) = candidate else {
// Removed, ignored, answered or owned by another step. The view may have been
// replaced while the request was queued; a later refresh re-queues it.
continue;
};
if candidate.ignored {
continue;
}
if candidate.suggestion.is_some() || candidate.likely_non_game {
engine.lock().local_suggestion_searched.insert(fingerprint);
continue;
}
let title = candidate.title.clone();
let title_key = normalize_title(&title);
engine
.lock()
.local_suggestion_in_flight
.insert(title_key.clone());
let years = years_in(&format!("{} {}", candidate.title, candidate.root));
let result = api.search_page(&credential, &title).await;
let rejected = matches!(result, Err(ApiError::Rejected));
Expand Down
27 changes: 19 additions & 8 deletions crates/app/src/engine/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,19 @@ impl Harness {
})
}

/// Waits for the credential write that follows `SignedIn`; it runs on the storage worker
/// after the snapshot is published, so a direct read can race it on a loaded runner.
async fn stored_credential(&self, expected: &str) -> Option<String> {
let deadline = tokio::time::Instant::now() + Duration::from_secs(45);
loop {
let stored = self.creds.stored();
if stored.as_deref() == Some(expected) || tokio::time::Instant::now() >= deadline {
return stored;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
}

async fn signed_out(&mut self) {
self.engine.start();
self.until("signed out", |s| s.auth == AuthView::SignedOut)
Expand Down Expand Up @@ -945,13 +958,10 @@ async fn fixture_browser_flow_signs_in_stores_and_shows_the_user() {
assert_eq!(user.display_name(), "Ana");
// The credential was verified with GameNode, then stored, exactly as received.
assert_eq!(*h.api.verified.lock().unwrap(), ["signed.token+/=="]);
for _ in 0..50 {
if h.creds.stored().is_some() {
break;
}
tokio::time::sleep(Duration::from_millis(20)).await;
}
assert_eq!(h.creds.stored().as_deref(), Some("signed.token+/=="));
assert_eq!(
h.stored_credential("signed.token+/==").await.as_deref(),
Some("signed.token+/==")
);
assert_eq!(snapshot.notice, None);
}

Expand Down Expand Up @@ -1007,7 +1017,7 @@ async fn reopen_failure_keeps_the_pending_attempt_and_reports_it() {
.until("signed in", |s| matches!(s.auth, AuthView::SignedIn { .. }))
.await;
assert!(matches!(snapshot.auth, AuthView::SignedIn { .. }));
assert_eq!(h.creds.stored().as_deref(), Some("tok"));
assert_eq!(h.stored_credential("tok").await.as_deref(), Some("tok"));
}

#[tokio::test(flavor = "multi_thread")]
Expand Down Expand Up @@ -1043,6 +1053,7 @@ async fn failed_replayed_and_late_callbacks_cannot_replace_the_session() {
let mut h = harness(FakeApi::new(), FakeCreds::with(None));
h.signed_out().await;
h.sign_in("good").await;
assert_eq!(h.stored_credential("good").await.as_deref(), Some("good"));

// Replay of the consumed callback, an intruder's attempt id, and garbage.
let replay = h.callback("evil");
Expand Down
19 changes: 17 additions & 2 deletions crates/app/src/engine/tests/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,24 @@ async fn a_selected_unmatched_entry_gets_engine_fetched_suggestions_and_can_use_
.suggestions
.is_empty()
);
let covers = h.api.covers_fetched.lock().unwrap();
// The cover download is planned with the match and runs after the snapshot that shows it.
let fetched = tokio::time::timeout(std::time::Duration::from_secs(45), async {
loop {
if h.api
.covers_fetched
.lock()
.unwrap()
.iter()
.any(|url| url.ends_with("co7.jpg"))
{
break;
}
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
})
.await;
assert!(
covers.iter().any(|url| url.ends_with("co7.jpg")),
fetched.is_ok(),
"the chosen game's cover is fetched like a match-dialog choice"
);
}
68 changes: 40 additions & 28 deletions crates/app/src/engine/tests/drm_free.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2799,34 +2799,46 @@ async fn local_custom_cover_commits_survives_a_match_and_cleans_up_on_replace_ca
})
.await;
assert!(!h.covers.path().join(&second).exists());
{
// The in-memory match this test set is not persisted; re-apply it while checking that the
// cleared registration falls back to the GameNode cover.
let mut state = h.engine.lock();
state.library.set_local_inference(
key.clone(),
Some(10),
Some(GameRecord {
game_id: 10,
name: "Matched".into(),
release_year: Some(2020),
cover_url: None,
cover_file: Some("10.jpg".into()),
cover_failed_at: None,
platforms: vec![],
}),
);
}
let row = h
.engine
.snapshot()
.library
.unwrap()
.rows
.iter()
.find(|row| row.key == key)
.cloned()
.unwrap();
// The in-memory match this test set is not persisted; re-apply it while checking that the
// cleared registration falls back to the GameNode cover. The edit above starts an inference
// for the new revision whose result, and the refresh after it, replace the in-memory match
// when they land, so re-apply until no such write is left in flight.
let row = tokio::time::timeout(Duration::from_secs(45), async {
loop {
{
let mut state = h.engine.lock();
state.library.set_local_inference(
key.clone(),
Some(10),
Some(GameRecord {
game_id: 10,
name: "Matched".into(),
release_year: Some(2020),
cover_url: None,
cover_file: Some("10.jpg".into()),
cover_failed_at: None,
platforms: vec![],
}),
);
}
tokio::time::sleep(Duration::from_millis(50)).await;
let row = h
.engine
.snapshot()
.library
.unwrap()
.rows
.iter()
.find(|row| row.key == key)
.cloned()
.unwrap();
if row.cover_file.as_deref() == Some("10.jpg") {
break row;
}
}
})
.await
.expect("the cleared registration falls back to the matched GameNode cover");
assert_eq!(row.cover_file.as_deref(), Some("10.jpg"));
assert_eq!(row.game_cover_file.as_deref(), Some("10.jpg"));

Expand Down
8 changes: 5 additions & 3 deletions crates/app/src/plugin_host/shared_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,9 +908,11 @@ mod tests {
use gamenode_core::drm_free::InstallationHost as _;
use std::process::{Command, Stdio};

// A private executable identity keeps the check independent of unrelated processes.
// A private executable identity keeps the check independent of unrelated processes. The
// copy keeps the name `sleep`: multicall coreutils builds (uutils, the Ubuntu 26.04
// default) pick the utility from the file name and exit at once under any other name.
let dir = tempfile::tempdir().unwrap();
let executable = dir.path().join("approved-runtime");
let executable = dir.path().join("sleep");
std::fs::copy("/bin/sleep", &executable).unwrap();
let registration = gamenode_platform::installations::LinuxInstallations
.inspect(executable.to_str().unwrap())
Expand Down Expand Up @@ -944,7 +946,7 @@ mod tests {
.unwrap_or_default()
.into_iter()
.filter(|process| {
process.pid == child.id() || process.exe_path.contains("approved-runtime")
process.pid == child.id() || process.exe_path == registration.executable
})
.map(|process| (process.pid, process.exe_path))
.collect::<Vec<_>>();
Expand Down
Loading