diff --git a/test/archethic/utils/hydrating_cache/caches_manager_test.exs b/test/archethic/utils/hydrating_cache/caches_manager_test.exs index 4151a6e912..4c0e850947 100644 --- a/test/archethic/utils/hydrating_cache/caches_manager_test.exs +++ b/test/archethic/utils/hydrating_cache/caches_manager_test.exs @@ -6,15 +6,18 @@ defmodule HydratingCacheTest do test "starting service from manager returns value once first hydrating have been done" do CachesManager.new_service_async("test_services", [ - {:key1, __MODULE__, :waiting_function, [4000], 6000}, - {:key2, __MODULE__, :waiting_function, [2000], 6000}, - {:key3, __MODULE__, :waiting_function, [4000], 6000} + {:key1, __MODULE__, :waiting_function, [2000], 6000}, + {:key2, __MODULE__, :waiting_function, [1000], 6000}, + {:key3, __MODULE__, :waiting_function, [2000], 6000} ]) + ## wait a little so at least keys are registered + :timer.sleep(500) + assert HydratingCache.get( :"Elixir.Archethic.Utils.HydratingCache.test_services", - "key2", - 3000 + :key2, + 1700 ) == {:ok, 1} end diff --git a/test/archethic/utils/hydrating_cache/hydrating_cache_test.exs b/test/archethic/utils/hydrating_cache/hydrating_cache_test.exs index 2537cbc46c..1d4e591888 100644 --- a/test/archethic/utils/hydrating_cache/hydrating_cache_test.exs +++ b/test/archethic/utils/hydrating_cache/hydrating_cache_test.exs @@ -233,6 +233,68 @@ defmodule HydratingCacheTest do ## We query the value with timeout smaller than timed function result = HydratingCache.get(pid, "delayed_result", 1000) - assert result == {:error, :timeout} + assert result = {:error, :timeout} + end + + test "Multiple process can wait for a delayed value" do + {:ok, pid} = HydratingCache.start_link(:test_service) + + _ = + HydratingCache.register_function( + pid, + fn -> + IO.puts("Hydrating function Sleeping 3 secs") + :timer.sleep(3000) + IO.puts("Hydrating function done") + {:ok, :valid_result} + end, + "delayed_result", + 80000, + 70000 + ) + + ## We query the value with timeout smaller than timed function + results = + Task.async_stream(1..10, fn _ -> HydratingCache.get(pid, "delayed_result", 4000) end) + + assert Enum.all?(results, fn + {:ok, {:ok, :valid_result}} -> true + other -> IO.puts("Unk #{inspect(other)}") + end) + end + + ## Resilience tests + test "If hydrating function crash, key fsm will still be oprationnal" do + {:ok, pid} = HydratingCache.start_link(:test_service) + + _ = + HydratingCache.register_function( + pid, + fn -> + ## Trigger badmatch + exit(1) + {:ok, :badmatch} + end, + :key, + 80000, + 70000 + ) + + :timer.sleep(1000) + + _ = + HydratingCache.register_function( + pid, + fn -> + ## Trigger badmatch + {:ok, :value} + end, + :key, + 80000, + 70000 + ) + + result = HydratingCache.get(pid, :key, 3000) + assert result == {:ok, :value} end end