Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Make three load- and order-dependent tests deterministic (#5425)

David Gil9344d65a834b
diff --git a/test/controllers/web_push_subscriptions_controller_test.rb b/test/controllers/web_push_subscriptions_controller_test.rb
index 5ea10f2ad5..2d0590e4ad 100644
--- a/test/controllers/web_push_subscriptions_controller_test.rb
+++ b/test/controllers/web_push_subscriptions_controller_test.rb
@@ -30,6 +30,10 @@ class WebPushSubscriptionsControllerTest < ActionDispatch::IntegrationTest
test "registers for the current session in self hosted mode without a backlog" do
Rails.application.config.stubs(:app_mode).returns("self_hosted".inquiry)
+ # Any request also auto-syncs a family whose last sync is not from today
+ # (AutoSync), which enqueues two jobs. The fixture sync is stamped when the
+ # fixtures load, so whether that fires depended on the clock.
+ @user.family.update!(auto_sync_on_login: false)
assert_no_enqueued_jobs do
assert_difference "WebPushSubscription.count", 1 do
post "/web_push_subscriptions", params: @attributes, as: :json
diff --git a/test/integration/push_delivery_concurrency_test.rb b/test/integration/push_delivery_concurrency_test.rb
index 7b3f860bf2..4823332948 100644
--- a/test/integration/push_delivery_concurrency_test.rb
+++ b/test/integration/push_delivery_concurrency_test.rb
@@ -36,15 +36,18 @@ class PushDeliveryConcurrencyTest < ActiveSupport::TestCase
errors << error
end
Timeout.timeout(10) { started.pop }
+ registrar_pid = Queue.new
registrar = Thread.new do
- ActiveRecord::Base.connection_pool.with_connection do
+ ActiveRecord::Base.connection_pool.with_connection do |connection|
+ registrar_pid << connection.select_value("SELECT pg_backend_pid()")
replacement = WebPushSubscription.register_for!(user: User.find(user.id), session: Session.find(new_session.id), **attributes)
end
rescue => error
errors << error
end
- assert registrar.join(3), "Registration must not wait for a provider response or take an inverse delivery lock"
+ assert_finishes_without_lock_wait registrar, Timeout.timeout(10) { registrar_pid.pop },
+ "Registration must not wait for a provider response or take an inverse delivery lock"
release << true
assert worker.join(10), "The late provider response must finish without deadlocking"
assert errors.empty?, errors.empty? ? nil : errors.pop.full_message
@@ -92,4 +95,20 @@ class PushDeliveryConcurrencyTest < ActiveSupport::TestCase
end
session&.destroy!
end
+
+ private
+ # The worker stays inside provider IO until `release`, so a registrar that
+ # depends on it cannot finish before then. Postgres reports a wait on the
+ # worker's locks as soon as it happens, whatever the machine's load; the
+ # deadline only keeps a registrar waiting in Ruby from hanging the suite.
+ def assert_finishes_without_lock_wait(thread, pid, message)
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + 10
+ until thread.join(0.01)
+ wait_event_type = ActiveRecord::Base.connection.select_value(
+ "SELECT wait_event_type FROM pg_stat_activity WHERE pid = #{Integer(pid)}"
+ )
+ flunk "#{message}: it is waiting on a database lock" if wait_event_type == "Lock"
+ flunk message if Process.clock_gettime(Process::CLOCK_MONOTONIC) > deadline
+ end
+ end
end
diff --git a/test/models/rule/condition_regex_test.rb b/test/models/rule/condition_regex_test.rb
index 25e24c6d7a..64d89ba0b0 100644
--- a/test/models/rule/condition_regex_test.rb
+++ b/test/models/rule/condition_regex_test.rb
@@ -21,11 +21,15 @@ class Rule::ConditionRegexTest < ActiveSupport::TestCase
"a pattern Postgres calls too complex" => [ "^(a{1,255}){1,255}(b)", :regex_invalid ],
"an over-long pattern" => [ "a" * 201, :regex_too_long ]
}.each do |description, (pattern, kind)|
+ # Postgres needs a few milliseconds of CPU to reject the too-complex pattern,
+ # and on a loaded machine the probe's time limit could fire first. A
+ # statement_timeout of 0 lifts the limit so each case gets Postgres' verdict;
+ # Rule::SafeRegexTest covers the limit.
test "a rule with #{description} is rejected and nothing is saved" do
rule = build_rule(value: pattern)
assert_no_difference [ -> { @family.rules.count }, -> { Rule::Condition.count } ] do
- assert_not rule.save
+ stub_const(Rule::SafeRegex, :PROBE_TIMEOUT_MS, 0) { assert_not rule.save }
end
assert rule.conditions.first.errors.of_kind?(:value, kind), rule.conditions.first.errors.details.inspect
diff --git a/test/models/rule/safe_regex_test.rb b/test/models/rule/safe_regex_test.rb
index 618eb31f28..1046f87c7e 100644
--- a/test/models/rule/safe_regex_test.rb
+++ b/test/models/rule/safe_regex_test.rb
@@ -44,12 +44,13 @@ class Rule::SafeRegexTest < ActiveSupport::TestCase
end
# Postgres needs a few milliseconds of CPU to give up on this pattern, so under
- # a loaded suite the probe's time limit could fire first. The limit is lifted
- # here: this test is about Postgres' verdict, the next one about the limit.
+ # a loaded suite the probe's time limit could fire first. A statement_timeout
+ # of 0 lifts the limit here: this test is about Postgres' verdict, the next
+ # one about the limit.
test "a pattern Postgres calls too complex is refused" do
- Rule::SafeRegex.stubs(:with_timeout).yields
-
- assert_equal :invalid, Rule::SafeRegex.error_for("^(a{1,255}){1,255}(b)")
+ stub_const(Rule::SafeRegex, :PROBE_TIMEOUT_MS, 0) do
+ assert_equal :invalid, Rule::SafeRegex.error_for("^(a{1,255}){1,255}(b)")
+ end
end
test "a pattern whose probe runs out of time is refused as too complex" do