Browse Source

Maintenance: Fix handling of retryable exceptions in capybara tests.

Martin Gruner 1 year ago
parent
commit
af6fa862e6
1 changed files with 14 additions and 8 deletions
  1. 14 8
      spec/support/capybara/websocket_server.rb

+ 14 - 8
spec/support/capybara/websocket_server.rb

@@ -10,7 +10,7 @@ RSpec.configure do |config|
 
       ensure_port_available!(port)
 
-      websocket_server = Thread.new do
+      ws_thread = Thread.new do
         WebsocketServer.run(
           p:           port,
           b:           '0.0.0.0',
@@ -26,18 +26,24 @@ RSpec.configure do |config|
     end
 
     example.run
+  rescue => e
+    # Handle any errors occuring within this hook, for example Net::ReadTimeout errors of the WS server.
+    #   Otherwise, they would not cause the retry to kick in, but abort the process.
+    example.example.set_exception(e)
+  ensure
+    stop_websocket_server(ws_thread) if server_required
+  end
 
-    next if !server_required
-
+  def stop_websocket_server(ws_thread)
     # returns immediately and thread may be still shutting down
-    EventMachine.stop_event_loop
+    EventMachine.stop_event_loop if ws_thread.status
 
     # give thread time to terminate
-    sleep 0.01 while websocket_server.status
+    sleep 0.01 while ws_thread.status
   rescue => e
-    # Handle any errors occuring within this hook, for example Net::ReadTimeout errors of the WS server.
-    #   Otherwise, they would not cause the retry to kick in, but abort the process.
-    example.example.set_exception(e)
+    Rails.logger.error "Error occurred during web socket server shutdown: #{e}"
+    $stderr.puts "Error occurred during web socket server shutdown: #{e}" # rubocop:disable Style/StderrPuts
+    # Ignore this error and continue, to allow for the rspec-retry mechanism to work.
   end
 
   def ensure_port_available!(port)