I have implemented Optimistic Locking for Race Condition. If lock_version
doesn't match with the updated lock_version
in the database, then it will trigger retry
three times. Can you suggest how to test this retry
event
#Product: Model's new field:
# lock_version :integer(4) default(0), not null
def recalculate
method_1
self.save!
end
private
def method_1
begin
####
####
if self.lock_version == Product.find(self.id).lock_version
Product.where(:id => self.id).update_all(attributes)
else
raise ActiveRecord::StaleObjectError.new(self, "test")
end
rescue ActiveRecord::StaleObjectError => e
if tries < 3
tries += 1
sleep(1 + tries)
self.reload
retry
else
raise Exception.new(timeout.inspect)
end
end
end
Rspec Unit Test:
it 'if car is updated then ActiveRecord::StaleObjectError should be raised' do
prod_v1 =Product.find(@prod.id)
prod_v2 = Car.find(@prod.id)
prod_v1.recalculate
prod_v1.reload # will make lock_version of prod_v1 to 1
prod_v2.recalculate # howvever lock_version of prod_v2 is still 0.
expect{car_v2.send(:method1)}.to receive(:retry)
end
Aucun commentaire:
Enregistrer un commentaire