Skip to content

Commit 81a4b14

Browse files
committedMar 19, 2025
tests
1 parent 8ae7dd1 commit 81a4b14

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed
 

‎lib/closure_tree/support.rb

+3
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

+1-1
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

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
11
require 'spec_helper'
22

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

0 commit comments

Comments
 (0)