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
37 changes: 37 additions & 0 deletions crates/accshift-core/src/platforms/battle_net/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,3 +663,40 @@ fn a_setup_that_fails_after_quitting_the_launcher_starts_it_again() {
assert_eq!(result, Ok(7));
assert!(!restarted);
}

#[test]
fn a_local_config_without_owed_names_still_loads() {
let _guard = crate::config::config_io_test_mutex()
.lock()
.unwrap_or_else(|e| e.into_inner());
let root = std::env::temp_dir().join(format!(
"accshift-battlenet-owed-legacy-{}-{:?}",
std::process::id(),
std::thread::current().id()
));
let _ = std::fs::remove_dir_all(&root);
let ctx = TestCtx { root: root.clone() };
// A local file as 2.0.1 leaves it with nothing owed (the field is skipped
// when empty) or as 2.0.0 left it, before the field existed.
let local = crate::storage::local_config_path(&ctx).unwrap();
std::fs::create_dir_all(local.parent().unwrap()).unwrap();
std::fs::write(
&local,
r#"{"steam":{"path_override":""},"battleNet":{"path_override":"D:/Games/Battle.net"}}"#,
)
.unwrap();

let config = crate::config::load_config(&ctx);
assert_eq!(config.battle_net.path_override, "D:/Games/Battle.net");
assert!(config.battle_net.owed_saved_accounts.is_empty());
let (merged, owed) = with_owed_saved_accounts(&ctx, vec!["new@example.com".to_string()]);
assert!(owed.is_empty());
assert_eq!(merged, vec!["new@example.com".to_string()]);

// The first owed list lands beside the fields already there.
owe_saved_accounts(&ctx, &["old@example.com".to_string()]);
let config = crate::config::load_config(&ctx);
assert_eq!(config.battle_net.path_override, "D:/Games/Battle.net");
assert_eq!(config.battle_net.owed_saved_accounts, ["old@example.com"]);
let _ = std::fs::remove_dir_all(&root);
}
2 changes: 1 addition & 1 deletion crates/accshift-egui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ image-webp = "0.2"
[target.'cfg(not(windows))'.dependencies]
egui = { version = "0.37", features = ["default_fonts"] }

# Unix termination signals (window/signal.rs): SIGTERM and SIGINT run the same
# Unix termination signals (window/signal.rs): SIGTERM, SIGINT and SIGHUP run the same
# orderly shutdown as a normal quit. `libc` is already in the workspace graph.
[target.'cfg(unix)'.dependencies]
libc = "0.2"
Expand Down
9 changes: 9 additions & 0 deletions crates/accshift-egui/src/app/saves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ impl AppModel {
.run(move |backend| backend.save_client_storage_store(store_id, &save.value));
self.saves.push((store_id, pending));
}
let mut reported = false;
self.saves.retain_mut(|(id, pending)| {
let result = match pending.poll() {
Poll::Waiting => return true,
Expand All @@ -103,7 +104,15 @@ impl AppModel {
self.backup.save_failed(error);
}
b.stores.finish_save(id, &result);
reported = true;
false
});
// The job's wake brought this frame and is spent. A write that ends
// frees its store, whose next write the timed repaint above left
// out and may be due already, and a backup's drain ran earlier in
// this frame: without one more frame both wait for the next input.
if reported {
ctx.request_repaint();
}
}
}
2 changes: 2 additions & 0 deletions crates/accshift-egui/src/app/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use super::*;

mod backup_restore;
mod early_boot;
mod held;
mod native_regressions;
mod pump;
mod real_unreadable;
mod shortcuts;
mod updates;
Expand Down
140 changes: 110 additions & 30 deletions crates/accshift-egui/src/app/tests/backup_restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//! closing under it. An export waits behind the same boundary.

use super::super::settings_panel::SettingsAction;
use super::held::HeldCommands;
use super::pump;
use super::*;
use accshift_app::i18n::{translate, Locale};
use serde_json::json;
Expand Down Expand Up @@ -68,13 +70,38 @@ fn frame(app: &mut AppModel, ctx: &Context) {
output.textures_delta.clear();
}

