-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathcache_test.exs
151 lines (109 loc) · 4.71 KB
/
cache_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2017-2023, Matteo Cafasso.
# All rights reserved.
defmodule RabbitMQMessageDeduplication.Cache.Test do
use ExUnit.Case
alias :timer, as: Timer
alias :mnesia, as: Mnesia
alias RabbitMQMessageDeduplication.Cache, as: Cache
setup do
cache = :test_cache
cache_ttl = :test_cache_ttl
cache_simple = :cache_simple
on_exit fn ->
Mnesia.delete_table(cache)
Mnesia.delete_table(cache_ttl)
Mnesia.delete_table(cache_simple)
end
cache_simple_options = [persistence: :memory]
cache_options = [size: 1, ttl: nil, persistence: :memory]
cache_ttl_options = [size: 1, ttl: Timer.seconds(1), persistence: :memory]
:ok = Cache.create(cache, true, cache_options)
:ok = Cache.create(cache_ttl, false, cache_ttl_options)
:ok = Cache.create(cache_simple, true, cache_simple_options)
%{cache: cache, cache_ttl: cache_ttl, cache_simple: cache_simple}
end
test "basic insertion",
%{cache: cache, cache_ttl: _, cache_simple: _} do
{:ok, :inserted} = Cache.insert(cache, "foo")
{:ok, :exists} = Cache.insert(cache, "foo")
end
test "TTL at insertion",
%{cache: cache, cache_ttl: _, cache_simple: _} do
{:ok, :inserted} = Cache.insert(cache, "foo", Timer.seconds(1))
{:ok, :exists} = Cache.insert(cache, "foo")
1 |> Timer.seconds() |> Timer.sleep()
:ok = Cache.delete_expired_entries(cache)
{:ok, :inserted} = Cache.insert(cache, "foo")
end
test "TTL at table creation",
%{cache: _, cache_ttl: cache, cache_simple: _} do
{:ok, :inserted} = Cache.insert(cache, "foo")
{:ok, :exists} = Cache.insert(cache, "foo")
1 |> Timer.seconds() |> Timer.sleep()
:ok = Cache.delete_expired_entries(cache)
{:ok, :inserted} = Cache.insert(cache, "foo")
end
test "entries are deleted after TTL",
%{cache: cache, cache_ttl: _, cache_simple: _} do
{:ok, :inserted} = Cache.insert(cache, "foo", Timer.seconds(1))
{:ok, :exists} = Cache.insert(cache, "foo")
Timer.sleep(1200)
:ok = Cache.delete_expired_entries(cache)
{:atomic, []} = Mnesia.transaction(fn -> Mnesia.all_keys(cache) end)
end
test "entries are deleted if cache is full",
%{cache: cache, cache_ttl: _, cache_simple: _} do
{:ok, :inserted} = Cache.insert(cache, "foo")
{:ok, :exists} = Cache.insert(cache, "foo")
{:ok, :inserted} = Cache.insert(cache, "bar")
{:ok, :exists} = Cache.insert(cache, "bar")
{:ok, :inserted} = Cache.insert(cache, "foo")
end
test "cache entry deletion", %{cache: cache, cache_ttl: _, cache_simple: _} do
{:ok, :inserted} = Cache.insert(cache, "foo")
{:ok, :exists} = Cache.insert(cache, "foo")
Cache.delete(cache, "foo")
{:ok, :inserted} = Cache.insert(cache, "foo")
end
test "cache information",
%{cache: cache, cache_ttl: _, cache_simple: _} do
{:ok, :inserted} = Cache.insert(cache, "foo")
[entries: 1, bytes: _, nodes: _, size: 1] = Cache.info(cache)
end
test "simple cache information",
%{cache: _, cache_ttl: _, cache_simple: cache_simple} do
{:ok, :inserted} = Cache.insert(cache_simple, "foo")
[entries: 1, bytes: _, nodes: _] = Cache.info(cache_simple)
end
test "flush the cache", %{cache: cache, cache_ttl: _, cache_simple: _} do
{:ok, :inserted} = Cache.insert(cache, "foo")
{:ok, :exists} = Cache.insert(cache, "foo")
:ok = Cache.flush(cache)
{:ok, :inserted} = Cache.insert(cache, "foo")
end
test "drop the cache", %{cache: cache, cache_ttl: _, cache_simple: _} do
:ok = Cache.drop(cache)
assert Enum.member?(Mnesia.system_info(:tables), cache) == false
end
test "reconfigure the cache", %{cache: cache, cache_ttl: _, cache_simple: _} do
:ok = Cache.change_option(cache, :size, 10)
[entries: _, bytes: _, nodes: _, size: 10] = Cache.info(cache)
{:error, {:invalid, :wrong_key}} = Cache.change_option(cache, :wrong_key, 10)
end
test "reconfigure old cache on creation", %{cache: cache, cache_ttl: _, cache_simple: _} do
Mnesia.delete_table_property(cache, :distributed)
Mnesia.delete_table_property(cache, :size)
Mnesia.delete_table_property(cache, :ttl)
Mnesia.write_table_property(cache, {:limit, 100})
Mnesia.write_table_property(cache, {:default_ttl, 100})
cache_options = [size: 1, ttl: nil, persistence: :memory]
Cache.create(cache, true, cache_options)
{:ttl, 100} = Mnesia.read_table_property(cache, :ttl)
{:size, 100} = Mnesia.read_table_property(cache, :size)
{:distributed, true} = Mnesia.read_table_property(cache, :distributed)
end
end