aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorRafael França <rafael@franca.dev>2019-07-25 16:47:03 -0400
committerGitHub <noreply@github.com>2019-07-25 16:47:03 -0400
commitbd5edc29709116104345cf09c17f1db89428753e (patch)
tree63c8ba5a61b4f9a1a57435b3cd4aaeb92707a86b /activesupport
parentc9b7b9ff8adb3f01db0f9af90359030028a33b5b (diff)
parent218f0777d6abf1b2f933e44bd08f75f3d28c75ca (diff)
downloadrails-bd5edc29709116104345cf09c17f1db89428753e.tar.gz
rails-bd5edc29709116104345cf09c17f1db89428753e.tar.bz2
rails-bd5edc29709116104345cf09c17f1db89428753e.zip
Merge pull request #36318 from itsWill/fix_event_object_payload
Merge payload for EventObject subscribers
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/notifications/fanout.rb1
-rw-r--r--activesupport/lib/active_support/notifications/instrumenter.rb3
-rw-r--r--activesupport/test/notifications_test.rb21
3 files changed, 24 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/notifications/fanout.rb b/activesupport/lib/active_support/notifications/fanout.rb
index aa602329ec..dda71b880e 100644
--- a/activesupport/lib/active_support/notifications/fanout.rb
+++ b/activesupport/lib/active_support/notifications/fanout.rb
@@ -218,6 +218,7 @@ module ActiveSupport
def finish(name, id, payload)
stack = Thread.current[:_event_stack]
event = stack.pop
+ event.payload = payload
event.finish!
@delegate.call event
end
diff --git a/activesupport/lib/active_support/notifications/instrumenter.rb b/activesupport/lib/active_support/notifications/instrumenter.rb
index 7ab39c9bfb..24e1ab313a 100644
--- a/activesupport/lib/active_support/notifications/instrumenter.rb
+++ b/activesupport/lib/active_support/notifications/instrumenter.rb
@@ -52,7 +52,8 @@ module ActiveSupport
end
class Event
- attr_reader :name, :time, :end, :transaction_id, :payload, :children
+ attr_reader :name, :time, :end, :transaction_id, :children
+ attr_accessor :payload
def self.clock_gettime_supported? # :nodoc:
defined?(Process::CLOCK_PROCESS_CPUTIME_ID) &&
diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb
index c9c63680e4..08277e5436 100644
--- a/activesupport/test/notifications_test.rb
+++ b/activesupport/test/notifications_test.rb
@@ -41,6 +41,27 @@ module Notifications
assert_operator event.duration, :>, 0
end
+ def test_subscribe_to_events_where_payload_is_changed_during_instrumentation
+ @notifier.subscribe do |event|
+ assert_equal "success!", event.payload[:my_key]
+ end
+
+ ActiveSupport::Notifications.instrument("foo") do |payload|
+ payload[:my_key] = "success!"
+ end
+ end
+
+ def test_subscribe_to_events_can_handle_nested_hashes_in_the_paylaod
+ @notifier.subscribe do |event|
+ assert_equal "success!", event.payload[:some_key][:key_one]
+ assert_equal "great_success!", event.payload[:some_key][:key_two]
+ end
+
+ ActiveSupport::Notifications.instrument("foo", some_key: { key_one: "success!" }) do |payload|
+ payload[:some_key][:key_two] = "great_success!"
+ end
+ end
+
def test_subscribe_via_top_level_api
old_notifier = ActiveSupport::Notifications.notifier
ActiveSupport::Notifications.notifier = ActiveSupport::Notifications::Fanout.new