service_config_spec.rb 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe BackgroundServices::ServiceConfig do
  4. let(:sample_service_class) { BackgroundServices::Service::ProcessDelayedJobs }
  5. describe '#enabled?' do
  6. it 'enabled if not disabled' do
  7. instance = described_class.new(service: sample_service_class, disabled: false, workers: 0)
  8. expect(instance).to be_enabled
  9. end
  10. it 'not enabled if disabled' do
  11. instance = described_class.new(service: sample_service_class, disabled: true, workers: 0)
  12. expect(instance).not_to be_enabled
  13. end
  14. end
  15. describe '#start_as' do
  16. it 'fork when workers number is set' do
  17. instance = described_class.new(service: sample_service_class, disabled: false, workers: 123)
  18. expect(instance.start_as).to be(:fork)
  19. end
  20. it 'thread when workers not set' do
  21. instance = described_class.new(service: sample_service_class, disabled: false, workers: 0)
  22. expect(instance.start_as).to be(:thread)
  23. end
  24. end
  25. describe '#workers' do
  26. it 'returns number capped to max workers' do
  27. allow(sample_service_class).to receive(:max_workers).and_return(90)
  28. instance = described_class.new(service: sample_service_class, disabled: false, workers: 123)
  29. expect(instance.workers).to be(90)
  30. end
  31. end
  32. describe '.configuration_from_env' do
  33. it 'returns configurations for all known services' do
  34. configurations = described_class.configuration_from_env({})
  35. expect(configurations.map(&:service)).to contain_exactly(BackgroundServices::Service::ProcessScheduledJobs, BackgroundServices::Service::ProcessDelayedJobs)
  36. end
  37. it 'parses configuration for a service' do
  38. hash = { 'ZAMMAD_PROCESS_DELAYED_JOBS_WORKERS' => 12 }
  39. configurations = described_class.configuration_from_env(hash)
  40. single_config = configurations.find { |config| config.service == sample_service_class }
  41. expect(single_config.workers).to be(12)
  42. end
  43. end
  44. describe '.single_configuration_from_env' do
  45. def run(hash)
  46. described_class.single_configuration_from_env(sample_service_class, hash)
  47. end
  48. it 'takes disabled value when true' do
  49. hash = {
  50. 'ZAMMAD_PROCESS_DELAYED_JOBS_DISABLED' => true,
  51. }
  52. expect(run(hash).disabled).to be_truthy
  53. end
  54. it 'takes disabled value when false' do
  55. hash = {
  56. 'ZAMMAD_PROCESS_DELAYED_JOBS_DISABLED' => 0,
  57. }
  58. expect(run(hash).disabled).to be(false)
  59. end
  60. it 'missing disabled value taken as false' do
  61. expect(run({}).disabled).to be(false)
  62. end
  63. it 'takes workers count' do
  64. hash = {
  65. 'ZAMMAD_PROCESS_DELAYED_JOBS_WORKERS' => 12,
  66. }
  67. expect(run(hash).workers).to be(12)
  68. end
  69. it 'missing workers count taken as 0' do
  70. expect(run({}).workers).to be(0)
  71. end
  72. end
  73. end