Skip to content

Harden timing-sensitive tests: replace Process.sleep with proper synchronization #5

@ppsplus-bradh

Description

@ppsplus-bradh

Problem

Several tests use Process.sleep for synchronization instead of message-based waiting (assert_receive, MockCLI.poll_until, etc.). These are brittle and can fail on slow or fast CI runners.

Additionally, some assertions are effectively meaningless due to overly loose conditions.

Affected Tests

Process.sleep used for synchronization

test/claude_code/session_test.exs (lines 110, 118, 311, 324, 453, 461, 811, 851)

  • Process.sleep(50) between action and state inspection
  • Should use assert_receive or poll-based waiting

test/claude_code_test.exs (lines 197, 213)

  • Process.sleep(100) after stop() before checking Process.alive?
  • Should poll or use monitor-based waiting

test/claude_code/supervisor_test.exs (lines 65, 115, 211, 237, 264)

  • Process.sleep(100) for supervisor restart/shutdown timing
  • Should use assert_receive on supervisor events or poll child counts

Meaningless assertions

test/claude_code/supervisor_test.exs (line 117)

  • assert ClaudeSupervisor.count_sessions(supervisor) >= 0 — always true, provides no test value
  • Should assert a specific expected count or use polling to wait for a known state

Recommended Approach

The codebase already has a good pattern in port_test.exs using MockCLI.poll_until:

MockCLI.poll_until(fn ->
  state = :sys.get_state(adapter)
  if condition_met?(state), do: {:ok, state}, else: :retry
end, timeout: 5000)

Replace Process.sleep + assertion pairs with this pattern or assert_receive where applicable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions