Class: RSpec::Notifications::Subscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/notifications/subscriber.rb

Overview

Subscribes to ActiveSupport::Notifications for the duration of a block, capturing every matching event, then unsubscribes.

Thread-safe: notifications may be delivered from threads other than the one running the block, so appends are guarded by a mutex.

Defined Under Namespace

Classes: Event

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ Subscriber

Returns a new instance of Subscriber.



30
31
32
33
34
# File 'lib/rspec/notifications/subscriber.rb', line 30

def initialize(pattern)
  @pattern = pattern
  @events = []
  @mutex = Mutex.new
end

Class Method Details

.capture(pattern, &block) ⇒ Object

Subscribe to pattern, run block, and return the captured events.



26
27
28
# File 'lib/rspec/notifications/subscriber.rb', line 26

def self.capture(pattern, &block)
  new(pattern).capture(&block)
end

Instance Method Details

#captureObject



36
37
38
39
40
41
42
# File 'lib/rspec/notifications/subscriber.rb', line 36

def capture
  subscription = subscribe
  yield
  @mutex.synchronize { @events.dup }
ensure
  ActiveSupport::Notifications.unsubscribe(subscription) if subscription
end