From afb1ea1a6dc6894537ce0bb76dd18b24f0a56c3a Mon Sep 17 00:00:00 2001 From: Matteo Trentin Date: Sat, 2 Mar 2024 14:54:06 +0100 Subject: [PATCH] test(worker): add limit-based eviction test --- .../adapters/resource_cache/supervisor.ex | 3 +- .../adapters/resource_cache_test.exs | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/worker/lib/worker/adapters/resource_cache/supervisor.ex b/worker/lib/worker/adapters/resource_cache/supervisor.ex index a413e8f..1038b55 100644 --- a/worker/lib/worker/adapters/resource_cache/supervisor.ex +++ b/worker/lib/worker/adapters/resource_cache/supervisor.ex @@ -32,14 +32,13 @@ defmodule Worker.Adapters.ResourceCache.Supervisor do |> Integer.parse() [ - # TODO: LRW eviction policy; max N entries (N comes from env_var, default 10); in the future, implement own memory-based policy {Cachex, name: @cache, limit: Cachex.Spec.limit( size: size_limit, policy: Cachex.Policy.LRW, - reclaim: 0.5, + reclaim: 0.2, options: [batch_size: 100, frequency: 1000] )} ] diff --git a/worker/test/integration/adapters/resource_cache_test.exs b/worker/test/integration/adapters/resource_cache_test.exs index 12f2eda..bde00af 100644 --- a/worker/test/integration/adapters/resource_cache_test.exs +++ b/worker/test/integration/adapters/resource_cache_test.exs @@ -47,4 +47,42 @@ defmodule Integration.ResourceCacheTest do result = ResourceCache.delete("test", "ns", <<0, 0, 0>>) assert result == :ok end + + # we test this on the default value of 5 entries for :cachex_limit + # to overwrite this, we would need to insert the value before the application starts + # (since it's read directly in the ResourceCache Supervisor) + test "oldest keys are evicted when the cache contains more than :cachex_limit entries" do + runtime1 = %ExecutionResource{resource: "runtime1"} + hash1 = <<1, 1, 1>> + runtime2 = %ExecutionResource{resource: "runtime2"} + hash2 = <<2, 2, 2>> + runtime3 = %ExecutionResource{resource: "runtime3"} + hash3 = <<3, 3, 3>> + runtime4 = %ExecutionResource{resource: "runtime4"} + hash4 = <<4, 4, 4>> + runtime5 = %ExecutionResource{resource: "runtime5"} + hash5 = <<5, 5, 5>> + runtime6 = %ExecutionResource{resource: "runtime6"} + hash6 = <<6, 6, 6>> + + ResourceCache.insert("test-threshold1", "ns", hash1, runtime1) + :timer.sleep(200) + ResourceCache.insert("test-threshold2", "ns", hash2, runtime2) + :timer.sleep(200) + ResourceCache.insert("test-threshold3", "ns", hash3, runtime3) + :timer.sleep(200) + ResourceCache.insert("test-threshold4", "ns", hash4, runtime4) + :timer.sleep(200) + ResourceCache.insert("test-threshold5", "ns", hash5, runtime5) + :timer.sleep(200) + ResourceCache.insert("test-threshold6", "ns", hash6, runtime6) + :timer.sleep(2000) + + assert ResourceCache.get("test-threshold1", "ns", hash1) == :resource_not_found + assert ResourceCache.get("test-threshold2", "ns", hash2) == :resource_not_found + assert ResourceCache.get("test-threshold3", "ns", hash3) == runtime3 + assert ResourceCache.get("test-threshold4", "ns", hash4) == runtime4 + assert ResourceCache.get("test-threshold5", "ns", hash5) == runtime5 + assert ResourceCache.get("test-threshold6", "ns", hash6) == runtime6 + end end