Skip to content

A platform for community discussion. Free, open, simple.

DEV: Monkey patch `Selenium::WebDriver::Platform.localhost` to retry (#27335)

Alan Guo Xiang Tan9ff0805a1de5
diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb
index 1910fed89af..e6b48cb5eb0 100644
--- a/spec/rails_helper.rb
+++ b/spec/rails_helper.rb
@@ -519,29 +519,34 @@ RSpec.configure do |config|
end
end
- config.around do |example|
- example.run
-
- if example.exception.is_a?(Socket::ResolutionError)
- info = Socket.getaddrinfo("localhost", 80, Socket::AF_INET, Socket::SOCK_STREAM)
- etc_hosts = `cat /etc/hosts`
- resolve_conf = `cat /etc/resolv.conf`
- nsswitch = `cat /etc/nsswitch.conf`
-
- puts <<~MSG
- Failed to resolve localhost, available addresses: #{info}
-
- /etc/hosts:
- #{etc_hosts}
-
- /etc/resolv.conf:
- #{resolve_conf}
-
- /etc/nsswitch.conf:
- #{nsswitch}
- MSG
+ # This is a monkey patch for the `Selenium::WebDriver::Platform.localhost` method in `selenium-webdriver`. For some
+ # unknown reasons on Github Actions, we are seeing system tests failing intermittently with the error
+ # `Socket::ResolutionError: getaddrinfo: Temporary failure in name resolution` when `selenium-webdriver` tries to
+ # resolve `localhost` in a `Capybara#using_session` block.
+ #
+ # Too much time has been spent trying to debug this issue and the root cause is still unknown so we are just dropping
+ # this workaround for now.
+ module Selenium
+ module WebDriver
+ module Platform
+ def self.localhost_with_retry
+ attempts = 0
+
+ begin
+ localhost_without_retry
+ rescue Socket::ResolutionError
+ attempts += 1
+ attempts <= 3 ? retry : raise
+ end
+ end
+ end
end
end
+
+ Selenium::WebDriver::Platform.singleton_class.class_eval do
+ alias_method :localhost_without_retry, :localhost
+ alias_method :localhost, :localhost_with_retry
+ end
end
if ENV["DISCOURSE_RSPEC_PROFILE_EACH_EXAMPLE"]