Sha256: 0ef5d194143c2c1673a74b8a15d3e16df4dce00bf3e59c8701cd024508bb3096

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

require 'spec_helper'

describe Wisper::TemporaryListeners do
  let(:listener_1) { double('listener', :to_a => nil) } # [1]
  let(:listener_2) { double('listener', :to_a => nil) }
  let(:publisher)  { Object.class_eval { include Wisper::Publisher } }

  describe '.with' do
    it 'globally subscribes listener for duration of given block' do

      expect(listener_1).to receive(:success)
      expect(listener_1).to_not receive(:failure)

      Wisper::TemporaryListeners.with(listener_1) do
        publisher.instance_eval { broadcast(:success) }
      end

      publisher.instance_eval { broadcast(:failure) }
    end

    it 'globally subscribes listeners for duration of given block' do

      expect(listener_1).to receive(:success)
      expect(listener_1).to_not receive(:failure)

      expect(listener_2).to receive(:success)
      expect(listener_2).to_not receive(:failure)

      Wisper::TemporaryListeners.with(listener_1, listener_2) do
        publisher.instance_eval { broadcast(:success) }
      end

      publisher.instance_eval { broadcast(:failure) }
    end

    it 'ensures registrations are thread local' do
      num_threads = 20
      (1..num_threads).to_a.map do
        Thread.new do
          Wisper::TemporaryListeners.registrations << Object.new
          expect(Wisper::TemporaryListeners.registrations.size).to eql 1
        end
      end.each(&:join)

      expect(Wisper::TemporaryListeners.registrations.size).to eql 0
    end

    it 'ensures registrations are cleared after exception raised in block' do
      begin
        Wisper::TemporaryListeners.with(listener_1) do
          raise StandardError
        end
      rescue StandardError
      end

      expect(Wisper::TemporaryListeners.registrations.size).to eql 0
    end
  end
end

# [1] stubbing `to_a` prevents `Double "listener" received unexpected message
# :to_a with (no args)` on MRI 1.9.2 when a double is passed to `Array()`.

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wisper-1.4.0 spec/lib/temporary_global_listeners_spec.rb