# `Alternera.Plugin.Test.MemorySource`
[🔗](https://github.com/10e-ab/alternera-sdk/blob/v0.9.0/alternera_plugin_sdk/lib/alternera/plugin/test/memory_source.ex#L1)

A message source (`Alternera.Plugin.Provisional.Source`) over a queue
held in memory, for tests of the code that consumes a source, with no
broker around.

A queue is an `Agent` the test starts and names (`start_queue/1`); the
source's one setting, `"queue"`, names it. The agent is registered
globally, so a receive from any process (a runner's task) finds it, and
it is linked to the test process, so it goes when the test does.

    {:ok, _pid} = MemorySource.start_queue("orders")
    MemorySource.push("orders", ~s({"id": 1}))
    # ... a consumer opens the source with %{"queue" => "orders"}
    MemorySource.settled("orders")  #=> [{"m-1", :ack}]

It behaves as a queue with a timed lock does: a received message is
hidden for the hold the receive asked for, its `receive_count` goes up
each time it is delivered, and each delivery has a handle of its own, so a
settle with the handle of an earlier delivery fails `stale`. `:ack`
removes a message, `{:retry, ms}` makes it visible again after `ms`,
`:reject` moves it to the queue's rejected list. With the setting
`"extend_hold"` true it can lengthen a hold (`extend/2`). `calls/1` lists
the settles and extends it was asked for, with how many messages each
took. The queue's clock is the
node's monotonic clock plus what `advance/2` added, so a test can let a
hold lapse without waiting for it. `fail_next/4` makes the next call of a
callback fail.

# `advance`

```elixir
@spec advance(String.t(), non_neg_integer()) :: :ok
```

Moves the queue's clock on by `ms`.

# `calls`

```elixir
@spec calls(String.t()) :: [{:settle | :extend, pos_integer()}]
```

The settles and extends asked of the queue, oldest first, as
`{:settle | :extend, how many messages}`.

# `dead_letter`

```elixir
@spec dead_letter(String.t(), String.t()) :: :ok
```

Takes the message `id` out of the queue as a broker's redrive policy
moves one to its dead-letter queue: it is never delivered again.

# `fail_next`

```elixir
@spec fail_next(
  String.t(),
  :open | :receive | :settle | :extend,
  String.t(),
  String.t()
) :: :ok
```

Makes the next call of `callback` (`:open`, `:receive`, `:settle` or
`:extend`) on the queue answer `{:error, type, message}`.

# `pending`

```elixir
@spec pending(String.t()) :: [String.t()]
```

The ids of the messages still in the queue, hidden or not.

# `push`

```elixir
@spec push(String.t(), binary(), %{required(String.t()) =&gt; String.t()}) :: String.t()
```

Puts a message with `body` and `attributes` at the back of the queue; answers its id.

# `rejected`

```elixir
@spec rejected(String.t()) :: [String.t()]
```

The ids of the messages rejected, oldest first.

# `settled`

```elixir
@spec settled(String.t()) :: [{String.t(), term()}]
```

The verdicts settled on the queue, oldest first, as `{message_id, verdict}`.

# `start_queue`

```elixir
@spec start_queue(String.t()) :: {:ok, pid()} | {:error, term()}
```

Starts the queue named `name`, linked to the calling process.

# `visible_at`

```elixir
@spec visible_at(String.t(), String.t()) :: integer() | nil
```

When the message `id` becomes visible again, on the queue's clock
(monotonic ms plus what `advance/2` added), or nil when it is not in the
queue.

---

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