@@ -8,9 +8,7 @@ def test_write_multi_instrumentation
8
8
value_2 = SecureRandom . alphanumeric
9
9
writes = { key_1 => value_1 , key_2 => value_2 }
10
10
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 ) }
14
12
15
13
assert_equal %w[ cache_write_multi.active_support ] , events . map ( &:name )
16
14
assert_nil events [ 0 ] . payload [ :super_operation ]
@@ -23,7 +21,7 @@ def test_instrumentation_with_fetch_multi_as_super_operation
23
21
24
22
key_2 = SecureRandom . uuid
25
23
26
- events = with_instrumentation "read_multi" do
24
+ events = capture_notifications ( "cache_read_multi.active_support" ) do
27
25
@cache . fetch_multi ( key_2 , key_1 ) { |key | key * 2 }
28
26
end
29
27
@@ -35,17 +33,14 @@ def test_instrumentation_with_fetch_multi_as_super_operation
35
33
end
36
34
37
35
def test_fetch_multi_instrumentation_order_of_operations
38
- operations = [ ]
39
- callback = -> ( name , *) { operations << name }
40
-
41
36
key_1 = SecureRandom . uuid
42
37
key_2 = SecureRandom . uuid
43
38
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
45
40
@cache . fetch_multi ( key_1 , key_2 ) { |key | key * 2 }
46
41
end
47
42
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 )
49
44
end
50
45
51
46
def test_read_multi_instrumentation
@@ -54,9 +49,7 @@ def test_read_multi_instrumentation
54
49
55
50
key_2 = SecureRandom . uuid
56
51
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 ) }
60
53
61
54
assert_equal %w[ cache_read_multi.active_support ] , events . map ( &:name )
62
55
assert_equal [ normalized_key ( key_2 ) , normalized_key ( key_1 ) ] , events [ 0 ] . payload [ :key ]
@@ -68,9 +61,7 @@ def test_read_instrumentation
68
61
key = SecureRandom . uuid
69
62
@cache . write ( key , SecureRandom . alphanumeric )
70
63
71
- events = with_instrumentation "read" do
72
- @cache . read ( key )
73
- end
64
+ events = capture_notifications ( "cache_read.active_support" ) { @cache . read ( key ) }
74
65
75
66
assert_equal %w[ cache_read.active_support ] , events . map ( &:name )
76
67
assert_equal normalized_key ( key ) , events [ 0 ] . payload [ :key ]
@@ -81,9 +72,7 @@ def test_read_instrumentation
81
72
def test_write_instrumentation
82
73
key = SecureRandom . uuid
83
74
84
- events = with_instrumentation "write" do
85
- @cache . write ( key , SecureRandom . alphanumeric )
86
- end
75
+ events = capture_notifications ( "cache_write.active_support" ) { @cache . write ( key , SecureRandom . alphanumeric ) }
87
76
88
77
assert_equal %w[ cache_write.active_support ] , events . map ( &:name )
89
78
assert_equal normalized_key ( key ) , events [ 0 ] . payload [ :key ]
@@ -94,9 +83,8 @@ def test_delete_instrumentation
94
83
key = SecureRandom . uuid
95
84
96
85
options = { namespace : "foo" }
97
- events = with_instrumentation "delete" do
98
- @cache . delete ( key , options )
99
- end
86
+
87
+ events = capture_notifications ( "cache_delete.active_support" ) { @cache . delete ( key , options ) }
100
88
101
89
assert_equal %w[ cache_delete.active_support ] , events . map ( &:name )
102
90
assert_equal normalized_key ( key , options ) , events [ 0 ] . payload [ :key ]
@@ -109,9 +97,8 @@ def test_delete_multi_instrumentation
109
97
key_2 = SecureRandom . uuid
110
98
111
99
options = { namespace : "foo" }
112
- events = with_instrumentation "delete_multi" do
113
- @cache . delete_multi ( [ key_2 , key_1 ] , options )
114
- end
100
+
101
+ events = capture_notifications ( "cache_delete_multi.active_support" ) { @cache . delete_multi ( [ key_2 , key_1 ] , options ) }
115
102
116
103
assert_equal %w[ cache_delete_multi.active_support ] , events . map ( &:name )
117
104
assert_equal [ normalized_key ( key_2 , options ) , normalized_key ( key_1 , options ) ] , events [ 0 ] . payload [ :key ]
@@ -122,9 +109,7 @@ def test_increment_instrumentation
122
109
key_1 = SecureRandom . uuid
123
110
@cache . write ( key_1 , 0 )
124
111
125
- events = with_instrumentation "increment" do
126
- @cache . increment ( key_1 )
127
- end
112
+ events = capture_notifications ( "cache_increment.active_support" ) { @cache . increment ( key_1 ) }
128
113
129
114
assert_equal %w[ cache_increment.active_support ] , events . map ( &:name )
130
115
assert_equal normalized_key ( key_1 ) , events [ 0 ] . payload [ :key ]
@@ -136,27 +121,14 @@ def test_decrement_instrumentation
136
121
key_1 = SecureRandom . uuid
137
122
@cache . write ( key_1 , 0 )
138
123
139
- events = with_instrumentation "decrement" do
140
- @cache . decrement ( key_1 )
141
- end
124
+ events = capture_notifications ( "cache_decrement.active_support" ) { @cache . decrement ( key_1 ) }
142
125
143
126
assert_equal %w[ cache_decrement.active_support ] , events . map ( &:name )
144
127
assert_equal normalized_key ( key_1 ) , events [ 0 ] . payload [ :key ]
145
128
assert_equal @cache . class . name , events [ 0 ] . payload [ :store ]
146
129
end
147
130
148
131
private
149
- def with_instrumentation ( method )
150
- event_name = "cache_#{ method } .active_support"
151
-
152
- [ ] . tap do |events |
153
- ActiveSupport ::Notifications . subscribe ( event_name ) { |event | events << event }
154
- yield
155
- end
156
- ensure
157
- ActiveSupport ::Notifications . unsubscribe event_name
158
- end
159
-
160
132
def normalized_key ( key , options = nil )
161
133
@cache . send ( :normalize_key , key , options )
162
134
end
0 commit comments