PropertyDamage.MockServiceRegistry (PropertyDamage v0.2.0)

View Source

Registry for managing mock service adapters and their state.

The MockServiceRegistry:

  • Manages state for all active mock adapters
  • Collects events injected by mocks
  • Notifies mocks of commands and events
  • Provides projection state to mocks for request handling

Lifecycle

  1. start_link/1 - Start the registry
  2. register/2 - Register mock adapters
  3. During execution:
  4. stop/1 - Stop the registry

Usage

{:ok, registry} = MockServiceRegistry.start_link([])

# Register mock adapters
MockServiceRegistry.register(registry, PaymentMock)
MockServiceRegistry.register(registry, EmailMock)

# During test execution
MockServiceRegistry.notify_command(registry, command)
MockServiceRegistry.update_projections(registry, projections)

# Mock calls push_event when SUT calls them
MockServiceRegistry.push_event(registry, PaymentMock, %PaymentProcessed{})

# Executor collects injected events
events = MockServiceRegistry.flush_events(registry)

# Notify mocks of events
for event <- events do
  MockServiceRegistry.notify_event(registry, event)
end

Summary

Functions

Returns a specification to start this module under a supervisor.

Flush all pending injected events.

Get the combined state for request handling.

Get the current state for a mock adapter.

Notify all mocks of a command being executed.

Notify all mocks of an event.

Push an event from a mock adapter.

Push multiple events from a mock adapter.

Register a mock adapter with the registry.

Start the mock service registry.

Stop the registry.

Unregister a mock adapter.

Update the projection state available to mocks.

Update the state for a mock adapter.

Types

t()

@type t() :: pid()

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

flush_events(registry)

@spec flush_events(t()) :: [struct()]

Flush all pending injected events.

Returns events in the order they were pushed. Events are cleared from the registry after flushing.

get_handler_state(registry, adapter_module)

@spec get_handler_state(t(), module()) :: {:ok, map()} | {:error, :not_found}

Get the combined state for request handling.

Returns mock state merged with current projections.

get_state(registry, adapter_module)

@spec get_state(t(), module()) :: {:ok, map()} | {:error, :not_found}

Get the current state for a mock adapter.

Used by mock handlers to access their state during request handling.

notify_command(registry, command)

@spec notify_command(
  t(),
  struct()
) :: :ok

Notify all mocks of a command being executed.

Each mock's on_command/2 is called with the command.

notify_event(registry, event)

@spec notify_event(
  t(),
  struct()
) :: :ok

Notify all mocks of an event.

Each mock's on_event/2 is called with the event.

push_event(registry, adapter_module, event)

@spec push_event(t(), module(), struct()) :: :ok

Push an event from a mock adapter.

Called by mock adapters when they inject events.

push_events(registry, adapter_module, events)

@spec push_events(t(), module(), [struct()]) :: :ok

Push multiple events from a mock adapter.

register(registry, adapter_module)

@spec register(t(), module()) :: :ok

Register a mock adapter with the registry.

The adapter's init_state/0 is called to initialize its state.

start_link(opts \\ [])

@spec start_link(keyword()) :: {:ok, pid()} | {:error, term()}

Start the mock service registry.

Options

  • :name - Optional name for the registry process

stop(registry)

@spec stop(t()) :: :ok

Stop the registry.

unregister(registry, adapter_module)

@spec unregister(t(), module()) :: :ok

Unregister a mock adapter.

update_projections(registry, projections)

@spec update_projections(t(), map()) :: :ok

Update the projection state available to mocks.

Called after each command execution so mocks have current state.

update_state(registry, adapter_module, new_state)

@spec update_state(t(), module(), map()) :: :ok

Update the state for a mock adapter.

Used by mock handlers if they need to update state during request handling.