Skip to content

fix: replace Thread.sleep() with synchronization in TmuxEncodingTest#1827

Merged
gnodet merged 1 commit into
masterfrom
fix/tmux-encoding-test-sleep
Apr 24, 2026
Merged

fix: replace Thread.sleep() with synchronization in TmuxEncodingTest#1827
gnodet merged 1 commit into
masterfrom
fix/tmux-encoding-test-sleep

Conversation

@gnodet
Copy link
Copy Markdown
Member

@gnodet gnodet commented Apr 24, 2026

Summary

Summary by CodeRabbit

  • Tests
    • Improved test reliability by optimizing synchronization mechanisms in test infrastructure, replacing unconditional waits with event-driven signaling to ensure proper thread coordination during test execution.

Use CountDownLatch.await() for pane thread keep-alive and
Object.wait() for output polling, fixing SonarCloud S2925.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 24, 2026

📝 Walkthrough

Walkthrough

A test class is modified to improve synchronization by introducing a latch mechanism that replaces unconditional thread sleep with a wait condition. The polling loop now uses synchronized Object.wait(100) instead of Thread.sleep(100), and the test signals completion via the latch once expected output is detected.

Changes

Cohort / File(s) Summary
Test Synchronization
builtins/src/test/java/org/jline/builtins/TmuxEncodingTest.java
Replaces unconditional Thread.sleep() with synchronized Object.wait() under a latch mechanism, eliminating race conditions and reducing test execution time through proper signaling instead of blocking delays.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

bug, java

Poem

🐰 A test once slept without a care,
Now waits for signals in the air,
With latches true and locks aligned,
No more race conditions bind,
The timing's tight, the flow refined! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: replacing Thread.sleep() calls with synchronization mechanisms in TmuxEncodingTest, which is the primary focus of this pull request.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/tmux-encoding-test-sleep

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@builtins/src/test/java/org/jline/builtins/TmuxEncodingTest.java`:
- Around line 77-79: The synchronized wait on masterOut (synchronized
(masterOut) { masterOut.wait(100); }) is ineffective because nothing notifies
that monitor (ByteArrayOutputStream.write never calls notify), so replace the
pattern with a real event-driven signal or an explicit poll: either swap the
ByteArrayOutputStream instance (masterOut) for a small subclass/wrapper that
overrides write(...) to synchronize on masterOut and call notifyAll() after
writing so the existing synchronized wait becomes meaningful, or remove the
wait/notify dance and use a clear polling helper (e.g., Awaitility or a
short-loop with Thread.sleep and a timeout) around the contains("世") check;
touch the code paths involving masterOut, paneTerminal.writer().print/flush, and
the LineDisciplineTerminal output to ensure the writer signals the monitor if
you choose the notify-on-write approach.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d2e07a1d-545e-4898-aea4-30906c0ce25d

📥 Commits

Reviewing files that changed from the base of the PR and between 8212bf5 and d97f31f.

📒 Files selected for processing (1)
  • builtins/src/test/java/org/jline/builtins/TmuxEncodingTest.java

Comment on lines +77 to +79
synchronized (masterOut) {
masterOut.wait(100);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

masterOut.wait(100) is effectively a Thread.sleep(100) — nothing ever notifies this monitor.

Nothing in the code path (paneTerminal.writer().print/flushLineDisciplineTerminalByteArrayOutputStream.write) calls masterOut.notify() / notifyAll(). ByteArrayOutputStream's write methods do not notify waiters. As a result, every iteration will block the full 100 ms and the loop exits only via the 3 s deadline or the contains("世") check — exactly the same behavior as the previous Thread.sleep(100), just dressed up to silence SonarCloud S2925. A future reader will reasonably assume there is a producer-side notification and may be misled when modifying this code.

If the goal is a real event-driven wait, let the writer actually signal the monitor, e.g. by swapping ByteArrayOutputStream for a subclass that notifies on write:

♻️ Option A: notify on write so `wait(100)` is meaningful
-        ByteArrayOutputStream masterOut = new ByteArrayOutputStream();
+        ByteArrayOutputStream masterOut = new ByteArrayOutputStream() {
+            `@Override`
+            public synchronized void write(int b) {
+                super.write(b);
+                notifyAll();
+            }
+            `@Override`
+            public synchronized void write(byte[] b, int off, int len) {
+                super.write(b, off, len);
+                notifyAll();
+            }
+        };

With this in place the existing synchronized (masterOut) { masterOut.wait(100); } becomes a genuine latency-reducing wakeup instead of a fixed delay.

♻️ Option B: keep intent explicit with a well-known helper (e.g. Awaitility) or a small polling utility

Avoids the sleep-vs-wait debate entirely and documents the polling intent clearly.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@builtins/src/test/java/org/jline/builtins/TmuxEncodingTest.java` around lines
77 - 79, The synchronized wait on masterOut (synchronized (masterOut) {
masterOut.wait(100); }) is ineffective because nothing notifies that monitor
(ByteArrayOutputStream.write never calls notify), so replace the pattern with a
real event-driven signal or an explicit poll: either swap the
ByteArrayOutputStream instance (masterOut) for a small subclass/wrapper that
overrides write(...) to synchronize on masterOut and call notifyAll() after
writing so the existing synchronized wait becomes meaningful, or remove the
wait/notify dance and use a clear polling helper (e.g., Awaitility or a
short-loop with Thread.sleep and a timeout) around the contains("世") check;
touch the code paths involving masterOut, paneTerminal.writer().print/flush, and
the LineDisciplineTerminal output to ensure the writer signals the monitor if
you choose the notify-on-write approach.

@gnodet gnodet merged commit 2d44904 into master Apr 24, 2026
15 checks passed
@gnodet gnodet added the fix Bug fix label May 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant