search_index_associations_job_spec.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe SearchIndexAssociationsJob, performs_jobs: true, searchindex: true, type: :job do
  4. let(:organization) { create(:organization) }
  5. let(:user) { create(:user, organization: organization) }
  6. before do
  7. user
  8. searchindex_model_reload([User, Organization])
  9. perform_enqueued_jobs
  10. end
  11. it 'does not enqueue new jobs if not needed', :aggregate_failures do
  12. organization.update(name: SecureRandom.uuid)
  13. expect(described_class).to have_been_enqueued
  14. # first run will have updated the organization
  15. # and so it will requeue the job
  16. perform_enqueued_jobs
  17. expect(described_class).to have_been_enqueued
  18. # second job should not requeue again
  19. allow(SearchIndexBackend).to receive(:update_by_query).and_return({ 'total' => 0 }) # fake it because of unstable version conflicts in ES
  20. perform_enqueued_jobs
  21. expect(described_class).not_to have_been_enqueued
  22. end
  23. it 'does update objects until there are no conflicts or unprocessed left' do
  24. organization.update(name: SecureRandom.uuid)
  25. result = false
  26. 30.times do
  27. result = described_class.perform_now('Organization', organization.id)
  28. puts 'Waiting for elastic search to complete mass update...' # rubocop:disable Rails/Output
  29. break if result == true
  30. sleep 1
  31. end
  32. expect(result).to be(true)
  33. end
  34. end