DEV: Use freeze_time_safe in more places (#25949)

Followup to 120a2f70a9,
uses new method to avoid time-based spec flakiness
This commit is contained in:
Martin Brennan 2024-03-01 10:07:35 +10:00 committed by GitHub
parent 8503d3afe1
commit 6bcbe56116
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
21 changed files with 36 additions and 31 deletions

View File

@ -55,7 +55,6 @@ RSpec.describe Jobs::BulkInvite do
it "handles daylight savings time correctly" do
# EDT (-04:00) transitions to EST (-05:00) on the first Sunday in November.
# Freeze time to the last Day of October, so that the creation and expiration date will be in different time zones.
Time.use_zone("Eastern Time (US & Canada)") do
freeze_time DateTime.parse("2023-10-31 06:00:00 -0400")
described_class.new.execute(current_user_id: east_coast_user.id, invites: invites)

View File

@ -3,7 +3,7 @@
require_relative "shared_context_for_backup_restore"
RSpec.describe BackupRestore::BackupFileHandler, type: :multisite do
include_context "with shared stuff"
include_context "with shared backup restore context"
it "works with old backup file format" do
test_multisite_connection("second") do

View File

@ -3,7 +3,7 @@
require_relative "shared_context_for_backup_restore"
RSpec.describe BackupRestore::BackupFileHandler do
include_context "with shared stuff"
include_context "with shared backup restore context"
it "works with current backup file format" do
expect_decompress_and_clean_up_to_work(

View File

@ -5,7 +5,7 @@ require_relative "shared_context_for_backup_restore"
RSpec.describe BackupRestore::DatabaseRestorer, type: :multisite do
subject(:restorer) { BackupRestore::DatabaseRestorer.new(logger, current_db) }
include_context "with shared stuff"
include_context "with shared backup restore context"
let(:current_db) { RailsMultisite::ConnectionManagement.current_db }

View File

@ -5,7 +5,7 @@ require_relative "shared_context_for_backup_restore"
RSpec.describe BackupRestore::DatabaseRestorer do
subject(:restorer) { BackupRestore::DatabaseRestorer.new(logger, current_db) }
include_context "with shared stuff"
include_context "with shared backup restore context"
let(:current_db) { RailsMultisite::ConnectionManagement.current_db }

View File

@ -3,7 +3,7 @@
require_relative "shared_context_for_backup_restore"
RSpec.describe BackupRestore::MetaDataHandler do
include_context "with shared stuff"
include_context "with shared backup restore context"
let!(:backup_filename) { "discourse-2019-11-18-143242-v20191108000414.tar.gz" }

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true
RSpec.shared_context "with shared stuff" do
RSpec.shared_context "with shared backup restore context" do
let!(:logger) do
Class
.new do

View File

@ -5,7 +5,7 @@ require_relative "shared_context_for_backup_restore"
RSpec.describe BackupRestore::SystemInterface, type: :multisite do
subject(:system_interface) { BackupRestore::SystemInterface.new(logger) }
include_context "with shared stuff"
include_context "with shared backup restore context"
describe "#flush_redis" do
it "removes only keys from the current site in a multisite" do

View File

@ -5,7 +5,7 @@ require_relative "shared_context_for_backup_restore"
RSpec.describe BackupRestore::SystemInterface do
subject(:system_interface) { BackupRestore::SystemInterface.new(logger) }
include_context "with shared stuff"
include_context "with shared backup restore context"
describe "readonly mode" do
after { Discourse::READONLY_KEYS.each { |key| Discourse.redis.del(key) } }

View File

@ -6,7 +6,7 @@ require_relative "shared_context_for_backup_restore"
RSpec.describe BackupRestore::UploadsRestorer do
subject(:restorer) { BackupRestore::UploadsRestorer.new(logger) }
include_context "with shared stuff"
include_context "with shared backup restore context"
def with_temp_uploads_directory(name: "default", with_optimized: false)
Dir.mktmpdir do |directory|

View File

@ -260,7 +260,7 @@ RSpec.describe Middleware::RequestTracker do
# rate limiter tests depend on checks for retry-after
# they can be sensitive to clock skew during test runs
freeze_time DateTime.parse("2021-01-01 01:00")
freeze_time_safe
end
use_redis_snapshotting

View File

@ -680,8 +680,7 @@ RSpec.describe PostCreator do
fab!(:topic) { Fabricate(:topic, user: user) }
it "whispers do not mess up the public view" do
# turns out this can fail on leap years if we don't do this
freeze_time DateTime.parse("2010-01-01 12:00")
freeze_time_safe
first = PostCreator.new(user, topic_id: topic.id, raw: "this is the first post").create
@ -760,7 +759,7 @@ RSpec.describe PostCreator do
fab!(:topic) { Fabricate(:topic, user: user) }
it "silent do not mess up the public view" do
freeze_time DateTime.parse("2010-01-01 12:00")
freeze_time_safe
first = PostCreator.new(user, topic_id: topic.id, raw: "this is the first post").create

View File

@ -45,7 +45,7 @@ RSpec.describe DirectoryItem do
describe ".refresh!" do
before do
freeze_time DateTime.parse("2017-02-02 12:00")
freeze_time_safe
UserActionManager.enable
end

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
RSpec.describe IncomingLinksReport do
before { freeze_time DateTime.parse("2010-01-01 6:00") }
before { freeze_time_safe }
describe "integration" do
it "runs correctly" do

View File

@ -2202,7 +2202,7 @@ RSpec.describe Post do
describe "public_posts_count_per_day" do
before do
freeze_time DateTime.parse("2017-03-01 12:00")
freeze_time_safe
Fabricate(:post)
Fabricate(:post, created_at: 1.day.ago)

View File

@ -49,7 +49,7 @@ RSpec.describe Report do
subject(:json) { Report.find("http_total_reqs").as_json }
before do
freeze_time DateTime.parse("2017-03-01 12:00")
freeze_time_safe
# today, an incomplete day:
application_requests = [
@ -97,7 +97,7 @@ RSpec.describe Report do
describe "topics" do
before do
Report.clear_cache
freeze_time DateTime.parse("2017-03-01 12:00")
freeze_time_safe
user = Fabricate(:user)
topics =
((0..32).to_a + [60, 61, 62, 63]).map do |i|
@ -145,7 +145,7 @@ RSpec.describe Report do
let(:user) { Fabricate(:user) }
it "returns a report with data" do
freeze_time DateTime.parse("2000-01-01")
freeze_time_safe
user.user_visits.create(visited_at: 1.hour.from_now)
user.user_visits.create(visited_at: 1.day.ago)
user.user_visits.create(visited_at: 2.days.ago, mobile: true)
@ -169,7 +169,7 @@ RSpec.describe Report do
let(:user) { Fabricate(:user) }
it "returns a report with data" do
freeze_time DateTime.parse("2000-01-01")
freeze_time_safe
user.user_visits.create(visited_at: 1.hour.from_now)
user.user_visits.create(visited_at: 2.days.ago, mobile: true)
user.user_visits.create(visited_at: 45.days.ago)
@ -197,7 +197,7 @@ RSpec.describe Report do
context "with #{pluralized}" do
before(:each) do
freeze_time DateTime.parse("2017-03-01 12:00")
freeze_time_safe
if arg == :flag
user = Fabricate(:user, refresh_auto_groups: true)
@ -262,7 +262,7 @@ RSpec.describe Report do
context "with #{request_type}" do
before do
freeze_time DateTime.parse("2017-03-01 12:00")
freeze_time_safe
application_requests = [
{
date: 35.days.ago.to_time,
@ -519,7 +519,7 @@ RSpec.describe Report do
context "with different users/visits" do
before do
freeze_time DateTime.parse("2017-03-01 12:00")
freeze_time_safe
arpit = Fabricate(:user)
arpit.user_visits.create(visited_at: 1.day.ago)
@ -552,7 +552,7 @@ RSpec.describe Report do
context "with different activities" do
before do
freeze_time DateTime.parse("2017-03-01 12:00")
freeze_time_safe
UserActionManager.enable
@ -1146,7 +1146,7 @@ RSpec.describe Report do
context "with data" do
it "works" do
freeze_time DateTime.parse("2017-03-01 12:00")
freeze_time_safe
ip = [81, 2, 69, 142]

View File

@ -29,8 +29,7 @@ RSpec.describe TopicHotScore do
end
it "can correctly update like counts and post counts and account for activity" do
# freeze to specific date + time to avoid flakiness from leap years
freeze_time(DateTime.parse("2024-02-01 01:00"))
freeze_time_safe
TopicHotScore.create!(topic_id: -1, score: 0.0, recent_likes: 99, recent_posters: 0)

View File

@ -2519,7 +2519,7 @@ RSpec.describe Topic do
describe "#listable_count_per_day" do
before(:each) do
freeze_time DateTime.parse("2017-03-01 12:00")
freeze_time_safe
Fabricate(:topic)
Fabricate(:topic, created_at: 1.day.ago)

View File

@ -442,7 +442,7 @@ RSpec.describe User do
describe "#count_by_signup_date" do
before(:each) do
User.destroy_all
freeze_time DateTime.parse("2017-02-01 12:00")
freeze_time_safe
Fabricate(:user)
Fabricate(:user, created_at: 1.day.ago)
Fabricate(:user, created_at: 1.day.ago)

View File

@ -781,6 +781,14 @@ def set_cdn_url(cdn_url)
end
end
# Time.now can cause flaky tests, especially in cases like
# leap days. This method freezes time at a "safe" specific
# time (the Discourse 1.1 release date), so it will not be
# affected by further temporal disruptions.
def freeze_time_safe
freeze_time(DateTime.parse("2014-08-26 12:00:00"))
end
def freeze_time(now = Time.now)
time = now
datetime = now

View File

@ -977,7 +977,7 @@ RSpec.describe UserMerger do
end
it "merges user visits" do
freeze_time DateTime.parse("2010-01-01 12:00")
freeze_time_safe
UserVisit.create!(
user_id: source_user.id,