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
@type gen_child_specs_opts() :: [prefix: String.t(), id_type: :string | :atom]
Functions
@spec all_messages() :: [term()]
Returns all messages in the mailbox.
@spec gen_child_specs(integer(), gen_child_specs_opts()) :: list()
Generates child specs for testing.
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 throughkey- The key to search fordefault- 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)
@spec hook_erlang_send( term(), atom() | pid() | port() | reference() | {atom(), node()}, any() ) :: any()
Sends hook messages to the given process.
@spec receive_multiple(pos_integer(), term(), Keyword.t()) :: any()
Waits and receives multiple messages.
@spec recv_hook(atom(), pid()) :: ProcessHub.Service.HookManager.t()
Generates a hook manager for receiving messages.
@spec timestamp(:microsecond | :millisecond | :nanosecond | :second | pos_integer()) :: integer()
Returns the current timestamp in the given precision.