active_job.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (C) 2012-2024 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, &)
  17. if kwargs[:commit_transaction]
  18. TransactionDispatcher.commit
  19. kwargs.delete :commit_transaction
  20. end
  21. ActiveJobLock.destroy_all
  22. original_perform_enqueued_jobs(**kwargs, &)
  23. end
  24. end
  25. RSpec.configure do |config|
  26. activate_for = {
  27. type: :job, # actual Job examples
  28. performs_jobs: true, # examples performing Jobs
  29. }
  30. config.include ZammadActiveJobSystemHelper, performs_jobs: true, type: :system
  31. activate_for.each do |key, value|
  32. config.include ZammadActiveJobHelper, key => value
  33. config.include RSpec::Rails::JobExampleGroup, key => value
  34. config.around(:each, key => value) do |example|
  35. default_queue_adapter = ActiveJob::Base.queue_adapter
  36. ActiveJob::Base.queue_adapter = :test
  37. clear_jobs
  38. example.run
  39. ensure
  40. ActiveJob::Base.queue_adapter = default_queue_adapter
  41. end
  42. end
  43. # Workaround needed for behavior change introduced in Rails >= 5.2
  44. # see: https://github.com/rails/rails/issues/37270
  45. config.before do
  46. (ActiveJob::Base.descendants << ActiveJob::Base).each(&:disable_test_adapter)
  47. end
  48. end