-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eventsimple outbox processor is instantiated once
- Loading branch information
Showing
17 changed files
with
188 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module Eventsimple | ||
VERSION = '1.3.3' | ||
VERSION = '1.4.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require 'eventsimple/outbox/consumer' | ||
|
||
module UserComponent | ||
class Consumer | ||
extend Eventsimple::Outbox::Consumer | ||
|
||
consumes_event UserEvent | ||
processor EventProcessor | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module UserComponent | ||
class EventProcessor | ||
def call(event) | ||
# no-op | ||
end | ||
end | ||
end |
13 changes: 13 additions & 0 deletions
13
spec/dummy/db/migrate/20240419175459_create_eventsimple_outbox_cursor.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateEventsimpleOutboxCursor < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :eventsimple_outbox_cursors do |t| | ||
t.string :event_klass, null: false | ||
t.integer :group_number, null: false | ||
t.bigint :cursor, null: false | ||
|
||
t.index [:event_klass, :group_number], unique: true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
spec/dummy/spec/components/user_component/consumer_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
RSpec.describe UserComponent::Consumer do | ||
subject(:run_consumer) { described_class.run_consumer(group_number: 0) } | ||
|
||
before do | ||
allow(described_class).to receive(:sleep) do | ||
described_class.stop_consumer = true | ||
end | ||
end | ||
|
||
after do | ||
described_class.stop_consumer = false | ||
end | ||
|
||
it 'consumes an event' do | ||
expect(described_class._event_klass).to eq(UserEvent) | ||
end | ||
|
||
it 'has a processor' do | ||
expect(described_class._processor_klass).to eq(UserComponent::EventProcessor) | ||
end | ||
|
||
describe '.run_consumer' do | ||
it 'records the last processed event position' do | ||
event = create(:user_event) | ||
|
||
cursor = Eventsimple::Outbox::Cursor.fetch(UserEvent, group_number: 0) | ||
expect(cursor).to be(0) | ||
|
||
expect(described_class._processor).to receive(:call).once | ||
|
||
run_consumer | ||
|
||
cursor = Eventsimple::Outbox::Cursor.fetch(UserEvent, group_number: 0) | ||
expect(cursor).to eq(event.id) | ||
end | ||
|
||
context 'when consumer is stopped inside batch' do | ||
let!(:events) { create_list(:user_event, 5) } | ||
|
||
it 'sets the cursor to the last processed event position' do | ||
allow(described_class._processor).to receive(:call) do |e| | ||
expect(e.id).to be_in(events[0..1].map(&:id)) | ||
|
||
# stop consumer after the second event in the batch is processed | ||
if e.id == events[1].id | ||
described_class.stop_consumer = true | ||
end | ||
end | ||
|
||
run_consumer | ||
|
||
expect(described_class._processor).to have_received(:call).exactly(2).times | ||
expect(Eventsimple::Outbox::Cursor.fetch(UserEvent)).to eq(events[1].id) | ||
end | ||
end | ||
|
||
context 'with an existing cursor' do | ||
let!(:events) { create_list(:user_event, 5) } | ||
|
||
before do | ||
Eventsimple::Outbox::Cursor.set(UserEvent, events[2].id) | ||
allow(described_class._processor).to receive(:call) | ||
end | ||
|
||
it 'starts after the last processed event position' do | ||
run_consumer | ||
|
||
expect(described_class._processor).to have_received(:call).twice | ||
expect(Eventsimple::Outbox::Cursor.fetch(UserEvent)).to eq(events[4].id) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :user_event do | ||
user | ||
type { 'UserComponent::Events::Created' } | ||
data { | ||
{ | ||
canonical_id: SecureRandom.uuid, | ||
username: 'test-user', | ||
email: '[email protected]', | ||
} | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.