Skip to content

Commit 618ef8a

Browse files
tests
1 parent 8ae7dd1 commit 618ef8a

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

lib/closure_tree/support.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ def initialize(model_class, options)
2727
:numeric_order => false
2828
}.merge(options)
2929
raise ArgumentError, "name_column can't be 'path'" if options[:name_column] == 'path'
30+
if !options[:with_advisory_lock] && options[:advisory_lock_timeout_seconds].present?
31+
raise ArgumentError, "advisory_lock_timeout_seconds cannot be provided when advisory lock is disabled"
32+
end
3033
if order_is_numeric?
3134
extend NumericOrderSupport.adapter_for_connection(connection)
3235
end

spec/closure_tree/parallel_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def run_workers(worker_class = FindOrCreateWorker)
103103

104104
it 'creates dupe roots without advisory locks' do
105105
# disable with_advisory_lock:
106-
allow(Tag).to receive(:with_advisory_lock) { |_lock_name, &block| block.call }
106+
allow(Tag).to receive(:with_advisory_lock!) { |_lock_name, &block| block.call }
107107
run_workers
108108
# duplication from at least one iteration:
109109
expect(Tag.where(name: @names).size).to be > @iterations

spec/closure_tree/support_spec.rb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
11
require 'spec_helper'
22

33
RSpec.describe ClosureTree::Support do
4-
let(:sut) { Tag._ct }
4+
let(:mock_model) { double('TagModel') }
5+
let(:options) { {} }
6+
let(:sut) { described_class.new(mock_model, options) }
7+
58
it 'passes through table names without prefix and suffix' do
69
expected = 'some_random_table_name'
710
expect(sut.remove_prefix_and_suffix(expected)).to eq(expected)
811
end
12+
913
it 'extracts through table name with prefix and suffix' do
1014
expected = 'some_random_table_name'
1115
tn = ActiveRecord::Base.table_name_prefix + expected + ActiveRecord::Base.table_name_suffix
1216
expect(sut.remove_prefix_and_suffix(tn)).to eq(expected)
1317
end
14-
end
18+
19+
[
20+
[true, 10, { timeout_seconds: 10 }],
21+
[true, nil, {}],
22+
[false, nil, {}]
23+
].each do |with_lock, timeout, expected_options|
24+
context "when with_advisory_lock is #{with_lock} and timeout is #{timeout}" do
25+
let(:options) { { with_advisory_lock: with_lock, advisory_lock_timeout_seconds: timeout } }
26+
27+
it "uses advisory lock with timeout options: #{expected_options}" do
28+
if with_lock
29+
expect(mock_model).to receive(:with_advisory_lock!).with(anything, expected_options).and_yield
30+
else
31+
expect(mock_model).not_to receive(:with_advisory_lock!)
32+
end
33+
34+
expect { |b| sut.with_advisory_lock!(&b) }.to yield_control
35+
end
36+
end
37+
end
38+
end

0 commit comments

Comments
 (0)