Klife.Testing (Klife v1.0.0)

View Source

Testing helper functions.

In order to test Kafka behaviour on tests we can have 2 approaches:

  • Having a running Kafka broker locally and testing against it
  • Mocking all external calls to the broker

Klife.Testing supports the first approach by offering helper functions to verify if a record with the given list of properties exists in the broker.

You can use it like this:

# on test_helper.exs
Klife.Testing.setup(MyClient)

# on your test file
Klife.Testing.all_produced(MyClient, "my_topic_a", value: "abc")

The mocks approach is not supported directly by Klife but can be achieved using some awesome community libraries such as Mimic or Mox.

Summary

Functions

Returns a list of Klife.Record that match the given filters.

Sets up Klife.Testing. Call it in your test_helper.exs.

Functions

all_produced(client, topic, search_opts)

Returns a list of Klife.Record that match the given filters.

You can filter by the following fields:

  • value: binary
  • key: binary
  • headers: list of %{key: binary, value: binary} maps

All provided filters are combined with "and" semantics, only records matching every filter are returned. The same applies within the headers list: a record must contain all specified headers to match.

Examples

iex> val = :rand.bytes(1000)
iex> rec = %Klife.Record{value: val, topic: "my_topic_1"}
iex> {:ok, %Klife.Record{}} = MyClient.produce(rec)
iex> [%Klife.Record{}] = Klife.Testing.all_produced(MyClient, "my_topic_1", value: val)

setup(client)

Sets up Klife.Testing. Call it in your test_helper.exs.

Snapshots the latest offset for every topic/partition so that all_produced/3 only searches records produced after this point, avoiding false matches from pre-existing data.