|
3 | 3 | require 'test_helper' |
4 | 4 |
|
5 | 5 | describe ClosureTree::Support do |
6 | | - let(:sut) { Tag._ct } |
| 6 | + let(:model) { Tag } |
| 7 | + let(:sut) { model._ct } |
7 | 8 |
|
8 | 9 | it 'passes through table names without prefix and suffix' do |
9 | 10 | expected = 'some_random_table_name' |
|
15 | 16 | tn = ActiveRecord::Base.table_name_prefix + expected + ActiveRecord::Base.table_name_suffix |
16 | 17 | assert_equal expected, sut.remove_prefix_and_suffix(tn) |
17 | 18 | end |
| 19 | + |
| 20 | + it 'initializes without error when with_advisory_lock is false' do |
| 21 | + assert ClosureTree::Support.new(model, { with_advisory_lock: false }) |
| 22 | + end |
| 23 | + |
| 24 | + it 'initializes without error when with_advisory_lock is true and advisory_lock_timeout_seconds is set' do |
| 25 | + assert ClosureTree::Support.new(model, { with_advisory_lock: true, advisory_lock_timeout_seconds: 10 }) |
| 26 | + end |
| 27 | + |
| 28 | + it 'raises ArgumentError when advisory_lock_timeout_seconds is set but with_advisory_lock is false' do |
| 29 | + error = assert_raises(ArgumentError) do |
| 30 | + ClosureTree::Support.new(model, with_advisory_lock: false, advisory_lock_timeout_seconds: 10) |
| 31 | + end |
| 32 | + assert_match(/advisory_lock_timeout_seconds can't be specified when advisory_lock is disabled/, error.message) |
| 33 | + end |
| 34 | + |
| 35 | + it 'calls :with_advisory_lock! when with_advisory_lock is true and timeout is 10' do |
| 36 | + options = sut.options.merge(with_advisory_lock: true, advisory_lock_timeout_seconds: 10) |
| 37 | + received_lock_name = nil |
| 38 | + received_options = nil |
| 39 | + called = false |
| 40 | + sut.stub(:options, options) do |
| 41 | + model.stub(:with_advisory_lock!, ->(lock_name, opts, &block) { |
| 42 | + received_lock_name = lock_name |
| 43 | + received_options = opts |
| 44 | + block.call |
| 45 | + }) do |
| 46 | + sut.with_advisory_lock { called = true } |
| 47 | + end |
| 48 | + end |
| 49 | + assert called, 'block should have been called' |
| 50 | + assert_equal sut.advisory_lock_name, received_lock_name, 'lock name should be passed to with_advisory_lock!' |
| 51 | + assert_equal({ timeout_seconds: 10 }, received_options, 'options should include timeout_seconds when timeout is configured') |
| 52 | + end |
| 53 | + |
| 54 | + it 'calls :with_advisory_lock when with_advisory_lock is true and timeout is nil' do |
| 55 | + received_options = nil |
| 56 | + called = false |
| 57 | + model.stub(:with_advisory_lock, ->(_lock_name, opts, &block) { |
| 58 | + received_options = opts |
| 59 | + block.call |
| 60 | + }) do |
| 61 | + sut.with_advisory_lock { called = true } |
| 62 | + end |
| 63 | + assert called, 'block should have been called' |
| 64 | + assert_equal({}, received_options, 'options should be empty when timeout is not configured') |
| 65 | + end |
| 66 | + |
| 67 | + it 'does not call advisory lock methods when with_advisory_lock is false' do |
| 68 | + options = sut.options.merge(with_advisory_lock: false, advisory_lock_timeout_seconds: nil) |
| 69 | + called = false |
| 70 | + sut.stub(:options, options) do |
| 71 | + sut.with_advisory_lock { called = true } |
| 72 | + end |
| 73 | + assert called, 'block should have been called' |
| 74 | + end |
| 75 | + |
| 76 | + it 'raises WithAdvisoryLock::FailedToAcquireLock when lock cannot be acquired within timeout' do |
| 77 | + lock_held = false |
| 78 | + holder_thread = Thread.new do |
| 79 | + model.connection_pool.with_connection do |
| 80 | + model.with_advisory_lock(sut.advisory_lock_name) do |
| 81 | + lock_held = true |
| 82 | + sleep 2 |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + # Wait for holder to acquire the lock |
| 88 | + sleep 0.2 until lock_held |
| 89 | + |
| 90 | + support_with_timeout = ClosureTree::Support.new( |
| 91 | + model, |
| 92 | + sut.options.merge(with_advisory_lock: true, advisory_lock_timeout_seconds: 1) |
| 93 | + ) |
| 94 | + |
| 95 | + assert_raises(WithAdvisoryLock::FailedToAcquireLock) do |
| 96 | + support_with_timeout.with_advisory_lock { nil } |
| 97 | + end |
| 98 | + |
| 99 | + holder_thread.join |
| 100 | + end |
18 | 101 | end |
0 commit comments