Alternera.Plugin.Test.MemorySource (alternera_plugin_sdk v0.9.0)

Copy Markdown View Source

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.

Summary

Functions

Moves the queue's clock on by ms.

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

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.

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

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

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

The ids of the messages rejected, oldest first.

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

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

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.

Functions

advance(name, ms)

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

Moves the queue's clock on by ms.

calls(name)

@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(name, id)

@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(name, callback, type, message)

@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(name)

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

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

push(name, body, attributes \\ %{})

@spec push(String.t(), binary(), %{required(String.t()) => String.t()}) :: String.t()

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

rejected(name)

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

The ids of the messages rejected, oldest first.

settled(name)

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

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

start_queue(name)

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

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

visible_at(name, id)

@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.