/// Frames until `condition` holds: [`pump::until`].
#[track_caller]
fn pump_until(app: &mut AppModel, ctx: &Context, condition: impl Fn(&AppModel) -> bool) {
let deadline = Instant::now() + std::time::Duration::from_secs(3);
while !condition(app) {
assert!(Instant::now() < deadline, "the condition never held");
frame(app, ctx);
std::thread::sleep(std::time::Duration::from_millis(1));
}
pump::until(app, ctx, frame, condition);
}

#[test]
fn a_slow_frame_is_not_a_hang() {
// Regression (SLOW_FRAME): the wait's deadline counted the frames
// themselves. On a loaded runner one debug frame outlasted it, and a
// condition two frames away "never held" though nothing was stuck.
let ctx = Context::default();
let (mut app, mock) = restore_app(&ctx);
let mut held = HeldCommands::new(&mock);
// The panel's mount job cannot land before the first frame polls it.
held.hold("steam_has_api_key");
app.toggle_settings_panel();
let limit = std::time::Duration::from_millis(50);
pump::within(
&mut app,
&ctx,
limit,
|app, ctx| {
frame(app, ctx);
held.release("steam_has_api_key");
// The frame outlasts the limit, and asks for the next one itself:
// however late the job lands, no wait for it counts as idle.
std::thread::sleep(limit * 3);
ctx.request_repaint();
},
|a| a.settings_panel.as_ref().is_some_and(|p| p.hydrated),
);
}

