Sha256: 4747310918fb794bd1b717162d0e831bb8db25c727f5d5eaef3f7518d2b65105

Contents?: true

Size: 1.6 KB

Versions: 18

Compression:

Stored size: 1.6 KB

Contents

require "spec_helper"
require "bunny/concurrent/condition"

describe Bunny::Concurrent::Condition do
  describe "#wait" do
    it "blocks current thread until notified" do
      condition = described_class.new
      xs        = []

      t = Thread.new do
        xs << :notified

        sleep 0.25
        condition.notify
      end
      t.abort_on_exception = true

      condition.wait
      xs.should == [:notified]
    end
  end

  describe "#notify" do
    it "notifies a single thread waiting on the latch" do
      mutex     = Mutex.new
      condition = described_class.new
      xs        = []

      t1 = Thread.new do
        condition.wait
        mutex.synchronize { xs << :notified1 }
      end
      t1.abort_on_exception = true

      t2 = Thread.new do
        condition.wait
        mutex.synchronize { xs << :notified2 }
      end
      t2.abort_on_exception = true

      sleep 0.25
      condition.notify
      sleep 0.5
      xs.should satisfy { |ys| ys.size == 1 && (ys.include?(:notified1) || ys.include?(:notified2)) }
    end
  end

  describe "#notify_all" do
    let(:n) { 120 }

    it "notifies all the threads waiting on the latch" do
      mutex     = Mutex.new
      condition = described_class.new
      @xs       = []

      n.times do |i|
        t = Thread.new do
          condition.wait
          mutex.synchronize { @xs << "notified#{i + 1}".to_sym }
        end
        t.abort_on_exception = true
      end

      sleep 0.5
      condition.notify_all
      sleep 0.5

      n.times do |i|
        item = "notified#{i + 1}".to_sym

        @xs.should include(item)
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
bunny-0.10.0 spec/unit/concurrent/condition_spec.rb
bunny-1.0.0.pre3 spec/unit/concurrent/condition_spec.rb
bunny-0.9.8 spec/unit/concurrent/condition_spec.rb
bunny-1.0.0.pre2 spec/unit/concurrent/condition_spec.rb
bunny-0.9.7 spec/unit/concurrent/condition_spec.rb
bunny-0.9.6 spec/unit/concurrent/condition_spec.rb
bunny-0.9.5 spec/unit/concurrent/condition_spec.rb
bunny-0.9.4 spec/unit/concurrent/condition_spec.rb
bunny-1.0.0.pre1 spec/unit/concurrent/condition_spec.rb
bunny-0.9.3 spec/unit/concurrent/condition_spec.rb
bunny-0.9.2 spec/unit/concurrent/condition_spec.rb
bunny-0.9.1 spec/unit/concurrent/condition_spec.rb
bunny-0.9.0 spec/unit/concurrent/condition_spec.rb
bunny-0.9.0.rc2 spec/unit/concurrent/condition_spec.rb
bunny-0.9.0.rc1 spec/unit/concurrent/condition_spec.rb
bunny-0.9.0.pre13 spec/unit/concurrent/condition_spec.rb
bunny-0.9.0.pre12 spec/unit/concurrent/condition_spec.rb
bunny-0.9.0.pre11 spec/unit/concurrent/condition_spec.rb