Sha256: 4cb8cf830936e731153a586b11aea0b2ad95ab00648d836c3f94e4c292e25c74

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

shared_examples "MongoLock driver that can find if a lock is available" do

  describe Mongo::Lock do

    describe '.available?' do

      it "creates a new Mongo::Lock instance and returns whether it is available" do
        expect(Mongo::Lock.available? 'my_lock').to be_true
      end

      it "calls #available?" do
        expect_any_instance_of(Mongo::Lock).to receive(:available?)
        Mongo::Lock.available? 'my_lock'
      end

      context "when options are provided" do

        it "passes them to the new lock" do
          Mongo::Lock.acquire 'my_lock', { owner: 'spence' }
          expect(Mongo::Lock.available?('my_lock', { owner: 'spence' })).to be_true
        end

      end

    end

    describe '#available?' do

      let(:lock) { Mongo::Lock.new 'my_lock', owner: 'spence', timeout_in: 0.01, frequency: 0.01 }

      context "when the lock is available" do

        it "returns true" do
          expect(lock.available?).to be_true
        end

      end

      context "when the lock is expired" do

        it "returns true" do
          my_collection.insert key: 'my_lock', owner: 'tobie', expires_at: 1.minute.ago
          expect(lock.available?).to be_true
        end

      end

      context "when the lock is already acquired but by this owner" do

        it "returns true" do
          my_collection.insert key: 'my_lock', owner: 'spence', expires_at: 1.minute.from_now
          expect(lock.available?).to be_true
        end

      end

      context "when the lock is already acquired" do

        it "returns false" do
          my_collection.insert key: 'my_lock', owner: 'tobie', expires_at: 1.minute.from_now
          expect(lock.available?).to be_false
        end

      end

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongo-lock-1.2.0 spec/examples/available_example.rb