fn note(app: &AppModel) -> String {
Expand Down Expand Up @@ -156,6 +183,71 @@ fn a_restore_waits_for_a_running_save_and_holds_new_writes() {
assert!(app.backup.card.notice.contains("pre-restore-1"));
}

/// Well inside the next deadline the app asks a frame for on its own (a
/// toast's expiry, seconds away), so a wait that needs a frame nobody asked
/// for does not hold within it.
const NO_TIMER: std::time::Duration = std::time::Duration::from_millis(500);

/// Answers the note write in flight in place of the mock, whose held call is
/// then released: the answer is in before the next frame and no wake came
/// with it, as when the job's wake brought a frame drawn before this one.
fn answer_note_write(app: &mut AppModel, held: &mut HeldCommands) {
let (_, pending) = app
.saves
.iter_mut()
.find(|(store, _)| *store == "client.account-card-notes")
.expect("no note write in flight");
let (answer, answered) = std::sync::mpsc::channel();
answer.send(Ok(None)).unwrap();
// The held call's own Pending is dropped before its release: its
// answer then has no one to go to, and its job wakes nothing.
*pending = Pending::from_receiver(answered);
held.release(SAVE);
}

#[test]
fn a_write_ending_under_the_boundary_wakes_the_restore() {
// Regression (DRAIN_WAKE): the frame that reports a write runs the
// boundary's drain before it, and asked for no frame after: the restore
// waited for an unrelated deadline or the next mouse move.
let ctx = Context::default();
let (mut app, mock) = restore_app(&ctx);
let mut held = HeldCommands::new(&mock);
frame(&mut app, &ctx);
held.hold(SAVE);
set_note(&mut app, "typed before");
pump_until(&mut app, &ctx, |_| mock.waiting(SAVE) == 1);
app.start_backup_restore("C:/b.json".into(), String::new());
held.hold("backup_restore");
answer_note_write(&mut app, &mut held);
pump::within(&mut app, &ctx, NO_TIMER, frame, |_| {
mock.waiting("backup_restore") == 1
});
held.release("backup_restore");
pump_until(&mut app, &ctx, |a| !a.backup.holds_writes());
}

#[test]
fn a_note_typed_while_its_store_writes_is_written_when_that_write_ends() {
// Regression (DRAIN_WAKE): a write due while its store still wrote was
// left out of the timed repaint, and the frame that reported the write
// asked for none: it waited for an unrelated deadline or the next input.
let ctx = Context::default();
let (mut app, mock) = restore_app(&ctx);
let mut held = HeldCommands::new(&mock);
frame(&mut app, &ctx);
held.hold(SAVE);
set_note(&mut app, "first");
pump_until(&mut app, &ctx, |_| mock.waiting(SAVE) == 1);
set_note(&mut app, "second");
answer_note_write(&mut app, &mut held);
pump::within(&mut app, &ctx, NO_TIMER, frame, |_| mock.seen(SAVE) == 2);
pump_until(&mut app, &ctx, |a| {
!a.booted().unwrap().stores.storage().has_unsaved()
});
assert_eq!(note(&app), "second");
}

#[test]
fn restored_settings_keep_the_restored_platform_they_enable() {
let ctx = Context::default();
Expand Down Expand Up @@ -353,11 +445,7 @@ fn a_picker_that_fails_says_why() {
app.import_backup();
}
pump_until(&mut app, &ctx, |_| mock.seen(command) == 1);
let deadline = Instant::now() + std::time::Duration::from_secs(3);
while !app.backup.card.error && Instant::now() < deadline {
frame(&mut app, &ctx);
std::thread::sleep(std::time::Duration::from_millis(1));
}
pump_until(&mut app, &ctx, |a| a.backup.card.error);
assert!(
app.backup.card.error && app.backup.card.notice.contains(odd),
"PICKER_ERROR: {command} said {:?}",
Expand Down Expand Up @@ -770,12 +858,12 @@ fn a_restore_whose_theme_cannot_be_read_keeps_it_selected_until_it_loads() {
assert!(mock.fail("list_custom_themes", None));
focus(&mut app, &ctx, false);
focus(&mut app, &ctx, true);
let deadline = Instant::now() + std::time::Duration::from_secs(3);
while app.booted().unwrap().registry.document("mine").is_none() {
assert!(Instant::now() < deadline, "the theme never loaded");
focus(&mut app, &ctx, true);
std::thread::sleep(std::time::Duration::from_millis(1));
}
pump::until(
&mut app,
&ctx,
|app, ctx| focus(app, ctx, true),
|a| a.booted().unwrap().registry.document("mine").is_some(),
);
let b = app.booted().unwrap();
assert_eq!(b.stores.settings().theme_id, "mine");
assert_ne!(b.theme.vars.bg_card, before, "the theme does not draw");
Expand Down Expand Up @@ -807,15 +895,7 @@ fn theme_restore_app(
fn refocus_until(app: &mut AppModel, ctx: &Context, loaded: impl Fn(&AppModel) -> bool) {
focus(app, ctx, false);
focus(app, ctx, true);
let deadline = Instant::now() + std::time::Duration::from_secs(3);
while !loaded(app) {
assert!(
Instant::now() < deadline,
"the themes were not read again on the next focus"
);
focus(app, ctx, true);
std::thread::sleep(std::time::Duration::from_millis(1));
}
pump::until(app, ctx, |app, ctx| focus(app, ctx, true), loaded);
}

#[test]
Expand Down Expand Up @@ -1260,11 +1340,11 @@ fn a_panel_edit_made_during_the_restore_never_lands_over_it() {
mock.release("backup_restore");
pump_until(&mut app, &ctx, |a| !a.backup.holds_writes());
assert!(!app.backup.card.error, "{}", app.backup.card.notice);
// Past the panel's save debounce.
let until = Instant::now() + std::time::Duration::from_millis(400);
while Instant::now() < until {
// Until the panel has no save left to run: one still queued runs at
// its deadline, which the app asks a frame for.
pump_until(&mut app, &ctx, |a| a.settings_panel_deadline().is_none());
for _ in 0..3 {
frame(&mut app, &ctx);
std::thread::sleep(std::time::Duration::from_millis(5));
}
let panel = app.settings_panel.as_ref().unwrap().draft.language;
let stored = app.booted().unwrap().stores.settings().language;
Expand Down
41 changes: 41 additions & 0 deletions crates/accshift-egui/src/app/tests/held.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//! A guard for commands a test holds at the mock.

use std::sync::Arc;

use accshift_app::mock::MockBackend;

/// Commands held at the mock for one test, released when it drops. A failed
/// assertion then unwinds through the release: otherwise a worker stays parked
/// at the hold and `Jobs::drop`, joining it, hangs the suite.
pub(super) struct HeldCommands {
mock: Arc<MockBackend>,
commands: Vec<&'static str>,
}

impl HeldCommands {
/// Declare it after the app so it drops first.
pub(super) fn new(mock: &Arc<MockBackend>) -> Self {
Self {
mock: mock.clone(),
commands: Vec::new(),
}
}

pub(super) fn hold(&mut self, command: &'static str) {
self.commands.push(command);
assert!(self.mock.hold(command), "{command} could not be held");
}

pub(super) fn release(&mut self, command: &str) {
self.commands.retain(|held| *held != command);
self.mock.release(command);
}
}

impl Drop for HeldCommands {
fn drop(&mut self) {
for command in self.commands.drain(..) {
self.mock.release(command);
}
}
}
58 changes: 7 additions & 51 deletions crates/accshift-egui/src/app/tests/native_regressions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use super::held::HeldCommands;
use super::pump;
use super::*;
use egui::{pos2, vec2, Id, RawInput, Rect, Sense, ViewportId};
use serde_json::json;
Expand Down Expand Up @@ -110,26 +112,16 @@ fn frame(app: &mut AppModel, ctx: &Context, focused: bool, minimized: bool) {
output.textures_delta.clear();
}

/// Focused frames until `condition` holds: [`pump::until`].
#[track_caller]
fn pump_until(app: &mut AppModel, ctx: &Context, condition: impl Fn(&AppModel) -> bool) {
let deadline = Instant::now() + std::time::Duration::from_secs(10);
while !condition(app) {
assert!(
Instant::now() < deadline,
"condition did not become true within 10 s"
);
frame(app, ctx, true, false);
std::thread::sleep(std::time::Duration::from_millis(1));
}
pump::until(app, ctx, |app, ctx| frame(app, ctx, true, false), condition);
}

/// Frames until no external refresh runs, accounts it reloads included.
#[track_caller]
fn wait_refresh_over(app: &mut AppModel, ctx: &Context) {
let deadline = Instant::now() + std::time::Duration::from_secs(10);
while app.external_refresh.running() {
assert!(Instant::now() < deadline, "the refresh never ended");
frame(app, ctx, true, false);
std::thread::sleep(std::time::Duration::from_millis(1));
}
pump_until(app, ctx, |a| !a.external_refresh.running());
}

#[test]
Expand All @@ -154,42 +146,6 @@ fn native_external_storage_on_focus_refreshes_stores_and_accounts() {
assert_eq!(b.accounts.loading.loader.current_account(), "external");
}

/// Commands held at the mock for one test, released when it drops. A failed
/// assertion then unwinds through the release: otherwise a worker stays parked
/// at the hold and `Jobs::drop`, joining it, hangs the suite.
struct HeldCommands {
mock: Arc<MockBackend>,
commands: Vec<&'static str>,
}

impl HeldCommands {
/// Declare it after the app so it drops first.
fn new(mock: &Arc<MockBackend>) -> Self {
Self {
mock: mock.clone(),
commands: Vec::new(),
}
}

fn hold(&mut self, command: &'static str) {
self.commands.push(command);
assert!(self.mock.hold(command), "{command} could not be held");
}

fn release(&mut self, command: &str) {
self.commands.retain(|held| *held != command);
self.mock.release(command);
}
}

impl Drop for HeldCommands {
fn drop(&mut self) {
for command in self.commands.drain(..) {
self.mock.release(command);
}
}
}

/// Waits until a call of `command` sits at its hold in the mock.
fn wait_held(mock: &MockBackend, command: &str) {
let deadline = Instant::now() + std::time::Duration::from_secs(10);
Expand Down
Loading
Loading