Skip to content

Commit ccbd3b2

Browse files
authored
feat: add #current_advisory_locks method (#111)
1 parent cc89cec commit ccbd3b2

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/with_advisory_lock/concern.rb

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def current_advisory_lock
3333
lock_stack_key && lock_stack_key[0]
3434
end
3535

36+
def current_advisory_locks
37+
WithAdvisoryLock::Base.lock_stack.map(&:name)
38+
end
39+
3640
private
3741

3842
def impl_class

test/with_advisory_lock/lock_test.rb

+31
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,35 @@ class LockTest < GemTestCase
7777

7878
assert_equal(expected, actual)
7979
end
80+
81+
test 'current_advisory_locks returns empty array outside an advisory lock request' do
82+
assert_equal([], Tag.current_advisory_locks)
83+
end
84+
85+
test 'current_advisory_locks returns an array with names of the acquired locks' do
86+
Tag.with_advisory_lock(@lock_name) do
87+
locks = Tag.current_advisory_locks
88+
assert_equal(1, locks.size)
89+
assert_match(/#{@lock_name}/, locks.first)
90+
end
91+
end
92+
93+
test 'current_advisory_locks returns array of all nested lock names' do
94+
first_lock = 'outer lock'
95+
second_lock = 'inner lock'
96+
97+
Tag.with_advisory_lock(first_lock) do
98+
Tag.with_advisory_lock(second_lock) do
99+
locks = Tag.current_advisory_locks
100+
assert_equal(2, locks.size)
101+
assert_match(/#{first_lock}/, locks.first)
102+
assert_match(/#{second_lock}/, locks.last)
103+
end
104+
105+
locks = Tag.current_advisory_locks
106+
assert_equal(1, locks.size)
107+
assert_match(/#{first_lock}/, locks.first)
108+
end
109+
assert_equal([], Tag.current_advisory_locks)
110+
end
80111
end

0 commit comments

Comments
 (0)