@@ -8,9 +8,7 @@ def test_write_multi_instrumentation
88 value_2 = SecureRandom . alphanumeric
99 writes = { key_1 => value_1 , key_2 => value_2 }
1010
11- events = with_instrumentation "write_multi" do
12- @cache . write_multi ( writes )
13- end
11+ events = capture_notifications ( "cache_write_multi.active_support" ) { @cache . write_multi ( writes ) }
1412
1513 assert_equal %w[ cache_write_multi.active_support ] , events . map ( &:name )
1614 assert_nil events [ 0 ] . payload [ :super_operation ]
@@ -23,7 +21,7 @@ def test_instrumentation_with_fetch_multi_as_super_operation
2321
2422 key_2 = SecureRandom . uuid
2523
26- events = with_instrumentation "read_multi" do
24+ events = capture_notifications ( "cache_read_multi.active_support" ) do
2725 @cache . fetch_multi ( key_2 , key_1 ) { |key | key * 2 }
2826 end
2927
@@ -35,17 +33,14 @@ def test_instrumentation_with_fetch_multi_as_super_operation
3533 end
3634
3735 def test_fetch_multi_instrumentation_order_of_operations
38- operations = [ ]
39- callback = -> ( name , *) { operations << name }
40-
4136 key_1 = SecureRandom . uuid
4237 key_2 = SecureRandom . uuid
4338
44- ActiveSupport :: Notifications . subscribed ( callback , /^cache_(read_multi|write_multi)\. active_support$/ ) do
39+ operations = capture_notifications ( /^cache_(read_multi|write_multi)\. active_support$/ ) do
4540 @cache . fetch_multi ( key_1 , key_2 ) { |key | key * 2 }
4641 end
4742
48- assert_equal %w[ cache_read_multi.active_support cache_write_multi.active_support ] , operations
43+ assert_equal %w[ cache_read_multi.active_support cache_write_multi.active_support ] , operations . map ( & :name )
4944 end
5045
5146 def test_read_multi_instrumentation
@@ -54,23 +49,67 @@ def test_read_multi_instrumentation
5449
5550 key_2 = SecureRandom . uuid
5651
57- events = with_instrumentation "read_multi" do
58- @cache . read_multi ( key_2 , key_1 )
59- end
52+ events = capture_notifications ( "cache_read_multi.active_support" ) { @cache . read_multi ( key_2 , key_1 ) }
6053
6154 assert_equal %w[ cache_read_multi.active_support ] , events . map ( &:name )
6255 assert_equal [ normalized_key ( key_2 ) , normalized_key ( key_1 ) ] , events [ 0 ] . payload [ :key ]
6356 assert_equal [ normalized_key ( key_1 ) ] , events [ 0 ] . payload [ :hits ]
6457 assert_equal @cache . class . name , events [ 0 ] . payload [ :store ]
6558 end
6659
60+ def test_read_instrumentation
61+ key = SecureRandom . uuid
62+ @cache . write ( key , SecureRandom . alphanumeric )
63+
64+ events = capture_notifications ( "cache_read.active_support" ) { @cache . read ( key ) }
65+
66+ assert_equal %w[ cache_read.active_support ] , events . map ( &:name )
67+ assert_equal normalized_key ( key ) , events [ 0 ] . payload [ :key ]
68+ assert_same true , events [ 0 ] . payload [ :hit ]
69+ assert_equal @cache . class . name , events [ 0 ] . payload [ :store ]
70+ end
71+
72+ def test_write_instrumentation
73+ key = SecureRandom . uuid
74+
75+ events = capture_notifications ( "cache_write.active_support" ) { @cache . write ( key , SecureRandom . alphanumeric ) }
76+
77+ assert_equal %w[ cache_write.active_support ] , events . map ( &:name )
78+ assert_equal normalized_key ( key ) , events [ 0 ] . payload [ :key ]
79+ assert_equal @cache . class . name , events [ 0 ] . payload [ :store ]
80+ end
81+
82+ def test_delete_instrumentation
83+ key = SecureRandom . uuid
84+
85+ options = { namespace : "foo" }
86+
87+ events = capture_notifications ( "cache_delete.active_support" ) { @cache . delete ( key , options ) }
88+
89+ assert_equal %w[ cache_delete.active_support ] , events . map ( &:name )
90+ assert_equal normalized_key ( key , options ) , events [ 0 ] . payload [ :key ]
91+ assert_equal @cache . class . name , events [ 0 ] . payload [ :store ]
92+ assert_equal "foo" , events [ 0 ] . payload [ :namespace ]
93+ end
94+
95+ def test_delete_multi_instrumentation
96+ key_1 = SecureRandom . uuid
97+ key_2 = SecureRandom . uuid
98+
99+ options = { namespace : "foo" }
100+
101+ events = capture_notifications ( "cache_delete_multi.active_support" ) { @cache . delete_multi ( [ key_2 , key_1 ] , options ) }
102+
103+ assert_equal %w[ cache_delete_multi.active_support ] , events . map ( &:name )
104+ assert_equal [ normalized_key ( key_2 , options ) , normalized_key ( key_1 , options ) ] , events [ 0 ] . payload [ :key ]
105+ assert_equal @cache . class . name , events [ 0 ] . payload [ :store ]
106+ end
107+
67108 def test_increment_instrumentation
68109 key_1 = SecureRandom . uuid
69110 @cache . write ( key_1 , 0 )
70111
71- events = with_instrumentation "increment" do
72- @cache . increment ( key_1 )
73- end
112+ events = capture_notifications ( "cache_increment.active_support" ) { @cache . increment ( key_1 ) }
74113
75114 assert_equal %w[ cache_increment.active_support ] , events . map ( &:name )
76115 assert_equal normalized_key ( key_1 ) , events [ 0 ] . payload [ :key ]
@@ -82,28 +121,15 @@ def test_decrement_instrumentation
82121 key_1 = SecureRandom . uuid
83122 @cache . write ( key_1 , 0 )
84123
85- events = with_instrumentation "decrement" do
86- @cache . decrement ( key_1 )
87- end
124+ events = capture_notifications ( "cache_decrement.active_support" ) { @cache . decrement ( key_1 ) }
88125
89126 assert_equal %w[ cache_decrement.active_support ] , events . map ( &:name )
90127 assert_equal normalized_key ( key_1 ) , events [ 0 ] . payload [ :key ]
91128 assert_equal @cache . class . name , events [ 0 ] . payload [ :store ]
92129 end
93130
94131 private
95- def with_instrumentation ( method )
96- event_name = "cache_#{ method } .active_support"
97-
98- [ ] . tap do |events |
99- ActiveSupport ::Notifications . subscribe ( event_name ) { |event | events << event }
100- yield
101- end
102- ensure
103- ActiveSupport ::Notifications . unsubscribe event_name
104- end
105-
106- def normalized_key ( key )
107- @cache . send ( :normalize_key , key , @cache . options )
132+ def normalized_key ( key , options = nil )
133+ @cache . send ( :normalize_key , key , options )
108134 end
109135end
0 commit comments