Skip to content

Commit

Permalink
Move logic from Rake task to PEP.run
Browse files Browse the repository at this point in the history
This moves as much logic as possible from the Rake task (hard to test)
to a new `PublishingEventPipeline.run` method (easier to test).
  • Loading branch information
csutter committed Sep 26, 2023
1 parent b04f61d commit 1505dfa
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 15 deletions.
11 changes: 11 additions & 0 deletions lib/publishing_event_pipeline.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "govuk_message_queue_consumer"

require "publishing_event_pipeline/configuration"
require "publishing_event_pipeline/document_lifecycle_event"

Expand All @@ -11,4 +13,13 @@ def self.configuration
def self.configure
yield(configuration)
end

def self.run
GovukMessageQueueConsumer::Consumer.new(
queue_name: PublishingEventPipeline.configuration.message_queue_name,
processor: PublishingEventPipeline::MessageProcessor.new(
repository: configuration.repository,
),
).run
end
end
2 changes: 1 addition & 1 deletion lib/publishing_event_pipeline/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module PublishingEventPipeline
class Configuration
attr_accessor :logger, :repository
attr_accessor :logger, :message_queue_name, :repository

def initialize
@logger = Logger.new($stdout)
Expand Down
6 changes: 3 additions & 3 deletions lib/publishing_event_pipeline/message_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ class MessageProcessor
attr_reader :event_class, :repository

def initialize(
event_class: DocumentLifecycleEvent,
repository: PublishingEventPipeline.configuration.repository
repository:,
event_class: DocumentLifecycleEvent
)
@event_class = event_class
@repository = repository
@event_class = event_class
end

# Implements the callback interface required by `govuk_message_queue_consumer`
Expand Down
8 changes: 2 additions & 6 deletions lib/tasks/publishing_event_pipeline.rake
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "govuk_message_queue_consumer"

require "publishing_event_pipeline"
require "search_repositories/null/null_repository"

Expand Down Expand Up @@ -32,12 +30,10 @@ namespace :publishing_event_pipeline do
# be set to the real repository. Until then, this allows us to verify that the pipeline is
# working as expected through the logs.
config.repository = SearchRepositories::Null::NullRepository.new
config.message_queue_name = ENV.fetch("PUBLISHING_EVENT_MESSAGE_QUEUE_NAME")
end

GovukMessageQueueConsumer::Consumer.new(
queue_name: ENV.fetch("PUBLISHING_EVENT_MESSAGE_QUEUE_NAME"),
processor: PublishingEventPipeline::MessageProcessor.new,
).run
PublishingEventPipeline.run
end
end
# rubocop:enable Rails/RakeEnvironment
6 changes: 1 addition & 5 deletions spec/integration/publishing_event_pipeline_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
let(:message) { GovukMessageQueueConsumer::MockMessage.new(payload) }

before do
PublishingEventPipeline.configure do |config|
config.repository = repository
end

PublishingEventPipeline::MessageProcessor.new.process(message)
PublishingEventPipeline::MessageProcessor.new(repository:).process(message)
end

describe "when a message is received that a document is published" do
Expand Down
26 changes: 26 additions & 0 deletions spec/lib/publishing_event_pipeline_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "govuk_message_queue_consumer"

RSpec.describe PublishingEventPipeline do
describe ".run" do
let(:consumer) { instance_double(GovukMessageQueueConsumer::Consumer, run: nil) }
let(:repository) { double }

before do
described_class.configure do |config|
config.message_queue_name = "test-queue"
config.repository = repository
end

allow(GovukMessageQueueConsumer::Consumer).to receive(:new).with(
queue_name: "test-queue",
processor: an_instance_of(PublishingEventPipeline::MessageProcessor),
).and_return(consumer)
end

it "runs our processor through govuk_message_queue_consumer" do
described_class.run

expect(consumer).to have_received(:run)
end
end
end

0 comments on commit 1505dfa

Please sign in to comment.