-
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.
Integrate better with sidekiq (#215)
- Loading branch information
1 parent
a3e426c
commit 575f943
Showing
12 changed files
with
207 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module OutboxerIntegration | ||
class Test | ||
class StartedJob | ||
include Sidekiq::Job | ||
|
||
def perform(args) | ||
started_event = Test::StartedEvent.find(args['event_id']) | ||
|
||
Test::CompletedEvent.create!( | ||
body: { | ||
'test' => { | ||
'id' => started_event.body['test']['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,6 @@ | ||
module OutboxerIntegration | ||
class Test | ||
class CompletedEvent < ::Event | ||
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,6 @@ | ||
module OutboxerIntegration | ||
class Test | ||
class StartedEvent < ::Event | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require 'spec_helper' | ||
|
||
require 'sidekiq' | ||
require 'sidekiq/testing' | ||
|
||
require File.join(Dir.pwd, 'app/models/event') | ||
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 } }) | ||
|
||
outboxer_publisher_env = { | ||
"OUTBOXER_ENV" => "test", | ||
"OUTBOXER_REDIS_URL" => "redis://localhost:6379/0" } | ||
outboxer_publisher_cmd = File.join(Dir.pwd, 'bin', 'outboxer_publisher') | ||
outboxer_publisher_pid = spawn(outboxer_publisher_env, outboxer_publisher_cmd) | ||
|
||
sidekiq_env = { | ||
"RAILS_ENV" => "test", | ||
"REDIS_URL" => "redis://localhost:6379/0" } | ||
sidekiq_cmd = "bundle exec sidekiq -c 10 -q default -r ./config/sidekiq.rb" | ||
sidekiq_pid = spawn(sidekiq_env, sidekiq_cmd) | ||
|
||
max_attempts = 10 | ||
|
||
completed_event = nil | ||
|
||
max_attempts.times do |attempt| | ||
completed_event = OutboxerIntegration::Test::CompletedEvent.last | ||
break if completed_event | ||
|
||
sleep 1 | ||
|
||
puts "OutboxerIntegration::Test::CompletedEvent not found. "\ | ||
"Retrying (attempt #{attempt + 1}/#{max_attempts})..." | ||
end | ||
|
||
expect(completed_event.body['test']['id']).to eql(test_id) | ||
ensure | ||
if sidekiq_pid | ||
Process.kill("TERM", sidekiq_pid) | ||
Process.wait(sidekiq_pid) | ||
end | ||
|
||
if outboxer_publisher_pid | ||
Process.kill('TERM', outboxer_publisher_pid) | ||
Process.wait(outboxer_publisher_pid) | ||
end | ||
|
||
Sidekiq::Testing.fake! | ||
end | ||
end |
66 changes: 66 additions & 0 deletions
66
spec/jobs/outboxer_integration/message/publish_job_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,66 @@ | ||
require 'spec_helper' | ||
require 'sidekiq' | ||
|
||
require_relative '../../../../app/jobs/outboxer_integration/message/publish_job' | ||
require_relative '../../../../app/jobs/outboxer_integration/test/started_job' | ||
|
||
require 'sidekiq/testing' | ||
|
||
module OutboxerIntegration | ||
module Message | ||
RSpec.describe PublishJob, type: :job do | ||
describe '#perform' do | ||
context 'when OutboxerIntegration::Test::StartedEvent' do | ||
let(:args) do | ||
{ | ||
'messageable_type' => 'OutboxerIntegration::Test::StartedEvent', | ||
'messageable_id' => '123' | ||
} | ||
end | ||
|
||
it 'performs OutboxerIntegration::Test::StartedJob async' do | ||
PublishJob.new.perform(args) | ||
|
||
expect(OutboxerIntegration::Test::StartedJob.jobs).to match([ | ||
hash_including('args' => [include('event_id' => '123')]) | ||
]) | ||
end | ||
end | ||
|
||
context 'with invalid messageable_type' do | ||
let(:args) do | ||
{ | ||
'messageable_type' => 'Wrong::Format::Test', | ||
'messageable_id' => '123' | ||
} | ||
end | ||
|
||
it 'does not raise an error' do | ||
expect { | ||
PublishJob.new.perform(args) | ||
}.not_to raise_error | ||
end | ||
end | ||
|
||
context 'when job class does not exist' do | ||
let(:args) do | ||
{ | ||
'messageable_type' => 'OutboxerIntegration::Invoice::NonexistentEvent', | ||
'messageable_id' => '123' | ||
} | ||
end | ||
|
||
it 'does not raise an error' do | ||
allow_any_instance_of(String) | ||
.to receive(:constantize) | ||
.and_raise(NameError.new("uninitialized constant")) | ||
|
||
expect { | ||
PublishJob.new.perform(args) | ||
}.not_to raise_error | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |