Skip to content

Commit a269985

Browse files
committed
Migrate applicable activesupport tests to use NotificationAssertions
1 parent 6a62eed commit a269985

File tree

5 files changed

+38
-68
lines changed

5 files changed

+38
-68
lines changed

activesupport/test/cache/behaviors/cache_instrumentation_behavior.rb

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -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,9 +49,7 @@ 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]
@@ -68,9 +61,7 @@ def test_read_instrumentation
6861
key = SecureRandom.uuid
6962
@cache.write(key, SecureRandom.alphanumeric)
7063

71-
events = with_instrumentation "read" do
72-
@cache.read(key)
73-
end
64+
events = capture_notifications("cache_read.active_support") { @cache.read(key) }
7465

7566
assert_equal %w[ cache_read.active_support ], events.map(&:name)
7667
assert_equal normalized_key(key), events[0].payload[:key]
@@ -81,9 +72,7 @@ def test_read_instrumentation
8172
def test_write_instrumentation
8273
key = SecureRandom.uuid
8374

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) }
8776

8877
assert_equal %w[ cache_write.active_support ], events.map(&:name)
8978
assert_equal normalized_key(key), events[0].payload[:key]
@@ -94,9 +83,8 @@ def test_delete_instrumentation
9483
key = SecureRandom.uuid
9584

9685
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) }
10088

10189
assert_equal %w[ cache_delete.active_support ], events.map(&:name)
10290
assert_equal normalized_key(key, options), events[0].payload[:key]
@@ -109,9 +97,8 @@ def test_delete_multi_instrumentation
10997
key_2 = SecureRandom.uuid
11098

11199
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) }
115102

116103
assert_equal %w[ cache_delete_multi.active_support ], events.map(&:name)
117104
assert_equal [normalized_key(key_2, options), normalized_key(key_1, options)], events[0].payload[:key]
@@ -122,9 +109,7 @@ def test_increment_instrumentation
122109
key_1 = SecureRandom.uuid
123110
@cache.write(key_1, 0)
124111

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) }
128113

129114
assert_equal %w[ cache_increment.active_support ], events.map(&:name)
130115
assert_equal normalized_key(key_1), events[0].payload[:key]
@@ -136,27 +121,14 @@ def test_decrement_instrumentation
136121
key_1 = SecureRandom.uuid
137122
@cache.write(key_1, 0)
138123

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) }
142125

143126
assert_equal %w[ cache_decrement.active_support ], events.map(&:name)
144127
assert_equal normalized_key(key_1), events[0].payload[:key]
145128
assert_equal @cache.class.name, events[0].payload[:store]
146129
end
147130

148131
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-
160132
def normalized_key(key, options = nil)
161133
@cache.send(:normalize_key, key, options)
162134
end

activesupport/test/cache/behaviors/local_cache_behavior.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
module LocalCacheBehavior
44
def test_instrumentation_with_local_cache
55
key = SecureRandom.uuid
6-
events = with_instrumentation "write" do
6+
events = capture_notifications("cache_write.active_support") do
77
@cache.write(key, SecureRandom.uuid)
88
end
99
assert_equal @cache.class.name, events[0].payload[:store]
1010

1111
@cache.with_local_cache do
12-
events = with_instrumentation "read" do
12+
events = capture_notifications("cache_read.active_support") do
1313
@cache.read(key)
1414
@cache.read(key)
1515
end

activesupport/test/cache/stores/memory_store_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_cleanup_instrumentation
4444
size = 3
4545
size.times { |i| @cache.write(i.to_s, i) }
4646

47-
events = with_instrumentation "cleanup" do
47+
events = capture_notifications("cache_cleanup.active_support") do
4848
@cache.cleanup
4949
end
5050

activesupport/test/deprecation_test.rb

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -263,20 +263,17 @@ def initialize(label, lineno)
263263
@deprecator.behavior = :notify
264264
behavior = @deprecator.behavior.first
265265

266-
begin
267-
events = []
268-
ActiveSupport::Notifications.subscribe("deprecation.my_gem_custom") { |*args|
269-
events << args.extract_options!
270-
}
271-
272-
behavior.call("Some error!", ["call stack!"], @deprecator)
273-
assert_equal 1, events.size
274-
assert_equal "Some error!", events.first[:message]
275-
assert_equal ["call stack!"], events.first[:callstack]
276-
assert_equal "horizon", events.first[:deprecation_horizon]
277-
assert_equal "MyGem::Custom", events.first[:gem_name]
278-
ensure
279-
ActiveSupport::Notifications.unsubscribe("deprecation.my_gem_custom")
266+
expected_payload = {
267+
message: "Some error!",
268+
callstack: ["call stack!"],
269+
deprecation_horizon: "horizon",
270+
gem_name: "MyGem::Custom"
271+
}
272+
273+
assert_notifications_count("deprecation.my_gem_custom", 1) do
274+
assert_notification("deprecation.my_gem_custom", expected_payload) do
275+
behavior.call("Some error!", ["call stack!"], @deprecator)
276+
end
280277
end
281278
end
282279

activesupport/test/messages/serializer_with_fallback_test.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,18 @@ class MessagesSerializerWithFallbackTest < ActiveSupport::TestCase
6464
value = { "foo" => "bar" }
6565
dumped = serializer(:json).dump(value)
6666

67-
payloads = []
68-
callback = -> (*args) { payloads << args.extract_options! }
69-
ActiveSupport::Notifications.subscribed(callback, "message_serializer_fallback.active_support") do
70-
serializer(:marshal).load(dumped)
67+
expected_payload = {
68+
serializer: :marshal,
69+
fallback: :json,
70+
serialized: dumped,
71+
deserialized: value,
72+
}
73+
74+
assert_notifications_count("message_serializer_fallback.active_support", 1) do
75+
assert_notification("message_serializer_fallback.active_support", expected_payload) do
76+
serializer(:marshal).load(dumped)
77+
end
7178
end
72-
73-
assert_equal 1, payloads.length
74-
assert_equal :marshal, payloads.first[:serializer]
75-
assert_equal :json, payloads.first[:fallback]
76-
assert_equal dumped, payloads.first[:serialized]
77-
assert_equal value, payloads.first[:deserialized]
7879
end
7980

8081
test "raises on invalid format name" do

0 commit comments

Comments
 (0)