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

The boot lane (design notes §10.8): a mutant whose site runs while the app starts or the
tests load is tested on a fresh worker booted with that mutant already on, since no
job on a warm worker runs that code again.

`run/2` starts a `Juggernaut.Pool.Worker` (index `:boot`) whose boot sets the switch
before `app.start`, after marking a progress file (`Juggernaut.Peer.Boot.mark_switched/1`).
How a failed boot counts (`boot_failure/2`):

  * it fails in the `start` or `tests` stage (the switch is on: the app does not
    start, or the test helpers or files do not load): a kill of kind `:boot`;
  * the peer is lost once the progress file is marked (the switch is on): it died
    (`System.halt/1`, a VM abort, exit 137) - a kill of kind `:crash` - or hung past
    the boot timeout - a kill of kind `:timeout` (design notes §10.7);
  * anything else fails before the switch is on (the `project` or `setup` stage, or a
    peer lost before the mark): nothing to do with the mutant, an infrastructure error.

Otherwise the whole suite runs with the mutant on and `max_failures: 1`, in its lanes
(`Juggernaut.Runner.Schedule.stages/3`): its unit tests, then, when they all pass, its
`:integration` tests, each lane's part of the `:strike` first when there is one
(`staged/3`). The job's result, with the test lists of the runs it made, in order,
under `:runs`, is returned for the runner to classify. With `:replay` (such a list of
runs) those runs are made instead, in the same order, until one does not pass
(`replay/3`): with switch 0, a struck kill's control. Each run has its lane's per-test
timeout and deadline. The worker is stopped and the progress file removed either way.

With `:files` (each test's file), a job of more than one run boots with only its first
run's test files loaded (`lazy_files/2`), and loads the others before its next run
(`Juggernaut.Peer.Boot.load_pending/0`): compiling the whole suite on a worker's two
schedulers is most of a boot, and a struck kill (and its control, which loads the same
files) needs only its strike's. A job that runs on past its first run has every test
file loaded before its second, so a survivor has loaded and run the whole suite with
the mutant on, as before. A file that fails to load then (the mutant on) is a kill of
kind `:boot`, as at boot. A boot that fails to load its first run's files is booted
again with every file, since a test file can need a module another one defines: only
that boot's failure counts.

# `outcome`

```elixir
@type outcome() ::
  {:killed, :boot | :crash | :timeout, String.t()}
  | {:ran, map()}
  | {:error, Juggernaut.Error.t()}
```

# `boot_failure`

```elixir
@spec boot_failure(Juggernaut.Error.t(), boolean()) :: outcome()
```

How the boot-lane worker's boot `error` counts (see the module doc), given whether the
switch was on (`switched?`: the progress file was marked) when the peer was lost.

# `concurrency`

```elixir
@spec concurrency(Juggernaut.Config.t(), %{
  jobs: non_neg_integer(),
  pool: pos_integer(),
  live: non_neg_integer()
}) :: {non_neg_integer(), String.t()}
```

How many boot-lane workers run at once, and why, as `{n, reason}` (design notes §10.8).

`lane` gives the boot-lane `jobs`, the run's `pool` size (its `workers` within the
connection budget, `Juggernaut.Pool.Env.size/2`) and the pool workers still `live`
while the lane runs (none in a run of its own, whose pool has stopped; the server's,
which stay up). Every peer holds the same connections (`Env.per_worker/1`), whatever
its kind, so:

  * with a `connections` budget, the lane gets what the live workers leave of it (the
    server's pool keeps one peer's room for it: `Juggernaut.Engine.pool_options/2`);
    it never goes over the budget, so when the live workers leave no room (a budget
    that fits one peer, which the server's pool holds) it gets 0 and the reason says
    what budget would give it one;
  * with no budget and no live worker, as many as the pool had: the pool's boot has
    already held that many peers' connections at once;
  * with no budget and live workers, one at a time: nothing says the database takes
    more (set `connections: [budget: N]` to allow more).

Never more than the pool size or the jobs, never fewer than one but for a budget with
no room left.

# `lane_runs?`

```elixir
@spec lane_runs?([[{module(), atom()}]], keyword()) :: boolean()
```

Whether a kill's `runs` (an outcome's `:runs`) are covered by the boot lane's control:
they are the first of the runs the suite makes with no strike (its unit tests, then its
integration tests), as `run/2` is given `opts` (its `:tests` and `:integration`). A
struck kill's are not, and need a control of their own (`replay/3`).

# `lazy_files`

```elixir
@spec lazy_files([{module(), atom()}], keyword()) :: [Path.t()] | nil
```

