Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscriptions redesigned #665

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
be31df8
Redefine subscriptions - breaking changes
mpraglowski Aug 9, 2019
39bc7ad
Refactor subscriptions
mpraglowski Aug 9, 2019
8ff0e36
Subscription storage is optional
mpraglowski Aug 9, 2019
972be0a
All dispatchers & schedulers receive now subscription object instead …
mpraglowski Aug 10, 2019
b693e49
Single subscriptions store is enough
mpraglowski Aug 16, 2019
33f34cb
Subscription global or assigned to specific type - it's just an imple…
mpraglowski Aug 16, 2019
2c9e3aa
Minor improvement & fixes
mpraglowski Aug 16, 2019
554cdbe
Docs & lint/specs for subscriptions store
mpraglowski Aug 16, 2019
4f76f27
Docs & specs for Subscription object
mpraglowski Aug 16, 2019
e18ceb3
No more local & global subscriptions, there are main subscription sto…
mpraglowski Aug 16, 2019
c8dedab
Do not need that anymore
mpraglowski Aug 16, 2019
29e62b4
Simplify subscriptions store API
mpraglowski Aug 16, 2019
acc8184
Must be excluded from mutation tests as test where it could be trigge…
mpraglowski Aug 16, 2019
2bd6c6d
Kill mutant
mpraglowski Aug 16, 2019
9aefcdf
Typo fix
mpraglowski Aug 16, 2019
636cafb
It does not make sense to require subscription store to be chainable
mpraglowski Aug 16, 2019
ff945e7
Fix subscriptions store lint namespace
mpraglowski Aug 16, 2019
cec639c
Add instrumented subscriptions store
mpraglowski Aug 16, 2019
b82767b
Use store factory for thread store instead of just a class
mpraglowski Aug 16, 2019
570590f
Again, keep Ruby 2.4 compatibility for now
mpraglowski Sep 6, 2019
7140093
Kill mutations
mpraglowski Sep 6, 2019
25dd989
Add frozen_string_literal: true in all changed files
mpraglowski Sep 6, 2019
4ba25e3
Document subscriptions provider class
mpraglowski Sep 6, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rails_event_store/spec/active_job_scheduler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module RailsEventStore
describe "#call" do
specify do
scheduler = ActiveJobScheduler.new
subscription = RubyEventStore::GlobalSubscription.new(MyAsyncHandler)
subscription = RubyEventStore::Subscription.new(MyAsyncHandler)
scheduler.call(subscription, serialized_event)

enqueued_jobs = ActiveJob::Base.queue_adapter.enqueued_jobs
Expand Down
24 changes: 12 additions & 12 deletions rails_event_store/spec/after_commit_async_dispatcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class DummyRecord < ActiveRecord::Base

