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

The coverage recorder of a worker (design notes §10.5): which sites run, and on behalf of
which test.

A metamutant site in probe mode (`Juggernaut.Peer.Switch.probe/0`) calls `hit/1` with
its first switch, in whatever process runs it. The recorder stores `{site, label}` once
in an ETS set, where the label says what the hit is owed to:

  * `:boot` - the app was starting (or the test helpers loading): `arm_boot/0` up to
    `take_boot/0`. Such a site goes to the boot lane (§10.8);
  * `{:test, {module, name}}` - a test, found from the hitting process;
  * `:job` - a process of the running job that no test owns: a `setup_all` process, an
    `on_exit` runner, or a process one of them started. The probe runs one test file
    per job, so the controller reads `:job` as that whole file;
  * `{:none, {kind, who}}` - no test and not the job: a process of the app serving a
    cast, a timer or another message, or a call from a process no test owns. `kind` is
    `:call`, `:cast`, `:info` or `:process`, `who` names the process.

Labels are resolved from process provenance, never from time windows:

  1. the process's own label: ExUnit labels each test process `{module, name}`
     (`Process.set_label/1`), and a label counts only when `module` is a test module;
  2. the processes it acts for, walked in order: the caller of the GenServer call it is
     serving (`$juggernaut_from`, set by `debug/3`, the `:sys` debug function
     `start_job/1` installs on the app's GenServers), its `$callers` (set by `Task`),
     its `$ancestors` (set by `proc_lib`) and its spawning parent (`Process.info/2`
     `:parent`). Each pid reached is resolved through its own `$callers`, `$ancestors`
     and parent, never through its served caller: a server's `$juggernaut_from` names
     the caller it serves now, not the one it served when it started a helper, so
     following it from another process would owe a hit to whichever test called last.
     A process related to a server only by spawning (a helper, a `Task` the server
     starts) is therefore unattributed unless its own `$callers` reach a test.
     Reaching the job's own process (the one running `ExUnit.run/1`) gives `:job`;
  3. otherwise `{:none, reason}`.

A dead pid ends its branch of the walk (its links cannot be read), so a process that
outlives the test that started it is unattributed: the conservative answer, since its
site then runs the whole suite. The walk's answer is kept in the process dictionary
until the process's `$callers`, served caller or message kind changes, or the process
that gave the label dies; a test process's own label is read on every hit.

This module runs inside workers and is never mutated. Nothing here raises into a
metamutant: a failed record is dropped.

# `label`

```elixir
@type label() ::
  :boot
  | :job
  | {:test, {module(), atom()}}
  | {:none, {:call | :cast | :info | :process, String.t()}}
```

# `app_processes`

```elixir
@spec app_processes(atom()) :: [pid()]
```

The processes of `app`'s supervision tree that are not supervisors (named or
anonymous children), found from its application master.

# `arm`

```elixir
@spec arm() :: :ok
```

Creates the hit table, owned by a process of its own that lives as long as the VM (a
`:peer` call runs in a process that exits when the call returns), or empties it.

# `arm_boot`

```elixir
@spec arm_boot() :: :ok
```

Records every hit as `:boot` from now until `take_boot/0`.

# `debug`

```elixir
@spec debug(term(), term(), term()) :: term()
```

The `:sys` debug function installed on the app's GenServers during a probe job. It runs
in the server's process before each incoming message is handled and records, in that
process's dictionary, the caller of a call (`$juggernaut_from`) and the kind of
message (`$juggernaut_in`): `:call`, `:cast` or `:info`. Other events are left alone.

# `disarm`

```elixir
@spec disarm() :: :ok
```

Stops recording and removes the hit table (for tests).

# `finish_job`

```elixir
@spec finish_job() :: [{pos_integer(), label()}]
```

Ends the probe job: removes the debug functions and returns the job's `{site, label}`
pairs, sorted; the table is emptied.

# `hit`

```elixir
@spec hit(pos_integer()) :: :ok
```

Records a hit on `site` with the label of the calling process (see the module doc).

# `label`

```elixir
@spec label(pid()) :: label()
```

The label of the calling process in a job run by `root`.

# `links`

```elixir
@spec links(pid()) :: [pid()]
```

The processes another process `pid` acts for, as the walk follows them: its
`$callers`, `$ancestors` and parent. Its served caller (`$juggernaut_from`) is left
out: only the server's own process follows it, while it serves that call.

# `name`

```elixir
@spec name(pid()) :: String.t()
```

A short name for `pid`: its registered name, else its initial call.

# `start_job`

```elixir
@spec start_job(pid(), [atom()]) :: :ok
```

Starts a probe job run by `root` (the process that calls `ExUnit.run/1`): empties the
table and installs `debug/3` on every GenServer of `apps`' supervision trees.

# `take_boot`

```elixir
@spec take_boot() :: [pos_integer()]
```

Stops boot recording: the sites hit since `arm_boot/0`, sorted; the table is emptied.

# `test_label`

```elixir
@spec test_label(term()) :: {module(), atom()} | nil
```

`{module, name}` when `label` is an ExUnit test's label, else `nil`.

# `watch`

```elixir
@spec watch([pid()]) :: [pid()]
```

Installs `debug/3` on each of `pids` that is an idle `gen_server` or `gen_statem`, until
`finish_job/0`; returns those it was installed on.

---

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