The test files to load at boot for a job whose first run is `first`, given `run/2`
options: `nil` (every file) with no `:files` or for a job of one run (one stage and no
`:replay`), else the files of `first`'s tests (`:files`, by test), sorted. A replay's
first run is the kill's, whose boot loaded the same files.

# `limits`

```elixir
@spec limits(keyword(), Enumerable.t()) :: {pos_integer(), pos_integer()}
```

The per-test timeout and the deadline (ms) of a boot-lane run of `tests`, given `run/2`
options: `:integration_timeouts` when every one of `tests` is in `:integration` (and
it is given), else `:timeout_ms` (60 000) and `:deadline_ms` (the per-test timeout for
each test of the suite).

# `replay`

```elixir
@spec replay(
  (map() -&gt;
     {:ok, map()} | {:error, Juggernaut.Error.t()} | {:load_error, String.t()}),
  map(),
  [[{module(), atom()}]]
) :: outcome()
```

Makes `runs` (test lists, as an outcome's `:runs` gives them) again through `run`, in
order, with `job`'s switch and seed, stopping at the first that does not pass: that
run's result is the outcome, or the last run's when all pass. With switch 0 on a fresh
boot it is a struck kill's control (design notes §10.8): the kill's tests in the kill's
runs with its seed, so in the same order, with every mutant off.

# `run`

```elixir
@spec run(non_neg_integer(), keyword()) :: outcome()
```

Runs the mutant `switch` in the boot lane. Options:

  * `:tests` (required) - the runnable tests of the suite (`[{module, name}]`);
  * `:worker` (required) - `Juggernaut.Pool.Worker` options other than `:index`
    (`:sandbox`, `:env`, `:boot`, ...);
  * `:strike` (`[]`) - tests of `:tests` to run first in their lane, the rest of the
    lane only when they all pass (`staged/3`);
  * `:integration` (`[]`) - the integration tests of `:tests`, run after the others
    have all passed;
  * `:replay` - the `:runs` of an earlier outcome, made again in the same order in place
    of the suite (`replay/3`; `:strike` is then ignored);
  * `:seed` (0), `:timeout_ms` (per test, 60 000), `:deadline_ms` (each run's, default
    the per-test timeout for each test of the suite), and `:integration_timeouts`
    (`{timeout_ms, deadline_ms}` of a run of integration tests only; default the
    same as the others);
  * `:slot` (1) and `:slot_env` - the runner's slot this boot takes, and a function of
    the slot giving the worker's `:env` (`Juggernaut.Pool.Env.render/3` with
    `{:boot, slot}`), so boots running at once in different slots each have their own
    test partition and database;
  * `:held` - `%{slot => pid}` from `start_held/2`: the slot's held worker boots the
    mutant from its parked spare (`Juggernaut.Pool.Worker.reboot/2`) in place of a new
    worker, and its peer is stopped after the job (`retire/1`). A slot with none gets a
    new worker, as with no `:held`.

# `staged`

```elixir
@spec staged(
  (map() -&gt;
     {:ok, map()} | {:error, Juggernaut.Error.t()} | {:load_error, String.t()}),
  map(),
  [[{module(), atom()}]]
) :: outcome()
```

The boot-lane job's runs with the mutant on, through `run` (given `job` with its
`only_test_ids` set; `Juggernaut.Pool.Worker.run/3` on the boot worker), as an
`t:outcome/0`: each of `stages` (`Juggernaut.Runner.Schedule.stages/3`: the strike's
part of the unit lane, the rest of it, then the same of the integration lane) in
order, until one does not pass.

A run that kills, or gives no verdict, ends it: its result, with the outcomes and
counts of the runs before it in front, is the outcome. When every run passes, the last
one's, with the others' in front, is. So a survivor has run the whole suite with the
mutant on, as with no strike. A result carries the test lists of the runs made, in
order, under `:runs` (`[tests]`, `[strike]`, `[strike, rest]`, `[unit, integration]`,
...).

# `start_held`

```elixir
@spec start_held([pos_integer()], keyword()) :: %{required(pos_integer()) =&gt; pid()}
```

Starts a held worker (`Juggernaut.Pool.Worker` with `hold: true` and `spare: :always`)
for each of `slots`, linked to the caller, from the lane options `opts` (`:worker` and
`:slot_env`, as `run/2` takes them): `%{slot => pid}`, for `run/2`'s `:held`. Each parks
a spare at once, which holds no database connection, so every job of the slot finds a
VM started and the project loaded. Stop them with `stop_held/1`.

# `stop_held`

```elixir
@spec stop_held(%{required(pos_integer()) =&gt; pid()}) :: :ok
```

Stops the held workers of `start_held/2`. `:ok`.

---

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