spec/event_spec.rb in reactor-0.8.3 vs spec/event_spec.rb in reactor-0.9.0
- old
+ new
@@ -12,10 +12,11 @@
end
describe Reactor::Event do
+ let(:model) { ArbitraryModel.create! }
let(:event_name) { :user_did_this }
describe 'publish' do
it 'fires the first perform and sets message event_id' do
expect(Reactor::Event).to receive(:perform_async).with(event_name, 'actor_id' => '1', 'event' => :user_did_this)
@@ -25,27 +26,27 @@
describe 'perform' do
before { Reactor::Subscriber.create(event_name: :user_did_this) }
after { Reactor::Subscriber.destroy_all }
it 'fires all subscribers' do
- expect_any_instance_of(Reactor::Subscriber).to receive(:fire).with(hash_including(actor_id: '1'))
- Reactor::Event.perform(event_name, actor_id: '1')
+ expect_any_instance_of(Reactor::Subscriber).to receive(:fire).with(hash_including(actor_id: model.id.to_s))
+ Reactor::Event.perform(event_name, actor_id: model.id.to_s, actor_type: model.class.to_s)
end
it 'sets a fired_at key in event data' do
expect_any_instance_of(Reactor::Subscriber).to receive(:fire).with(hash_including(fired_at: anything))
- Reactor::Event.perform(event_name, actor_id: '1')
+ Reactor::Event.perform(event_name, actor_id: model.id.to_s, actor_type: model.class.to_s)
end
it 'works with the legacy .process method, too' do
- expect_any_instance_of(Reactor::Subscriber).to receive(:fire).with(hash_including(actor_id: '1'))
- Reactor::Event.perform(event_name, actor_id: '1')
+ expect_any_instance_of(Reactor::Subscriber).to receive(:fire).with(hash_including(actor_id: model.id.to_s))
+ Reactor::Event.perform(event_name, actor_id: model.id.to_s, actor_type: model.class.to_s)
end
describe 'when subscriber throws exception', :sidekiq do
let(:mock) { double(:thing, some_method: 3) }
- let(:barfing_event) { Reactor::Event.perform('barfed', somethin: 'up') }
+ let(:barfing_event) { Reactor::Event.perform('barfed', somethin: 'up', actor_id: model.id.to_s, actor_type: model.class.to_s) }
before do
Reactor::SUBSCRIBERS['barfed'] ||= []
Reactor::SUBSCRIBERS['barfed'] << Reactor::Subscribable::StaticSubscriberFactory.create('barfed') do |event|
raise 'UNEXPECTED!'
@@ -63,10 +64,14 @@
describe 'reschedule', :sidekiq do
let(:scheduled) { Sidekiq::ScheduledSet.new }
let(:time) { 1.hour.from_now }
+ before do
+ Sidekiq::Worker.clear_all
+ end
+
it 'can schedule and reschedule an event in the future' do
expect {
jid = Reactor::Event.publish :turtle_time, at: time
expect(scheduled.find_job(jid).score).to eq(time.to_f)
}.to change { scheduled.size }.by(1)
@@ -74,10 +79,17 @@
expect {
jid = Reactor::Event.reschedule :turtle_time, at: (time + 2.hours), was: time
expect(scheduled.find_job(jid).score).to eq((time + 2.hours).to_f)
}.to_not change { scheduled.size }
end
+
+ it 'will schedule an event in the future even if that event was not previously scheduled in the past' do
+ expect {
+ jid = Reactor::Event.reschedule :no_old_turtule_time, at: (time + 2.hours), was: time
+ expect(scheduled.find_job(jid).score).to eq((time + 2.hours).to_f)
+ }.to change{ scheduled.size }.by(1)
+ end
end
describe 'event content' do
let(:cat) { MyModule::Cat.create }
let(:arbitrary_model) { ArbitraryModel.create }
@@ -87,28 +99,15 @@
describe 'data key fallthrough' do
subject { event }
describe 'getters' do
context 'basic key value' do
- describe '#random' do
- subject { super().random }
- it { is_expected.to eq('data') }
- end
+ its(:random) { is_expected.to eq('data') }
end
context 'foreign key and foreign type' do
- describe '#pet' do
- subject { super().pet }
- it { is_expected.to be_a MyModule::Cat }
- end
-
- describe '#pet' do
- subject { super().pet }
- describe '#id' do
- subject { super().id }
- it { is_expected.to eq(MyModule::Cat.last.id) }
- end
- end
+ its(:pet) { is_expected.to be_a MyModule::Cat }
+ its('pet.id') { is_expected.to eq(MyModule::Cat.last.id) }
end
end
describe 'setters' do
it 'sets simple keys' do