From f81ed652f0b42bf53a4bd93dc02ee7ae6be98c33 Mon Sep 17 00:00:00 2001 From: Alan Guo Xiang Tan Date: Thu, 17 Aug 2023 12:53:40 +0800 Subject: [PATCH] DEV: Fix chromedriver binary errors when running system tests in parallel (#23122) What is the problem here? The `selenium-webdriver` gem is responsible for downloading the right version of the `chromedriver` binary and it downloads it into the `~/.cache/selenium` folder. THe problem here is that when a user runs `bin/turbo_rspec spec/system` for the first time, all of the processes will try to download the `chromedriver` binary to the same path at the same time and will lead to concurrency errors. What is the fix here? Before running any RSpec suite, we first check if the `.cache/selenium` folder is present. If it is not present, we use a file system lock to download the `chromedriver` binary such that other processes that runs after will not need to install the `chromedriver` binary. The long term fix here is to get `selenium-manager` to download the `chromedriver` binary to a unique path for each process but the `--cache-path` option for `selenium-manager` is currently not supported in `selenium-webdriver`. --- spec/rails_helper.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index ae2d3109561..a625b29f289 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -230,6 +230,21 @@ RSpec.configure do |config| raise "There are pending migrations, run RAILS_ENV=test bin/rake db:migrate" end + # Use a file system lock to get `selenium-manager` to download the `chromedriver` binary that is requried for + # system tests to support running system tests in multiple processes. If we don't download the `chromedriver` binary + # before running system tests in multiple processes, each process will end up calling the `selenium-manager` binary + # to download the `chromedriver` binary at the same time but the problem is that the binary is being downloaded to + # the same location and this can interfere with the running tests in another process. + # + # The long term fix here is to get `selenium-manager` to download the `chromedriver` binary to a unique path for each + # process but the `--cache-path` option for `selenium-manager` is currently not supported in `selenium-webdriver`. + if !File.directory?("~/.cache/selenium") + File.open("#{Rails.root}/tmp/chrome_driver_flock", "w") do |file| + file.flock(File::LOCK_EX) + `#{Selenium::WebDriver::SeleniumManager.send(:binary)} --browser chrome` + end + end + Sidekiq.error_handlers.clear # Ugly, but needed until we have a user creator