cli_spec.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe BackgroundServices::Cli, ensure_threads_exited: true do
  4. context 'when invoking scripts/background-worker.rb via CLI' do
  5. context 'without arguments' do
  6. it 'shows a help screen' do
  7. expect { described_class.start([]) }.to output(%r{help \[COMMAND\]}).to_stdout
  8. end
  9. it 'returns success' do
  10. expect(described_class.start([])).to be_truthy
  11. end
  12. end
  13. context 'with wrong arguments' do
  14. it 'raises an error' do
  15. expect { ensure_block_keeps_running { described_class.start(['invalid-command-name']) } }.to raise_error(RuntimeError, 'Process tried to shut down unexpectedly.')
  16. end
  17. end
  18. context 'when running all services' do
  19. it 'starts scheduler correctly' do
  20. expect(ensure_block_keeps_running { described_class.start ['start'] }).to be(true)
  21. end
  22. end
  23. context 'when trying to run multiple instances' do
  24. it 'fails second instance' do
  25. thread = Thread.new { ensure_block_keeps_running { described_class.start ['start'] } }
  26. sleep 0.1
  27. expect { ensure_block_keeps_running { described_class.start ['start'] } }.to raise_error('Cannot start BackgroundServices, another process seems to be running.')
  28. thread.join
  29. end
  30. end
  31. end
  32. end