Skip to content

Commit

Permalink
Update to newer Minitest API to remove deprecation warnings (#49)
Browse files Browse the repository at this point in the history
* Update to kinda hideous new Minitest API to remove deprecation warnings

* Move to 'assert' syntax, as requested

* Update parantheses style on 'refute' to match Minitest style guide
  • Loading branch information
pond authored May 13, 2021
1 parent 478059f commit d7ac19d
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 104 deletions.
8 changes: 4 additions & 4 deletions test/concern_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

describe "with_advisory_lock.concern" do
it "adds with_advisory_lock to ActiveRecord classes" do
assert Tag.respond_to?(:with_advisory_lock)
assert(Tag.respond_to?(:with_advisory_lock))
end

it "adds with_advisory_lock to ActiveRecord instances" do
assert Label.new.respond_to?(:with_advisory_lock)
assert(Label.new.respond_to?(:with_advisory_lock))
end

it "adds advisory_lock_exists? to ActiveRecord classes" do
assert Tag.respond_to?(:advisory_lock_exists?)
assert(Tag.respond_to?(:advisory_lock_exists?))
end

it "adds advisory_lock_exists? to ActiveRecord classes" do
assert Label.new.respond_to?(:advisory_lock_exists?)
assert(Label.new.respond_to?(:advisory_lock_exists?))
end

end
16 changes: 9 additions & 7 deletions test/lock_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,45 @@

describe '.current_advisory_lock' do
it 'returns nil outside an advisory lock request' do
Tag.current_advisory_lock.must_be_nil
assert_nil(Tag.current_advisory_lock)
end

it 'returns the name of the last lock acquired' do
Tag.with_advisory_lock(lock_name) do
# The lock name may have a prefix if WITH_ADVISORY_LOCK_PREFIX env is set
Tag.current_advisory_lock.must_match(/#{lock_name}/)
assert_match(/#{lock_name}/, Tag.current_advisory_lock)
end
end

it 'can obtain a lock with a name that attempts to disrupt a SQL comment' do
dangerous_lock_name = 'test */ lock /*'
Tag.with_advisory_lock(dangerous_lock_name) do
Tag.current_advisory_lock.must_match(/#{Regexp.escape(dangerous_lock_name)}/)
assert_match(/#{Regexp.escape(dangerous_lock_name)}/, Tag.current_advisory_lock)
end

end
end

describe '.advisory_lock_exists?' do
it 'returns false for an unacquired lock' do
Tag.advisory_lock_exists?(lock_name).must_be_false
refute(Tag.advisory_lock_exists?(lock_name))
end

it 'returns the name of the last lock acquired' do
Tag.with_advisory_lock(lock_name) do
Tag.advisory_lock_exists?(lock_name).must_be_true
assert(Tag.advisory_lock_exists?(lock_name))
end
end
end

describe 'zero timeout_seconds' do
it 'attempts the lock exactly once with no timeout' do
expected = SecureRandom.base64
Tag.with_advisory_lock(lock_name, 0) do
actual = Tag.with_advisory_lock(lock_name, 0) do
expected
end.must_equal expected
end

assert_equal(expected, actual)
end
end
end
54 changes: 29 additions & 25 deletions test/nesting_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,81 +13,85 @@

it "doesn't request the same lock twice" do
impl = WithAdvisoryLock::Base.new(nil, nil, nil)
impl.lock_stack.must_be_empty
assert_empty(impl.lock_stack)
Tag.with_advisory_lock("first") do
impl.lock_stack.map(&:name).must_equal %w(first)
assert_equal(%w(first), impl.lock_stack.map(&:name))
# Even MySQL should be OK with this:
Tag.with_advisory_lock("first") do
impl.lock_stack.map(&:name).must_equal %w(first)
assert_equal(%w(first), impl.lock_stack.map(&:name))
end
impl.lock_stack.map(&:name).must_equal %w(first)
assert_equal(%w(first), impl.lock_stack.map(&:name))
end
impl.lock_stack.must_be_empty
assert_empty(impl.lock_stack)
end

it "raises errors with MySQL < 5.7.5 when acquiring nested lock" do
skip unless env_db == :mysql && ENV['MYSQL_VERSION'] != '5.7'
exc = proc {

exc = assert_raises(WithAdvisoryLock::NestedAdvisoryLockError) do
Tag.with_advisory_lock("first") do
Tag.with_advisory_lock("second") do
end
end
}.must_raise WithAdvisoryLock::NestedAdvisoryLockError
exc.lock_stack.map(&:name).must_equal %w(first)
end

assert_equal(%w(first), exc.lock_stack.map(&:name))
end

it "does not raise errors with MySQL < 5.7.5 when acquiring nested error force enabled" do
skip unless env_db == :mysql && ENV['MYSQL_VERSION'] != '5.7'
impl = WithAdvisoryLock::Base.new(nil, nil, nil)
impl.lock_stack.must_be_empty
assert_empty(impl.lock_stack)
Tag.with_advisory_lock("first", force_nested_lock_support: true) do
impl.lock_stack.map(&:name).must_equal %w(first)
assert_equal(%w(first), impl.lock_stack.map(&:name))
Tag.with_advisory_lock("second", force_nested_lock_support: true) do
impl.lock_stack.map(&:name).must_equal %w(first second)
assert_equal(%w(first second), impl.lock_stack.map(&:name))
Tag.with_advisory_lock("first", force_nested_lock_support: true) do
# Shouldn't ask for another lock:
impl.lock_stack.map(&:name).must_equal %w(first second)
assert_equal(%w(first second), impl.lock_stack.map(&:name))
Tag.with_advisory_lock("second", force_nested_lock_support: true) do
# Shouldn't ask for another lock:
impl.lock_stack.map(&:name).must_equal %w(first second)
assert_equal(%w(first second), impl.lock_stack.map(&:name))
end
end
end
impl.lock_stack.map(&:name).must_equal %w(first)
assert_equal(%w(first), impl.lock_stack.map(&:name))
end
impl.lock_stack.must_be_empty
assert_empty(impl.lock_stack)
end

it "supports nested advisory locks with !MySQL 5.6" do
skip if env_db == :mysql && ENV['MYSQL_VERSION'] != '5.7'
impl = WithAdvisoryLock::Base.new(nil, nil, nil)
impl.lock_stack.must_be_empty
assert_empty(impl.lock_stack)
Tag.with_advisory_lock("first") do
impl.lock_stack.map(&:name).must_equal %w(first)
assert_equal(%w(first), impl.lock_stack.map(&:name))
Tag.with_advisory_lock("second") do
impl.lock_stack.map(&:name).must_equal %w(first second)
assert_equal(%w(first second), impl.lock_stack.map(&:name))
Tag.with_advisory_lock("first") do
# Shouldn't ask for another lock:
impl.lock_stack.map(&:name).must_equal %w(first second)
assert_equal(%w(first second), impl.lock_stack.map(&:name))
Tag.with_advisory_lock("second") do
# Shouldn't ask for another lock:
impl.lock_stack.map(&:name).must_equal %w(first second)
assert_equal(%w(first second), impl.lock_stack.map(&:name))
end
end
end
impl.lock_stack.map(&:name).must_equal %w(first)
assert_equal(%w(first), impl.lock_stack.map(&:name))
end
impl.lock_stack.must_be_empty
assert_empty(impl.lock_stack)
end

it "raises with !MySQL 5.6 and nested error force disabled" do
skip unless env_db == :mysql && ENV['MYSQL_VERSION'] != '5.7'
exc = proc {

exc = assert_raises(WithAdvisoryLock::NestedAdvisoryLockError) do
Tag.with_advisory_lock("first", force_nested_lock_support: false) do
Tag.with_advisory_lock("second", force_nested_lock_support: false) do
end
end
}.must_raise WithAdvisoryLock::NestedAdvisoryLockError
exc.lock_stack.map(&:name).must_equal %w(first)
end

assert_equal(%w(first), exc.lock_stack.map(&:name))
end
end
46 changes: 23 additions & 23 deletions test/options_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,58 @@ def parse_options(options)

specify 'defaults (empty hash)' do
impl = parse_options({})
impl.timeout_seconds.must_be_nil
impl.shared.must_equal false
impl.transaction.must_equal false
assert_nil(impl.timeout_seconds)
refute(impl.shared)
refute(impl.transaction)
end

specify 'nil sets timeout to nil' do
impl = parse_options(nil)
impl.timeout_seconds.must_be_nil
impl.shared.must_equal false
impl.transaction.must_equal false
assert_nil(impl.timeout_seconds)
refute(impl.shared)
refute(impl.transaction)
end

specify 'integer sets timeout to value' do
impl = parse_options(42)
impl.timeout_seconds.must_equal 42
impl.shared.must_equal false
impl.transaction.must_equal false
assert_equal(42, impl.timeout_seconds)
refute(impl.shared)
refute(impl.transaction)
end

specify 'hash with invalid key errors' do
proc {
assert_raises(ArgumentError) do
parse_options(foo: 42)
}.must_raise ArgumentError
end
end

specify 'hash with timeout_seconds sets timeout to value' do
impl = parse_options(timeout_seconds: 123)
impl.timeout_seconds.must_equal 123
impl.shared.must_equal false
impl.transaction.must_equal false
assert_equal(123, impl.timeout_seconds)
refute(impl.shared)
refute(impl.transaction)
end

specify 'hash with shared option sets shared to true' do
impl = parse_options(shared: true)
impl.timeout_seconds.must_be_nil
impl.shared.must_equal true
impl.transaction.must_equal false
assert_nil(impl.timeout_seconds)
assert(impl.shared)
refute(impl.transaction)
end

specify 'hash with transaction option set transaction to true' do
impl = parse_options(transaction: true)
impl.timeout_seconds.must_be_nil
impl.shared.must_equal false
impl.transaction.must_equal true
assert_nil(impl.timeout_seconds)
refute(impl.shared)
assert(impl.transaction)
end

specify 'hash with multiple keys sets options' do
foo = mock
bar = mock
impl = parse_options(timeout_seconds: foo, shared: bar)
impl.timeout_seconds.must_equal foo
impl.shared.must_equal bar
impl.transaction.must_equal false
assert_equal(foo, impl.timeout_seconds)
assert_equal(bar, impl.shared)
refute(impl.transaction)
end
end
12 changes: 6 additions & 6 deletions test/parallelism_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ def run_workers
@use_advisory_lock = false
@iterations = 1
run_workers
Tag.all.size.must_be :>, @iterations # <- any duplicated rows will make me happy.
TagAudit.all.size.must_be :>, @iterations # <- any duplicated rows will make me happy.
Label.all.size.must_be :>, @iterations # <- any duplicated rows will make me happy.
assert_operator(Tag.all.size, :>, @iterations) # <- any duplicated rows will make me happy.
assert_operator(TagAudit.all.size, :>, @iterations) # <- any duplicated rows will make me happy.
assert_operator(Label.all.size, :>, @iterations) # <- any duplicated rows will make me happy.
end

it "doesn't create multiple duplicate rows with advisory locks" do
@use_advisory_lock = true
@iterations = 10
run_workers
Tag.all.size.must_equal @iterations # <- any duplicated rows will NOT make me happy.
TagAudit.all.size.must_equal @iterations # <- any duplicated rows will NOT make me happy.
Label.all.size.must_equal @iterations # <- any duplicated rows will NOT make me happy.
assert_equal(@iterations, Tag.all.size) # <- any duplicated rows will NOT make me happy.
assert_equal(@iterations, TagAudit.all.size) # <- any duplicated rows will NOT make me happy.
assert_equal(@iterations, Label.all.size) # <- any duplicated rows will NOT make me happy.
end
end
36 changes: 19 additions & 17 deletions test/shared_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ def work

it 'does not allow two exclusive locks' do
one = SharedTestWorker.new(false)
one.locked?.must_equal true
assert(one.locked?)

two = SharedTestWorker.new(false)
two.locked?.must_equal false
refute(two.locked?)

one.cleanup!
two.cleanup!
Expand All @@ -57,11 +57,13 @@ def work

it 'raises an error when attempting to use a shared lock' do
one = SharedTestWorker.new(true)
one.locked?.must_be_nil
exception = proc {
assert_nil(one.locked?)

exception = assert_raises(ArgumentError) do
one.cleanup!
}.must_raise ArgumentError
exception.message.must_include 'not supported'
end

assert_match(/#{Regexp.escape('not supported')}/, exception.message)
end
end

Expand All @@ -72,24 +74,24 @@ def work

it 'does allow two shared locks' do
one = SharedTestWorker.new(true)
one.locked?.must_equal true
assert(one.locked?)

two = SharedTestWorker.new(true)
two.locked?.must_equal true
assert(two.locked?)

one.cleanup!
two.cleanup!
end

it 'does not allow exclusive lock with shared lock' do
one = SharedTestWorker.new(true)
one.locked?.must_equal true
assert(one.locked?)

two = SharedTestWorker.new(false)
two.locked?.must_equal false
refute(two.locked?)

three = SharedTestWorker.new(true)
three.locked?.must_equal true
assert(three.locked?)

one.cleanup!
two.cleanup!
Expand All @@ -98,10 +100,10 @@ def work

it 'does not allow shared lock with exclusive lock' do
one = SharedTestWorker.new(false)
one.locked?.must_equal true
assert(one.locked?)

two = SharedTestWorker.new(true)
two.locked?.must_equal false
refute(two.locked?)

one.cleanup!
two.cleanup!
Expand All @@ -117,14 +119,14 @@ def pg_lock_modes
end

it 'allows shared lock to be upgraded to an exclusive lock' do
pg_lock_modes.must_equal %w[]
assert_equal(%w[], pg_lock_modes)
Tag.with_advisory_lock 'test', shared: true do
pg_lock_modes.must_equal %w[ShareLock]
assert_equal(%w[ShareLock], pg_lock_modes)
Tag.with_advisory_lock 'test', shared: false do
pg_lock_modes.must_equal %w[ShareLock ExclusiveLock]
assert_equal(%w[ShareLock ExclusiveLock], pg_lock_modes)
end
end
pg_lock_modes.must_equal %w[]
assert_equal(%w[], pg_lock_modes)
end
end
end
Expand Down
Binary file added test/sqlite.db
Binary file not shown.
Loading

0 comments on commit d7ac19d

Please sign in to comment.