Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into create_test_db_in_s…
Browse files Browse the repository at this point in the history
…etup
  • Loading branch information
Joe Sustaric committed Jun 15, 2017
2 parents d61dd06 + 94c2aa8 commit 48d32a9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 40 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.2.0] - 2017-6-1
### Changed
- Make `EventSourcery::Postgres::OptimisedEventPollWaiter#shutdown` private
- Make `EventSourcery::Postgres::OptimisedEventPollWaiter#shutdown` private
- Updated `EventSourcery::Postgres::OptimisedEventPollWaiter#poll` to ensure that `#shutdown!` is run when an error is raised
or when the loop stops
- Remove dynamic emit events methods from reactors (e.g. emit_item_added)
- The emit_events method now accepts typed events instead of symbols

### Added
- Configure projector tracker table name via `EventSourcery::Postgres.configure`
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# EventSourcery::Postgres

## Development Status

EventSourcery is currently being used in production by multiple apps but we
haven't finalized the API yet and things are still moving rapidly. Until we
release a 1.0 things may change without first being deprecated.

## Installation

Add this line to your application's Gemfile:
Expand Down
16 changes: 3 additions & 13 deletions lib/event_sourcery/postgres/reactor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,7 @@ def self.included(base)

module ClassMethods
def emits_events(*event_types)
@emits_event_types = event_types.map(&:to_s)

@emits_event_types.each do |event_type|
define_method "emit_#{event_type}" do |aggregate_id, body|
emit_event(
type: event_type,
aggregate_id: aggregate_id,
body: body
)
end
end
@emits_event_types = event_types
end

def emit_events
Expand All @@ -34,7 +24,7 @@ def emits_events?
end

def emits_event?(event_type)
emit_events.include?(event_type.to_s)
emit_events.include?(event_type)
end
end

Expand Down Expand Up @@ -67,7 +57,7 @@ def emit_event(event_or_hash, &block)
else
Event.new(event_or_hash)
end
raise UndeclaredEventEmissionError unless self.class.emits_event?(event.type)
raise UndeclaredEventEmissionError unless self.class.emits_event?(event.class)
event.body.merge!(DRIVEN_BY_EVENT_PAYLOAD_KEY => _event.id)
invoke_action_and_emit_event(event, block)
EventSourcery.logger.debug { "[#{self.processor_name}] Emitted event: #{event.inspect}" }
Expand Down
48 changes: 22 additions & 26 deletions spec/event_sourcery/postgres/reactor_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
RSpec.describe EventSourcery::Postgres::Reactor do
TermsConfirmationEmailSent = Class.new(EventSourcery::Event)
ItemViewed = Class.new(EventSourcery::Event)
EchoEvent = Class.new(EventSourcery::Event)

