Mediate.Test (mediate v0.1.0)

Copy Markdown View Source

Helpers every test tier and a third party's adapter suite share:

  • the configuration override
  • the clock a test sets
  • the settle of the configured adapter's own state
  • a poll with a deadline in place of a sleep

The core package ships them, so Tier 1 can run outside this repository.

Summary

Functions

The access events the current process published while fun ran, in order, with what fun returned. Only this process's reads count.

The change events the current process published while fun ran, in order, with what fun returned. Only this process's changes count, so async tests never see one another's.

Calls fun until it returns a truthy value or timeout milliseconds pass. Returns the truthy value. Raises with the last value on timeout. This is the one place the suite sleeps.

The poll interval, the floor of any measurement poll/2 takes, in milliseconds.

The SQL statements the current process ran on repo while fun ran, in order. They come from the repo's [..., :query] telemetry, without transaction control. Only this process's queries count, so async tests never see one another's.

Bring the configured adapter's own state into step with the tables and answer :ok. An adapter that keeps no state of its own has nothing to settle, and the answer is :none. So a shared scenario can settle after it writes facts without an adapter's name. A settle that fails raises what it failed with, because a scenario that cannot settle cannot ask its question.

Override configuration fields for the rest of the current process. Mediate.Config.resolve/0 reads the override from the caller and from its $callers chain, over the boot struct when one exists.

Override configuration fields around a function, and restore the previous override after it.

Functions

accesses(fun)

@spec accesses((-> term())) :: {term(), [map()]}

The access events the current process published while fun ran, in order, with what fun returned. Only this process's reads count.

changes(fun)

@spec changes((-> term())) :: {term(), [map()]}

The change events the current process published while fun ran, in order, with what fun returned. Only this process's changes count, so async tests never see one another's.

poll(fun, timeout \\ 5000)

@spec poll((-> term()), pos_integer()) :: term()

Calls fun until it returns a truthy value or timeout milliseconds pass. Returns the truthy value. Raises with the last value on timeout. This is the one place the suite sleeps.

poll_interval()

@spec poll_interval() :: pos_integer()

The poll interval, the floor of any measurement poll/2 takes, in milliseconds.

queries(repo, fun)

@spec queries(module(), (-> term())) :: {term(), [String.t()]}

The SQL statements the current process ran on repo while fun ran, in order. They come from the repo's [..., :query] telemetry, without transaction control. Only this process's queries count, so async tests never see one another's.

settle()

@spec settle() :: :ok | :none

Bring the configured adapter's own state into step with the tables and answer :ok. An adapter that keeps no state of its own has nothing to settle, and the answer is :none. So a shared scenario can settle after it writes facts without an adapter's name. A settle that fails raises what it failed with, because a scenario that cannot settle cannot ask its question.

with_config(overrides)

@spec with_config(keyword()) :: :ok

Override configuration fields for the rest of the current process. Mediate.Config.resolve/0 reads the override from the caller and from its $callers chain, over the boot struct when one exists.

with_config(overrides, fun)

@spec with_config(keyword(), (-> result)) :: result when result: term()

Override configuration fields around a function, and restore the previous override after it.