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

Ouverte
#169 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
3/5
Temps estimé
1-2 jours
Accessibilité débutants
68/100
Type d'issue
Refactorisation
Clarté
Clairement spécifiée
Activité
Calme
Stack technique
java
Domaine
testing

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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
Langage dominant
Java
Étoiles
6
Forks
6
Merge moyen
10 h 11 min
PR mergées (30 j)
12

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de JDiskMark/jdm-java

Toutes les issues de JDiskMark/jdm-java

Issues similaires

Plus d'issues Java

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.