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

Runs scheduled jobs on the warm pool and turns each into a `Juggernaut.Result`
(design notes §10.6-§10.8).

`run/3` keeps one job per worker: jobs are taken in queue order
(`Juggernaut.Runner.Schedule.queue/1`, longest first) and each goes to the first idle
worker. Each job runs in a process of its own, so a job that crashes the controller's
side is a `harness_error` for that mutant, never the end of the run. One job:

  1. its strike, when it has one (`Juggernaut.Runner.Schedule`), then, unless the
     strike killed, the rest of its tests, with the mutant on and `max_failures: 1`;
     the result is classified by `Juggernaut.Runner.Classify`;
  2. a harness error recycles the worker and runs the job again, at most twice more;
     still failing, the mutant is a `harness_error` (exit 3);
  3. a survivor whose runs found state that earlier jobs left on the worker and the
     snapshot could not put back (the peer's `residue`: a protected ETS table's
     contents, a project server's state) recycles the worker and runs again; the fresh
     worker's verdict stands (a reconfirmation, already fresh, is never run again);
  4. with `control?` (gate and release lanes), a kill is controlled: the job's runs
     (strike, then rest) run again with the same tests and seed, so in the same order,
     with every mutant off on the same worker, the last narrowed to the tests the kill's
     run reported (`control_runs/1`). It passes: `control: :passed`. It fails: the
     worker is recycled and
     the control runs first on the fresh worker, before anything the mutant could
     write. Failing there: `harness_error` "test fails without the mutant",
     `control: :failed`. Passing: the worker is recycled again, the mutant runs on it
     and that verdict stands (`:recovered`).

Dispatch stops early on `--max-mutants` (only the first N jobs are taken), `--budget`
(no job starts once the budget is spent) and `--fail-fast` (no job starts once
`Juggernaut.Verdict.failed_already?/3` holds). Jobs not run are `pending`, with the
reason; jobs already running finish.

The worker is reached through `:run_job` and `:recycle` functions
(`Juggernaut.Pool.Worker.run/3` and `recycle/2` by default), so the dispatch logic is
tested without peers. A worker whose calls answer an error (it failed to boot again
after a recycle) takes no more jobs; jobs left with no worker are `harness_error`.

# `outcome`

```elixir
@type outcome() :: %{
  results: %{required(String.t()) =&gt; Juggernaut.Result.t()},
  deferred: %{required(String.t()) =&gt; Juggernaut.Result.t()},
  stopped: String.t() | nil,
  footprints: %{
    required(String.t()) =&gt; [Juggernaut.Peer.Footprint.function_ref()]
  }
}
```

# `worker`

```elixir
@type worker() :: {pos_integer(), term()}
```

# `control_runs`

```elixir
@spec control_runs([{[test], map() | nil}]) :: [[test]] when test: {module(), atom()}
```

The test lists a kill's control runs, in order, from the kill's runs (`{tests, result}`,
`result` `nil` when not known): every run in full but the last, which is narrowed to
the tests its result shows ran: those it reported finished (`outcomes`) and those
ExUnit started but never reported (`unfinished`: an async module's test still running
when `max_failures` stopped the run), when it reported any.

The kill's run stopped at its first failure (`max_failures: 1`), so the tests ExUnit
never started never ran with the mutant on, and replaying them made a `suite` kill's
control cost a whole suite run (E5-1). A started test that was not reported did run
with the mutant on and could have leaked the state that killed, so it is replayed.
The order is unchanged: ExUnit shuffles a module's whole test list with the seed and
only then drops the tests not selected, and a worker names the modules to ExUnit in
the job's order (`module_order/1`, `Juggernaut.Peer.Runner.modules/2`) whatever the
subset, so sync modules run in the same relative order (`docs/dependencies/exunit.md`,
"Selecting tests"). A kill with no outcomes (a passed deadline, a dead worker) replays
its runs in full.

# `limits`

```elixir
@spec limits(map(), [{module(), atom()}]) :: {pos_integer(), pos_integer()}
```

The per-test timeout and the deadline (ms) of a run of `tests` in `job` (E3): its
integration lane's (`integration_timeouts`) when every test of the run is one of the
job's integration tests, else the job's own (its unit lane's).

# `module_order`

```elixir
@spec module_order([{module(), atom()}]) :: [module()]
```

The modules of a job's `tests` in the order the tests are scheduled (each module where
its first test is): the order a worker names them to ExUnit in
(`Juggernaut.Peer.Runner.modules/2`). With the killer and the tests that hit the site
first (`Juggernaut.Runner.Schedule.order/4`), their modules start first.

# `run`

```elixir
@spec run([map()], [worker()], keyword()) :: outcome()
```

Runs `jobs` (in dispatch order) on `workers`. Options:

  * `:inventory` (required) - `%{test_id => %{file:, line:}}`, for test references;
  * `:seed` (0), `:control?` (false);
  * `:footprint?` (false) - measure each job's footprint (`Juggernaut.Peer.Footprint`):
    the outcome's `footprints` then holds, by mutant id, the functions every run of the
    job executed with the mutant on, for the jobs whose runs were all measured;
  * `:max_mutants`, `:budget_ms` - limits on dispatch (none by default);
  * `:fail_fast` - `{mutants, known_results, config}`: stop once the gate has failed,
    counting the results of mutants that never get a job (`known_results`);
  * `:clock` - milliseconds now (`System.monotonic_time/1`);
  * `:on_result` - called with each `{result, job}` as it arrives;
  * `:run_job`, `:recycle`, `:boot_run` - the worker calls (see the module doc).

A job with a `:boot` key (`Juggernaut.Runner.BootLane.run/2` options) is a boot-lane
job: it runs on a fresh worker of its own through `:boot_run`, given its worker's slot
(the workers are then `{:boot, slot}`: one boot at a time per slot, so as many run at
once as there are slots). With `control?`, a boot-lane kill is
held until its slot's control has passed: the first boot-lane job's suite run once
through `:boot_run` with switch 0 in that slot, started there when the slot's first
kill arrives (never in a slot with none), while the other slots go on. Each slot has
its own database, which outlives the run, so a slot's control settles only that slot's
kills. Passing, the slot's held kills and every later one are `control: :passed`;
failing, each of them is a `harness_error` with `control: :failed`, while survivors
stand. The lane's control makes the suite's lane runs (its unit tests, then its
integration tests: `Juggernaut.Runner.BootLane.lane_runs?/2`), so it covers a kill in
either. A struck kill (its runs began with its strike, not with the lane runs) is not
held: its runs are
made again with switch 0 on a fresh boot in the same slot (`:boot_run` given
`:replay`), since the lane's control never makes them and a test that needs an earlier
test's state could fail first thing on a fresh boot; that control settles it the same
way. Without `control?` every boot-lane kill is `:skipped`.

A job with `warm_first: true` is a boot-set mutant tried on a warm worker first,
controlled as any job's. Its result never settles the mutant: on a warm worker the app
booted with the mutant off, so what boot built comes from the original code, and a test
that compares a direct call with that state can fail warm and pass in a real boot. The
result goes to the outcome's `deferred`, is not passed to `:on_result`, and the boot
lane decides the mutant; a warm kill (`strikes?/1`) only makes its tests the boot-lane
job's strike.
A job with `fresh?: true` (a reconfirmation) recycles its worker first. Unknown or
mistyped options raise `NimbleOptions.ValidationError`.

# `strikes?`

```elixir
@spec strikes?(Juggernaut.Result.t()) :: boolean()
```

Whether a warm-first job's `result` gives its boot-lane job a strike (its tests run
first there): a kill, controlled where the lane controls (a failed control is a
`harness_error`), other than a timeout (a slow test is worth no head start).

# `test_lane`

```elixir
@spec test_lane(map(), [{list(), term()}], map()) :: :unit | :integration | nil
```

The test lane of a kill (`Juggernaut.TestLanes`): `:integration` when the run that
killed (the last of a job's runs, which stop at the first that does not pass) was its
integration lane's, so every unit test of the job had passed first; else `:unit`.
`nil` for anything but a kill, and for a kill no test made (`:boot`: the app did not
start). `runs` are `{tests, result}`, as the runner makes them.

# `test_ref`

```elixir
@spec test_ref(
  {module(), atom()},
  map(),
  non_neg_integer() | nil,
  Juggernaut.TestRef.outcome() | nil
) ::
  Juggernaut.TestRef.t()
```

A test reference for `{module, name}` from the boot inventory.

---

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