Fix ActiveSupport::Notifications.publish_event
to preserve units
Ref: https://github.com/rails/rails/pull/43502 Fix: https://github.com/rails/rails/pull/50767 Fix: https://github.com/rails/rails/pull/50493 When republishing a an event into a `start, finish` tuple, we need to convert the timestamps back into seconds.
This commit is contained in:
parent
2fb05958a6
commit
de779f2bf7
@ -104,7 +104,7 @@ def unique_id
|
||||
end
|
||||
|
||||
class Event
|
||||
attr_reader :name, :time, :end, :transaction_id
|
||||
attr_reader :name, :transaction_id
|
||||
attr_accessor :payload
|
||||
|
||||
def initialize(name, start, ending, transaction_id, payload)
|
||||
@ -119,7 +119,15 @@ def initialize(name, start, ending, transaction_id, payload)
|
||||
@allocation_count_finish = 0
|
||||
end
|
||||
|
||||
def record
|
||||
def time
|
||||
@time / 1000.0 if @time
|
||||
end
|
||||
|
||||
def end
|
||||
@end / 1000.0 if @end
|
||||
end
|
||||
|
||||
def record # :nodoc:
|
||||
start!
|
||||
begin
|
||||
yield payload if block_given?
|
||||
@ -195,7 +203,7 @@ def parent_of?(event) # :nodoc:
|
||||
#
|
||||
# @event.duration # => 1000.138
|
||||
def duration
|
||||
self.end - time
|
||||
@end - @time
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -127,4 +127,25 @@ def test_supports_publish_event
|
||||
ensure
|
||||
TestSubscriber.detach_from :doodle
|
||||
end
|
||||
|
||||
def test_publish_event_preserve_units
|
||||
event = ActiveSupport::Notifications::Event.new("publish_event.test", nil, nil, 42, {})
|
||||
event.record { sleep 0.1 }
|
||||
|
||||
computed_duration = nil
|
||||
callback = -> (_, start, finish, _, _) { computed_duration = finish - start }
|
||||
|
||||
ActiveSupport::Notifications.subscribed(callback, "publish_event.test") do
|
||||
ActiveSupport::Notifications.publish_event(event)
|
||||
end
|
||||
|
||||
# Event#duration is in milliseconds, start and finish in seconds
|
||||
assert_in_delta event.duration / 1_000.0, computed_duration, 0.05
|
||||
|
||||
ActiveSupport::Notifications.subscribed(callback, "publish_event.test", monotonic: true) do
|
||||
ActiveSupport::Notifications.publish_event(event)
|
||||
end
|
||||
|
||||
assert_in_delta event.duration / 1_000.0, computed_duration, 0.05
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user