2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
require "abstract_unit"
|
2012-03-23 14:47:27 -04:00
|
|
|
|
|
|
|
module ActiveSupport
|
|
|
|
module Notifications
|
|
|
|
class EventedTest < ActiveSupport::TestCase
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
2012-03-23 14:47:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|