it "dispatch job immediately when no transaction is open" do
expect_to_have_enqueued_job(MyAsyncHandler) do
subscription = RubyEventStore::GlobalSubscription.new(MyAsyncHandler)
subscription = RubyEventStore::Subscription.new(MyAsyncHandler)
dispatcher.call(subscription, event, serialized_event)
end
expect(MyAsyncHandler.received).to be_nil
Expand All @@ -49,7 +49,7 @@ class DummyRecord < ActiveRecord::Base
expect_to_have_enqueued_job(MyAsyncHandler) do
ActiveRecord::Base.transaction do
expect_no_enqueued_job(MyAsyncHandler) do
subscription = RubyEventStore::GlobalSubscription.new(MyAsyncHandler)
subscription = RubyEventStore::Subscription.new(MyAsyncHandler)
dispatcher.call(subscription, event, serialized_event)
end
end
Expand All @@ -63,7 +63,7 @@ class DummyRecord < ActiveRecord::Base
it "does not dispatch job" do
expect_no_enqueued_job(MyAsyncHandler) do
ActiveRecord::Base.transaction do
subscription = RubyEventStore::GlobalSubscription.new(MyAsyncHandler)
subscription = RubyEventStore::Subscription.new(MyAsyncHandler)
dispatcher.call(subscription, event, serialized_event)
raise ActiveRecord::Rollback
end
Expand All @@ -87,7 +87,7 @@ class DummyRecord < ActiveRecord::Base
it "does not dispatch job" do
expect_no_enqueued_job(MyAsyncHandler) do
ActiveRecord::Base.transaction do
subscription = RubyEventStore::GlobalSubscription.new(MyAsyncHandler)
subscription = RubyEventStore::Subscription.new(MyAsyncHandler)
dispatcher.call(subscription, event, serialized_event)
raise ActiveRecord::Rollback
end
Expand All @@ -103,7 +103,7 @@ class DummyRecord < ActiveRecord::Base
ActiveRecord::Base.transaction do
expect_no_enqueued_job(MyAsyncHandler) do
ActiveRecord::Base.transaction(requires_new: false) do
subscription = RubyEventStore::GlobalSubscription.new(MyAsyncHandler)
subscription = RubyEventStore::Subscription.new(MyAsyncHandler)
dispatcher.call(subscription, event, serialized_event)
end
end
Expand All @@ -119,7 +119,7 @@ class DummyRecord < ActiveRecord::Base
ActiveRecord::Base.transaction do
expect_no_enqueued_job(MyAsyncHandler) do
ActiveRecord::Base.transaction(requires_new: true) do
subscription = RubyEventStore::GlobalSubscription.new(MyAsyncHandler)
subscription = RubyEventStore::Subscription.new(MyAsyncHandler)
dispatcher.call(subscription, event, serialized_event)
end
end
Expand All @@ -135,7 +135,7 @@ class DummyRecord < ActiveRecord::Base
ActiveRecord::Base.transaction do
expect_no_enqueued_job(MyAsyncHandler) do
ActiveRecord::Base.transaction(requires_new: true) do
subscription = RubyEventStore::GlobalSubscription.new(MyAsyncHandler)
subscription = RubyEventStore::Subscription.new(MyAsyncHandler)
dispatcher.call(subscription, event, serialized_event)
raise ActiveRecord::Rollback
end
Expand All @@ -157,7 +157,7 @@ class DummyRecord < ActiveRecord::Base
ActiveRecord::Base.transaction do
DummyRecord.new.save!
expect_no_enqueued_job(MyAsyncHandler) do
subscription = RubyEventStore::GlobalSubscription.new(MyAsyncHandler)
subscription = RubyEventStore::Subscription.new(MyAsyncHandler)
dispatcher.call(subscription, event, serialized_event)
end
end
Expand Down Expand Up @@ -189,7 +189,7 @@ class DummyRecord < ActiveRecord::Base
ActiveRecord::Base.transaction do
DummyRecord.new.save!
expect_no_enqueued_job(MyAsyncHandler) do
subscription = RubyEventStore::GlobalSubscription.new(MyAsyncHandler)
subscription = RubyEventStore::Subscription.new(MyAsyncHandler)
dispatcher.call(subscription, event, serialized_event)
end
end
Expand All @@ -214,7 +214,7 @@ class DummyRecord < ActiveRecord::Base

it "dispatches the job" do
expect_to_have_enqueued_job(MyAsyncHandler) do
subscription = RubyEventStore::GlobalSubscription.new(MyAsyncHandler)
subscription = RubyEventStore::Subscription.new(MyAsyncHandler)
dispatcher.call(subscription, event, serialized_event)
end
end
Expand All @@ -228,7 +228,7 @@ class DummyRecord < ActiveRecord::Base
expect_no_enqueued_job(MyAsyncHandler) do
ActiveRecord::Base.transaction do
expect_no_enqueued_job(MyAsyncHandler) do
subscription = RubyEventStore::GlobalSubscription.new(MyAsyncHandler)
subscription = RubyEventStore::Subscription.new(MyAsyncHandler)
dispatcher.call(subscription, event, serialized_event)
end
end
Expand All @@ -245,7 +245,7 @@ class DummyRecord < ActiveRecord::Base
expect_to_have_enqueued_job(MyAsyncHandler) do
ActiveRecord::Base.transaction do
expect_no_enqueued_job(MyAsyncHandler) do
subscription = RubyEventStore::GlobalSubscription.new(MyAsyncHandler)
subscription = RubyEventStore::Subscription.new(MyAsyncHandler)
dispatcher.call(subscription, event, serialized_event)
end
end
Expand Down
1 change: 0 additions & 1 deletion ruby_event_store/lib/ruby_event_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

require 'ruby_event_store/dispatcher'
require 'ruby_event_store/subscription'
require 'ruby_event_store/global_subscription'
require 'ruby_event_store/in_memory_subscriptions_store'
require 'ruby_event_store/subscriptions'
require 'ruby_event_store/broker'
Expand Down
1 change: 1 addition & 0 deletions ruby_event_store/lib/ruby_event_store/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

module RubyEventStore
GLOBAL_STREAM = Object.new
GLOBAL_SUBSCRIPTION = Object.new
PAGE_SIZE = 100.freeze
end
59 changes: 0 additions & 59 deletions ruby_event_store/lib/ruby_event_store/global_subscription.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@ def initialize
@subscriptions = Hash.new {|hsh, key| hsh[key] = [] }
end

