From 65a3fdbc571f594f9afbb2368d058b2da71c434b Mon Sep 17 00:00:00 2001 From: Krzysztof Kotlarek Date: Fri, 15 May 2020 09:24:07 +1000 Subject: [PATCH] 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!` --- spec/models/user_spec.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index bb27aa4af0a..637e2fd4181 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -873,6 +873,10 @@ describe User do SiteSetting.previous_visit_timeout_hours = 1 end + after do + Discourse.redis.del("user:#{user.id}:#{Time.zone.now.to_date}") + end + it "should act correctly" do expect(user.previous_visit_at).to eq(nil) @@ -903,6 +907,10 @@ describe User do let!(:first_visit_date) { Time.zone.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 expect(user.last_seen_at).to eq nil user.update_last_seen!(first_visit_date) @@ -976,7 +984,9 @@ describe User do describe 'with no previous values' 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 it "updates last_seen_at" do @@ -1305,6 +1315,9 @@ describe User do let!(:user) { Fabricate(:user) } let!(:now) { Time.zone.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 expect {