View Source ProcessHub.Utility.Bag (ProcessHub v0.4.0-beta)

Utility functions for testing.

Summary

Functions

Returns all messages in the mailbox.

Generates child specs for testing.

Retrieves the value associated with a key from a list of key-value tuples.

Sends hook messages to the given process.

Waits and receives multiple messages.

Generates a hook manager for receiving messages.

Returns the current timestamp in the given precision.

Types

Link to this type

gen_child_specs_opts()

View Source
@type gen_child_specs_opts() :: [prefix: String.t(), id_type: :string | :atom]

Functions

@spec all_messages() :: [term()]

Returns all messages in the mailbox.

Link to this function

gen_child_specs(count, opts \\ [])

View Source
@spec gen_child_specs(integer(), gen_child_specs_opts()) :: list()

Generates child specs for testing.

Link to this function

get_by_key(list, key, default \\ nil)

View Source
@spec get_by_key([{any(), any()}], any(), any()) :: any()

Retrieves the value associated with a key from a list of key-value tuples.

This function searches through a list of {key, value} tuples and returns the value associated with the first matching key. If no matching key is found, the default value is returned.

Parameters

  • list - A list of {key, value} tuples to search through
  • key - The key to search for
  • default - The value to return if the key is not found (default: nil)

Examples

iex> ProcessHub.Utility.Bag.get_by_key([{:a, 1}, {:b, 2}, {:c, 3}], :b)
2

iex> ProcessHub.Utility.Bag.get_by_key([{:a, 1}, {:b, 2}], :c, :not_found)
:not_found

iex> ProcessHub.Utility.Bag.get_by_key([], :any_key, "default")
"default"

iex> ProcessHub.Utility.Bag.get_by_key([{"key1", "value1"}, {"key2", "value2"}], "key1")
"value1"

Notes

  • Only searches for exact key matches using strict equality (===)
  • Returns the value from the first matching tuple found
  • Works with any key/value types (atoms, strings, integers, etc.)
  • Gracefully ignores non-tuple elements in the list
  • Only considers tuples with at least 2 elements (key-value pairs)
Link to this function

hook_erlang_send(hook_data, pid, msg)

View Source
@spec hook_erlang_send(
  term(),
  atom() | pid() | port() | reference() | {atom(), node()},
  any()
) :: any()

Sends hook messages to the given process.

Link to this function

receive_multiple(x, receive_key, opts \\ [])

View Source
@spec receive_multiple(pos_integer(), term(), Keyword.t()) :: any()

Waits and receives multiple messages.

Link to this function

recv_hook(key, recv_pid)

View Source
@spec recv_hook(atom(), pid()) :: ProcessHub.Service.HookManager.t()

Generates a hook manager for receiving messages.

Link to this function

timestamp(precision \\ :second)

View Source
@spec timestamp(:microsecond | :millisecond | :nanosecond | :second | pos_integer()) ::
  integer()

Returns the current timestamp in the given precision.