Skip to content

Commit 80e05df

Browse files
authored
chore: fix flaky DumbTerminalPasswordTest (#1287)
1 parent d41dcb9 commit 80e05df

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

reader/src/test/java/org/jline/reader/impl/DumbTerminalPasswordTest.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,29 @@ public void testPasswordMaskingWithDumbTerminal() throws IOException {
4242
assertTrue(output.contains("Password: "));
4343

4444
// The output should contain multiple instances of the prompt due to the masking thread
45-
int promptCount = countOccurrences(output, "Password: ");
46-
assertTrue(promptCount > 1, "Expected multiple instances of the prompt due to masking thread");
45+
// Add a delay to ensure the masking thread has time to run
46+
// The masking thread in LineReaderImpl runs every 3ms, so we need to wait
47+
// long enough for multiple refreshes to occur
48+
int promptCount = 0;
49+
long startTime = System.currentTimeMillis();
50+
long timeout = 500; // 500ms should be plenty of time
51+
52+
while (promptCount <= 1 && (System.currentTimeMillis() - startTime) < timeout) {
53+
try {
54+
Thread.sleep(50);
55+
} catch (InterruptedException e) {
56+
// Ignore
57+
}
58+
59+
// Get the output again after the delay
60+
output = out.toString();
61+
promptCount = countOccurrences(output, "Password: ");
62+
}
63+
64+
assertTrue(
65+
promptCount > 1,
66+
"Expected multiple instances of the prompt due to masking thread. " + "Prompt count: " + promptCount
67+
+ ", Output: " + output);
4768
}
4869
}
4970

0 commit comments

Comments
 (0)