Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix the crash when gc_interval is not set #183

Merged
merged 3 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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