|
25 | 25 | assert ClosureTree::Support.new(model, { with_advisory_lock: true, advisory_lock_timeout_seconds: 10 }) |
26 | 26 | end |
27 | 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 | + |
28 | 35 | it 'calls :with_advisory_lock! when with_advisory_lock is true and timeout is 10' do |
29 | 36 | options = sut.options.merge(with_advisory_lock: true, advisory_lock_timeout_seconds: 10) |
| 37 | + received_lock_name = nil |
| 38 | + received_options = nil |
30 | 39 | called = false |
31 | 40 | sut.stub(:options, options) do |
32 | | - model.stub(:with_advisory_lock!, ->(_lock_name, _options, &block) { block.call }) 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 |
33 | 46 | sut.with_advisory_lock { called = true } |
34 | 47 | end |
35 | 48 | end |
36 | 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') |
37 | 52 | end |
38 | 53 |
|
39 | 54 | it 'calls :with_advisory_lock when with_advisory_lock is true and timeout is nil' do |
| 55 | + received_options = nil |
40 | 56 | called = false |
41 | | - model.stub(:with_advisory_lock, ->(_lock_name, _options, &block) { block.call }) do |
| 57 | + model.stub(:with_advisory_lock, ->(_lock_name, opts, &block) { |
| 58 | + received_options = opts |
| 59 | + block.call |
| 60 | + }) do |
42 | 61 | sut.with_advisory_lock { called = true } |
43 | 62 | end |
44 | 63 | assert called, 'block should have been called' |
| 64 | + assert_equal({}, received_options, 'options should be empty when timeout is not configured') |
45 | 65 | end |
46 | 66 |
|
47 | 67 | it 'does not call advisory lock methods when with_advisory_lock is false' do |
|
52 | 72 | end |
53 | 73 | assert called, 'block should have been called' |
54 | 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 |
55 | 101 | end |
0 commit comments