Sha256: e1f2259161fdad7a368503962e208e9475f88af6555bc67ce562c0b06c9a0044

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe "Utils::FlockMutex" do
  before(:each) do
    FileUtils.rm_f(".mylockfile")
  end

  context "(on instantiation)" do
    it "should not accept an empty initialiser" do
      lambda{Utils::FlockMutex.new}.should raise_error
    end

    it "should accept a filename as initialiser" do
      lambda{Utils::FlockMutex.new(".mylockfile")}.should_not raise_error
    end

    it "should create a specified file if not present" do
      lambda{Utils::FlockMutex.new(".mylockfile")}.should_not raise_error
      File.exists?(".mylockfile").should be_true
    end

    it "should not complain if the specified file is present" do
      FileUtils.touch(".mylockfile")
      lambda{Utils::FlockMutex.new(".mylockfile")}.should_not raise_error
    end

    it "should not complain if the specified file is present and is locked" do
      FileUtils.touch(".mylockfile")
      File.open('.mylockfile', 'w+') do |f|
        f.flock(File::LOCK_EX)
        begin 
          lambda{Utils::FlockMutex.new(".mylockfile")}.should_not raise_error
        ensure
          f.flock(File::LOCK_UN)
        end
      end
    end
  end
  
  context "(an instance)" do
    it "should allow locking (against another process) around a code block" do
      # TODO: this is a crap test. Rewrite (and split it up).
      lock = Utils::FlockMutex.new(".mylockfile")
      lock.synchronize do
        IO.popen("-","w+") do |io|
          File.open('.mylockfile', 'w+') do |f|
            f.flock(File::LOCK_EX | File::LOCK_NB)
          end
        end.should be_false
      end

      IO.popen("-","w+") do |io|
        File.open('.mylockfile', 'w+') do |f|
          f.flock(File::LOCK_EX | File::LOCK_NB)
        end
      end.should be_true
      
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kissifer-utils-0.0.1 spec/flock_mutex_spec.rb