spec/models/concerns/subscribable_spec.rb in reactor-0.19.0 vs spec/models/concerns/subscribable_spec.rb in reactor-1.0.0

- old
+ new

@@ -13,14 +13,10 @@ on_event :pooped, :pick_up_poop, delay: 5.minutes on_event '*' do |event| event.actor.more_puppies! if event.name == 'another_event' end - on_event :cat_delivered, async: false do |event| - puppies! - end - on_event :a_high_frequency_event, deprecated: true do |event| raise 'hell' end on_event :event_with_ui_bound, @@ -47,11 +43,11 @@ class KittenMailer < ActionMailer::Base include Reactor::Subscribable on_event :auction, handler_name: 'auction' do |event| - raise "Event auction" + @@fired = true end on_event :kitten_streaming do |event| kitten_livestream(event) end @@ -65,24 +61,18 @@ format.text { 'Your favorite kittens are now live!' } end end end -Reactor.in_test_mode do - class TestModeAuction < ApplicationRecord - on_event :test_puppy_delivered, -> (event) { "success" } - end +class TestModeAuction < ApplicationRecord + on_event :test_puppy_delivered, -> (event) { "success" } end describe Reactor::Subscribable do let(:scheduled) { Sidekiq::ScheduledSet.new } describe 'on_event' do - before do - Reactor.enable_test_mode_subscriber(Auction) - end - it 'binds block of code statically to event being fired' do expect_any_instance_of(Auction).to receive(:update_column).with(:status, 'first_bid_made') Reactor::Event.publish(:bid_made, target: Auction.create!(start_at: 10.minutes.from_now)) end @@ -90,23 +80,29 @@ it 'adds a static subscriber to the global lookup constant' do expect(Reactor::SUBSCRIBERS['puppy_delivered'][0]).to eq(Reactor::StaticSubscribers::Auction::PuppyDeliveredHandler) expect(Reactor::SUBSCRIBERS['puppy_delivered'][1]).to eq(Reactor::StaticSubscribers::Auction::DoNothingHandler) end - it 'adds a static subscriber for namespaced classes' do - expect(Reactor::SUBSCRIBERS['rain'][0]).to eq(Reactor::StaticSubscribers::MyClass::RainHandler) + it 'adds a static subscriber for namespaced classes at the same module depth' do + expect(Reactor::SUBSCRIBERS['rain'][0]).to eq(Reactor::StaticSubscribers::MyNamespace::MyClass::RainHandler) end end describe 'binding symbol of class method' do let(:pooped_handler) { Reactor::StaticSubscribers::Auction::PoopedHandler } it 'fires on event' do + expect(Reactor::StaticSubscribers::Auction::WildcardHandler). + to receive(:perform_async).and_call_original expect(Auction).to receive(:ring_bell) Reactor::Event.publish(:puppy_delivered) end + it 'fires perform_async when true / default' do + Reactor::Event.publish(:puppy_delivered) + end + it 'can be delayed' do expect(Auction).to receive(:pick_up_poop) expect(pooped_handler).to receive(:perform_in).with(5.minutes, anything).and_call_original Reactor::Event.perform('pooped', {}) end @@ -130,23 +126,10 @@ # have to ensure multiple subscribers are loaded expect(KittenMailer).to be_a(Class) expect { Reactor::Event.publish :auction }.not_to raise_error end - describe 'async flag' do - it 'doesnt fire perform_async when false' do - expect(Auction).to receive(:puppies!) - expect(Reactor::StaticSubscribers::Auction::CatDeliveredHandler).not_to receive(:perform_async) - Reactor::Event.publish(:cat_delivered) - end - - it 'fires perform_async when true / default' do - expect(Reactor::StaticSubscribers::Auction::WildcardHandler).to receive(:perform_async) - Reactor::Event.publish(:puppy_delivered) - end - end - describe 'deprecate flag for high-frequency events in production deployments' do it 'doesnt enqueue subscriber worker when true' do # so subscriber can be safely deleted in next deploy expect { Reactor::Event.publish(:a_high_frequency_event) @@ -180,30 +163,19 @@ Reactor::Event.publish(:puppy_delivered) end end describe '#perform' do - around(:each) do |example| - Reactor.in_test_mode { example.run } - end - - it 'returns :__perform_aborted__ when Reactor is in test mode' do - expect(Reactor::StaticSubscribers::TestModeAuction::TestPuppyDeliveredHandler.new.perform({})).to eq(:__perform_aborted__) - Reactor::Event.publish(:test_puppy_delivered) - end - it 'performs normally when specifically enabled' do - Reactor.with_subscriber_enabled(TestModeAuction) do + allow_reactor_subscriber(TestModeAuction) do expect(Reactor::StaticSubscribers::TestModeAuction::TestPuppyDeliveredHandler.new.perform({})).not_to eq(:__perform_aborted__) Reactor::Event.publish(:test_puppy_delivered) end end end end describe 'mailers', type: :mailer do - before { Reactor.enable_test_mode_subscriber KittenMailer } - after { Reactor.disable_test_mode_subscriber KittenMailer } def deliveries ActionMailer::Base.deliveries end