Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Spree::Event's test interface to run only selected listeners #151

Closed
wants to merge 1 commit into from

Conversation

waiting-for-dev
Copy link

Description

Given the global nature of our event bus, we need a system to scope the
execution of a block to a selected list of subscribers. That's useful
for testing purposes, as we need to be sure that others subscribers are
not interfering with our expectations.

This commit introduces a Spree::Event.performing_only(listeners)
method. It takes a block during the execution of which only the provided
listeners are subscribed:

listener1 = Spree::Event.subscribe('foo') { do_something }
listener2 = Spree::Event.subscribe('foo') { do_something_else }

Spree::Event.performing_only(listener1) do
  Spree::Event.fire('foo') # only listener1 will run
end

Spree::Event.fire('foo') # both listener1 & listener2 will run

This behavior is only available for the new
Spree::Event::Adapters::Default adapter.

We only need that for testing purposes, so the method is made available
after calling Spree::Event.enable_test_interface. It prevents the main
Spree::Event API from being bloated and sends a more explicit message
to users.

We also add a Spree::Subscriber#listeners method, which returns the
set of generated listeners for a given subscriber module. It's called
automatically by Spree::Event.performing_only so that users can
directly specify that they only want the listeners for a given
subscriber module to be run. Spree::Subscriber#listeners accepts an
array of event names as arguments in case more fine-grained control is
required.

module EmailSubscriber
  include Spree::Event::Subscriber

  event_action :foo
  event_action :bar

  def foo(_event)
    do_something
  end

  def bar(_event)
    do_something_else
  end
end

Spree::Event.performing_only(EmailSubscriber) do
  Spree::Event.fire('foo') # both foo & bar methods will run
end

Spree::Event.performing_only(EmailSubscriber.listeners('foo')) do
  Spree::Event.fire('foo') # only foo method will run
end

Checklist:

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • I have updated Guides and README accordingly to this change (if needed)
  • I have added tests to cover this change (if needed)
  • I have attached screenshots to this PR for visual changes (if needed)

@waiting-for-dev waiting-for-dev force-pushed the waiting-for-dev/events_testability branch 5 times, most recently from cb6db00 to 270af04 Compare July 28, 2021 09:20
@waiting-for-dev waiting-for-dev force-pushed the waiting-for-dev/events_testability branch 3 times, most recently from 63290f2 to e12da7e Compare August 3, 2021 15:24
Copy link
Member

@aldesantis aldesantis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@waiting-for-dev looks good! Couple of comments and then it's GTM. ❤️ Approving because it's pretty small stuff.

core/lib/spree/event/test_interface.rb Show resolved Hide resolved
core/lib/spree/event/test_interface.rb Outdated Show resolved Hide resolved
@waiting-for-dev waiting-for-dev force-pushed the waiting-for-dev/events_testability branch from e12da7e to e98d018 Compare September 9, 2021 08:16
@waiting-for-dev
Copy link
Author

Thanks for your review @aldesantis, I forced a push with the updates to rebase the dependant branches more easily.

Given the global nature of our event bus, we need a system to scope the
execution of a block to a selected list of subscribers. That's useful
for testing purposes, as we need to be sure that others subscribers are
not interfering with our expectations.

This commit introduces a `Spree::Event.performing_only(listeners)`
method. It takes a block during the execution of which only the provided
listeners are subscribed:

```ruby
listener1 = Spree::Event.subscribe('foo') { do_something }
listener2 = Spree::Event.subscribe('foo') { do_something_else }

Spree::Event.performing_only(listener1) do
  Spree::Event.fire('foo') # only listener1 will run
end

Spree::Event.fire('foo') # both listener1 & listener2 will run
```

This behavior is only available for the new
`Spree::Event::Adapters::Default` adapter.

We only need that for testing purposes, so the method is made available
after calling `Spree::Event.enable_test_interface`. It prevents the main
`Spree::Event` API from being bloated and sends a more explicit message
to users.

We also add a `Spree::Subscriber#listeners` method, which returns the
set of generated listeners for a given subscriber module. It's called
automatically by `Spree::Event.performing_only` so that users can
directly specify that they only want the listeners for a given
subscriber module to be run. `Spree::Subscriber#listeners` accepts an
array of event names as arguments in case more fine-grained control is
required.

```ruby
module EmailSubscriber
  include Spree::Event::Subscriber

  event_action :foo
  event_action :bar

  def foo(_event)
    do_something
  end

  def bar(_event)
    do_something_else
  end
end

Spree::Event.performing_only(EmailSubscriber) do
  Spree::Event.fire('foo') # both foo & bar methods will run
end

Spree::Event.performing_only(EmailSubscriber.listeners('foo')) do
  Spree::Event.fire('foo') # only foo method will run
end
```

A specialized `Spree::Event.performing_nothing` method calls
`Spree::Event.performing_only` with no listeners at all.
@waiting-for-dev waiting-for-dev force-pushed the waiting-for-dev/events_testability branch from e98d018 to bde5b62 Compare November 16, 2021 05:23
@waiting-for-dev waiting-for-dev changed the base branch from waiting-for-dev/events to master November 16, 2021 05:23
@waiting-for-dev
Copy link
Author

Moved to solidusio#4204

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants