Sha256: fc5407e48bdb8fd9dd1b2ae127c8024f1a8e88c961cb45cba8f3fbe32202dd23

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 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
      condition = described_class.new
      xs        = []

      t1 = Thread.new do
        condition.wait
        xs << :notified1
      end
      t1.abort_on_exception = true

      t2 = Thread.new do
        condition.wait
        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
    it "notifies all the threads waiting on the latch" do
      condition = described_class.new
      @xs        = []

      t1 = Thread.new do
        condition.wait
        @xs << :notified1
      end
      t1.abort_on_exception = true

      t2 = Thread.new do
        condition.wait
        @xs << :notified2
      end
      t2.abort_on_exception = true

      sleep 0.5
      condition.notify_all
      sleep 0.5
      @xs.should include(:notified1)
      @xs.should include(:notified2)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bunny-0.9.0.pre9 spec/unit/concurrent/condition_spec.rb
bunny-0.9.0.pre8 spec/unit/concurrent/condition_spec.rb