Specs enhancement: reusable websocket status check
This commit is contained in:
parent
ed830f61b2
commit
fa143749e4
2 changed files with 51 additions and 27 deletions
48
spec/support/capybara/ensure_websocket.rb
Normal file
48
spec/support/capybara/ensure_websocket.rb
Normal file
|
@ -0,0 +1,48 @@
|
|||
module EnsureWebsocket
|
||||
# Ensures that websocket is connectged
|
||||
#
|
||||
# @param timeout [Integer] seconds to wait
|
||||
# @param check_if_pinged [Boolean] checks if was pinged to prevent stale connections
|
||||
#
|
||||
# @yield block to execute between disruptive action (e.g. browser refresh) and action that requires websocket
|
||||
def ensure_websocket(timeout: 2.minutes, check_if_pinged: true)
|
||||
timestamp = Time.zone.now if check_if_pinged
|
||||
|
||||
yield if block_given?
|
||||
|
||||
wait(timeout).until do
|
||||
next if check_if_pinged && !pinged_since?(timestamp)
|
||||
|
||||
connection_open?
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Checks if session was active since given time
|
||||
#
|
||||
# @param timestamp [Time] when session was (re)activated
|
||||
# @return [Boolean]
|
||||
def pinged_since?(timestamp)
|
||||
unix_time = timestamp.to_i
|
||||
|
||||
Sessions
|
||||
.list
|
||||
.values
|
||||
.map { |elem| elem.dig(:meta, :last_ping) }
|
||||
.any? { |elem| elem >= unix_time }
|
||||
end
|
||||
|
||||
# Checks if websocket connection is active. Javascript function returns string identifier or empty string
|
||||
#
|
||||
# @return [Boolean]
|
||||
def connection_open?
|
||||
page
|
||||
.evaluate_script('App.WebSocket.channel()')
|
||||
.present?
|
||||
end
|
||||
end
|
||||
|
||||
RSpec.configure do |config|
|
||||
config.include EnsureWebsocket, type: :system
|
||||
end
|
|
@ -21,9 +21,7 @@ RSpec.describe 'Login Message', type: :system, authenticated: false do
|
|||
|
||||
Setting.set 'maintenance_login', false
|
||||
|
||||
wait(10).until_disappears { find '.js-maintenanceLogin', text: message, wait: false }
|
||||
|
||||
expect(page).not_to have_css('.js-maintenanceLogin')
|
||||
expect(page).to have_no_css('.js-maintenanceLogin', text: message, wait: 10)
|
||||
end
|
||||
|
||||
it 'changes message text on the go' do
|
||||
|
@ -31,9 +29,7 @@ RSpec.describe 'Login Message', type: :system, authenticated: false do
|
|||
|
||||
Setting.set 'maintenance_login_message', alt_message
|
||||
|
||||
wait(10).until_exists { find '.js-maintenanceLogin', text: alt_message, wait: false }
|
||||
|
||||
expect(page).to have_css('.js-maintenanceLogin', text: alt_message)
|
||||
expect(page).to have_no_css('.js-maintenanceLogin', text: alt_message, wait: 10)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -59,28 +55,8 @@ RSpec.describe 'Login Message', type: :system, authenticated: false do
|
|||
end
|
||||
|
||||
def open_login_page
|
||||
timestamp = Time.zone.now.to_i
|
||||
|
||||
visit '/'
|
||||
|
||||
wait(8).until do
|
||||
pinged_since?(timestamp) && connection_open?
|
||||
end
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
def pinged_since?(timestamp)
|
||||
Sessions
|
||||
.list
|
||||
.values
|
||||
.map { |elem| elem.dig(:meta, :last_ping) }
|
||||
.any? { |elem| elem >= timestamp }
|
||||
end
|
||||
|
||||
def connection_open?
|
||||
page
|
||||
.evaluate_script('App.WebSocket.channel()')
|
||||
.present?
|
||||
ensure_websocket
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue