Skip to content

Commit c88d3ea

Browse files
authored
Merge pull request #6711 from NHSDigital/fix-testing-api-for-imms-search-job
Fix prematurely returning testing API (to hopefully fix flakey tests)
2 parents 3dfc186 + 44c2c52 commit c88d3ea

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
# frozen_string_literal: true
22

33
class API::Testing::VaccinationsSearchInNHSController < API::Testing::BaseController
4+
POLL_INTERVAL = 0.25
5+
POLL_TIMEOUT = 300
6+
47
def create
58
if params[:wait].present?
69
EnqueueVaccinationsSearchInNHSJob.perform_now
10+
wait_for_search_jobs_to_complete
711
render status: :ok
812
else
913
EnqueueVaccinationsSearchInNHSJob.perform_later
1014
render status: :accepted
1115
end
1216
end
17+
18+
private
19+
20+
# EnqueueVaccinationsSearchInNHSJob fans out to per-patient
21+
# SearchVaccinationRecordsInNHSJob jobs via perform_bulk. Poll
22+
# until Sidekiq workers have drained the queue so callers see
23+
# updated patient statuses when the response arrives.
24+
def wait_for_search_jobs_to_complete
25+
queue = Sidekiq::Queue.new("immunisations_api_search")
26+
deadline = Time.current + POLL_TIMEOUT
27+
28+
# rubocop:disable Style/ZeroLengthPredicate -- Sidekiq::Queue has no #empty?
29+
sleep POLL_INTERVAL until queue.size.zero? || Time.current > deadline
30+
# rubocop:enable Style/ZeroLengthPredicate
31+
end
1332
end

spec/requests/api/testing/vaccinations_search_in_nhs_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
context "with wait=true" do
1818
before do
1919
allow(EnqueueVaccinationsSearchInNHSJob).to receive(:perform_now)
20+
allow(Sidekiq::Queue).to receive(:new).with(
21+
"immunisations_api_search"
22+
).and_return(instance_double(Sidekiq::Queue, size: 0))
2023
end
2124

2225
it "runs the job synchronously and responds with ok" do

0 commit comments

Comments
 (0)