Skip to content

Instantly share code, notes, and snippets.

@marzzz21
Created September 8, 2016 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marzzz21/56ed5cb9e98217dc87448160af9d2251 to your computer and use it in GitHub Desktop.
Save marzzz21/56ed5cb9e98217dc87448160af9d2251 to your computer and use it in GitHub Desktop.
#code
def write_pid
puts "writing pid"
begin
File.open(PID_FILE, ::File::CREAT | ::File::EXCL | ::File::WRONLY){|f| f.write("#{Process.pid}") }
at_exit { File.delete(PID_FILE) if File.exist?(PID_FILE) }
rescue Errno::EEXIST
check_status
retry
end
end
#test
it "should raise Errno::EEXIST if PID_FILE already exists" do
stub_const("MirrorSyncDaemon::PID_FILE", './testfile.pid')
File.new(MirrorSyncDaemon::PID_FILE, 'w+')
allow(mirrorsyncdaemon).to receive(:checkstatus) {}
expect(File).to receive(:open).with(MirrorSyncDaemon::PID_FILE, ::File::CREAT | ::Fil e::EXCL | ::File::WRONLY).and_raise(Errno::EEXIST)
mirrorsyncdaemon.write_pid
FileUtils.rm(MirrorSyncDaemon::PID_FILE) if File.exist?(MirrorSyncDaemon::PID_FILE)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment