downstream 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3806c4f875e378357e47571ed0bd5dbac1b45a80f9a01a78187b3c971378f425
4
- data.tar.gz: e038408a8fb1ce3e18dfd7614e9fe3931fd72c7913249457c29fa00b14a22aa4
3
+ metadata.gz: 5ceb9c34b8c3146b89ff1a6668ebadc7b4306abdf5b113fca512b1d2fb27cb64
4
+ data.tar.gz: 18986657d7665ecd183224025d72ace1013c6ebc3351792863cacca250ff16e7
5
5
  SHA512:
6
- metadata.gz: 6f81acf6c0da05267067ac4d4c60d9edcb8aefba35d326643651afcf4ba17df09b88a791b379f10318e0c63aeaa691397d10b3a2cad10e23a38b2c29990fa9bf
7
- data.tar.gz: 97bf35f16b9542bb032fb0ed52b6192b24ea92a3157d2f0cfc883546f50ee04e759965d6281fbb956e30029ee8bd716b129b8340b5638c3d619be78ca4494faf
6
+ metadata.gz: 880ea8f11ba29203402f975c523ac695af7befd8c5824a38805d4c48deec84123f0331f354296fe741cfd1341ba29a38c1f6273a4cdb0dccafc312536aa72de1
7
+ data.tar.gz: 06faab1f5d2a5d4be4d5a6e82109c197de85e7306f8b6a11d64d304206bf8fe220a3bd1b74dd61a25cb4cf104f1cffae9d5514aea75c60823c6d687e58fe0ac2
data/README.md CHANGED
@@ -131,6 +131,12 @@ store.subscribe OnProfileCreated::DoThat, async: {queue: :low_priority}
131
131
 
132
132
  You can test subscribers as normal Ruby objects.
133
133
 
134
+ First, load testing helpers in the `spec_helper.rb`:
135
+
136
+ ```ruby
137
+ require "downstream/rspec"
138
+ ```
139
+
134
140
  To test that a given subscriber exists, you can do the following:
135
141
 
136
142
  ```ruby
@@ -143,6 +149,14 @@ it "is subscribed to some event" do
143
149
 
144
150
  expect(MySubscriberService).to have_received(:call).with(event)
145
151
  end
152
+
153
+ # for asynchronous subscriptions
154
+ it "is subscribed to some event" do
155
+ event = MyEvent.new(some: "data")
156
+ expect { Downstream.publish event }.
157
+ to have_enqueued_async_subscriber_for(MySubscriberService).
158
+ with(event)
159
+ end
146
160
  ```
147
161
 
148
162
  To test publishing use `have_published_event` matcher:
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Downstream
2
4
  class AbstractPubsub
3
5
  def subscribe(identifier, callable)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/notifications"
2
4
  require_relative "subscriber"
3
5
 
@@ -5,10 +7,7 @@ module Downstream
5
7
  module Stateless
6
8
  class Pubsub < AbstractPubsub
7
9
  def subscribe(identifier, callable, async: false)
8
- ActiveSupport::Notifications.subscribe(
9
- identifier,
10
- Subscriber.new(callable, async: async)
11
- )
10
+ Subscriber.new(callable, async: async).tap { |s| s.subscribe(identifier) }
12
11
  end
13
12
 
14
13
  def subscribed(identifier, callable, &block)
@@ -1,4 +1,4 @@
1
- require_relative "subscriber_job"
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Downstream
4
4
  module Stateless
@@ -38,8 +38,21 @@ module Downstream
38
38
  end
39
39
  end
40
40
 
41
+ def subscribe(identifier)
42
+ @notification_subscriber = ActiveSupport::Notifications.subscribe(
43
+ identifier,
44
+ self
45
+ )
46
+ end
47
+
48
+ def unsubscribe
49
+ ActiveSupport::Notifications.unsubscribe(notification_subscriber)
50
+ end
51
+
41
52
  private
42
53
 
54
+ attr_reader :notification_subscriber
55
+
43
56
  def async_queue_name
44
57
  return @async_queue_name if defined?(@async_queue_name)
45
58
 
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec/rails/matchers/active_job"
4
+
5
+ module Downstream
6
+ class HaveEnqueuedAsyncSubscriberFor < RSpec::Rails::Matchers::ActiveJob::HaveEnqueuedJob
7
+ class EventMatcher
8
+ include ::RSpec::Matchers::Composable
9
+
10
+ attr_reader :event
11
+
12
+ def initialize(event)
13
+ @event = event
14
+ end
15
+
16
+ def matches?(actual)
17
+ actual == event
18
+ end
19
+
20
+ def description
21
+ "be #{event.inspect}"
22
+ end
23
+ end
24
+
25
+ attr_reader :callable
26
+
27
+ def initialize(callable)
28
+ @callable = callable
29
+ super(SubscriberJob)
30
+ end
31
+
32
+ def with(event)
33
+ super(EventMatcher.new(event), callable.name)
34
+ end
35
+
36
+ def matches?(proc)
37
+ raise ArgumentError, "have_enqueued_async_subscriber_for only supports block expectations" unless Proc === proc
38
+ super
39
+ end
40
+ end
41
+ end
42
+
43
+ RSpec.configure do |config|
44
+ config.include(Module.new do
45
+ def have_enqueued_async_subscriber_for(*args)
46
+ Downstream::HaveEnqueuedAsyncSubscriberFor.new(*args)
47
+ end
48
+ end)
49
+ end
@@ -1,3 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "rspec/have_published_event"
4
+ require_relative "rspec/have_enqueued_async_subscriber_for"
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Downstream
4
+ class SubscriberJob < ActiveJob::Base
5
+ def perform(event, callable)
6
+ callable.constantize.call(event)
7
+ end
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Downstream
4
- VERSION = "1.2.0"
4
+ VERSION = "1.3.0"
5
5
  end
data/lib/downstream.rb CHANGED
@@ -9,7 +9,7 @@ require "after_commit_everywhere"
9
9
  require "downstream/config"
10
10
  require "downstream/event"
11
11
  require "downstream/pubsub_adapters/abstract_pubsub"
12
- require "downstream/rspec" if defined?(RSpec)
12
+ require "downstream/subscriber_job"
13
13
 
14
14
  module Downstream
15
15
  class << self
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: downstream
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - merkushin.m.s@gmail.com
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2021-11-01 00:00:00.000000000 Z
12
+ date: 2021-11-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: after_commit_everywhere
@@ -194,9 +194,10 @@ files:
194
194
  - lib/downstream/pubsub_adapters/abstract_pubsub.rb
195
195
  - lib/downstream/pubsub_adapters/stateless/pubsub.rb
196
196
  - lib/downstream/pubsub_adapters/stateless/subscriber.rb
197
- - lib/downstream/pubsub_adapters/stateless/subscriber_job.rb
198
197
  - lib/downstream/rspec.rb
198
+ - lib/downstream/rspec/have_enqueued_async_subscriber_for.rb
199
199
  - lib/downstream/rspec/have_published_event.rb
200
+ - lib/downstream/subscriber_job.rb
200
201
  - lib/downstream/version.rb
201
202
  homepage: https://github.com/bibendi/downstream
202
203
  licenses:
@@ -1,9 +0,0 @@
1
- module Downstream
2
- module Stateless
3
- class SubscriberJob < ActiveJob::Base
4
- def perform(event, callable)
5
- callable.constantize.call(event)
6
- end
7
- end
8
- end
9
- end