From 1da2129ea619272fd473a44269693797ada9d052 Mon Sep 17 00:00:00 2001 From: dongfuye <118151011+dongfuye@users.noreply.github.com> Date: Sat, 14 Jan 2023 16:20:06 +0800 Subject: [PATCH] [#182][#181] Fix the crash when `:gc_interval` is not set (#183) --- lib/nebulex/adapters/local/generation.ex | 7 +++++-- .../adapters/local/generation_test.exs | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) 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