Skip to content

Commit

Permalink
Base Aruba event bus on the core Cucumber event bus
Browse files Browse the repository at this point in the history
  • Loading branch information
mvz committed May 31, 2024
1 parent 45b5f03 commit 4cbb4dc
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 67 deletions.
3 changes: 0 additions & 3 deletions lib/aruba/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,4 @@ class CommandAlreadyStartedError < Error; end

# Raised if an event name cannot be resolved
class EventNameResolveError < StandardError; end

# Raised if given object is not an event
class NoEventError < StandardError; end
end
51 changes: 6 additions & 45 deletions lib/aruba/event_bus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,23 @@

require "aruba/event_bus/name_resolver"
require "aruba/errors"
require "cucumber/core/event_bus"

module Aruba
# Event bus
#
# Implements and in-process pub-sub events broadcaster allowing multiple observers
# to subscribe to different events that fire as your tests are executed.
#
class EventBus
# Create EventBus
#
# @param [#transform] resolver
# A resolver which transforms Symbol, String, Class into an event Class.
def initialize(resolver)
@resolver = resolver
@handlers = Hash.new { |h, k| h[k] = [] }
end

# Register for an event
#
# @param [String, Symbol, Class, Array] event_ids
# If Array, register multiple events witht the same handler. If String,
# Symbol, Class register handler for given event.
#
# @param [#call] handler_object
# The handler object, needs to have method `#call`. Either
# `handler_object` or `block` can be defined. The handler object gets the
# event passed to `#call`.
#
# @yield
# Handler block which gets the event passed as parameter.
def register(event_ids, handler_object = nil, &handler_proc)
handler = handler_proc || handler_object

if handler.nil? || !handler.respond_to?(:call)
raise ArgumentError, "Please pass either an object#call or a handler block"
class EventBus < Cucumber::Core::EventBus
def register(ids, handler_object = nil, &handler_proc)
Array(ids).each do |event_id|
on(event_id, handler_object, &handler_proc)
end

Array(event_ids).flatten.each do |id|
@handlers[
@resolver.transform(id).to_s
] << handler
end

nil
end

# Broadcast an event
#
# @param [Object] event
# An object of registered event class. This object is passed to the event
# handler.
#
def notify(event)
raise NoEventError, "Please pass an event object, not a class" if event.is_a?(Class)

@handlers[event.class.to_s].each { |handler| handler.call(event) }
broadcast(event)
end
end
end
3 changes: 2 additions & 1 deletion lib/aruba/event_bus/name_resolver.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# frozen_string_literal: true

require "aruba/errors"
require "cucumber/core/event_bus"

# Event notification library
module Aruba
# EventBus
class EventBus
class EventBus < Cucumber::Core::EventBus
# Resolve name to Event name
class NameResolver
# @private
Expand Down
20 changes: 13 additions & 7 deletions lib/aruba/events.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "cucumber/core/event"

# Aruba
module Aruba
# Events
Expand All @@ -10,13 +12,7 @@ module Events
# inherited by normal events
#
# @private
class BasicEvent
attr_reader :entity

def initialize(entity)
@entity = entity
end
end
class BasicEvent < Cucumber::Core::Event.new(:entity); end

# Command was stopped
class CommandStopped < BasicEvent; end
Expand All @@ -38,5 +34,15 @@ class ChangedWorkingDirectory < BasicEvent; end

# The configuration was changed
class ChangedConfiguration < BasicEvent; end

def self.registry
[CommandStarted,
CommandStopped,
AddedEnvironmentVariable,
ChangedEnvironmentVariable,
DeletedEnvironmentVariable,
ChangedWorkingDirectory,
ChangedConfiguration].to_h { |klass| [klass.event_id, klass] }
end
end
end
2 changes: 1 addition & 1 deletion lib/aruba/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Runtime
attr_accessor :config, :environment, :logger, :command_monitor, :announcer, :event_bus

def initialize(opts = {})
@event_bus = EventBus.new(EventBus::NameResolver.new(Aruba::Events))
@event_bus = EventBus.new(Aruba::Events.registry)
@announcer = opts.fetch(:announcer, Aruba.platform.announcer.new)
@config = opts.fetch(:config,
ConfigWrapper.new(Aruba.config.make_copy, @event_bus))
Expand Down
15 changes: 5 additions & 10 deletions spec/aruba/event_bus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
describe Aruba::EventBus do
let(:bus) { described_class.new(name_resolver) }

let(:name_resolver) { Aruba::EventBus::NameResolver.new("Events") }
let(:name_resolver) do
{ test_event: Events::TestEvent,
another_test_event: Events::AnotherTestEvent }
end

let(:event_klass) { Events::TestEvent }
let(:event_instance) { event_klass.new }
Expand All @@ -16,7 +19,6 @@
before do
stub_const("Events::TestEvent", Class.new)
stub_const("Events::AnotherTestEvent", Class.new)
stub_const("Events::MalformedTestEvent", Module.new)
stub_const("MyHandler", Class.new { def call(*); end })
stub_const("MyMalformedHandler", Class.new)
end
Expand Down Expand Up @@ -44,7 +46,7 @@

context "when event is not an event instance" do
it "raises an error" do
expect { bus.notify event_klass }.to raise_error Aruba::NoEventError
expect { bus.notify event_klass }.to raise_error ArgumentError
end
end
end
Expand Down Expand Up @@ -107,13 +109,6 @@
it { expect { bus.notify event_instance }.not_to raise_error }
end

context "when malformed custom handler" do
it "raises an ArgumentError" do
expect { bus.register(:test_event, MyMalformedHandler.new) }
.to raise_error ArgumentError
end
end

context "when no handler is given" do
it "raises an ArgumentError" do
expect { bus.register(event_klass) }.to raise_error ArgumentError
Expand Down

0 comments on commit 4cbb4dc

Please sign in to comment.