Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
netboz committed Jan 21, 2023
1 parent 757e55c commit b525ecc
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
13 changes: 8 additions & 5 deletions test/archethic/utils/hydrating_cache/caches_manager_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
64 changes: 63 additions & 1 deletion test/archethic/utils/hydrating_cache/hydrating_cache_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b525ecc

Please sign in to comment.