Modeling the Circuit Breaker: The Test Module
Explore modeling a circuit breaker using state machine properties in PropEr with Elixir. Understand how to define preconditions, update state after commands, and validate postconditions to create robust, reliable tests for real-world stateful systems.
We'll cover the following...
We'll cover the following...
The preconditions
Let’s start off by taking a look at the preconditions.
### Picks whether a command should be valid
def precondition(:unregistered, :ok, _, {:call, _, call, _}) do
call == :success
end
def precondition(:ok, to, %{errors: n, limit: l}, {:call, _, :err, _}) do
(to == :tripped and n + 1 == l) or (to == :ok and n + 1 != l)
end
def precondition(
:ok,
to,
%{timeouts: n, limit: l},
{:call, _, :timeout, _}
) do
(to == :tripped and n + 1 == l) or (to == :ok and n + 1 != l)
end
def precondition(_from, _to, _data, _call) do
true
end
Notice how both calls to erroneous cases are only valid in mutually exclusive instances:
(to ==