Skip to content

Commit

Permalink
Add spec for BatchBuilder contention
Browse files Browse the repository at this point in the history
Previously we had an issue where BatchBuilder would create the wrong
number of promotion codes if it encountered a conflict. Out specs didn't
catch this.

This commit adds an additional spec which runs the BatchBuilder with
settings that make it extremely likely that there will a conflict: from
a space of 100 possible codes (00 to 99) we generate 50.

An online birthday problem calculator tells me this is 99.99997%
likely that the generator will encounter at least one conflict. That
seems plenty. I haven't verified this, but intuitively it seems very
likely.
  • Loading branch information
jhawthorn committed Feb 16, 2018
1 parent 8ee0cb3 commit 17f0ef0
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion core/spec/models/spree/promotion_code/batch_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
let(:promotion) { create(:promotion) }
let(:base_code) { "abc" }
let(:options) { {} }
let(:number_of_codes) { 10 }
let(:promotion_code_batch) do
Spree::PromotionCodeBatch.create!(
promotion_id: promotion.id,
base_code: base_code,
number_of_codes: 10,
number_of_codes: number_of_codes,
email: "test@email.com"
)
end
Expand Down Expand Up @@ -54,6 +55,22 @@
expect(promotion_code_batch.state).to eq("completed")
end
end

context "with likely code contention" do
let(:number_of_codes) { 50 }
let(:options) do
{
batch_size: 10,
sample_characters: (0..9).to_a.map(&:to_s),
random_code_length: 2
}
end

it "creates the correct number of codes" do
subject.build_promotion_codes
expect(promotion.codes.size).to eq(number_of_codes)
end
end
end

describe "#join_character" do
Expand Down

0 comments on commit 17f0ef0

Please sign in to comment.