let(:reactor_class) {
Class.new do
include EventSourcery::Postgres::Reactor
Expand All @@ -17,7 +21,7 @@ def process(event)
include EventSourcery::Postgres::Reactor

processes_events :terms_accepted
emits_events :blah
emits_events TermsConfirmationEmailSent

def process(event)
end
Expand Down Expand Up @@ -127,18 +131,15 @@ def process(event)

describe '.emits_event?' do
it 'returns true if the event has been defined' do
expect(reactor_class_with_emit.emits_event?('blah')).to eq true
expect(reactor_class_with_emit.emits_event?(:blah)).to eq true
expect(reactor_class_with_emit.emits_event?(TermsConfirmationEmailSent)).to eq true
end

it "returns false if the event hasn't been defined" do
expect(reactor_class_with_emit.emits_event?('item_viewed')).to eq false
expect(reactor_class_with_emit.emits_event?(:item_viewed)).to eq false
expect(reactor_class_with_emit.emits_event?(ItemViewed)).to eq false
end

it "returns false if the reactor doesn't emit events" do
expect(reactor_class.emits_event?('blah')).to eq false
expect(reactor_class.emits_event?(:blah)).to eq false
expect(reactor_class.emits_event?(TermsConfirmationEmailSent)).to eq false
end
end

Expand All @@ -151,12 +152,12 @@ def process(event)
end

context 'with a reactor that emits events' do
let(:event_1) { EventSourcery::Event.new(id: 1, type: 'terms_accepted', aggregate_id: aggregate_id, body: { time: Time.now }) }
let(:event_2) { EventSourcery::Event.new(id: 2, type: 'echo_event', aggregate_id: aggregate_id, body: event_1.body.merge(EventSourcery::Postgres::Reactor::DRIVEN_BY_EVENT_PAYLOAD_KEY => 1)) }
let(:event_3) { EventSourcery::Event.new(id: 3, type: 'terms_accepted', aggregate_id: aggregate_id, body: { time: Time.now }) }
let(:event_4) { EventSourcery::Event.new(id: 4, type: 'terms_accepted', aggregate_id: aggregate_id, body: { time: Time.now }) }
let(:event_5) { EventSourcery::Event.new(id: 5, type: 'terms_accepted', aggregate_id: aggregate_id, body: { time: Time.now }) }
let(:event_6) { EventSourcery::Event.new(id: 6, type: 'echo_event', aggregate_id: aggregate_id, body: event_3.body.merge(EventSourcery::Postgres::Reactor::DRIVEN_BY_EVENT_PAYLOAD_KEY => 3)) }
let(:event_1) { TermsAccepted.new(id: 1, aggregate_id: aggregate_id, body: { time: Time.now }) }
let(:event_2) { EchoEvent.new(id: 2, aggregate_id: aggregate_id, body: event_1.body.merge(EventSourcery::Postgres::Reactor::DRIVEN_BY_EVENT_PAYLOAD_KEY => 1)) }
let(:event_3) { TermsAccepted.new(id: 3, aggregate_id: aggregate_id, body: { time: Time.now }) }
let(:event_4) { TermsAccepted.new(id: 4, aggregate_id: aggregate_id, body: { time: Time.now }) }
let(:event_5) { TermsAccepted.new(id: 5, aggregate_id: aggregate_id, body: { time: Time.now }) }
let(:event_6) { EchoEvent.new(id: 6, aggregate_id: aggregate_id, body: event_3.body.merge(EventSourcery::Postgres::Reactor::DRIVEN_BY_EVENT_PAYLOAD_KEY => 3)) }
let(:events) { [event_1, event_2, event_3, event_4] }
let(:action_stub_class) {
Class.new do
Expand All @@ -174,11 +175,11 @@ def self.actioned
include EventSourcery::Postgres::Reactor

processes_events :terms_accepted
emits_events :echo_event
emits_events EchoEvent

def process(event)
@event = event
emit_event(EventSourcery::Event.new(aggregate_id: event.aggregate_id, type: 'echo_event', body: event.body)) do
emit_event(EchoEvent.new(aggregate_id: event.aggregate_id, body: event.body)) do
TestActioner.action(event.id)
end
end
Expand Down Expand Up @@ -206,10 +207,10 @@ def latest_events(n = 1)
include EventSourcery::Postgres::Reactor

processes_events :terms_accepted
emits_events :echo_event
emits_events EchoEvent

def process(event)
emit_event(EventSourcery::Event.new(aggregate_id: event.aggregate_id, type: 'echo_event', body: event.body))
emit_event(EchoEvent.new(aggregate_id: event.aggregate_id, body: event.body))
end
end
}
Expand All @@ -228,10 +229,10 @@ def process(event)
include EventSourcery::Postgres::Reactor

processes_events :terms_accepted
emits_events :echo_event
emits_events EchoEvent

def process(event)
emit_event(EventSourcery::Event.new(aggregate_id: event.aggregate_id, type: 'echo_event_2', body: event.body))
emit_event(ItemViewed.new(aggregate_id: event.aggregate_id, body: event.body))
end
end
}
Expand All @@ -250,10 +251,10 @@ def process(event)
include EventSourcery::Postgres::Reactor

processes_events :terms_accepted
emits_events :echo_event
emits_events EchoEvent

def process(event)
emit_event(EventSourcery::Event.new(aggregate_id: event.aggregate_id, type: 'echo_event')) do |body|
emit_event(EchoEvent.new(aggregate_id: event.aggregate_id)) do |body|
body[:token] = 'secret-identifier'
end
end
Expand All @@ -270,11 +271,6 @@ def process(event)
expect(latest_events(1).first.body["_driven_by_event_id"]).to eq event_1.id
end
end

it 'adds methods to emit permitted events' do
allow(reactor).to receive(:emit_event).with(type: 'echo_event', aggregate_id: 123, body: { a: :b })
reactor.emit_echo_event(123, { a: :b })
end
end
end
end

0 comments on commit 48d32a9

Please sign in to comment.