# `Juggernaut.Peer.Runner`
[🔗](https://github.com/holsee/juggernaut/blob/v0.4.0/lib/juggernaut/peer/runner.ex#L1)

Runs one job in a booted worker (design notes §10.5): the selected tests, with one
switch on, through ExUnit again in the same VM.

A job is a map:

  * `only_test_ids` - a `MapSet` of `{module, test_name}`. ExUnit silently runs nothing
    for a list (`docs/dependencies/exunit.md`), so anything else is refused;
  * `switch` - the switch on while the tests run (`0` for none, `-1` to probe);
  * `seed`, `timeout_ms` (per test), `max_failures` (a positive integer or
    `:infinity`);
  * `footprint` - `true` to measure which of the project's functions the tests ran
    (`Juggernaut.Peer.Footprint`), for the result cache;
  * `order` - the modules in the order to name them to ExUnit (`modules/2`); the
    selected modules it leaves out follow, sorted;
  * `left` - `true` to report, under `left`, what the job leaves that the restore could
    not put back (the same check as `residue`, after the job instead of before), so the
    host can recycle the worker before the next job rather than run that job again; the
    check costs a read of the project's servers and tables, so only the probe asks;
  * `load_pending` - `true` to load first the test files the boot left for later
    (`Juggernaut.Peer.Boot.load_pending/0`; a boot-lane job's runs after its first).

`run_job/1`:

  0. with `load_pending`, loads the test files the boot left; a file that fails to load
     gives `status: :load_error` (with the `message`), and no test runs;
  1. checks the selection against the boot inventory (every id known);
  2. configures every ExUnit key the job relies on (settings persist between runs),
     with `Juggernaut.Peer.Formatter` as the only formatter, sets the switch, runs
     only the selected tests' modules, in the job's order (`modules/2`), and sets the
     switch back to 0. A probe job (switch `-1`) runs between
     `Juggernaut.Peer.Recorder.start_job/2` (this process as the job's root, the app's
     GenServers watched) and `finish_job/0`;
  3. checks that ExUnit ran exactly the tests selected (fewer only once
     `max_failures` stopped it);
  4. puts back the state the job changed (`Juggernaut.Peer.Snapshot.restore/1`);
  5. checks health against the boot fingerprint (`Juggernaut.Peer.Health.check/3`).

The result has `status: :ok`, `:load_error` or `:harness_error` (with a `message`;
never a verdict),
the outcome of each test that ran, `unfinished` (the tests ExUnit started but never
reported finished, in start order: an async module's test still running when
`max_failures` stopped the run), the counts, what was restored, `residue` (what
earlier jobs left that a restore cannot put back, found before this job ran:
`Juggernaut.Peer.Snapshot.residue/1`), `lasting` (what changed under the lasting
supervisors since boot, before this job ran: `Juggernaut.Peer.Health.lasting_residue/2`),
`left` (the `residue` check again after the job, when it asked; else `[]`),
the drift reasons,
the job's clock in microseconds, memory and atom counts after the job, and `hits`: a
probe job's `{site, label}` pairs (`Juggernaut.Peer.Recorder`), `[]` for any other, and
`footprint`: the functions run (or `:incomplete`) when measured, else `nil`.
Steps 4 and 5 run whatever steps 1 to 3 gave.

This module runs inside workers and is never mutated.

# `job`

```elixir
@type job() :: %{
  :only_test_ids =&gt; MapSet.t({module(), atom()}),
  optional(:switch) =&gt; integer(),
  optional(:seed) =&gt; non_neg_integer(),
  optional(:timeout_ms) =&gt; pos_integer(),
  optional(:max_failures) =&gt; pos_integer() | :infinity,
  optional(:footprint) =&gt; boolean(),
  optional(:order) =&gt; [module()],
  optional(:load_pending) =&gt; boolean()
}
```

# `counted`

```elixir
@spec counted(non_neg_integer(), [Juggernaut.Peer.Formatter.outcome()], boolean()) ::
  map() | {:error, String.t()}
```

The job's result from what the formatter reported, or `{:error, message}` when ExUnit
ran a different number of tests than `selected` (fewer is fine once `max_failures`
stopped the run).

# `modules`

```elixir
@spec modules(Enumerable.t({module(), atom()}), [module()]) :: [module()]
```

The modules of the selected tests `ids`, the list `ExUnit.run/1` is given: those
named in `order` first, in its order, then the others, sorted.

ExUnit runs sync modules one at a time in exactly that order, and starts async ones in
it, so the order must not depend on how many tests are selected: a `MapSet` of up to 32
elements iterates in sorted order, a larger one in hash order. Given the same `order`,
the modules of fewer of a run's tests (a kill's narrowed control,
`Juggernaut.Runner.control_runs/1`) run in the order they ran in the whole run. The
host gives the job's order (`Juggernaut.Runner.module_order/1`: the killer's and the
hitting tests' modules first), so a kill comes after fewer modules (D2).

# `run_job`

```elixir
@spec run_job(map()) :: map()
```

Runs `job` (see the module doc).

# `selection`

```elixir
@spec selection(term(), map()) :: {:ok, MapSet.t()} | {:error, String.t()}
```

`{:ok, ids}` when `ids` is a non-empty `MapSet` of tests in the inventory `tests`,
else `{:error, message}`.

# `switched`

```elixir
@spec switched(integer(), [atom()], (-&gt; result)) ::
  {result, [{pos_integer(), term()}]}
when result: term()
```

Runs `fun` with `switch` on and returns `{fun's result, hits}`. A probe switch runs it
between `Juggernaut.Peer.Recorder.start_job/2` (the calling process as the job's
root, `apps`' GenServers watched) and `finish_job/0`, whose hits are returned; any
other switch gives `[]`. The switch is set back to 0, and a probe's debug functions
removed, even when `fun` raises, throws or exits.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
