Sha256: 822405973784dc95860408a4a8ab23d609781512a39595f94570c31436a23023

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

require 'spec_helper'

describe Wisper::GlobalListeners do
  let(:global_listener)  { double('listener') }
  let(:local_listener)   { double('listener') }
  let(:publisher)        { publisher_class.new }

  after(:each) { Wisper::GlobalListeners.clear }

  describe '.add_listener' do
    it 'adds given listener to every publisher' do
      Wisper::GlobalListeners.add_listener(global_listener)
      global_listener.should_receive(:it_happened)
      publisher.send(:broadcast, :it_happened)
    end

    it 'works along side local listeners' do
      # global listener
      Wisper::GlobalListeners.add_listener(global_listener)

      # local listener
      publisher.add_listener(local_listener)

      global_listener.should_receive(:it_happened)
      local_listener.should_receive(:it_happened)

      publisher.send(:broadcast, :it_happened)
    end

    it 'is threadsafe' do
      num_threads = 100
      (1..num_threads).to_a.map do
        Thread.new do
          Wisper::GlobalListeners.add_listener(Object.new)
          sleep(rand) # a little chaos
        end
      end.each(&:join)

      Wisper::GlobalListeners.listeners.size.should == num_threads
    end
  end

  describe '.listeners' do
    it 'returns collection of global listeners' do
      Wisper::GlobalListeners.add_listener(global_listener)
      Wisper::GlobalListeners.listeners.should == [global_listener]
    end

    it 'returns an immutable collection' do
      Wisper::GlobalListeners.listeners.frozen?.should be_true
      expect { Wisper::GlobalListeners.listeners << global_listener }.to raise_error(RuntimeError)
    end
  end

  it '.clear clears all global listeners' do
    Wisper::GlobalListeners.add_listener(global_listener)
    Wisper::GlobalListeners.clear
    Wisper::GlobalListeners.listeners.should be_empty
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wisper-1.1.0 spec/lib/global_subscribers_spec.rb