SnakeBridge.Examples (SnakeBridge v0.16.0)

Copy Markdown View Source

Helpers for SnakeBridge example projects.

Provides failure tracking to hard-fail on unexpected errors in demo scripts, and telemetry-based dispatch monitoring to deterministically wait for pool dispatch events instead of using Process.sleep.

Summary

Functions

Raises if any failures were recorded in the current process.

Raises if result is an {:error, reason} tuple.

Attaches a telemetry handler that forwards [:snakepit, :pool, :call, :dispatched] events to the calling process as {:call_dispatched, metadata} messages.

Waits for a {:call_dispatched, metadata} message from an attached dispatch monitor.

Detaches a previously attached dispatch monitor by handler ID.

Returns the current per-process failure count.

Increments the per-process failure counter by one.

Resets the per-process failure counter to zero.

Functions

assert_no_failures!()

@spec assert_no_failures!() :: :ok

Raises if any failures were recorded in the current process.

assert_script_ok(result)

@spec assert_script_ok(term()) :: :ok

Raises if result is an {:error, reason} tuple.

attach_dispatch_monitor(handler_id \\ nil)

@spec attach_dispatch_monitor(String.t() | nil) :: String.t()

Attaches a telemetry handler that forwards [:snakepit, :pool, :call, :dispatched] events to the calling process as {:call_dispatched, metadata} messages.

Returns the handler ID, which should be passed to detach_dispatch_monitor/1 when monitoring is no longer needed.

await_dispatch(opts \\ [])

@spec await_dispatch(keyword()) :: map() | nil

Waits for a {:call_dispatched, metadata} message from an attached dispatch monitor.

Options

  • :timeout - maximum wait time in milliseconds (default: 2_000)
  • :command - if set, only matches dispatches for the given command

Returns the dispatch metadata map, or nil on timeout.

detach_dispatch_monitor(handler_id)

@spec detach_dispatch_monitor(String.t()) :: :ok | {:error, :not_found}

Detaches a previously attached dispatch monitor by handler ID.

failure_count()

@spec failure_count() :: non_neg_integer()

Returns the current per-process failure count.

record_failure()

@spec record_failure() :: :ok

Increments the per-process failure counter by one.

reset_failures()

@spec reset_failures() :: :ok

Resets the per-process failure counter to zero.