Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIX: Upsert topic votes count atomically (#71)
The `Topic#update_vote_count` method should be concurrency-safe to prevent unique constraint violation errors when multiple processes try to insert a topic_votes_count record for the same topic simultaneously. Signed-off-by: OsamaSayegh <asooomaasoooma90@gmail.com>
- Loading branch information
1 parent
148d76f
commit 8dbc75f
Showing
5 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
describe DiscourseVoting::TopicExtension do | ||
let(:user) { Fabricate(:user) } | ||
let(:user2) { Fabricate(:user) } | ||
|
||
let(:topic) { Fabricate(:topic) } | ||
let(:topic2) { Fabricate(:topic) } | ||
|
||
before do | ||
SiteSetting.voting_enabled = true | ||
SiteSetting.voting_show_who_voted = true | ||
end | ||
|
||
describe '#update_vote_count' do | ||
it 'upserts topic votes count' do | ||
topic.update_vote_count | ||
topic2.update_vote_count | ||
|
||
expect(topic.reload.topic_vote_count.votes_count).to eq(0) | ||
expect(topic2.reload.topic_vote_count.votes_count).to eq(0) | ||
|
||
DiscourseVoting::Vote.create!(user: user, topic: topic) | ||
topic.update_vote_count | ||
topic2.update_vote_count | ||
|
||
expect(topic.reload.topic_vote_count.votes_count).to eq(1) | ||
expect(topic2.reload.topic_vote_count.votes_count).to eq(0) | ||
|
||
DiscourseVoting::Vote.create!(user: user2, topic: topic) | ||
DiscourseVoting::Vote.create!(user: user, topic: topic2) | ||
topic.update_vote_count | ||
topic2.update_vote_count | ||
|
||
expect(topic.reload.topic_vote_count.votes_count).to eq(2) | ||
expect(topic2.reload.topic_vote_count.votes_count).to eq(1) | ||
end | ||
end | ||
end |