A platform for community discussion. Free, open, simple.
DEV: Consistently wait for message_bus in system specs (#40076)
Jarek Radoszca8b84c052d7
diff --git a/spec/rails_helper.rb b/spec/rails_helper.rbindex 626a65e5d6a..575a18cd7c6 100644--- a/spec/rails_helper.rb+++ b/spec/rails_helper.rb@@ -475,31 +475,45 @@ RSpec.configure do |config|def synchronize(seconds = nil, errors: nil)return super if session.synchronized # Nested synchronize. We only want our logic on the outermost call.+ mb_behind = nil+beginsuperrescue StandardError => e- seconds = session_options.default_max_wait_time if [nil, true].include? seconds- if catch_error?(e, errors) && seconds != 0- # This error will only have been raised if the timer expired- timeout_error = CapybaraTimedOut.new(seconds, e)- if RSpec.current_example- # Store timeout for later, we'll only raise it if the test otherwise passes- RSpec.current_example.metadata[:_capybara_timeout_exception] ||= timeout_error-- if RSpec.current_example.metadata[:dump_threads_on_failure]- RSpec.current_example.metadata[:_capybara_server_threads_backtraces] = Thread- .list- .reduce([]) { |array, thread| array << thread.backtrace }- .uniq- end+ raise unless catch_error?(e, errors) && seconds != 0++ # On timeout, give a pending MessageBus publish one chance to land,+ # then retry the matcher once. Cap the retry's wait so the original+ # wait + flush + retry can't blow PER_SPEC_TIMEOUT_SECONDS.+ if mb_behind.nil? && MessageBusTestSync.pending?+ mb_behind = MessageBusTestSync.flush!(session, timeout: 2)+ seconds = 5+ retry+ end- raise # re-raise original error- else- # Outside an example... maybe a `before(:all)` hook?- raise timeout_error+ if mb_behind&.any?+ warn "[MessageBusTestSync] client never caught up on: #{mb_behind.inspect}"+ end++ # This error will only have been raised if the timer expired+ effective_seconds =+ [nil, true].include?(seconds) ? session_options.default_max_wait_time : seconds+ timeout_error = CapybaraTimedOut.new(effective_seconds, e)+ if RSpec.current_example+ # Store timeout for later, we'll only raise it if the test otherwise passes+ RSpec.current_example.metadata[:_capybara_timeout_exception] ||= timeout_error++ if RSpec.current_example.metadata[:dump_threads_on_failure]+ RSpec.current_example.metadata[:_capybara_server_threads_backtraces] = Thread+ .list+ .reduce([]) { |array, thread| array << thread.backtrace }+ .uniqend++ raise # re-raise original errorelse- raise+ # Outside an example... maybe a `before(:all)` hook?+ raise timeout_errorendendend@@ -514,6 +528,9 @@ RSpec.configure do |config|endend+ config.before(:each, type: :system) { MessageBusTestSync.start }+ config.after(:each, type: :system) { MessageBusTestSync.stop }+config.before(:each, type: :system) do |example|# Only set ENV["EMBER_RAISE_ON_DEPRECATION"] if not already setif ENV["EMBER_RAISE_ON_DEPRECATION"].nil?diff --git a/spec/support/message_bus_test_sync.rb b/spec/support/message_bus_test_sync.rbnew file mode 100644index 00000000000..8f920bcd61b--- /dev/null+++ b/spec/support/message_bus_test_sync.rb@@ -0,0 +1,77 @@+# frozen_string_literal: true++# Records MessageBus publishes in system tests and waits for the client+# to catch up, to prevent flakes in "publish then assert" scenarios.+module MessageBusTestSync+ MUTEX = Mutex.new+ CATCH_UP_SCRIPT = <<~JS+ const [pending, timeoutMs, done] = arguments;+ const deadline = Date.now() + timeoutMs;++ function poll() {+ const callbacks = window.MessageBus?.callbacks ?? [];+ const behind = [];++ for (const [channel, expectedId] of Object.entries(pending)) {+ const lastId = callbacks.find((c) => c.channel === channel)?.last_id;+ if (Number.isInteger(lastId) && lastId < expectedId) {+ behind.push({ channel, lastId, expectedId });+ }+ }++ if (behind.length === 0 || Date.now() >= deadline) {+ done(behind);+ } else {+ setTimeout(poll, 10);+ }+ }++ poll();+ JS++ @pending = nil++ def self.start+ MUTEX.synchronize { @pending = {} }+ end++ def self.stop+ MUTEX.synchronize { @pending = nil }+ end++ def self.pending?+ @pending&.any?+ end++ def self.record(channel, id)+ return if id.nil? || @pending.nil?+ MUTEX.synchronize do+ next if @pending.nil?+ @pending[channel] = id if id > (@pending[channel] || -1)+ end+ end++ # Waits until each recorded id is observed in the browser, or+ # `timeout` elapses. Channels with no `last_id` are skipped.+ def self.flush!(session, timeout:)+ snapshot =+ MUTEX.synchronize do+ return if @pending.nil? || @pending.empty?+ taken = @pending+ @pending = {}+ taken+ end++ session.evaluate_async_script(CATCH_UP_SCRIPT, snapshot, (timeout * 1000).to_i)+ end++ module PublishHook+ def publish(channel, data, opts = nil)+ id = super+ MessageBusTestSync.record(channel, id)+ id+ end+ end++ MessageBus::Implementation.prepend(PublishHook)+end