def add(subscription, type = nil)
def add(subscription, type = GLOBAL_SUBSCRIPTION)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

I'd rather call it ANY_EVENT

@subscriptions[type.to_s] << subscription
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why to_s? Aren't we expecting Event#type after all?

end

def delete(subscription, type = nil)
def delete(subscription, type = GLOBAL_SUBSCRIPTION)
@subscriptions.fetch(type.to_s).delete(subscription)
end

def all_for(event_type)
@subscriptions[event_type.to_s]
end

def all_global
all_for(nil)
end

def value
self
end
Expand Down
4 changes: 2 additions & 2 deletions ruby_event_store/lib/ruby_event_store/subscription.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module RubyEventStore
class Subscription
def initialize(subscriber, event_types, store: nil)
def initialize(subscriber, event_types = [GLOBAL_SUBSCRIPTION], store: nil)
@subscriber = subscriber
@event_types = event_types
@store = store
Expand All @@ -18,7 +18,7 @@ def unsubscribe
def inspect
<<~EOS.strip
#<#{self.class}:0x#{__id__.to_s(16)}>
- event types: #{event_types.inspect}
- #{ event_types != [GLOBAL_SUBSCRIPTION] ? "event types: #{event_types.inspect}" : "global subscription"}
- subscriber: #{subscriber.inspect}
EOS
end
Expand Down
4 changes: 2 additions & 2 deletions ruby_event_store/lib/ruby_event_store/subscriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ def initialize(store)
end

def add(subscription)
GlobalSubscription.new(subscription, store: @store.value)
Subscription.new(subscription, [GLOBAL_SUBSCRIPTION], store: @store.value)
end

def all_for(_event_type)
@store.value.all_global
@store.value.all_for(GLOBAL_SUBSCRIPTION)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions ruby_event_store/spec/composed_dispatcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def verify(_subscriber)
composed_dispatcher = ComposedDispatcher.new(dispatcher)
event = instance_double(::RubyEventStore::Event)
serialized_event = instance_double(::RubyEventStore::SerializedRecord)
subscription = GlobalSubscription.new(-> { })
subscription = Subscription.new(-> { })

composed_dispatcher.call(subscription, event, serialized_event)

Expand All @@ -75,7 +75,7 @@ def verify(_subscriber)
composed_dispatcher = ComposedDispatcher.new(skippy, real)
event = instance_double(::RubyEventStore::Event)
serialized_event = instance_double(::RubyEventStore::SerializedRecord)
subscription = GlobalSubscription.new(-> { })
subscription = Subscription.new(-> { })

composed_dispatcher.call(subscription, event, serialized_event)

Expand All @@ -89,7 +89,7 @@ def verify(_subscriber)
composed_dispatcher = ComposedDispatcher.new(real1, real2)
event = instance_double(::RubyEventStore::Event)
serialized_event = instance_double(::RubyEventStore::SerializedRecord)
subscription = GlobalSubscription.new(-> { })
subscription = Subscription.new(-> { })

composed_dispatcher.call(subscription, event, serialized_event)

Expand Down
4 changes: 2 additions & 2 deletions ruby_event_store/spec/dispatcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ def call

specify "calls subscribed instance" do
expect(handler).to receive(:call).with(event)
subscription = GlobalSubscription.new(handler)
subscription = Subscription.new(handler)
Dispatcher.new.call(subscription, event, serialized_event)
end

specify "calls subscribed class" do
expect(HandlerClass).to receive(:new).and_return(handler)
expect(handler).to receive(:call).with(event)
subscription = GlobalSubscription.new(HandlerClass)
subscription = Subscription.new(HandlerClass)
Dispatcher.new.call(subscription, event, serialized_event)
end

Expand Down
4 changes: 2 additions & 2 deletions ruby_event_store/spec/instrumented_dispatcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module RubyEventStore
event = Object.new
serialized_event = Object.new
subscriber = -> { }
subscription = GlobalSubscription.new(subscriber)
subscription = Subscription.new(subscriber)

instrumented_dispatcher.call(subscription, event, serialized_event)

Expand All @@ -26,7 +26,7 @@ module RubyEventStore
event = Object.new
serialized_event = Object.new
subscriber = -> { }
subscription = GlobalSubscription.new(subscriber)
subscription = Subscription.new(subscriber)

instrumented_dispatcher.call(subscription, event, serialized_event)

Expand Down