Sha256: f7c575a2eba0a7826243489fc65cabd51bd3cd98c661b39ef398bb0b0ee6b465

Contents?: true

Size: 1.56 KB

Versions: 6

Compression:

Stored size: 1.56 KB

Contents

# Test event subscriber.
#
# @since 2.5.0
class EventSubscriber

  module Impl

    # The started events.
    #
    # @since 2.5.0
    attr_reader :started_events

    # The succeeded events.
    #
    # @since 2.5.0
    attr_reader :succeeded_events

    # The failed events.
    #
    # @since 2.5.0
    attr_reader :failed_events

    attr_reader :published_events

    # Cache the succeeded event.
    #
    # @param [ Event ] event The event.
    #
    # @since 2.5.0
    def succeeded(event)
      @mutex.synchronize do
        succeeded_events.push(event)
      end
    end

    # Cache the started event.
    #
    # @param [ Event ] event The event.
    #
    # @since 2.5.0
    def started(event)
      @mutex.synchronize do
        started_events.push(event)
      end
    end

    # Cache the failed event.
    #
    # @param [ Event ] event The event.
    #
    # @since 2.5.0
    def failed(event)
      @mutex.synchronize do
        failed_events.push(event)
      end
    end

    def select_published_events(cls)
      @published_events.select do |event|
        event.is_a?(cls)
      end
    end

    def published(event)
      @mutex.synchronize do
        @published_events << event
      end
    end

    # Clear all cached events.
    #
    # @since 2.5.1
    def clear_events!
      @started_events = []
      @succeeded_events = []
      @failed_events = []
      @published_events = []
      self
    end

    def initialize
      @mutex = Mutex.new
      clear_events!
    end
  end

  include Impl

  class << self
    include Impl
    public :initialize
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mongo-2.9.2 spec/support/event_subscriber.rb
mongo-2.9.1 spec/support/event_subscriber.rb
mongo-2.9.1.rc0 spec/support/event_subscriber.rb
mongo-2.9.0 spec/support/event_subscriber.rb
mongo-2.9.0.rc1 spec/support/event_subscriber.rb
mongo-2.9.0.rc0 spec/support/event_subscriber.rb