diff --git a/lib/nebulex/adapters/local/generation.ex b/lib/nebulex/adapters/local/generation.ex index b2d629cf..1a7cf822 100644 --- a/lib/nebulex/adapters/local/generation.ex +++ b/lib/nebulex/adapters/local/generation.ex @@ -496,9 +496,12 @@ defmodule Nebulex.Adapters.Local.Generation do Metadata.put(meta_tab, :deprecated, older) end - defp start_timer(time, ref \\ nil, event \\ :heartbeat) do - _ = if ref, do: Process.cancel_timer(ref) + defp start_timer(time, ref \\ nil, event \\ :heartbeat) + + defp start_timer(nil, _, _), do: nil + defp start_timer(time, ref, event) do + _ = if ref, do: Process.cancel_timer(ref) Process.send_after(self(), event, time) end diff --git a/test/nebulex/adapters/local/generation_test.exs b/test/nebulex/adapters/local/generation_test.exs index 16878d20..611563f1 100644 --- a/test/nebulex/adapters/local/generation_test.exs +++ b/test/nebulex/adapters/local/generation_test.exs @@ -307,6 +307,26 @@ defmodule Nebulex.Adapters.Local.GenerationTest do end end + describe "cleanup cover" do + test "cleanup when gc_interval not set" do + {:ok, _pid} = + LocalWithSizeLimit.start_link( + max_size: 3, + gc_cleanup_min_timeout: 1000, + gc_cleanup_max_timeout: 1500 + ) + + # Put some entries to exceed the max size + _ = cache_put(LocalWithSizeLimit, 1..4) + + # Wait the max cleanup timeout + :ok = Process.sleep(1600) + + # assert not crashed + assert LocalWithSizeLimit.count_all() == 4 + end + end + ## Private Functions defp check_cache_size(cache) do