Skip to content

Commit

Permalink
[#182][#181] Fix the crash when :gc_interval is not set (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
dongfuye authored Jan 14, 2023
1 parent 53e570d commit 1da2129
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/nebulex/adapters/local/generation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions test/nebulex/adapters/local/generation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1da2129

Please sign in to comment.