Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shane Kilkelly
committed
Mar 30, 2018
1 parent
1d93b6e
commit 7a354bf
Showing
3 changed files
with
69 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
defmodule ETSTest do | ||
use ExUnit.Case | ||
|
||
setup _context do | ||
{:ok, hammer_ets_pid} = Hammer.Backend.ETS.start_link() | ||
{:ok, [pid: hammer_ets_pid]} | ||
end | ||
|
||
test "count_hit", context do | ||
pid = context[:pid] | ||
{stamp, key} = Hammer.Utils.stamp_key("one", 200_000) | ||
assert {:ok, 1} = Hammer.Backend.ETS.count_hit(pid, key, stamp) | ||
assert {:ok, 2} = Hammer.Backend.ETS.count_hit(pid, key, stamp) | ||
assert {:ok, 3} = Hammer.Backend.ETS.count_hit(pid, key, stamp) | ||
end | ||
|
||
test "get_bucket", context do | ||
pid = context[:pid] | ||
{stamp, key} = Hammer.Utils.stamp_key("two", 200_000) | ||
# With no hits | ||
assert {:ok, nil} = Hammer.Backend.ETS.get_bucket(pid, key) | ||
# With one hit | ||
assert {:ok, 1} = Hammer.Backend.ETS.count_hit(pid, key, stamp) | ||
assert {:ok, {{_, "two"}, 1, _, _}} = Hammer.Backend.ETS.get_bucket(pid, key) | ||
# With two hits | ||
assert {:ok, 2} = Hammer.Backend.ETS.count_hit(pid, key, stamp) | ||
assert {:ok, {{_, "two"}, 2, _, _}} = Hammer.Backend.ETS.get_bucket(pid, key) | ||
end | ||
|
||
test "delete_buckets", context do | ||
pid = context[:pid] | ||
{stamp, key} = Hammer.Utils.stamp_key("three", 200_000) | ||
# With no hits | ||
assert {:ok, 0} = Hammer.Backend.ETS.delete_buckets(pid, "three") | ||
# With three hits in same bucket | ||
assert {:ok, 1} = Hammer.Backend.ETS.count_hit(pid, key, stamp) | ||
assert {:ok, 2} = Hammer.Backend.ETS.count_hit(pid, key, stamp) | ||
assert {:ok, 3} = Hammer.Backend.ETS.count_hit(pid, key, stamp) | ||
assert {:ok, 1} = Hammer.Backend.ETS.delete_buckets(pid, "three") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule UtilsTest do | ||
use ExUnit.Case | ||
|
||
test "timestamp" do | ||
assert is_integer(Hammer.Utils.timestamp()) | ||
end | ||
|
||
test "stamp_key" do | ||
id = "test_one_two" | ||
{stamp, key} = Hammer.Utils.stamp_key(id, 60_000) | ||
assert is_integer(stamp) | ||
assert is_tuple(key) | ||
{bucket_number, b_id} = key | ||
assert is_integer(bucket_number) | ||
assert b_id == id | ||
end | ||
|
||
test "get_backend_module" do | ||
# With :single and default backend config | ||
assert Hammer.Utils.get_backend_module(:single) == Hammer.Backend.ETS | ||
# With :single and configured backend config | ||
Application.put_env(:hammer, :backend, {Hammer.Backend.SomeBackend, []}) | ||
assert Hammer.Utils.get_backend_module(:single) == Hammer.Backend.SomeBackend | ||
# with a specific backend config | ||
Application.put_env(:hammer, :backend, one: {Hammer.Backend.SomeBackend, []}) | ||
assert Hammer.Utils.get_backend_module(:one) == Hammer.Backend.SomeBackend | ||
end | ||
end |