From 9f0b8eb5843b8f8db19440fcb1962727f655eed1 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Mon, 17 Jan 2022 10:49:31 -0800 Subject: [PATCH] Deprecate Event#{children,parent_of} --- activesupport/CHANGELOG.md | 2 ++ .../notifications/instrumenter.rb | 28 ++++++++++++------- activesupport/test/notifications_test.rb | 15 ---------- 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 1d4f2f64de..8507a00a05 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,5 @@ +* Deprecate `Notification::Event`'s `#children` and `#parent_of?` + * Change default serialization format of `MessageEncryptor` from `Marshal` to `JSON` for Rails 7.1. Existing apps are provided with an upgrade path to migrate to `JSON` as described in `guides/source/upgrading_ruby_on_rails.md` diff --git a/activesupport/lib/active_support/notifications/instrumenter.rb b/activesupport/lib/active_support/notifications/instrumenter.rb index c69e8cd8d4..867cb7c6ef 100644 --- a/activesupport/lib/active_support/notifications/instrumenter.rb +++ b/activesupport/lib/active_support/notifications/instrumenter.rb @@ -56,7 +56,7 @@ module ActiveSupport end class Event - attr_reader :name, :time, :end, :transaction_id, :children + attr_reader :name, :time, :end, :transaction_id attr_accessor :payload def initialize(name, start, ending, transaction_id, payload) @@ -65,7 +65,6 @@ module ActiveSupport @time = start ? start.to_f * 1_000.0 : start @transaction_id = transaction_id @end = ending ? ending.to_f * 1_000.0 : ending - @children = [] @cpu_time_start = 0.0 @cpu_time_finish = 0.0 @allocation_count_start = 0 @@ -117,6 +116,23 @@ module ActiveSupport @allocation_count_finish - @allocation_count_start end + def children # :nodoc: + ActiveSupport::Deprecation.warn <<~EOM + ActiveSupport::Notifications::Event#children is deprecated and will + be removed in Rails 7.2. + EOM + [] + end + + def parent_of?(event) # :nodoc: + ActiveSupport::Deprecation.warn <<~EOM + ActiveSupport::Notifications::Event#parent_of? is deprecated and will + be removed in Rails 7.2. + EOM + start = (time - event.time) * 1000 + start <= 0 && (start + duration >= event.duration) + end + # Returns the difference in milliseconds between when the execution of the # event started and when it ended. # @@ -133,14 +149,6 @@ module ActiveSupport self.end - time end - def <<(event) - @children << event - end - - def parent_of?(event) - @children.include? event - end - private def now Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb index 2964e752dc..775c470862 100644 --- a/activesupport/test/notifications_test.rb +++ b/activesupport/test/notifications_test.rb @@ -462,21 +462,6 @@ module Notifications assert_equal Hash[payload: :bar], event.payload end - def test_event_is_parent_based_on_children - time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - - parent = event(:foo, Process.clock_gettime(Process::CLOCK_MONOTONIC), Process.clock_gettime(Process::CLOCK_MONOTONIC) + 100, random_id, {}) - child = event(:foo, time, time + 10, random_id, {}) - not_child = event(:foo, time, time + 100, random_id, {}) - - parent.children << child - - assert parent.parent_of?(child) - assert_not child.parent_of?(parent) - assert_not parent.parent_of?(not_child) - assert_not not_child.parent_of?(parent) - end - def test_subscribe_raises_error_on_non_supported_arguments notifier = ActiveSupport::Notifications::Fanout.new