FauxRedis.Server (faux_redis v1.0.2)

Copy Markdown View Source

OTP process that owns the TCP listener, manages connection processes, holds the in-memory store and mock rules, and implements Redis command semantics.

Users normally interact with this module indirectly via the FauxRedis facade, but the functions here are public to make supervision and advanced control easier when needed.

Summary

Types

Internal server mode.

t()

Server state kept in the GenServer.

Functions

Registers a new stub or expectation rule.

Returns the list of recorded calls, ordered from oldest to newest.

Returns a child spec suitable for supervision trees.

Returns the TCP port the server is listening on.

Resets the server state (store, rules, history, auth, pub/sub).

Starts a new server process.

Types

mode()

@type mode() :: :mock_first | :stateful_only | :mock_only

Internal server mode.

t()

@type t() :: %FauxRedis.Server{
  authed: MapSet.t(non_neg_integer()),
  calls: [FauxRedis.call_record()],
  channels: %{required(binary()) => MapSet.t(pid())},
  connections: %{required(non_neg_integer()) => pid()},
  ets_kv: term(),
  listener: port() | nil,
  max_calls: pos_integer(),
  mode: mode(),
  next_conn_id: non_neg_integer(),
  password: binary() | nil,
  port: non_neg_integer() | nil,
  require_auth?: boolean(),
  rules: [FauxRedis.MockRule.t()],
  store: FauxRedis.Store.t(),
  subscriptions: %{required(pid()) => MapSet.t(binary())}
}

Server state kept in the GenServer.

Functions

add_rule(server, kind, opts)

@spec add_rule(GenServer.server(), FauxRedis.MockRule.kind(), Keyword.t()) ::
  {:ok, reference()}

Registers a new stub or expectation rule.

This is the backing implementation for FauxRedis.stub/2, stub/3 and FauxRedis.expect/3.

calls(server)

@spec calls(GenServer.server()) :: [FauxRedis.call_record()]

Returns the list of recorded calls, ordered from oldest to newest.

child_spec(init_arg)

@spec child_spec(Keyword.t()) :: Supervisor.child_spec()

Returns a child spec suitable for supervision trees.

port(server)

@spec port(GenServer.server()) :: non_neg_integer()

Returns the TCP port the server is listening on.

reset!(server)

@spec reset!(GenServer.server()) :: :ok

Resets the server state (store, rules, history, auth, pub/sub).

start_link(opts \\ [])

@spec start_link(Keyword.t()) :: GenServer.on_start()

Starts a new server process.

Options:

  • :port – TCP port to listen on (0 = random free port, default)
  • :mode:mock_first (default), :stateful_only, or :mock_only
  • :require_auth? – whether AUTH is required before most commands
  • :password – the expected password for AUTH
  • :name – optional registered name (e.g. via Registry)