Skip to content

Commit

Permalink
Already-scheduled events might not have correlation_id
Browse files Browse the repository at this point in the history
Scenario:
- an event is published but not yet picked by background queue
- RES version is bumped and is expecting correlation_id presence in
  CorrelatedHandler
- queued event arrives to handler

Not immediately obvious scenario, test added so that mutant is aware of
expectations.

Even more common when replaying an event (retries).
  • Loading branch information
mostlyobvious committed Nov 2, 2020
1 parent 76b978b commit 8e725fd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Expand Up @@ -10,8 +10,8 @@ def perform(payload)
module CorrelatedHandler
def perform(event)
Rails.configuration.event_store.with_metadata(
correlation_id: event.metadata.fetch(:correlation_id),
causation_id: event.event_id
correlation_id: event.metadata[:correlation_id],
causation_id: event.event_id
) do
super
end
Expand Down
21 changes: 21 additions & 0 deletions rails_event_store/spec/rails_event_store_spec.rb
Expand Up @@ -136,8 +136,29 @@ def verify(subscriber)
expect(SidekiqHandlerWithHelper.event).to eq(ev)
end

specify 'CorrelatedHandler with event not yet scheduled with correlation_id' do
MetadataHandler.metadata = nil
event_store.append(ev = RailsEventStore::Event.new)
ev = event_store.read.event(ev.event_id)
MetadataHandler.perform_now(serialize_without_correlation_id(ev))
expect(MetadataHandler.metadata).to eq({
correlation_id: nil,
causation_id: ev.event_id,
})
end

private

def serialize_without_correlation_id(ev)
serialized =
event_store
.__send__(:mapper)
.event_to_serialized_record(ev)
.to_h
serialized[:metadata] = "---\n:timestamp: 2020-11-02 14:32:53.747308000 Z\n"
serialized
end

def wait_until(&block)
Timeout.timeout(1) do
loop do
Expand Down

0 comments on commit 8e725fd

Please sign in to comment.