FIX: randomly failing user_spec (#9754)

* FIX: randomly falling user_spec

When we evaluate `update_last_seen!` we relay on Redis to not run that code too often

https://github.com/discourse/discourse/blob/master/app/models/user.rb#L753

The problem is that not all specs which are running `update_last_seen!` are not cleaning after themselves

For examples specs in that block https://github.com/discourse/discourse/blob/master/spec/models/user_spec.rb#L901

So it can be replicated when you run a few times
`bundle exec rspec  ./spec/models/user_spec.rb -e "should not update the first seen value if it doesn't exist" -e "should have 0 for days_visited"`

We should delete Redis key after each spec which is evaluating `update_last_seen!`
This commit is contained in:
Krzysztof Kotlarek 2020-05-15 09:24:07 +10:00 committed by GitHub
parent 869f9b20a2
commit 65a3fdbc57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 1 deletions

View File

@ -873,6 +873,10 @@ describe User do
SiteSetting.previous_visit_timeout_hours = 1 SiteSetting.previous_visit_timeout_hours = 1
end end
after do
Discourse.redis.del("user:#{user.id}:#{Time.zone.now.to_date}")
end
it "should act correctly" do it "should act correctly" do
expect(user.previous_visit_at).to eq(nil) expect(user.previous_visit_at).to eq(nil)
@ -903,6 +907,10 @@ describe User do
let!(:first_visit_date) { Time.zone.now } let!(:first_visit_date) { Time.zone.now }
let!(:second_visit_date) { 2.hours.from_now } let!(:second_visit_date) { 2.hours.from_now }
after do
Discourse.redis.del("user:#{user.id}:#{Time.zone.now.to_date}")
end
it "should update the last seen value" do it "should update the last seen value" do
expect(user.last_seen_at).to eq nil expect(user.last_seen_at).to eq nil
user.update_last_seen!(first_visit_date) user.update_last_seen!(first_visit_date)
@ -976,7 +984,9 @@ describe User do
describe 'with no previous values' do describe 'with no previous values' do
after do after do
Discourse.redis.flushall Discourse.redis.del("user:#{user.id}:#{Time.zone.now.to_date}")
unfreeze_time
Discourse.redis.del("user:#{user.id}:#{Time.zone.now.to_date}")
end end
it "updates last_seen_at" do it "updates last_seen_at" do
@ -1305,6 +1315,9 @@ describe User do
let!(:user) { Fabricate(:user) } let!(:user) { Fabricate(:user) }
let!(:now) { Time.zone.now } let!(:now) { Time.zone.now }
before { user.update_last_seen!(now) } before { user.update_last_seen!(now) }
after do
Discourse.redis.del("user:#{user.id}:#{now.to_date}")
end
it "with existing UserVisit record, increments the posts_read value" do it "with existing UserVisit record, increments the posts_read value" do
expect { expect {