Skip to content

Commit

Permalink
Merge branch 'fix_named_metrics' into 'main'
Browse files Browse the repository at this point in the history
fix Unleash.Metrics to provide proper start_link/1 behaviour

See merge request afontaine/unleash_ex!23
  • Loading branch information
Andrew Fontaine committed Jan 28, 2022
2 parents 1395d72 + f998601 commit adce683
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/unleash/metrics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ defmodule Unleash.Metrics do
{:ok, init_state()}
end

def start_link(state, opts \\ []) do
def start_link(opts) do
start_link(init_state(), opts)
end

def start_link(state, opts) do
{:ok, pid} = GenServer.start_link(__MODULE__, state, opts)

unless Config.test? do
unless Config.test?() do
initialize(pid)
end

Expand All @@ -53,7 +57,7 @@ defmodule Unleash.Metrics do
{:noreply, send_metrics(state)}
end

if Config.test? do
if Config.test?() do
def handle_call(:send_metrics, _from, state) do
{:reply, :ok, send_metrics(state)}
end
Expand Down
14 changes: 14 additions & 0 deletions test/unleash/metrics_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,18 @@ defmodule Unleash.MetricsTest do
end
end
end

describe "start metrics" do
test "should start named process under supervisor" do
{:ok, _} =
Supervisor.start_link(
[
{Unleash.Metrics, name: :metric_named_process}
],
strategy: :one_for_one
)

assert Process.whereis(:metric_named_process) != nil
end
end
end

0 comments on commit adce683

Please sign in to comment.