File tree Expand file tree Collapse file tree
main/kotlin/cz/vutbr/fit/interlockSim/gui
test/kotlin/cz/vutbr/fit/interlockSim/gui Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ import javax.swing.JFrame
2626import javax.swing.JOptionPane
2727import javax.swing.JPanel
2828import javax.swing.JScrollPane
29+ import javax.swing.SwingUtilities
2930import javax.swing.Timer
3031
3132/* *
@@ -419,11 +420,17 @@ class Frame : JFrame(PROGRAM_FULL_NAME) {
419420 }
420421 }
421422
423+ /* *
424+ * Execute [action] on EDT.
425+ *
426+ * Runs immediately if already on EDT; otherwise schedules asynchronously via
427+ * [javax.swing.SwingUtilities.invokeLater] to avoid blocking monitor/background threads.
428+ */
422429 private fun runOnEdt (action : () -> Unit ) {
423- if (javax.swing. SwingUtilities .isEventDispatchThread()) {
430+ if (SwingUtilities .isEventDispatchThread()) {
424431 action()
425432 } else {
426- javax.swing. SwingUtilities .invokeLater(action)
433+ SwingUtilities .invokeLater(action)
427434 }
428435 }
429436
Original file line number Diff line number Diff line change @@ -35,10 +35,13 @@ import java.beans.PropertyChangeListener
3535 * nulls it.
3636 * - Callbacks are invoked on whichever thread performs the lifecycle change.
3737 *
38- * @param onStateChanged Callback for lifecycle state updates.
39- * @param onSpeedChanged Callback for speed indicator updates.
40- * @param onCompleted Callback invoked when the simulation finishes naturally.
41- * Defaults to a no-op.
38+ * @param onStateChanged Callback for lifecycle state updates. Invoked on the same
39+ * thread that performs the state change (caller thread for [start]/[stop], monitor
40+ * thread for natural completion).
41+ * @param onSpeedChanged Callback for speed indicator updates. Invoked on the thread
42+ * that emits the speed change; callers are responsible for EDT marshalling as needed.
43+ * @param onCompleted Callback invoked when the simulation finishes naturally on the
44+ * monitor thread. Defaults to a no-op.
4245 * @since 2026-04-20 (extracted from Frame for testability)
4346 * @see Frame
4447 */
Original file line number Diff line number Diff line change @@ -680,6 +680,13 @@ class SimulationControllerTest {
680680 onCompleted = onCompleted
681681 )
682682
683+ /* *
684+ * Execute [action] on EDT synchronously for deterministic assertions in unit tests.
685+ *
686+ * Unlike the production callback wiring pattern (which dispatches asynchronously),
687+ * this helper blocks until EDT work completes. That makes follow-up assertions
688+ * observe final state without timing races.
689+ */
683690 private fun runOnEdtSync (action : () -> Unit ) {
684691 if (SwingUtilities .isEventDispatchThread()) {
685692 action()
You can’t perform that action at this time.
0 commit comments