Sha256: 90db0aa279f020bba7fa4e8c25190cace04d58086c02c16f269412f331232428

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

require 'fileutils'
require 'timeout'
root  = File.expand_path(File.join(File.dirname(__FILE__), '..'))
cmd   = File.join root, 'bin/cron_wrapper'

def remove_lock
  FileUtils.rm_f "tmp/locks/awesome_script.lock" rescue nil
end


describe "cron_wrapper" do
  
  before(:all) do
    FileUtils.mkdir_p "#{root}/tmp/locks"
    FileUtils.mkdir_p "#{root}/log"
    FileUtils.mkdir_p "#{root}/lib/cron"
    File.open("#{root}/lib/cron/awesome_script.rb", 'w') do |file|
      file. << 'sleep(1); puts "awesome"'
    end
  end
    
  before(:each) do
    @pid = nil
    remove_lock
  end
  
  after(:all) do
    FileUtils.rm_rf "#{root}/tmp"
    FileUtils.rm_rf "#{root}/log"
    FileUtils.rm_rf "#{root}/lib/cron"
  end
  
  
  it "should create a lock file in lock_dir if no lock file exists" do
    t = Thread.new { `#{cmd} awesome_script --wrap-verbose --wrap-skip-rails --wrap-root #{root} --wrap-log log/awesome_script.log` }
    Timeout::timeout(2) do
      begin
        @pid = File.read "#{root}/tmp/locks/awesome_script.lock"
      rescue
        retry
      end
    end.should_not raise_error TimeoutError
    
    @pid.nil?.should_not == true
    t.join
  end
  
  
  it "should remove the lock file in lock_dir after execution" do
    `#{cmd} awesome_script --wrap-skip-rails --wrap-root #{root} --wrap-log log/awesome_script.log`
    File.exists?("tmp/locks/awesome_script.lock").should == false
  end
  
  
  it "should have the pid of the currently executing thread in the lock file" do
    read_pid = nil
    @pid = Process.fork { `#{cmd} awesome_script --wrap-verbose --wrap-skip-rails --wrap-root #{root} --wrap-log log/awesome_script.log` }
    
    begin
      read_pid = File.read "#{root}/tmp/locks/awesome_script.lock"
    rescue
      retry
    end
    
    Process.wait
    read_pid.should == (@pid + 1).to_s
  end
  
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cron_wrapper-0.0.5 spec/cron_wrapper_spec.rb
cron_wrapper-0.0.4 spec/cron_wrapper_spec.rb