Sha256: 9306b8d84d3132128d2bebbb98347649b183fb92a7927e075be2bbb754ba5886

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

require File.join( File.dirname(__FILE__),  "spec_helper" )

include Updater

describe "Update Locking:" do
  
  class Foo
    include DataMapper::Resource
    
    property :id, Serial
    property :name, String
    
    def bar(*args)
      Foo.bar(:instance,*args)
    end
    
  end
  
  Foo.auto_migrate!
  
  before :each do
    @u = Update.immidiate(Foo,:bar,[])
    @w = Worker.new(:name=>"first", :quiet=>true)
  end
  
  it "An unlocked record should lock" do
    @u.lock(@w).should be_true
    @u.locked?.should be_true
    @u.locked_by.should == @w.name
  end
  
  it "A locked record should NOT lock" do
    @u.lock(@w).should be_true
    @u.lock(Worker.new(:quiet=>true)).should be_false
  end
  
  it "A record that failed to lock should not change" do
    @u.lock(@w).should be_true
    @u.lock(Worker.new(:quiet=>true)).should be_false
    @u.locked_by.should == @w.name
  end
  
  it "A record should report as locked if locked by the same worker twice" do
    @u.lock(@w).should be_true
    @u.lock(@w).should be_true
  end
  
  describe "#run_with_lock" do
    
    it "should run an unlocked record" do
      u = Update.immidiate(Foo,:bar,[:arg1,:arg2])
      Foo.should_receive(:bar).with(:arg1,:arg2)
      u.run_with_lock(@w).should be_true
    end
    
    it "should NOT run an already locked record" do
      u = Update.immidiate(Foo,:bar,[:arg1,:arg2])
      u.lock(Worker.new)
      Foo.should_not_receive(:bar)
      u.run_with_lock(@w).should be_nil
    end
    
    it "should return false if the update ran but there was an error" do
      u = Update.immidiate(Foo,:bar,[:arg1,:arg2])
      Foo.should_receive(:bar).with(:arg1,:arg2).and_raise(RuntimeError)
      u.run_with_lock(@w).should be_false
    end
    
  end
  
  it "#clear_locks should clear all locks from a worker" do
    @v = Update.immidiate(Foo,:bar,[:arg1,:arg2])
    @u.lock(@w)
    @v.lock(@w)
    @u.locked?.should be_true
    @w.clear_locks
    @u.reload.locked?.should be_false
    @v.reload.locked?.should be_false
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
updater-0.2.2 spec/lock_spec.rb
updater-0.2.1 spec/lock_spec.rb