background_services_spec.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. class SampleService
  4. def self.pre_run; end
  5. def run
  6. sleep 1
  7. end
  8. def self.service_name
  9. 'Sample'
  10. end
  11. def self.max_workers
  12. 1
  13. end
  14. end
  15. class ProcessService < SampleService
  16. def run
  17. f = File.new self.class.path, 'w'
  18. f.write 'run'
  19. f.close
  20. sleep 1
  21. end
  22. def self.path
  23. Rails.root.join('tmp/process.service')
  24. end
  25. end
  26. RSpec.describe BackgroundServices do
  27. let(:instance) { described_class.new(config) }
  28. let(:config) { [] }
  29. describe '.available_services' do
  30. it 'matches existing classes' do
  31. expect(described_class.available_services).to contain_exactly(BackgroundServices::Service::ProcessScheduledJobs, BackgroundServices::Service::ProcessDelayedJobs)
  32. end
  33. end
  34. describe '#run', ensure_threads_exited: true do
  35. let(:config) { described_class::ServiceConfig.new(service: SampleService, disabled: false, workers: 0) }
  36. it 'runs given services' do
  37. allow(instance).to receive(:run_service)
  38. ensure_block_keeps_running { instance.run }
  39. expect(instance).to have_received(:run_service).with(config)
  40. end
  41. context 'when config has multiple services' do
  42. let(:forked) { described_class::ServiceConfig.new(service: SampleService, disabled: false, workers: 3) }
  43. let(:threaded) { described_class::ServiceConfig.new(service: SampleService, disabled: false, workers: 0) }
  44. let(:config) { [threaded, forked] }
  45. it 'runs forked services before threaded', aggregate_failures: true do
  46. allow(instance).to receive(:run_service)
  47. ensure_block_keeps_running { instance.run }
  48. expect(instance).to have_received(:run_service).with(forked).ordered
  49. expect(instance).to have_received(:run_service).with(threaded).ordered
  50. end
  51. end
  52. end
  53. describe '#run_service' do
  54. let(:config) { described_class::ServiceConfig.new(service: SampleService, disabled: is_disabled, workers: workers_count) }
  55. let(:is_disabled) { false }
  56. before do
  57. allow(instance).to receive(:start_as_forks)
  58. allow(instance).to receive(:start_as_thread)
  59. end
  60. shared_examples 'stops early if disabled' do
  61. context 'when disabled' do
  62. let(:is_disabled) { true }
  63. it 'stops early if disabled', :aggregate_failures do
  64. allow(Rails.logger).to receive(:debug)
  65. instance.send(:run_service, config)
  66. expect(Rails.logger).to have_received(:debug).with(no_args) do |&block|
  67. expect(block.call).to match(%r{Skipping disabled service})
  68. end
  69. end
  70. end
  71. end
  72. shared_examples 'calls pre_run' do
  73. it 'calls pre_run' do
  74. allow(config.service).to receive(:pre_run)
  75. instance.send(:run_service, config)
  76. expect(config.service).to have_received(:pre_run)
  77. end
  78. end
  79. context 'when workers present' do
  80. let(:workers_count) { 1 }
  81. include_examples 'stops early if disabled'
  82. include_examples 'calls pre_run'
  83. it 'starts as fork' do
  84. instance.send(:run_service, config)
  85. expect(instance).to have_received(:start_as_forks).with(config.service, config.workers)
  86. end
  87. end
  88. context 'when workers not present' do
  89. let(:workers_count) { 0 }
  90. include_examples 'stops early if disabled'
  91. include_examples 'calls pre_run'
  92. it 'starts as thread' do
  93. instance.send(:run_service, config)
  94. expect(instance).to have_received(:start_as_thread).with(config.service)
  95. end
  96. end
  97. end
  98. describe '#start_as_forks' do
  99. context 'with a file check' do
  100. after do
  101. File.delete ProcessService.path
  102. end
  103. it 'runs Service#run' do
  104. instance.send(:start_as_forks, ProcessService, 1)
  105. sleep 0.1 until File.exist? ProcessService.path
  106. expect(File.read(ProcessService.path)).to eq('run')
  107. end
  108. end
  109. it 'forks a new process' do
  110. process_pids = instance.send(:start_as_forks, SampleService, 1)
  111. Process.wait process_pids.first
  112. expect($CHILD_STATUS).to be_success
  113. end
  114. end
  115. describe '#start_as_thread', ensure_threads_exited: true do
  116. let(:config) { described_class::ServiceConfig.new(service: SampleService, disabled: false, workers: 0) }
  117. context 'with logging' do
  118. let(:log) { [] }
  119. before do
  120. allow_any_instance_of(SampleService).to receive(:run) do
  121. log << :run_called
  122. end
  123. end
  124. it 'runs Service#run' do
  125. instance.send(:start_as_thread, SampleService)
  126. sleep 0.1 until log.any?
  127. expect(log).to be_present
  128. end
  129. end
  130. it 'starts a new thread' do
  131. thread = instance.send(:start_as_thread, SampleService)
  132. expect(thread).to be_alive
  133. end
  134. end
  135. end