service_config_spec.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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(
  36. BackgroundServices::Service::ManageSessionsJobs,
  37. BackgroundServices::Service::ProcessScheduledJobs,
  38. BackgroundServices::Service::ProcessSessionsJobs,
  39. BackgroundServices::Service::ProcessDelayedJobs
  40. )
  41. end
  42. it 'parses configuration for a service' do
  43. hash = { 'ZAMMAD_PROCESS_DELAYED_JOBS_WORKERS' => 12 }
  44. configurations = described_class.configuration_from_env(hash)
  45. single_config = configurations.find { |config| config.service == sample_service_class }
  46. expect(single_config.workers).to be(12)
  47. end
  48. it 'handles the deprecated setting ZAMMAD_SESSION_JOBS_CONCURRENT correctly', :aggregate_failures do
  49. allow(ActiveSupport::Deprecation).to receive(:warn)
  50. hash = { 'ZAMMAD_SESSION_JOBS_CONCURRENT' => 2 }
  51. configurations = described_class.configuration_from_env(hash)
  52. single_config = configurations.find { |config| config.service == BackgroundServices::Service::ProcessSessionsJobs }
  53. expect(single_config.workers).to be(2)
  54. expect(ActiveSupport::Deprecation).to have_received(:warn).once
  55. end
  56. end
  57. describe '.single_configuration_from_env' do
  58. def run(hash)
  59. described_class.single_configuration_from_env(sample_service_class, hash)
  60. end
  61. it 'takes disabled value when true' do
  62. hash = {
  63. 'ZAMMAD_PROCESS_DELAYED_JOBS_DISABLE' => true,
  64. }
  65. expect(run(hash).disabled).to be_truthy
  66. end
  67. it 'takes disabled value when false' do
  68. hash = {
  69. 'ZAMMAD_PROCESS_DELAYED_JOBS_DISABLE' => 0,
  70. }
  71. expect(run(hash).disabled).to be(false)
  72. end
  73. it 'missing disabled value taken as false' do
  74. expect(run({}).disabled).to be(false)
  75. end
  76. it 'takes workers count' do
  77. hash = {
  78. 'ZAMMAD_PROCESS_DELAYED_JOBS_WORKERS' => 12,
  79. }
  80. expect(run(hash).workers).to be(12)
  81. end
  82. it 'missing workers count taken as 0' do
  83. expect(run({}).workers).to be(0)
  84. end
  85. end
  86. end