Skip to content

Commit 1b1f459

Browse files
committed
issues #28, #30: add cache manager tests
Signed-off-by: Matteo Cafasso <[email protected]>
1 parent 1498aed commit 1b1f459

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

test/cache_manager_test.exs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# This Source Code Form is subject to the terms of the Mozilla Public
2+
# License, v. 2.0. If a copy of the MPL was not distributed with this
3+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
#
5+
# Copyright (c) 2017-2019, Matteo Cafasso.
6+
# All rights reserved.
7+
8+
defmodule RabbitMQ.MessageDeduplicationPlugin.CacheManager.Test do
9+
use ExUnit.Case
10+
11+
alias :timer, as: Timer
12+
alias :mnesia, as: Mnesia
13+
alias RabbitMQ.MessageDeduplicationPlugin.Cache, as: Cache
14+
alias RabbitMQ.MessageDeduplicationPlugin.CacheManager, as: CacheManager
15+
16+
@caches :message_deduplication_caches
17+
18+
setup do
19+
start_supervised!(%{id: :cache_manager,
20+
start: {CacheManager,
21+
:start_link,
22+
[]}})
23+
24+
%{}
25+
end
26+
27+
test "cache creation", %{} do
28+
options = [persistence: :memory]
29+
30+
CacheManager.create(:cache, options)
31+
{:atomic, [:cache]} = Mnesia.transaction(fn -> Mnesia.all_keys(@caches) end)
32+
CacheManager.destroy(:cache)
33+
end
34+
35+
test "cache deletion", %{} do
36+
options = [persistence: :memory]
37+
38+
:ok = CacheManager.create(:cache, options)
39+
{:atomic, [:cache]} = Mnesia.transaction(fn -> Mnesia.all_keys(@caches) end)
40+
:ok = CacheManager.destroy(:cache)
41+
{:atomic, []} = Mnesia.transaction(fn -> Mnesia.all_keys(@caches) end)
42+
end
43+
44+
test "cache cleanup routine", %{} do
45+
options = [persistence: :memory]
46+
47+
:ok = CacheManager.create(:cache, options)
48+
49+
{:ok, :inserted} = Cache.insert(:cache, "foo", 1000)
50+
51+
Timer.sleep(3200)
52+
53+
{:atomic, []} = Mnesia.transaction(fn -> Mnesia.all_keys(:cache) end)
54+
55+
:ok = CacheManager.destroy(:cache)
56+
end
57+
58+
end

test/cache_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# License, v. 2.0. If a copy of the MPL was not distributed with this
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44
#
5-
# Copyright (c) 2017-2018, Matteo Cafasso.
5+
# Copyright (c) 2017-2019, Matteo Cafasso.
66
# All rights reserved.
77

88
defmodule RabbitMQ.MessageDeduplicationPlugin.Cache.Test do
@@ -69,7 +69,7 @@ defmodule RabbitMQ.MessageDeduplicationPlugin.Cache.Test do
6969
{:ok, :inserted} = Cache.insert(cache, "foo", Timer.seconds(1))
7070
{:ok, :exists} = Cache.insert(cache, "foo")
7171

72-
Timer.sleep(3200)
72+
Timer.sleep(1200)
7373

7474
:ok = Cache.delete_expired_entries(cache)
7575

0 commit comments

Comments
 (0)