Skip to content

Commit

Permalink
add outboxer integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bedrock-adam committed Dec 29, 2024
1 parent 575f943 commit eb6bf24
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 31 deletions.
18 changes: 13 additions & 5 deletions app/jobs/outboxer_integration/test/started_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ class StartedJob
include Sidekiq::Job

def perform(args)
started_event = Test::StartedEvent.find(args['event_id'])
ActiveRecord::Base.transaction do
started_event = Test::StartedEvent.find(args['event_id'])

Test::CompletedEvent.create!(
body: {
'test' => {
'id' => started_event.body['test']['id'] } })
test = Test.find(started_event.eventable_id)
test.touch

Test::CompletedEvent.create!(
user_id: started_event.user_id,
tenant_id: started_event.tenant_id,
eventable: test,
body: {
'test' => {
'id' => started_event.body['test']['id'] } })
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class Event < ActiveRecord::Base
self.table_name = 'events'

# associations

belongs_to :eventable, polymorphic: true

# validations

validates :type, presence: true, length: { maximum: 255 }
Expand Down
7 changes: 7 additions & 0 deletions app/models/outboxer_integration/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module OutboxerIntegration
class Test < ActiveRecord::Base
self.table_name = 'outboxer_integration_tests'

has_many :events, as: :eventable
end
end
2 changes: 1 addition & 1 deletion app/models/outboxer_integration/test/completed_event.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module OutboxerIntegration
class Test
class Test < ActiveRecord::Base
class CompletedEvent < ::Event
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/outboxer_integration/test/started_event.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module OutboxerIntegration
class Test
class Test < ActiveRecord::Base
class StartedEvent < ::Event
end
end
Expand Down
4 changes: 2 additions & 2 deletions bin/outboxer_publisher
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ Outboxer::Publisher.publish(
OutboxerIntegration::Message::PublishJob.perform_async({
'messageable_id' => message[:messageable_id], 'messageable_type' => message[:messageable_type] })

logger.info "Outboxer published message #{message[:id]} to sidekiq for "\
"#{message[:messageable_type]}::#{message[:messageable_id]}"
logger.info "Outboxer published message id=#{message[:id]} "\
"messageable_type=#{message[:messageable_type]} messageable_id=#{message[:messageable_id]}"
end
10 changes: 5 additions & 5 deletions db/migrate/create_events.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class CreateEvents < ActiveRecord::Migration[7.0]
def up
create_table :events do |t|
# t.bigint :user_id
# t.bigint :tenant_id
t.bigint :user_id
t.bigint :tenant_id

# t.string :eventable_type, limit: 255
# t.bigint :eventable_id
# t.index [:eventable_type, :eventable_id]
t.string :eventable_type, limit: 255
t.bigint :eventable_id
t.index [:eventable_type, :eventable_id]

t.string :type, null: false, limit: 255
t.send(json_column_type, :body)
Expand Down
15 changes: 15 additions & 0 deletions db/migrate/create_outboxer_integration_tests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CreateOutboxerIntegrationTests < ActiveRecord::Migration[7.0]
def up
create_table :outboxer_integration_tests do |t|
t.bigint :tenant_id, null: false

t.integer :lock_version, default: 0, null: false

t.timestamps
end
end

def down
drop_table :outboxer_integration_tests
end
end
14 changes: 6 additions & 8 deletions db/migrate/create_outboxer_publishers.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
class CreateOutboxerPublishers < ActiveRecord::Migration[6.1]
def up
ActiveRecord::Base.transaction do
create_table :outboxer_publishers do |t|
t.string :name, limit: 263, null: false # 255 (hostname) + 1 (colon) + 7 (pid)
t.string :status, limit: 255, null: false
t.json :settings, null: false
t.json :metrics, null: false
create_table :outboxer_publishers do |t|
t.string :name, limit: 263, null: false # 255 (hostname) + 1 (colon) + 7 (pid)
t.string :status, limit: 255, null: false
t.json :settings, null: false
t.json :metrics, null: false

t.timestamps
end
t.timestamps
end
end

Expand Down
4 changes: 4 additions & 0 deletions generators/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def copy_models
"app/models/event.rb",
"app/models/event.rb")

copy_file(
"app/models/outboxer_integration/test.rb",
"app/models/outboxer_integration/test.rb")

copy_file(
"app/models/outboxer_integration/test/started_event.rb",
"app/models/outboxer_integration/test/started_event.rb")
Expand Down
31 changes: 22 additions & 9 deletions spec/bin/outboxer_publisher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,31 @@
require 'sidekiq/testing'

require File.join(Dir.pwd, 'app/models/event')
require File.join(Dir.pwd, 'app/models/outboxer_integration/test.rb')
require File.join(Dir.pwd, 'app/models/outboxer_integration/test/started_event')
require File.join(Dir.pwd, 'app/models/outboxer_integration/test/completed_event')

RSpec.describe 'bin/outboxer_publisher' do
let(:test_id) { rand(1_000) + 1 }

it 'performs event job handler async' do
Sidekiq::Testing.disable!

OutboxerIntegration::Test::StartedEvent.create!(
body: {
'test' => {
'id' => test_id } })
user_id = rand(1_000) + 1
tenant_id = rand(1_000) + 1

test = nil

ActiveRecord::Base.transaction do
test = OutboxerIntegration::Test.create!(
tenant_id: tenant_id)

OutboxerIntegration::Test::StartedEvent.create!(
user_id: user_id,
tenant_id: tenant_id,
eventable: test,
body: {
'test' => {
'id' => test.id } })
end

outboxer_publisher_env = {
"OUTBOXER_ENV" => "test",
Expand All @@ -35,16 +47,17 @@
completed_event = nil

max_attempts.times do |attempt|
test = OutboxerIntegration::Test.find(test.id)
completed_event = OutboxerIntegration::Test::CompletedEvent.last
break if completed_event
break if completed_event && (test.events.last == completed_event)

sleep 1

puts "OutboxerIntegration::Test::CompletedEvent not found. "\
Sidekiq.logger.warn "OutboxerIntegration::Test::CompletedEvent not found. "\
"Retrying (attempt #{attempt + 1}/#{max_attempts})..."
end

expect(completed_event.body['test']['id']).to eql(test_id)
expect(completed_event.body['test']['id']).to eql(test.id)
ensure
if sidekiq_pid
Process.kill("TERM", sidekiq_pid)
Expand Down
3 changes: 3 additions & 0 deletions tasks/database.rake
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ namespace :outboxer do
db_config = Outboxer::Database.config(env: env, pool: 1)
ActiveRecord::Base.establish_connection(db_config)

require_relative "../db/migrate/create_outboxer_integration_tests"
CreateOutboxerIntegrationTests.new.up

require_relative "../db/migrate/create_events"
CreateEvents.new.up

Expand Down

0 comments on commit eb6bf24

Please sign in to comment.