acquireInstanceLock() — extract exit/dialog seam to enable unit testing

Aperta
#169 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
68/100
Tipo di issue
Refactoring
Chiarezza
Specificata chiaramente
Stato di attività
Tranquilla
Stack tecnologico
java
Ambito
testing

Direzione di ricerca

Start in App.java around acquireInstanceLock() (the issue points to lines 312–314) and trace how the lock and already-running branch are handled. Add unit tests for acquiring, detecting and releasing the lock, including the injectable callback. Done means those lifecycle cases pass and GUI startup behavior remains unchanged.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

enhancement

Background

App.acquireInstanceLock() uses NIO FileLock to prevent multiple GUI instances from running simultaneously. When a second instance is detected, it calls System.exit(0) inside a SwingUtilities.invokeLater lambda:

SwingUtilities.invokeLater(() -> {
    JOptionPane.showMessageDialog(...);
    System.exit(0);  // ← terminates the test JVM if called during tests
});

This is correct production behavior, but it creates a test hazard: any future test that exercises the GUI startup path and encounters an already-held lock will terminate the entire Maven Surefire JVM rather than failing the individual test cleanly.

Problem

  1. System.exit in test JVM — if a test triggers the "already running" branch, the whole test suite crashes with a cryptic Surefire failure instead of a clean test failure.
  2. No coverage of the lock lifecycle — there are no tests verifying: first call acquires lock → true; second call finds it held → false; lock releases on channel close; next startup succeeds.
    Proposed fix
    Extract the exit and dialog side-effects behind an injectable strategy (e.g. a Runnable or BiConsumer parameter defaulting to the real System.exit + JOptionPane call). This makes acquireInstanceLock() unit-testable by passing a no-op or capture lambda in tests.
// Example seam
static boolean acquireInstanceLock(Runnable onAlreadyRunning) { ... }
// Production call (unchanged behavior)
acquireInstanceLock(() -> {
    SwingUtilities.invokeLater(() -> {
        JOptionPane.showMessageDialog(...);
        System.exit(0);
    });
});

Acceptance criteria

  • acquireInstanceLock() does not call System.exit directly — exit/dialog behavior is injectable
  • Unit test: first call acquires lock, returns true
  • Unit test: second call with lock held returns false and invokes the onAlreadyRunning callback
  • Unit test: releasing the channel allows a subsequent call to succeed
  • Existing GUI startup behavior unchanged

References

  • Flagged in PR code review (Copilot, medium severity) on icon-base branch, App.java lines 312–314
Lingua principale
Java
Stelle
6
Fork
6
Merge medio
10h 11m
PR unite (30g)
12

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di JDiskMark/jdm-java

Tutte le issue di JDiskMark/jdm-java

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.