@@ -17,16 +17,30 @@ defmodule Scenic.CacheTest do
17
17
@ agent_name :test_agent_name
18
18
19
19
# --------------------------------------------------------
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])
25
25
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 )
26
35
{ :ok , agent } = Agent . start ( fn -> 1 + 1 end , name: @ agent_name )
27
- on_exit ( fn -> Agent . stop ( agent ) end )
36
+ Process . sleep ( 4 )
28
37
29
- % { agent: agent }
38
+ on_exit ( fn ->
39
+ Agent . stop ( agent )
40
+ Process . exit ( cache , :normal )
41
+ end )
42
+
43
+ % { cache: cache , agent: agent }
30
44
end
31
45
32
46
# ============================================================================
@@ -117,6 +131,22 @@ defmodule Scenic.CacheTest do
117
131
assert Cache . get! ( "test_key_1" ) == "data_1"
118
132
end
119
133
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
+
120
150
# ============================================================================
121
151
# claim
122
152
@@ -246,6 +276,29 @@ defmodule Scenic.CacheTest do
246
276
refute Cache . release ( "test_key" , delay: 0 )
247
277
end
248
278
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
+
249
302
# ============================================================================
250
303
# status
251
304
@@ -402,6 +455,26 @@ defmodule Scenic.CacheTest do
402
455
Cache . stop_notification ( @ cache_release )
403
456
assert Registry . keys ( @ cache_registry , self ( ) ) == [ ]
404
457
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
405
478
end
406
479
407
480
# ==============================================================================
0 commit comments