From 153ec268b7e0211151a96c10d2ec88888a809d88 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 30 Mar 2021 12:34:03 +0100 Subject: [PATCH] Remove RackRequestBlocker --- config/environments/test.rb | 5 -- lib/open_food_network/rack_request_blocker.rb | 76 ------------------- spec/spec_helper.rb | 1 - 3 files changed, 82 deletions(-) delete mode 100644 lib/open_food_network/rack_request_blocker.rb diff --git a/config/environments/test.rb b/config/environments/test.rb index dd68bc0459..5bdab24837 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -47,11 +47,6 @@ Openfoodnetwork::Application.configure do # Print deprecation notices to the stderr config.active_support.deprecation = :stderr - # To block requests before running the database cleaner - require 'open_food_network/rack_request_blocker' - # Make sure the middleware is inserted first in middleware chain - config.middleware.insert_before(ActionDispatch::Static, RackRequestBlocker) - config.active_job.queue_adapter = :test end diff --git a/lib/open_food_network/rack_request_blocker.rb b/lib/open_food_network/rack_request_blocker.rb deleted file mode 100644 index 485564b225..0000000000 --- a/lib/open_food_network/rack_request_blocker.rb +++ /dev/null @@ -1,76 +0,0 @@ -# Copied from http://blog.salsify.com/engineering/tearing-capybara-ajax-tests -# https://gist.github.com/jturkel/9317269/raw/ff7838684370fd8a468ffe1e5ce1f3e46ba39951/rack_request_blocker.rb - -require 'atomic' - -# Rack middleware that keeps track of the number of active requests and can block new requests. -class RackRequestBlocker - @@num_active_requests = Atomic.new(0) - @@block_requests = Atomic.new(false) - - # Returns the number of requests the server is currently processing. - def self.num_active_requests - @@num_active_requests.value - end - - # Prevents the server from accepting new requests. Any new requests will return an HTTP - # 503 status. - def self.block_requests! - @@block_requests.value = true - end - - # Allows the server to accept requests again. - def self.allow_requests! - @@block_requests.value = false - end - - def initialize(app) - @app = app - end - - def call(env) - increment_active_requests - if block_requests? - block_request(env) - else - @app.call(env) - end - ensure - decrement_active_requests - end - - def self.wait_for_requests_complete - block_requests! - max_wait_time = 30 - polling_interval = 0.01 - wait_until = Time.now.in_time_zone + max_wait_time.seconds - loop do - return if num_active_requests == 0 - if Time.now.in_time_zone > wait_until - raise "Failed waiting for completing requests, #{num_active_requests} running." - else - sleep(polling_interval) - end - end - ensure - allow_requests! - end - - private - - def block_requests? - @@block_requests.value - end - - def block_request(_env) - [503, {}, []] - end - - def increment_active_requests - @@num_active_requests.update { |v| v + 1 } - end - - def decrement_active_requests - @@num_active_requests.update { |v| v - 1 } - end -end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 39f1441214..f82289890e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -120,7 +120,6 @@ RSpec.configure do |config| config.after(:each) { DatabaseCleaner.clean } config.after(:each, js: true) do Capybara.reset_sessions! - RackRequestBlocker.wait_for_requests_complete end def restart_driver