Skip to content

Commit

Permalink
chore: refactor test to use multi database syntax. model.connection s…
Browse files Browse the repository at this point in the history
…hould be used instead of ActiveRecord::Base.connection
  • Loading branch information
seuros committed Feb 11, 2024
1 parent f818a18 commit 3967d4f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
4 changes: 1 addition & 3 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@

ENV['WITH_ADVISORY_LOCK_PREFIX'] ||= SecureRandom.hex

ActiveRecord::Base.establish_connection

def env_db
@env_db ||= ActiveRecord::Base.connection_db_config.adapter.to_sym
@env_db ||= ActiveRecord::Base.configurations.find_db_config(:default_env).adapter
end

ActiveRecord::Migration.verbose = false
Expand Down
2 changes: 2 additions & 0 deletions test/test_models.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

ActiveRecord::Base.establish_connection(:default_env)

ActiveRecord::Schema.define(version: 1) do
create_table 'tags', force: true do |t|
t.string 'name'
Expand Down
8 changes: 4 additions & 4 deletions test/with_advisory_lock/parallelism_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(name, use_advisory_lock)

def work_later
sleep
ApplicationRecord.connection_pool.with_connection do
Tag.connection_pool.with_connection do
if @use_advisory_lock
Tag.with_advisory_lock(@name) { work }
else
Expand Down Expand Up @@ -46,16 +46,16 @@ def run_workers
workers.each(&:join)
end
# Ensure we're still connected:
ApplicationRecord.connection_pool.connection
Tag.connection_pool.connection
end

setup do
ApplicationRecord.connection.reconnect!
Tag.connection.reconnect!
@workers = 10
end

test 'creates multiple duplicate rows without advisory locks' do
skip if %i[sqlite3 jdbcsqlite3].include?(env_db)
skip if is_sqlite3_adapter?
@use_advisory_lock = false
@iterations = 1
run_workers
Expand Down
21 changes: 12 additions & 9 deletions test/with_advisory_lock/shared_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def cleanup!
private

def work
ApplicationRecord.connection_pool.with_connection do
Tag.connection_pool.with_connection do |connection|
Tag.with_advisory_lock('test', timeout_seconds: 0, shared: @shared) do
@locked = true
sleep 0.01 until @cleanup
Expand All @@ -36,9 +36,6 @@ def work
end

class SharedLocksTest < GemTestCase
def supported?
%i[trilogy mysql2 jdbcmysql].exclude?(env_db)
end

test 'does not allow two exclusive locks' do
one = SharedTestWorker.new(false)
Expand All @@ -52,12 +49,14 @@ def supported?
end
end

class NotSupportedEnvironmentTest < SharedLocksTest
setup do
skip if supported?
class NotSupportedEnvironmentTest < GemTestCase
def supported?
!is_mysql_adapter?
end

test 'raises an error when attempting to use a shared lock' do
skip "Not supported" unless supported?

one = SharedTestWorker.new(true)
assert_nil(one.locked?)

Expand All @@ -69,7 +68,11 @@ class NotSupportedEnvironmentTest < SharedLocksTest
end
end

class SupportedEnvironmentTest < SharedLocksTest
class SupportedEnvironmentTest < GemTestCase
def supported?
!is_mysql_adapter?
end

setup do
skip unless supported?
end
Expand Down Expand Up @@ -113,7 +116,7 @@ class SupportedEnvironmentTest < SharedLocksTest

class PostgreSQLTest < SupportedEnvironmentTest
setup do
skip unless env_db == :postgresql
skip unless is_postgresql_adapter?
end

def pg_lock_modes
Expand Down
4 changes: 2 additions & 2 deletions test/with_advisory_lock/transaction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class TransactionScopingTest < GemTestCase
def supported?
%i[postgresql jdbcpostgresql].include?(env_db)
is_postgresql_adapter?
end

test 'raises an error when attempting to use transaction level locks if not supported' do
Expand All @@ -23,7 +23,7 @@ def supported?

class PostgresqlTest < TransactionScopingTest
setup do
skip unless env_db == :postgresql
skip unless is_postgresql_adapter?
@pg_lock_count = lambda do
ApplicationRecord.connection.select_value("SELECT COUNT(*) FROM pg_locks WHERE locktype = 'advisory';").to_i
end
Expand Down

0 comments on commit 3967d4f

Please sign in to comment.