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"
|
2012-03-23 14:47:27 -04:00
|
|
|
|
|
|
|
module ActiveSupport
|
|
|
|
module Notifications
|
|
|
|
class EventedTest < ActiveSupport::TestCase
|
2021-10-04 19:07:50 -04:00
|
|
|
# we expect all exception types to be handled, so test with the most basic type
|
|
|
|
class BadListenerException < Exception; end
|
2021-09-21 21:35:09 -04:00
|
|
|
|
2012-03-23 14:47:27 -04:00
|
|
|
class Listener
|
|
|
|
attr_reader :events
|
|
|
|
|
|
|
|
def initialize
|
2016-10-28 23:05:58 -04:00
|
|
|
@events = []
|
2012-03-23 14:47:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def start(name, id, payload)
|
|
|
|
@events << [:start, name, id, payload]
|
|
|
|
end
|
|
|
|
|
|
|
|
def finish(name, id, payload)
|
|
|
|
@events << [:finish, name, id, payload]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-11 20:39:20 -04:00
|
|
|
class ListenerWithTimedSupport < Listener
|
|
|
|
def call(name, start, finish, id, payload)
|
|
|
|
@events << [:call, name, start, finish, id, payload]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-21 21:35:09 -04:00
|
|
|
class BadStartListener < Listener
|
|
|
|
def start(name, id, payload)
|
|
|
|
raise BadListenerException
|
|
|
|
end
|
|
|
|
|
|
|
|
def finish(name, id, payload)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class BadFinishListener < Listener
|
|
|
|
def start(name, id, payload)
|
|
|
|
end
|
|
|
|
|
|
|
|
def finish(name, id, payload)
|
|
|
|
raise BadListenerException
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-23 14:47:27 -04:00
|
|
|
def test_evented_listener
|
|
|
|
notifier = Fanout.new
|
|
|
|
listener = Listener.new
|
2016-08-06 12:03:25 -04:00
|
|
|
notifier.subscribe "hi", listener
|
|
|
|
notifier.start "hi", 1, {}
|
|
|
|
notifier.start "hi", 2, {}
|
|
|
|
notifier.finish "hi", 2, {}
|
|
|
|
notifier.finish "hi", 1, {}
|
2012-03-23 14:47:27 -04:00
|
|
|
|
|
|
|
assert_equal 4, listener.events.length
|
|
|
|
assert_equal [
|
2016-08-06 12:03:25 -04:00
|
|
|
[:start, "hi", 1, {}],
|
|
|
|
[:start, "hi", 2, {}],
|
|
|
|
[:finish, "hi", 2, {}],
|
|
|
|
[:finish, "hi", 1, {}],
|
2012-03-23 14:47:27 -04:00
|
|
|
], listener.events
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_evented_listener_no_events
|
|
|
|
notifier = Fanout.new
|
|
|
|
listener = Listener.new
|
2016-08-06 12:03:25 -04:00
|
|
|
notifier.subscribe "hi", listener
|
|
|
|
notifier.start "world", 1, {}
|
2012-03-23 14:47:27 -04:00
|
|
|
assert_equal 0, listener.events.length
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_listen_to_everything
|
|
|
|
notifier = Fanout.new
|
|
|
|
listener = Listener.new
|
|
|
|
notifier.subscribe nil, listener
|
2016-08-06 12:03:25 -04:00
|
|
|
notifier.start "hello", 1, {}
|
|
|
|
notifier.start "world", 1, {}
|
|
|
|
notifier.finish "world", 1, {}
|
|
|
|
notifier.finish "hello", 1, {}
|
2012-03-23 14:47:27 -04:00
|
|
|
|
|
|
|
assert_equal 4, listener.events.length
|
|
|
|
assert_equal [
|
2016-08-06 12:03:25 -04:00
|
|
|
[:start, "hello", 1, {}],
|
|
|
|
[:start, "world", 1, {}],
|
|
|
|
[:finish, "world", 1, {}],
|
|
|
|
[:finish, "hello", 1, {}],
|
2012-03-23 14:47:27 -04:00
|
|
|
], listener.events
|
|
|
|
end
|
2012-08-11 20:39:20 -04:00
|
|
|
|
2021-11-02 05:51:53 -04:00
|
|
|
def test_listen_start_multiple_exception_consistency
|
2021-09-21 21:35:09 -04:00
|
|
|
notifier = Fanout.new
|
|
|
|
listener = Listener.new
|
|
|
|
notifier.subscribe nil, BadStartListener.new
|
2021-11-02 05:51:53 -04:00
|
|
|
notifier.subscribe nil, BadStartListener.new
|
2021-09-21 21:35:09 -04:00
|
|
|
notifier.subscribe nil, listener
|
|
|
|
|
2021-11-02 05:51:53 -04:00
|
|
|
error = assert_raises InstrumentationSubscriberError do
|
2021-09-21 21:35:09 -04:00
|
|
|
notifier.start "hello", 1, {}
|
|
|
|
end
|
2021-11-02 05:51:53 -04:00
|
|
|
assert_instance_of BadListenerException, error.cause
|
|
|
|
|
|
|
|
error = assert_raises InstrumentationSubscriberError do
|
2021-09-21 21:35:09 -04:00
|
|
|
notifier.start "world", 1, {}
|
|
|
|
end
|
2021-11-02 05:51:53 -04:00
|
|
|
assert_instance_of BadListenerException, error.cause
|
|
|
|
|
2021-09-21 21:35:09 -04:00
|
|
|
notifier.finish "world", 1, {}
|
|
|
|
notifier.finish "hello", 1, {}
|
|
|
|
|
|
|
|
assert_equal 4, listener.events.length
|
|
|
|
assert_equal [
|
|
|
|
[:start, "hello", 1, {}],
|
|
|
|
[:start, "world", 1, {}],
|
|
|
|
[:finish, "world", 1, {}],
|
|
|
|
[:finish, "hello", 1, {}],
|
|
|
|
], listener.events
|
|
|
|
end
|
|
|
|
|
2021-11-02 05:51:53 -04:00
|
|
|
def test_listen_finish_multiple_exception_consistency
|
2021-09-21 21:35:09 -04:00
|
|
|
notifier = Fanout.new
|
|
|
|
listener = Listener.new
|
|
|
|
notifier.subscribe nil, BadFinishListener.new
|
2021-11-02 05:51:53 -04:00
|
|
|
notifier.subscribe nil, BadFinishListener.new
|
2021-09-21 21:35:09 -04:00
|
|
|
notifier.subscribe nil, listener
|
|
|
|
|
|
|
|
notifier.start "hello", 1, {}
|
|
|
|
notifier.start "world", 1, {}
|
2021-11-02 05:51:53 -04:00
|
|
|
error = assert_raises InstrumentationSubscriberError do
|
2021-09-21 21:35:09 -04:00
|
|
|
notifier.finish "world", 1, {}
|
|
|
|
end
|
2021-11-02 05:51:53 -04:00
|
|
|
assert_instance_of BadListenerException, error.cause
|
|
|
|
|
|
|
|
error = assert_raises InstrumentationSubscriberError do
|
2021-09-21 21:35:09 -04:00
|
|
|
notifier.finish "hello", 1, {}
|
|
|
|
end
|
2021-11-02 05:51:53 -04:00
|
|
|
assert_instance_of BadListenerException, error.cause
|
2021-09-21 21:35:09 -04:00
|
|
|
|
|
|
|
assert_equal 4, listener.events.length
|
|
|
|
assert_equal [
|
|
|
|
[:start, "hello", 1, {}],
|
|
|
|
[:start, "world", 1, {}],
|
|
|
|
[:finish, "world", 1, {}],
|
|
|
|
[:finish, "hello", 1, {}],
|
|
|
|
], listener.events
|
|
|
|
end
|
|
|
|
|
2012-08-11 20:39:20 -04:00
|
|
|
def test_evented_listener_priority
|
|
|
|
notifier = Fanout.new
|
|
|
|
listener = ListenerWithTimedSupport.new
|
2016-08-06 12:03:25 -04:00
|
|
|
notifier.subscribe "hi", listener
|
2012-08-11 20:39:20 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
notifier.start "hi", 1, {}
|
|
|
|
notifier.finish "hi", 1, {}
|
2012-08-11 20:39:20 -04:00
|
|
|
|
|
|
|
assert_equal [
|
2016-08-06 12:03:25 -04:00
|
|
|
[:start, "hi", 1, {}],
|
|
|
|
[:finish, "hi", 1, {}]
|
2012-08-11 20:39:20 -04:00
|
|
|
], listener.events
|
|
|
|
end
|
2019-02-07 14:58:50 -05:00
|
|
|
|
|
|
|
def test_listen_to_regexp
|
|
|
|
notifier = Fanout.new
|
|
|
|
listener = Listener.new
|
|
|
|
notifier.subscribe(/[a-z]*.world/, listener)
|
|
|
|
notifier.start("hi.world", 1, {})
|
|
|
|
notifier.finish("hi.world", 2, {})
|
|
|
|
notifier.start("hello.world", 1, {})
|
|
|
|
notifier.finish("hello.world", 2, {})
|
|
|
|
|
|
|
|
assert_equal [
|
|
|
|
[:start, "hi.world", 1, {}],
|
|
|
|
[:finish, "hi.world", 2, {}],
|
|
|
|
[:start, "hello.world", 1, {}],
|
|
|
|
[:finish, "hello.world", 2, {}]
|
|
|
|
], listener.events
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_listen_to_regexp_with_exclusions
|
|
|
|
notifier = Fanout.new
|
|
|
|
listener = Listener.new
|
|
|
|
notifier.subscribe(/[a-z]*.world/, listener)
|
|
|
|
notifier.unsubscribe("hi.world")
|
|
|
|
notifier.start("hi.world", 1, {})
|
|
|
|
notifier.finish("hi.world", 2, {})
|
|
|
|
notifier.start("hello.world", 1, {})
|
|
|
|
notifier.finish("hello.world", 2, {})
|
|
|
|
|
|
|
|
assert_equal [
|
|
|
|
[:start, "hello.world", 1, {}],
|
|
|
|
[:finish, "hello.world", 2, {}]
|
|
|
|
], listener.events
|
|
|
|
end
|
2012-03-23 14:47:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|