I am trying to build a persistent relational projection of an event stream in a event-sourced/CQRS and DDD-inspired Rails 5.2.x application using RailsEventStore.
To have my projector receive notification on new events I'm using Client#subscribe which works nicely. I set my projector subscription up in a Rails initializer file.
Now when I run tests, most of the tests don't require there to be a projection of the event stream. Therefore I would like to have the subscription only in highly integrated tests.
Because my initializer seems to run only once, before any individual tests are executed, I need to subscribe/unsubscribe the projector in tests initialization. Sadly I did not find any trace of an Client#unsubscribe method.
Is there a way to unsubscribe from an event stream after I already subscribed to it? Or is there overall a better way to handle this situation in RailsEventStore?
There's no
Client#unsubscribemethod. Instead each subscription returns a lambda that you call to revoke subscription:https://github.com/RailsEventStore/rails_event_store/blob/cfc91c9cb367e514ba1c6de1a711a7610780b520/ruby_event_store/lib/ruby_event_store/spec/subscriptions_lint.rb#L60-L102
My colleague RafaĆ described in https://blog.arkency.com/optimizing-test-suites-when-using-rails-event-store/ how particular subscribers can be enabled or disabled depending on a test case. In short the idea is to have new
event_storeinstance for each test and filter list of subscribers based on test metadata.With new
event_storeinstance per test (without subscribers to begin with) you no longer have to unsubscribe. You could then for example explicitly subscribe in an integration test case: