Supertester.Assertions (supertester v0.1.0)

View Source

Custom assertions for OTP patterns and system behavior.

This module provides specialized assertions that understand OTP patterns, making tests more expressive and providing better error messages.

Usage

import Supertester.Assertions

test "my test" do
  assert_process_alive(server_pid)
  assert_genserver_responsive(server_pid)
end

Summary

Functions

Asserts that all children of a supervisor are alive.

Asserts that a supervisor has the expected number of children.

Asserts that a GenServer handles a message correctly.

Asserts that a GenServer is responsive.

Asserts that a GenServer has the expected state.

Asserts that memory usage remains stable during an operation.

Asserts that no processes are leaked during an operation.

Asserts that performance is within expected bounds.

Asserts that a process is alive.

Asserts that a process is dead.

Asserts that a process has been restarted.

Asserts that a supervisor has the expected strategy.

Functions

assert_all_children_alive(supervisor)

@spec assert_all_children_alive(Supervisor.supervisor()) :: :ok

Asserts that all children of a supervisor are alive.

Parameters

  • supervisor - The supervisor pid or name

Example

assert_all_children_alive(supervisor)

assert_child_count(supervisor, expected_count)

@spec assert_child_count(Supervisor.supervisor(), non_neg_integer()) :: :ok

Asserts that a supervisor has the expected number of children.

Parameters

  • supervisor - The supervisor pid or name
  • expected_count - The expected number of children

Example

assert_child_count(supervisor, 3)

assert_genserver_handles_message(server, message, expected_response)

@spec assert_genserver_handles_message(GenServer.server(), term(), term()) :: :ok

Asserts that a GenServer handles a message correctly.

Parameters

  • server - The GenServer pid or name
  • message - The message to send
  • expected_response - The expected response

Example

assert_genserver_handles_message(server, :get_counter, {:ok, 5})

assert_genserver_responsive(server)

@spec assert_genserver_responsive(GenServer.server()) :: :ok

Asserts that a GenServer is responsive.

Parameters

  • server - The GenServer pid or name

Example

assert_genserver_responsive(server)

assert_genserver_state(server, expected_state)

@spec assert_genserver_state(GenServer.server(), term() | (term() -> boolean())) ::
  :ok

Asserts that a GenServer has the expected state.

Parameters

  • server - The GenServer pid or name
  • expected_state - The expected state or a function to test the state

Example

assert_genserver_state(server, %{counter: 5})
assert_genserver_state(server, fn state -> state.counter > 0 end)

assert_memory_usage_stable(operation_fun, tolerance \\ 0.1)

@spec assert_memory_usage_stable((-> any()), float()) :: :ok

Asserts that memory usage remains stable during an operation.

Parameters

  • operation_fun - Function to execute
  • tolerance - Memory tolerance as a percentage (default: 0.1)

Example

assert_memory_usage_stable(fn -> 
  Enum.each(1..1000, fn _ -> GenServer.call(server, :increment) end)
end, 0.05)

assert_no_process_leaks(operation_fun)

@spec assert_no_process_leaks((-> any())) :: :ok

Asserts that no processes are leaked during an operation.

Parameters

  • operation_fun - Function to execute

Example

assert_no_process_leaks(fn ->
  {:ok, server} = GenServer.start_link(MyServer, [])
  GenServer.stop(server)
end)

assert_performance_within_bounds(benchmark_result, expectations)

@spec assert_performance_within_bounds(map(), map()) :: :ok

Asserts that performance is within expected bounds.

Parameters

  • benchmark_result - Result from performance testing
  • expectations - Map of performance expectations

Example

expectations = %{max_time: 1000, max_memory: 1_000_000}
assert_performance_within_bounds(result, expectations)

assert_process_alive(pid)

@spec assert_process_alive(pid()) :: :ok

Asserts that a process is alive.

Parameters

  • pid - The process ID to check

Example

assert_process_alive(server_pid)

assert_process_dead(pid)

@spec assert_process_dead(pid()) :: :ok

Asserts that a process is dead.

Parameters

  • pid - The process ID to check

Example

assert_process_dead(old_pid)

assert_process_restarted(process_name, original_pid)

@spec assert_process_restarted(atom(), pid()) :: :ok

Asserts that a process has been restarted.

Parameters

  • process_name - The registered name of the process
  • original_pid - The original PID before restart

Example

original_pid = GenServer.whereis(MyServer)
GenServer.stop(MyServer)
assert_process_restarted(MyServer, original_pid)

assert_supervisor_strategy(supervisor, expected_strategy)

@spec assert_supervisor_strategy(Supervisor.supervisor(), atom()) :: :ok

Asserts that a supervisor has the expected strategy.

Parameters

  • supervisor - The supervisor pid or name
  • expected_strategy - The expected supervision strategy

Example

assert_supervisor_strategy(supervisor, :one_for_one)