Skip to content

Commit

Permalink
Merge pull request rails#52912 from tgwizard/as-cache-intstrument-del…
Browse files Browse the repository at this point in the history
…ete-options

Include options when instrumenting ActiveSupport::Cache::Store delete
  • Loading branch information
byroot authored Sep 14, 2024
2 parents cc2800a + 83fa6f8 commit 6e46b42
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
4 changes: 4 additions & 0 deletions activesupport/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Include options when instrumenting `ActiveSupport::Cache::Store#delete` and `ActiveSupport::Cache::Store#delete_multi`.

*Adam Renberg Tamm*

* Print test names when running `rails test -v` for parallel tests.

*John Hawthorn*, *Abeid Ahmed*
Expand Down
4 changes: 2 additions & 2 deletions activesupport/lib/active_support/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ def delete(name, options = nil)
options = merged_options(options)
key = normalize_key(name, options)

instrument(:delete, key) do
instrument(:delete, key, options) do
delete_entry(key, **options)
end
end
Expand All @@ -692,7 +692,7 @@ def delete_multi(names, options = nil)
options = merged_options(options)
names.map! { |key| normalize_key(key, options) }

instrument_multi :delete_multi, names do
instrument_multi(:delete_multi, names, options) do
delete_multi_entries(names, **options)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,60 @@ def test_read_multi_instrumentation
assert_equal @cache.class.name, events[0].payload[:store]
end

def test_read_instrumentation
key = SecureRandom.uuid
@cache.write(key, SecureRandom.alphanumeric)

events = with_instrumentation "read" do
@cache.read(key)
end

assert_equal %w[ cache_read.active_support ], events.map(&:name)
assert_equal normalized_key(key), events[0].payload[:key]
assert_same true, events[0].payload[:hit]
assert_equal @cache.class.name, events[0].payload[:store]
end

def test_write_instrumentation
key = SecureRandom.uuid

events = with_instrumentation "write" do
@cache.write(key, SecureRandom.alphanumeric)
end

assert_equal %w[ cache_write.active_support ], events.map(&:name)
assert_equal normalized_key(key), events[0].payload[:key]
assert_equal @cache.class.name, events[0].payload[:store]
end

def test_delete_instrumentation
key = SecureRandom.uuid

options = { namespace: "foo" }
events = with_instrumentation "delete" do
@cache.delete(key, options)
end

assert_equal %w[ cache_delete.active_support ], events.map(&:name)
assert_equal normalized_key(key, options), events[0].payload[:key]
assert_equal @cache.class.name, events[0].payload[:store]
assert_equal "foo", events[0].payload[:namespace]
end

def test_delete_multi_instrumentation
key_1 = SecureRandom.uuid
key_2 = SecureRandom.uuid

options = { namespace: "foo" }
events = with_instrumentation "delete_multi" do
@cache.delete_multi([key_2, key_1], options)
end

assert_equal %w[ cache_delete_multi.active_support ], events.map(&:name)
assert_equal [normalized_key(key_2, options), normalized_key(key_1, options)], events[0].payload[:key]
assert_equal @cache.class.name, events[0].payload[:store]
end

def test_increment_instrumentation
key_1 = SecureRandom.uuid
@cache.write(key_1, 0)
Expand Down Expand Up @@ -103,7 +157,7 @@ def with_instrumentation(method)
ActiveSupport::Notifications.unsubscribe event_name
end

def normalized_key(key)
@cache.send(:normalize_key, key, @cache.options)
def normalized_key(key, options = nil)
@cache.send(:normalize_key, key, options)
end
end

0 comments on commit 6e46b42

Please sign in to comment.