Guest User

Untitled

a guest
Nov 7th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. describe '#sync' do
  2. let(:mirror) do
  3. OpenStruct.new(name: 'dummy_mirror',
  4. local_dir: tmp_dir,
  5. pre_cmd: true)
  6. end
  7.  
  8. before(:each) do
  9. @sync = Sync.new(mirror)
  10. end
  11.  
  12. after(:each) do
  13. File.delete(@sync.send(:lock_file)) if File.exist?(@sync.send(:lock_file))
  14. end
  15.  
  16. # subject(:sync) { Sync.new(mirror) }
  17.  
  18. context 'when establishing lock' do
  19. it 'should create a lock file' do
  20. allow(@sync).to receive(:sync) # Get rid of error messages
  21. @sync.run
  22. expect(File.exist?(@sync.send(:lock_file))).to be true
  23. end
  24.  
  25. context 'when trying to acquire a lock when a lock file already exists' do
  26. before(:each) do
  27. File.open(File.join(mirror.local_dir, "#{mirror.name}.lock"), File::RDWR | File::CREAT) do |f|
  28. f.write(5)
  29. f.flush
  30. end
  31. end
  32. it 'should delete and create a lock file if corresponding process for the lock file does not exist' do
  33. allow(Process).to receive(:kill).and_raise Errno::ESRCH
  34. allow(@sync).to receive(:sync) # so as not to continue processing the script
  35. expect(File).to receive(:delete).with(File.join(mirror.local_dir, "#{mirror.name}.lock")).twice # once for sync.run, twice for after(:each)
  36. expect(File.exist?(@sync.send(:lock_file))).to be true
  37. @sync.run
  38. end
  39. it 'should log an error and exit if app does not have privilege to send signal to the process' do
  40. allow(Process).to receive(:kill).and_raise Errno::EPERM
  41. allow(@sync).to receive(:exit).and_throw(:exit)
  42. expect(@sync).to receive(:log_error).with('No permission/privilege to send signal to process(5)')
  43. expect { @sync.run }.to throw_symbol :exit
  44. end
  45. it 'should log an error and exit if the corresponding process for the lock file is running' do
  46. allow(@sync).to receive(:exit).and_throw(:exit)
  47. expect(@sync).to receive(:log_error).with('Cannot establish lock. Another process(5) is currently running and holding the lock.')
  48. expect { @sync.run }.to throw_symbol :exit
  49. end
  50. end
  51. end
  52. end
Add Comment
Please, Sign In to add comment