Skip to content

Commit 39ed618

Browse files
committed
add missing tests
1 parent b0e23d6 commit 39ed618

File tree

1 file changed

+80
-7
lines changed

1 file changed

+80
-7
lines changed

test/scenic/cache_test.exs

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,30 @@ defmodule Scenic.CacheTest do
1717
@agent_name :test_agent_name
1818

1919
# --------------------------------------------------------
20-
setup do
21-
assert :ets.info(@cache_table) == :undefined
22-
assert :ets.info(@scope_table) == :undefined
23-
:ets.new(@cache_table, [:set, :public, :named_table])
24-
:ets.new(@scope_table, [:bag, :public, :named_table])
20+
# setup do
21+
# assert :ets.info(@cache_table) == :undefined
22+
# assert :ets.info(@scope_table) == :undefined
23+
# :ets.new(@cache_table, [:set, :public, :named_table])
24+
# :ets.new(@scope_table, [:bag, :public, :named_table])
2525

26+
# {:ok, agent} = Agent.start(fn -> 1 + 1 end, name: @agent_name)
27+
# on_exit(fn -> Agent.stop(agent) end)
28+
29+
# %{agent: agent}
30+
# end
31+
32+
setup do
33+
Process.sleep(2)
34+
{:ok, cache} = Cache.start_link(nil)
2635
{:ok, agent} = Agent.start(fn -> 1 + 1 end, name: @agent_name)
27-
on_exit(fn -> Agent.stop(agent) end)
36+
Process.sleep(4)
2837

29-
%{agent: agent}
38+
on_exit(fn ->
39+
Agent.stop(agent)
40+
Process.exit(cache, :normal)
41+
end)
42+
43+
%{cache: cache, agent: agent}
3044
end
3145

3246
# ============================================================================
@@ -117,6 +131,22 @@ defmodule Scenic.CacheTest do
117131
assert Cache.get!("test_key_1") == "data_1"
118132
end
119133

134+
test "put overwrites existing items" do
135+
Cache.put({:test_key, 123}, "data")
136+
assert Cache.get!({:test_key, 123}) == "data"
137+
Cache.put({:test_key, 123}, "new data")
138+
assert Cache.get!({:test_key, 123}) == "new data"
139+
end
140+
141+
# ============================================================================
142+
# member?
143+
144+
test "member? works" do
145+
refute Cache.member?("test_key")
146+
Cache.put("test_key", "data")
147+
assert Cache.member?("test_key")
148+
end
149+
120150
# ============================================================================
121151
# claim
122152

@@ -246,6 +276,29 @@ defmodule Scenic.CacheTest do
246276
refute Cache.release("test_key", delay: 0)
247277
end
248278

279+
test "release delays the release by default" do
280+
assert Cache.put("test_key", "data") == {:ok, "test_key"}
281+
assert :ets.lookup(@cache_table, "test_key") == [{"test_key", 1, "data"}]
282+
assert :ets.lookup(@scope_table, self()) == [{self(), "test_key", self()}]
283+
284+
assert Cache.release("test_key")
285+
286+
assert :ets.lookup(@cache_table, "test_key") == [{"test_key", 1, "data"}]
287+
end
288+
289+
test "release delays the release by the specified amount" do
290+
assert Cache.put("test_key", "data") == {:ok, "test_key"}
291+
assert :ets.lookup(@cache_table, "test_key") == [{"test_key", 1, "data"}]
292+
assert :ets.lookup(@scope_table, self()) == [{self(), "test_key", self()}]
293+
294+
assert Cache.release("test_key", delay: 10)
295+
assert :ets.lookup(@cache_table, "test_key") == [{"test_key", 1, "data"}]
296+
297+
Process.sleep(20)
298+
assert :ets.lookup(@cache_table, "test_key") == []
299+
assert :ets.lookup(@scope_table, self()) == []
300+
end
301+
249302
# ============================================================================
250303
# status
251304

@@ -402,6 +455,26 @@ defmodule Scenic.CacheTest do
402455
Cache.stop_notification(@cache_release)
403456
assert Registry.keys(@cache_registry, self()) == []
404457
end
458+
459+
# ============================================================================
460+
# notifications - integration style
461+
462+
test "notifications work - integration style" do
463+
refute Cache.member?("test_key")
464+
465+
# sign up for notifications
466+
Cache.request_notification(@cache_put)
467+
refute_received({:"$gen_cast", {:cache_put, _}})
468+
469+
# put the key. This should send a notification
470+
Cache.put("test_key", 123)
471+
assert_received({:"$gen_cast", {:cache_put, "test_key"}})
472+
473+
# stop notifications
474+
Cache.stop_notification()
475+
Cache.put("test_key1", 123)
476+
refute_received({:"$gen_cast", {:cache_put, _}})
477+
end
405478
end
406479

407480
# ==============================================================================

0 commit comments

Comments
 (0)