DdDataStreams.Testing.Backend (DD Data Streams v0.3.0)

Copy Markdown View Source

An owner-scoped backend that records checkpoints per test, with NimbleOwnership.

Configure it in the test environment and start its ownership server from test_helper.exs:

# config/test.exs
config :dd_data_streams, backend: DdDataStreams.Testing.Backend

# test/test_helper.exs
{:ok, _} = DdDataStreams.Testing.Backend.start_link()

A process records only when it owns the checkpoints or was allowed to use them, so a test that never opts in sees the library exactly as if it were disabled. DdDataStreams.Testing is the public interface; this module is the seam it installs.

The ownership server is private by default, which is what makes concurrent tests independent. DdDataStreams.Testing.set_global/1 switches it to the shared, single-owner mode for an async: false case. Production code never starts this server and never depends on it running: without it enabled?/0 is false and record/1 drops the checkpoint.

Summary

Functions

Allows allowed to record into owner.

The checkpoints recorded for owner, oldest first.

A child specification for starting the ownership server in a supervision tree, instead of from test_helper.exs.

Picks private mode for an async case and shared mode otherwise.

Switches the ownership server to shared mode, with the calling process as the owner every caller records into.

Switches the ownership server to private mode, the default.

Makes owner an owner of the checkpoints, so its own calls and its $callers record into it.

Starts the ownership server under this module's name.

Subscribes the calling owner to {DdDataStreams.Testing, :checkpoint, point} messages, in addition to the pull API in checkpoints/1.

Functions

allow(owner, allowed)

@spec allow(pid() | atom(), pid() | atom()) ::
  :ok | {:error, NimbleOwnership.Error.t()}

Allows allowed to record into owner.

checkpoints(owner \\ self())

@spec checkpoints(pid() | atom()) :: [map()]

The checkpoints recorded for owner, oldest first.

child_spec(opts)

@spec child_spec(keyword()) :: Supervisor.child_spec()

A child specification for starting the ownership server in a supervision tree, instead of from test_helper.exs.

set_from_context(context)

@spec set_from_context(map()) :: :ok

Picks private mode for an async case and shared mode otherwise.

set_global(arg1)

@spec set_global(map()) :: :ok

Switches the ownership server to shared mode, with the calling process as the owner every caller records into.

set_private(context \\ %{})

@spec set_private(map()) :: :ok

Switches the ownership server to private mode, the default.

start(owner \\ self())

@spec start(pid() | atom()) :: :ok

Makes owner an owner of the checkpoints, so its own calls and its $callers record into it.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Starts the ownership server under this module's name.

Accepts the options NimbleOwnership.start_link/1 accepts. Calling it when the server already runs returns {:error, {:already_started, pid}}, as any named process does; a test helper usually starts it once.

subscribe()

@spec subscribe() :: :ok

Subscribes the calling owner to {DdDataStreams.Testing, :checkpoint, point} messages, in addition to the pull API in checkpoints/1.