ExZarr.Storage.Backend.Mock (ExZarr v1.1.0)

View Source

Mock storage backend for testing.

Provides a controllable storage backend that can simulate various scenarios including errors, delays, and state verification. Useful for testing error handling and edge cases without external dependencies.

Configuration

Optional configuration:

  • :pid - PID to receive operation messages (optional)
  • :error_mode - Simulate errors (:always, :random, or nil)
  • :delay - Simulate latency in milliseconds (optional)
  • :fail_on - List of operations to fail on (optional)

Example

# Register the mock backend
:ok = ExZarr.Storage.Registry.register(ExZarr.Storage.Backend.Mock)

# Create array with mock storage
{:ok, array} = ExZarr.create(
  shape: {100},
  chunks: {10},
  dtype: :int32,
  storage: :mock,
  pid: self()
)

# Operations will send messages to the test process
:ok = ExZarr.Array.set_slice(array, data, start: {0}, stop: {10})

# Verify operation was called
assert_received {:mock_storage, :write_chunk, [{0}, _data]}

Error Simulation

# Always fail
{:ok, array} = ExZarr.create(
  storage: :mock,
  error_mode: :always
)

# Fail specific operations
{:ok, array} = ExZarr.create(
  storage: :mock,
  fail_on: [:write_chunk, :read_metadata]
)

# Random failures (50% chance)
{:ok, array} = ExZarr.create(
  storage: :mock,
  error_mode: :random
)

Latency Simulation

# Simulate 100ms delay on all operations
{:ok, array} = ExZarr.create(
  storage: :mock,
  delay: 100
)

Testing Patterns

The mock backend is useful for:

  • Testing error handling without external services
  • Simulating network latency
  • Verifying operation sequences
  • Testing concurrent access patterns
  • Integration testing without dependencies

Summary

Functions

Get call count for operations.

Get the current state of a mock storage backend for verification.

Reset the mock storage state.

Functions

call_count(state, operation)

Get call count for operations.

Example

count = ExZarr.Storage.Backend.Mock.call_count(array.storage.state, :write_chunk)
assert count == 10

get_state(state)

Get the current state of a mock storage backend for verification.

Example

state = ExZarr.Storage.Backend.Mock.get_state(array.storage.state)
assert map_size(state.chunks) == 5

reset(state)

Reset the mock storage state.

Example

:ok = ExZarr.Storage.Backend.Mock.reset(array.storage.state)