aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2017-11-28 11:51:11 +0100
committerTeddy Wing2017-11-28 11:59:47 +0100
commit65c42c9f176cbaedb53e5c7a49ce4e3724b04fb8 (patch)
treefb6d824bd3baaa1dc5d2c1bfbf336ad77f19fb10
parentcd6e13d4ea86de054911a2c9bab0e733ba63095f (diff)
downloadchouette-core-65c42c9f176cbaedb53e5c7a49ce4e3724b04fb8.tar.bz2
Referential spec: Add spec that two similar Referentials being persisted
Here we formalise a bug whereby two "identical" `Referential`s are able to be saved even though Rails validation would normally prevent this. It can happen when two `Referential`s are functionally identical (but have different `slug`s, as this would trigger a Postgres error). In practise, this happens when multiple imports of the same data are launched at similar times. We'll want to make this test pass by not allowing the identical `Referential` to be created. This will be accomplished as agreed with the team by a database table lock. The threads and sleeps (in particular) are unfortunate necessities needed to get the two `Referential`s to be saved at the same time. TODO: Need to check this again with the threads & sleeps turned off to confirm that this passes in that case (actually it doesn't), so this test needs some modification in order to be correct. Refs #5024
-rw-r--r--spec/models/referential_spec.rb36
1 files changed, 36 insertions, 0 deletions
diff --git a/spec/models/referential_spec.rb b/spec/models/referential_spec.rb
index eed5467c1..d717511a1 100644
--- a/spec/models/referential_spec.rb
+++ b/spec/models/referential_spec.rb
@@ -126,4 +126,40 @@ describe Referential, :type => :model do
end
end
end
+
+ # two referentials created at the same time should not be possible when both have the same data
+ context "when two identical Referentials are created at the same time" do
+ # TODO: Rename js: true to no transaction something
+ it "only creates one Referential", js: true do
+ begin
+ referential_1 = build(:referential)
+ referential_2 = referential_1.dup
+ referential_2.slug = "#{referential_1.slug}_different"
+
+ thread_1 = Thread.new do
+ ActiveRecord::Base.transaction do
+ referential_1.save
+ sleep 10
+ end
+ end
+
+ thread_2 = Thread.new do
+ sleep 5
+ ActiveRecord::Base.transaction do
+ referential_2.save
+ end
+ end
+
+ thread_1.join
+ thread_2.join
+
+ expect(referential_1).to be_persisted
+ expect(referential_2).not_to be_persisted
+
+ ensure
+ Apartment::Tenant.drop(referential_1.slug)
+ Apartment::Tenant.drop(referential_2.slug)
+ end
+ end
+ end
end