From 30157a7b3644d280636b850b579a062fe45dcefb Mon Sep 17 00:00:00 2001 From: Ihor Katkov Date: Tue, 7 May 2024 16:50:21 +0200 Subject: [PATCH] feat: improve inspect function --- lib/leaky.ex | 21 +++++++-------------- test/leaky_test.exs | 7 ++++--- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/lib/leaky.ex b/lib/leaky.ex index 168461e..9c9d7c9 100644 --- a/lib/leaky.ex +++ b/lib/leaky.ex @@ -66,7 +66,7 @@ defmodule Leaky do @spec acquire(bucket :: bucket(), cost :: integer, name :: GenServer.name()) :: {:allow, integer} | :deny def acquire(bucket, cost, name \\ __MODULE__) do - GenServer.call(GenServer.whereis(name), {:acquire, bucket, cost}) + GenServer.call(name, {:acquire, bucket, cost}) end @doc """ @@ -85,7 +85,10 @@ defmodule Leaky do """ @spec inspect(bucket :: bucket(), name :: GenServer.name()) :: integer | nil def inspect(bucket, name \\ __MODULE__) do - GenServer.call(GenServer.whereis(name), {:inspect, bucket}) + case GenServer.call(name, {:acquire, bucket, 0}) do + {:allow, tokens_left} -> tokens_left + :deny -> 0 + end end @doc """ @@ -94,7 +97,7 @@ defmodule Leaky do """ @spec adjust_tokens(bucket :: bucket(), amount :: integer, name :: GenServer.name()) :: :ok def adjust_tokens(bucket, amount, name \\ __MODULE__) do - GenServer.cast(GenServer.whereis(name), {:increment_tokens_left, bucket, amount}) + GenServer.cast(name, {:increment_tokens_left, bucket, amount}) end @doc """ @@ -116,7 +119,7 @@ defmodule Leaky do """ @spec update_configuration(opts :: Keyword.t(), name :: GenServer.name()) :: :ok def update_configuration(opts, name \\ __MODULE__) do - GenServer.cast(GenServer.whereis(name), {:update_configuration, opts}) + GenServer.cast(name, {:update_configuration, opts}) end def start_link(opts) do @@ -182,16 +185,6 @@ defmodule Leaky do end @impl GenServer - def handle_call({:inspect, bucket}, _from, %State{} = state) do - available_tokens = - case :ets.lookup(state.bucket_name, bucket) do - [] -> nil - [{_bucket, tokens_left, _last_inserted_at}] -> tokens_left - end - - {:reply, available_tokens, state} - end - def handle_call({:acquire, bucket, cost}, _from, %State{} = state) do now = :erlang.system_time(:milli_seconds) table = state.bucket_name diff --git a/test/leaky_test.exs b/test/leaky_test.exs index 2b5cf56..3617408 100644 --- a/test/leaky_test.exs +++ b/test/leaky_test.exs @@ -3,9 +3,10 @@ defmodule LeakyTest do @bucket :bucket @cost 1 + @max_accumulated 4 setup do - configuration = [max_accumulated: 4, refill: 1, interval: 10, bucket_name: @bucket] + configuration = [max_accumulated: @max_accumulated, refill: 1, interval: 10, bucket_name: @bucket] start_supervised!({Leaky, configuration}) :ok @@ -31,7 +32,7 @@ defmodule LeakyTest do test "returns amount of available tokens" do assert {:allow, 3} == Leaky.acquire(@bucket, @cost) - assert 3 == Leaky.inspect(@bucket) + assert 3.0 == Leaky.inspect(@bucket) end end @@ -50,7 +51,7 @@ defmodule LeakyTest do :timer.sleep(2) - assert 2 == Leaky.inspect(@bucket) + assert 2 < Leaky.inspect(@bucket) end end