Skip to content

Commit

Permalink
chore: docs and rename test client
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveigah committed Jun 16, 2024
1 parent e662c43 commit b91716c
Show file tree
Hide file tree
Showing 14 changed files with 207 additions and 204 deletions.
5 changes: 3 additions & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Config

config :klife, Klife.MyClient,
config :klife, MyTestClient,
connection: [
bootstrap_servers: ["localhost:19092", "localhost:29092"],
# bootstrap_servers: ["localhost:19093", "localhost:29093"],
Expand Down Expand Up @@ -82,7 +82,8 @@ config :klife, Klife.MyClient,
],
[
name: "test_async_topic"
]
],
[name: "my_topic"]
]

if config_env() == :dev do
Expand Down
2 changes: 1 addition & 1 deletion lib/klife/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule Klife.Application do

defp handle_clients() do
[
Klife.MyClient
MyTestClient
]
end

Expand Down
12 changes: 6 additions & 6 deletions lib/klife/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ defmodule Klife.Client do
When used it expects an `:otp_app` option that is the OTP application that has the client configuration.
```elixir
defmodule MyApp.MyClient do
defmodule MyApp.MyTestClient do
use Klife.Client, otp_app: :my_app
end
```
Expand All @@ -73,8 +73,8 @@ defmodule Klife.Client do
>
> - Define it as a proxy to a subset of the functions on `Klife` module,
> using it's module's name as the `client_name` parameter.
> One example of this is the `MyClient.produce/2` that forwards
> both arguments to `Klife.produce/3` and inject `MyClient` as the
> One example of this is the `MyTestClient.produce/2` that forwards
> both arguments to `Klife.produce/3` and inject `MyTestClient` as the
> second argument.
>
> - Define it as a supervisor by calling `use Supervisor` and implementing
Expand Down Expand Up @@ -123,7 +123,7 @@ defmodule Klife.Client do
def start(_type, _args) do
children = [
# some other modules...,
MyApp.MyClient
MyApp.MyTestClient
]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Expand Down Expand Up @@ -160,7 +160,7 @@ defmodule Klife.Client do
```elixir
rec = %Klife.Record{value: "some_val", topic: "my_topic"}
{:ok, %Klife.Record{offset: offset, partition: partition}} = MyClient.produce(rec)
{:ok, %Klife.Record{offset: offset, partition: partition}} = MyTestClient.produce(rec)
```
"""
Expand All @@ -180,7 +180,7 @@ defmodule Klife.Client do
## Examples
iex> rec = %Klife.Record{value: "my_val", topic: "my_topic"}
iex> {:ok, %Klife.Record{} = enriched_rec} = MyClient.produce(rec)
iex> {:ok, %Klife.Record{} = enriched_rec} = MyTestClient.produce(rec)
iex> true = is_number(enriched_rec.offset)
iex> true = is_number(enriched_rec.partition)
Expand Down
2 changes: 1 addition & 1 deletion lib/klife/connection/controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ defmodule Klife.Connection.Controller do
:persistent_term.put({:known_brokers_ids, state.client_name}, new_brokers)

if to_start != [] or to_remove != [] do
PubSub.publish({:client_change, state.client_name}, %{
PubSub.publish({:cluster_change, state.client_name}, %{
added_brokers: to_start,
removed_brokers: to_remove
})
Expand Down
4 changes: 2 additions & 2 deletions lib/klife/producer/controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ defmodule Klife.Producer.Controller do

Utils.wait_connection!(client_name)

:ok = PubSub.subscribe({:client_change, client_name})
:ok = PubSub.subscribe({:cluster_change, client_name})

{:ok, state}
end

def handle_info(
{{:client_change, client_name}, _event_data, _callback_data},
{{:cluster_change, client_name}, _event_data, _callback_data},
%__MODULE__{client_name: client_name} = state
) do
Process.cancel_timer(state.check_metadata_timer_ref)
Expand Down
2 changes: 1 addition & 1 deletion lib/klife/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule Klife.Utils do
# the topic are created, thats why we need to create a connection from
# scratch here. Must solve it later.
def create_topics!() do
client_opts = Application.fetch_env!(:klife, Klife.MyClient)
client_opts = Application.fetch_env!(:klife, MyTestClient)

conn_defaults =
Klife.Connection.Controller.get_opts()
Expand Down
18 changes: 9 additions & 9 deletions lib/mix/tasks/benchmark.ex
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ if Mix.env() in [:dev] do
rec1 = Enum.random(records_1)
rec2 = Enum.random(records_2)

t0 = Task.async(fn -> Klife.MyClient.produce(rec0) end)
t1 = Task.async(fn -> Klife.MyClient.produce(rec1) end)
t2 = Task.async(fn -> Klife.MyClient.produce(rec2) end)
t0 = Task.async(fn -> MyTestClient.produce(rec0) end)
t1 = Task.async(fn -> MyTestClient.produce(rec1) end)
t2 = Task.async(fn -> MyTestClient.produce(rec2) end)

[{:ok, _}, {:ok, _}, {:ok, _}] = Task.await_many([t0, t1, t2])
end,
Expand Down Expand Up @@ -211,14 +211,14 @@ if Mix.env() in [:dev] do
rec1 = Enum.random(records_1)
rec2 = Enum.random(records_2)

[{:ok, _}, {:ok, _}, {:ok, _}] = Klife.MyClient.produce_batch([rec0, rec1, rec2])
[{:ok, _}, {:ok, _}, {:ok, _}] = MyTestClient.produce_batch([rec0, rec1, rec2])
end,
"produce_batch_txn" => fn ->
rec0 = Enum.random(records_0)
rec1 = Enum.random(records_1)
rec2 = Enum.random(records_2)

{:ok, [_rec1, _rec2, _rec3]} = Klife.MyClient.produce_batch_txn([rec0, rec1, rec2])
{:ok, [_rec1, _rec2, _rec3]} = MyTestClient.produce_batch_txn([rec0, rec1, rec2])
end
},
time: 15,
Expand All @@ -245,13 +245,13 @@ if Mix.env() in [:dev] do
Benchee.run(
%{
"klife" => fn ->
{:ok, _rec} = Klife.MyClient.produce(Enum.random(records_0))
{:ok, _rec} = MyTestClient.produce(Enum.random(records_0))
end,
"klife multi inflight" => fn ->
{:ok, _rec} = Klife.MyClient.produce(Enum.random(in_flight_records))
{:ok, _rec} = MyTestClient.produce(Enum.random(in_flight_records))
end,
"klife multi inflight linger" => fn ->
{:ok, _rec} = Klife.MyClient.produce(Enum.random(in_flight_linger_records))
{:ok, _rec} = MyTestClient.produce(Enum.random(in_flight_linger_records))
end
},
time: 15,
Expand Down Expand Up @@ -290,7 +290,7 @@ if Mix.env() in [:dev] do
Enum.map(tasks_recs_to_send, fn recs ->
Task.async(fn ->
Enum.map(recs, fn rec ->
{:ok, _rec} = Klife.MyClient.produce(rec)
{:ok, _rec} = MyTestClient.produce(rec)
end)
end)
end)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule Klife.MixProject do
defp groups_for_docs(group), do: {String.to_atom(group), &(&1[:group] == group)}

defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(:dev), do: ["lib"]
defp elixirc_paths(:dev), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]

# Run "mix help deps" to learn about dependencies.
Expand Down
4 changes: 4 additions & 0 deletions test/client_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule Klife.ClientTest do
use ExUnit.Case
doctest Klife.Client
end
18 changes: 9 additions & 9 deletions test/connection/system_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ defmodule Klife.Connection.SystemTest do
Enum.each(brokers_list_3, &check_broker_connection(client_name_3, &1))
end

@tag client_change: true, capture_log: true
test "client changes events" do
@tag cluster_change: true, capture_log: true
test "cluster changes events" do
config = [
connection: [
bootstrap_servers: ["localhost:19092", "localhost:29092"],
Expand All @@ -152,13 +152,13 @@ defmodule Klife.Connection.SystemTest do
]
]

Application.put_env(:klife, __MODULE__.MyClientTest, config)
Application.put_env(:klife, __MODULE__.MyOtherClient, config)

defmodule MyClientTest do
defmodule MyOtherClient do
use Klife.Client, otp_app: :klife
end

client_name = __MODULE__.MyClientTest
client_name = __MODULE__.MyOtherClient

assert {:ok, _pid} = start_supervised(client_name)

Expand All @@ -168,20 +168,20 @@ defmodule Klife.Connection.SystemTest do
broker_id_to_remove = List.first(brokers)
cb_ref = make_ref()

:ok = PubSub.subscribe({:client_change, client_name}, %{some_data: cb_ref})
:ok = PubSub.subscribe({:cluster_change, client_name}, %{some_data: cb_ref})

{:ok, service_name} = TestUtils.stop_broker(client_name, broker_id_to_remove)

assert_received({{:client_change, ^client_name}, event_data, %{some_data: ^cb_ref}})
assert_received({{:cluster_change, ^client_name}, event_data, %{some_data: ^cb_ref}})
assert broker_id_to_remove in Enum.map(event_data.removed_brokers, fn {b, _h} -> b end)
assert broker_id_to_remove not in :persistent_term.get({:known_brokers_ids, client_name})

{:ok, broker_id} = TestUtils.start_broker(service_name, client_name)

assert_received({{:client_change, ^client_name}, event_data, %{some_data: ^cb_ref}})
assert_received({{:cluster_change, ^client_name}, event_data, %{some_data: ^cb_ref}})
assert broker_id in Enum.map(event_data.added_brokers, fn {b, _h} -> b end)
assert broker_id in :persistent_term.get({:known_brokers_ids, client_name})

:ok = PubSub.unsubscribe({:client_change, client_name})
:ok = PubSub.unsubscribe({:cluster_change, client_name})
end
end
Loading

0 comments on commit b91716c

Please sign in to comment.