active_job.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. module ZammadActiveJobHelper
  3. delegate :enqueued_jobs, :performed_jobs, to: :queue_adapter
  4. def queue_adapter
  5. ::ActiveJob::Base.queue_adapter
  6. end
  7. def clear_jobs
  8. enqueued_jobs.clear
  9. performed_jobs.clear
  10. ActiveJobLock.destroy_all
  11. end
  12. end
  13. module ZammadActiveJobSystemHelper
  14. include ActiveJob::TestHelper
  15. alias original_perform_enqueued_jobs perform_enqueued_jobs
  16. def perform_enqueued_jobs(**kwargs, &block)
  17. ActiveJobLock.destroy_all
  18. original_perform_enqueued_jobs(**kwargs, &block)
  19. end
  20. end
  21. RSpec.configure do |config|
  22. activate_for = {
  23. type: :job, # actual Job examples
  24. performs_jobs: true, # examples performing Jobs
  25. }
  26. config.include ZammadActiveJobSystemHelper, performs_jobs: true, type: :system
  27. activate_for.each do |key, value|
  28. config.include ZammadActiveJobHelper, key => value
  29. config.include RSpec::Rails::JobExampleGroup, key => value
  30. config.around(:each, key => value) do |example|
  31. default_queue_adapter = ::ActiveJob::Base.queue_adapter
  32. ::ActiveJob::Base.queue_adapter = :test
  33. clear_jobs
  34. example.run
  35. ensure
  36. ::ActiveJob::Base.queue_adapter = default_queue_adapter
  37. end
  38. end
  39. # Workaround needed for behavior change introduced in Rails >= 5.2
  40. # see: https://github.com/rails/rails/issues/37270
  41. config.before do
  42. (ActiveJob::Base.descendants << ActiveJob::Base).each(&:disable_test_adapter)
  43. end
  44. end