LexCredo.Check.Warning.NoProcessSleepInTests (LexCredo v0.1.0)

Copy Markdown View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

This check has a base priority of high and works with any version of Elixir.

Explanation

Avoid Process.sleep/1 and Process.alive?/1 in test files.

Process.sleep/1 creates brittle, timing-dependent tests. Use Process.monitor/1 with assert_receive {:DOWN, ...} to wait for a process to finish, and Process.alive?/1 can be replaced with the same pattern.

To synchronise before the next call, use :sys.get_state/1 to ensure the process has handled prior messages.

# BAD
Process.sleep(100)
assert Process.alive?(pid)

# GOOD — wait for process to finish
ref = Process.monitor(pid)
assert_receive {:DOWN, ^ref, :process, ^pid, :normal}

# GOOD — ensure prior messages have been processed
_ = :sys.get_state(server)

Check-Specific Parameters

Use the following parameters to configure this check:

:exclude_test_files

When true, skips test files (effectively disabling this check). Default: false.

This parameter defaults to false.

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.