Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry flaky secure password test #47649

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 26 additions & 12 deletions activerecord/test/cases/secure_password_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,24 @@ class SecurePasswordTest < ActiveRecord::TestCase
# Warm-up (mostly to ensure the DB connection is established)
User.authenticate_by(token: @user.token, password: @user.password)

# Benchmark.realtime returns fractional seconds. Thus, summing over 1000
# iterations is equivalent to averaging over 1000 iterations and then
# multiplying by 1000 to convert to milliseconds.
found_average_time_in_ms = 1000.times.sum do
Benchmark.realtime do
User.authenticate_by(token: @user.token, password: @user.password)
retry_flaky_test do
# Benchmark.realtime returns fractional seconds. Thus, summing over 1000
# iterations is equivalent to averaging over 1000 iterations and then
# multiplying by 1000 to convert to milliseconds.
found_average_time_in_ms = 1000.times.sum do
Benchmark.realtime do
User.authenticate_by(token: @user.token, password: @user.password)
end
end
end

not_found_average_time_in_ms = 1000.times.sum do
Benchmark.realtime do
User.authenticate_by(token: "wrong", password: @user.password)
not_found_average_time_in_ms = 1000.times.sum do
Benchmark.realtime do
User.authenticate_by(token: "wrong", password: @user.password)
end
end
end

assert_in_delta found_average_time_in_ms, not_found_average_time_in_ms, 0.5
assert_in_delta found_average_time_in_ms, not_found_average_time_in_ms, 0.5
end
end

test "authenticate_by short circuits when password is nil" do
Expand Down Expand Up @@ -91,4 +93,16 @@ class SecurePasswordTest < ActiveRecord::TestCase
assert_nil User.authenticate_by(params)
end
end

private
def retry_flaky_test(retry_count: 3)
yield
rescue Minitest::Assertion
if retry_count > 0
retry_count -= 1
retry
else
raise
end
end
end