Skip to content

Replace Thread.sleep with CountDownLatch in tests with short sleeps (10-50ms) #559

Description

@stalep

Summary

Several test files use Thread.sleep with very short durations (10-50ms) to wait for async command execution. These are the most likely to flake on CI since the sleep may not be long enough for slower CI runners.

Proposed fix

Replace Thread.sleep + assertion with CommandExecutionListener + CountDownLatch pattern, as demonstrated in CommandExecutionListenerTest.java and the fix for AeshCommandInvocationServiceTest (commit 897d0b95).

Pattern:

CountDownLatch latch = new CountDownLatch(1);
// Register via SettingsBuilder:
.commandExecutionListener((line, result, durationMs) -> latch.countDown())

connection.read("command" + Config.getLineSeparator());
assertTrue(latch.await(5, TimeUnit.SECONDS));
// Assert output...

Files to convert (priority order by sleep duration)

File Sleep(ms) Occurrences
AeshCommandInputTest.java 10ms 3
AeshCommandDynamicTest.java 10ms 1
AeshScriptTest.java 20ms 1
AeshCommandOptionValidatorTest.java 20ms 1
AeshCommandNotFoundHandlerTest.java 50ms 1
AeshConverterInvocationProviderTest.java 50ms 1
AeshCommandPasteTest.java 50ms 1
ConsoleRedirectionTest.java 50ms 6
ExportCommandTest.java 50ms 1
AeshCommandRegistryTest.java 50ms 3

Notes

  • Not all Thread.sleep calls wait for command execution — some wait for completion rendering or other async events. TestConnection.waitForOutputContaining() may be more appropriate for output-based waits.
  • Each test should be verified individually after conversion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions