2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2018-09-29 20:50:43 -04:00
|
|
|
require_relative "../abstract_unit"
|
2016-08-06 12:03:25 -04:00
|
|
|
require "active_support/notifications/instrumenter"
|
2013-01-08 13:45:44 -05:00
|
|
|
|
|
|
|
module ActiveSupport
|
|
|
|
module Notifications
|
|
|
|
class InstrumenterTest < ActiveSupport::TestCase
|
|
|
|
class TestNotifier
|
|
|
|
attr_reader :starts, :finishes
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@starts = []
|
|
|
|
@finishes = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def start(*args); @starts << args; end
|
|
|
|
def finish(*args); @finishes << args; end
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :instrumenter, :notifier, :payload
|
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
@notifier = TestNotifier.new
|
|
|
|
@instrumenter = Instrumenter.new @notifier
|
2016-10-28 23:05:58 -04:00
|
|
|
@payload = { foo: Object.new }
|
2013-01-08 13:45:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_instrument
|
2016-10-28 23:05:58 -04:00
|
|
|
called = false
|
2013-01-08 13:45:44 -05:00
|
|
|
instrumenter.instrument("foo", payload) {
|
|
|
|
called = true
|
|
|
|
}
|
|
|
|
|
|
|
|
assert called
|
|
|
|
end
|
|
|
|
|
2013-03-02 18:50:41 -05:00
|
|
|
def test_instrument_yields_the_payload_for_further_modification
|
|
|
|
assert_equal 2, instrumenter.instrument("awesome") { |p| p[:result] = 1 + 1 }
|
|
|
|
assert_equal 1, notifier.finishes.size
|
|
|
|
name, _, payload = notifier.finishes.first
|
|
|
|
assert_equal "awesome", name
|
2016-08-06 13:38:33 -04:00
|
|
|
assert_equal Hash[result: 2], payload
|
2013-03-02 18:50:41 -05:00
|
|
|
end
|
|
|
|
|
2019-03-22 10:01:18 -04:00
|
|
|
def test_instrument_works_without_a_block
|
|
|
|
instrumenter.instrument("no.block", payload)
|
|
|
|
assert_equal 1, notifier.finishes.size
|
|
|
|
assert_equal "no.block", notifier.finishes.first.first
|
|
|
|
end
|
|
|
|
|
2013-01-08 13:45:44 -05:00
|
|
|
def test_start
|
|
|
|
instrumenter.start("foo", payload)
|
|
|
|
assert_equal [["foo", instrumenter.id, payload]], notifier.starts
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty notifier.finishes
|
2013-01-08 13:45:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_finish
|
|
|
|
instrumenter.finish("foo", payload)
|
|
|
|
assert_equal [["foo", instrumenter.id, payload]], notifier.finishes
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty notifier.starts
|
2013-01-08 13:45:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|