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