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

Turns each mutant's selection into a job and orders the jobs (design notes §10.6,
§10.7). Pure: the durations come from the baseline, the previous killers from the
last run.

A job's tests are ordered with the previous killer first, then the tests the probe saw
hit the site (`hits` of a `:suite` or `:file` selection; every test of a `:tests` one),
then the rest, each group by ascending recorded duration (the cheapest first). ExUnit
decides the order of tests inside a module (shuffled by the seed); a worker names the
modules to it in the order of the job's tests (`Juggernaut.Peer.Runner.modules/2`), so
the modules of the killer and of the hits start first. The order is made firmer by a
*strike*: a first run of the strike tests alone, and the rest only when the strike
does not kill.

  * a previous killer among the job's tests is the strike;
  * otherwise, a selection of the tests that hit the site (`:tests`) with at least
    three tests strikes with the cheapest of them, since most mutants of code that many
    tests reach die to any of them;
  * otherwise, a `:suite` or `:file` selection whose hits are some but not all of its
    tests strikes with the hits: a site that runs outside any test (a cast, a
    `setup_all`) is still most often killed by the tests that call it (D2);
  * otherwise there is no strike: the job runs every test at once.

Test lanes (E1, `Juggernaut.TestLanes`): a job's unit tests come first and its
integration tests (`:integration`) after them, each lane ordered as above, and its
runs are staged (`stages/3`): the unit lane's (its strike, then the rest), then, only
when every one of them passed, the integration lane's (its own strike, then the rest).
So a mutant that only an integration test kills was run against every unit test of
its selection first. A job with no integration test runs as before.

Timeouts are calibrated from the baseline, per lane (E3): each test's ExUnit timeout is
`max(min_ms, multiplier × the slowest selected test of the lane)`, and the lane's
deadline is `max(that timeout, multiplier × the lane's estimated cost)`, where the cost
is the sum of its selected tests' recorded durations (`Juggernaut.Pool.Worker` adds
its slack). The unit lane uses the `:timeout` option, the integration lane
`:integration_timeout`; a job's `timeout_ms` and `deadline_ms` are its unit lane's (its
integration lane's when it has no unit test), and `integration_timeouts` the
integration lane's.

Jobs are dispatched longest first (LPT) by estimated cost, ties by mutant id, each to
the first idle worker.

A boot-set mutant (`:boot`) is listed for the boot lane, and when tests run its code
again after boot (its selection's `warm` is `:tests`, `:file` or `:suite`) it also gets
a warm-first job (`warm_first: true`, selection `:warm_first`) with those tests, whose
result only gives the boot-lane job a strike (`Juggernaut.Runner.strikes?/1`). When its
function is in `:warm_safe` (a declaration that applies, `Juggernaut.WarmSafe`), that
warm job is an ordinary one instead (selection `:warm_first`, no `warm_first` flag): its
result is the verdict, and the mutant is not listed for the boot lane.

# `job`

```elixir
@type job() :: %{
  mutant: Juggernaut.Mutant.t(),
  selection: :tests | :file | :suite | :warm_first | :reconfirm,
  reason: String.t() | nil,
  tests: [test_id()],
  strike: [test_id()] | nil,
  integration: [test_id()],
  integration_timeouts: {pos_integer(), pos_integer()} | nil,
  cost_ms: non_neg_integer(),
  timeout_ms: pos_integer(),
  deadline_ms: pos_integer()
}
```

# `test_id`

```elixir
@type test_id() :: {module(), atom()}
```

# `timeout_config`

```elixir
@type timeout_config() :: [multiplier: number(), min_ms: pos_integer()]
```

# `cost_ms`

```elixir
@spec cost_ms([test_id()], %{required(test_id()) =&gt; non_neg_integer()}) ::
  non_neg_integer()
```

The estimated cost of running `tests`: their recorded durations summed, in ms.

# `integration_run?`

```elixir
@spec integration_run?([test_id()] | MapSet.t(), [test_id()] | MapSet.t()) ::
  boolean()
```

Whether a run of `tests` is an integration lane's: every one of them is in
`integration` (a list or set). An empty run is no lane's.

# `job`

```elixir
@spec job(Juggernaut.Mutant.t(), map(), keyword()) :: job()
```

One mutant's job from its selection (see `plan/2` for the options).

# `lanes`

```elixir
@spec lanes([test_id()], keyword()) :: %{
  tests: [test_id()],
  integration: [test_id()],
  timeout_ms: pos_integer(),
  deadline_ms: pos_integer(),
  integration_timeouts: {pos_integer(), pos_integer()} | nil
}
```

`tests` (in order) split into their lanes, with each lane's timeouts (see the module
doc; options as `plan/2`): `tests` with the unit tests first, `integration` (the
integration tests, in order), `timeout_ms` and `deadline_ms` (the unit lane's, or the
integration lane's when there is no unit test), and `integration_timeouts` (`nil` when
there is no integration test).

# `order`

```elixir
@spec order(
  [test_id()],
  %{required(test_id()) =&gt; non_neg_integer()},
  test_id() | nil,
  [test_id()]
) :: [
  test_id()
]
```

`tests` with `killer` first (when it is one of them), then those of `hits`, then the
rest, each group by ascending recorded duration (an unknown duration counts as 0), ties
by id.

# `plan`

```elixir
@spec plan([Juggernaut.Mutant.t()], keyword()) :: %{
  jobs: [job()],
  boot: [Juggernaut.Mutant.t()],
  no_coverage: [{Juggernaut.Mutant.t(), String.t()}]
}
```

The jobs of `mutants` (each with its `selection` by mutant id,
`Juggernaut.Coverage.Selection`), sorted for dispatch (`queue/1`), plus the mutants
for the boot lane and those no test covers. Options (all required):

  * `:selections` - `%{mutant_id => selection}`;
  * `:tests` - every runnable test, for a `:suite` selection;
  * `:durations` - µs by test id (the baseline);
  * `:killers` - the previous killer by mutant key;
  * `:timeout` - `[multiplier:, min_ms:]` from the config;
  * `:integration` - the integration tests (a set; optional, default none);
  * `:integration_timeout` - the integration lane's timeout config (optional, default
    `:timeout`; `Juggernaut.TestLanes.timeout/2`).

Optional: `:warm_safe`, the functions (`Juggernaut.WarmSafe.function_key/1`) whose
boot-set mutants are settled on a warm worker (none by default).

# `queue`

```elixir
@spec queue([job()]) :: [job()]
```

Jobs in dispatch order: longest estimated cost first, ties by mutant id.

# `stages`

```elixir
@spec stages([test_id()], [test_id()] | MapSet.t(), [test_id()] | nil) :: [
  [test_id()]
]
```

The runs a job makes, in order, each a list of tests (design notes §10.6): its unit
tests (those of `tests` not in `integration`) and then its integration tests, each lane
split into the tests of `strike` that are in it, then the rest of the lane. A lane with
no test makes no run, and a lane that is all strike or none of it makes one. The
runner makes them until one does not pass.

# `strike`

```elixir
@spec strike([test_id()], test_id() | nil, atom(), [test_id()]) :: [test_id()] | nil
```

The strike of an ordered job (see the module doc), or `nil`.

# `timeouts`

```elixir
@spec timeouts(
  [test_id()],
  %{required(test_id()) =&gt; non_neg_integer()},
  timeout_config()
) ::
  {pos_integer(), pos_integer()}
```

`{per-test timeout, job deadline}` in ms for `tests` (see the module doc).

---

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