app_version_spec.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe AppVersion, :aggregate_failures do
  4. describe '.get' do
  5. it 'returns the value of the "app_version" setting' do
  6. Setting.set('app_version', 'mytimestamp')
  7. expect(described_class.get).to eq('mytimestamp')
  8. end
  9. end
  10. describe '.trigger_browser_reload' do
  11. before do
  12. allow(Sessions).to receive(:broadcast)
  13. allow(Gql::Subscriptions::AppMaintenance).to receive(:trigger)
  14. end
  15. it 'updates the version and publishes it' do
  16. expect { described_class.trigger_browser_reload(AppVersion::MSG_CONFIG_CHANGED) }
  17. .to change(described_class, :get)
  18. expect(Sessions).to have_received(:broadcast)
  19. expect(Gql::Subscriptions::AppMaintenance).to have_received(:trigger)
  20. end
  21. end
  22. describe '.trigger_restart' do
  23. let(:auto_shutdown) { true }
  24. before do
  25. Setting.set('auto_shutdown', auto_shutdown)
  26. allow(described_class).to receive(:trigger_browser_reload)
  27. allow(described_class).to receive(:restart_required!)
  28. end
  29. context 'with auto_shutdown' do
  30. it 'triggers browser reload' do
  31. described_class.trigger_restart
  32. expect(described_class).to have_received(:trigger_browser_reload).with(AppVersion::MSG_RESTART_AUTO, anything)
  33. expect(described_class).to have_received(:restart_required!)
  34. end
  35. end
  36. context 'without auto_shutdown' do
  37. let(:auto_shutdown) { false }
  38. it 'triggers browser reload' do
  39. described_class.trigger_restart
  40. expect(described_class)
  41. .to have_received(:trigger_browser_reload).with(AppVersion::MSG_RESTART_MANUAL, anything)
  42. expect(described_class).not_to have_received(:restart_required!)
  43. end
  44. end
  45. end
  46. describe '.restart_required!' do
  47. it 'writes to Redis' do
  48. allow_any_instance_of(Redis).to receive(:set)
  49. described_class.send(:restart_required!, 'mytimestamp')
  50. expect(described_class.send(:redis))
  51. .to have_received(:set)
  52. .with(AppVersion::REDIS_RESTART_REQUIRED_KEY, 'mytimestamp', ex: AppVersion::REDIS_RESTART_REQUIRED_TTL)
  53. end
  54. end
  55. describe '.restart_required?' do
  56. before do
  57. allow_any_instance_of(Redis).to receive(:get).and_return(redis_value)
  58. end
  59. let(:known_value) { 'mytimestamp' }
  60. context 'when Redis has the same value' do
  61. let(:redis_value) { 'mytimestamp' }
  62. it 'returns false' do
  63. expect(described_class.send(:restart_required?, known_value)).to be(false)
  64. end
  65. end
  66. context 'when Redis has no value' do
  67. let(:redis_value) { nil }
  68. it 'returns false' do
  69. expect(described_class.send(:restart_required?, known_value)).to be(false)
  70. end
  71. end
  72. context 'when Redis has a different value' do
  73. let(:redis_value) { 'changed_value' }
  74. it 'returns true' do
  75. expect(described_class.send(:restart_required?, known_value)).to be(true)
  76. end
  77. end
  78. end
  79. describe '.start_maintenance_thread' do
  80. let(:auto_shutdown) { true }
  81. before do
  82. Setting.set('auto_shutdown', auto_shutdown)
  83. allow(described_class).to receive(:restart_required?)
  84. end
  85. context 'without auto_shutdown' do
  86. let(:auto_shutdown) { false }
  87. it 'does not start a thread' do
  88. expect(described_class.start_maintenance_thread(process_name: 'rspec')).to be_nil
  89. end
  90. end
  91. context 'with auto_shutdown' do
  92. it 'starts a thread and calls .restart_required?' do
  93. thread = described_class.start_maintenance_thread(process_name: 'rspec')
  94. expect(thread).to be_alive
  95. sleep 1 # wait for thread to start working
  96. expect(described_class)
  97. .to have_received(:restart_required?)
  98. .with(Setting.get('app_version'))
  99. ensure
  100. thread.kill
  101. thread.join
  102. end
  103. end
  104. end
  105. end