From af631bcfc65ebb00a819dc4e3d3c10a439fabc54 Mon Sep 17 00:00:00 2001 From: aakashb95 Date: Mon, 12 Jan 2026 12:33:56 +0530 Subject: [PATCH] add: spec to test concurrent updates --- spec/enterprise/models/account_spec.rb | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/spec/enterprise/models/account_spec.rb b/spec/enterprise/models/account_spec.rb index 58cf127ab..8fbe3f75e 100644 --- a/spec/enterprise/models/account_spec.rb +++ b/spec/enterprise/models/account_spec.rb @@ -79,6 +79,34 @@ RSpec.describe Account, type: :model do expect(responses_limits[:current_available]).to eq captain_limits[:startups][:responses] - 1 end + it 'handles concurrent increments without losing updates' do + concurrent_calls = 50 + + ready = Concurrent::CountDownLatch.new(concurrent_calls) + start = Concurrent::CountDownLatch.new(1) + + threads = concurrent_calls.times.map do + Thread.new do + thread_count = Account.find(account.id) + ready.count_down + start.wait + + thread_count.increment_response_usage + end + end + ready.wait + + start.count_down + threads.each(&:join) + + account.reload + + expect(account.custom_attributes['captain_responses_usage']).to eq concurrent_calls + responses_limits = account.usage_limits[:captain][:responses] + expect(responses_limits[:consumed]).to eq concurrent_calls + expect(responses_limits[:current_available]).to eq captain_limits[:startups][:responses] - concurrent_calls + end + it 'reseting responses limits updates usage_limits' do account.custom_attributes['captain_responses_usage'] = 30 account.save!