Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor test to use multi database syntax. model.connection s… #95

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.PHONY: test-sqlite test-pg test-mysql

test-sqlite:
appraisal rake test

test-pg:
docker compose up -d pg
sleep 10 # give some time for the service to start
DATABASE_URL=postgres://with_advisory:with_advisory_pass@localhost/with_advisory_lock_test appraisal rake test

test-mysql:
docker compose up -d mysql
sleep 10 # give some time for the service to start
DATABASE_URL=mysql2://with_advisory:[email protected]:3306/with_advisory_lock_test appraisal rake test


test: test-sqlite test-pg test-mysql
20 changes: 20 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: "3.9"
services:
pg:
image: postgres:16
environment:
POSTGRES_USER: with_advisory
POSTGRES_PASSWORD: with_advisory_pass
POSTGRES_DB: with_advisory_lock_test
ports:
- "5432:5432"
mysql:
image: mysql:8
environment:
MYSQL_USER: with_advisory
MYSQL_PASSWORD: with_advisory_pass
MYSQL_DATABASE: with_advisory_lock_test
MYSQL_RANDOM_ROOT_PASSWORD: "yes"
MYSQL_ROOT_HOST: '%'
ports:
- "3306:3306"
5 changes: 3 additions & 2 deletions lib/with_advisory_lock/database_adapter_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
module WithAdvisoryLock
class DatabaseAdapterSupport
attr_reader :adapter_name

def initialize(connection)
@connection = connection
@adapter_name = connection.adapter_name.downcase.to_sym
@adapter_name = connection.adapter_name.downcase.to_sym
end

def mysql?
Expand All @@ -17,7 +18,7 @@ def postgresql?
end

def sqlite?
[:sqlite3, :sqlite].include? adapter_name
%i[sqlite3 sqlite].include? adapter_name
end
end
end
7 changes: 4 additions & 3 deletions lib/with_advisory_lock/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class MySQL < Base
# Caches nested lock support by MySQL reported version
@@mysql_nl_cache = {}
@@mysql_nl_cache_mutex = Mutex.new
# See https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_get-lock
# See https://dev.mysql.com/doc/refman/5.7/en/locking-functions.html
# See https://dev.mysql.com/doc/refman/8.0/en/locking-functions.html
def try_lock
raise ArgumentError, 'shared locks are not supported on MySQL' if shared
raise ArgumentError, 'transaction level locks are not supported on MySQL' if transaction
Expand All @@ -18,8 +19,8 @@ def release_lock
end

def execute_successful?(mysql_function)
sql = "SELECT #{mysql_function} AS #{unique_column_name}"
connection.select_value(sql).to_i.positive?
sql = "SELECT #{mysql_function}"
connection.query_value(sql) == 1
end

# MySQL wants a string as the lock key.
Expand Down
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
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/thread_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SeparateThreadTest < GemTestCase
@t1_return_value = nil

@t1 = Thread.new do
ApplicationRecord.connection_pool.with_connection do
Label.connection_pool.with_connection do
@t1_return_value = Label.with_advisory_lock(@lock_name) do
@mutex.synchronize { @t1_acquired_lock = true }
sleep
Expand All @@ -21,7 +21,7 @@ class SeparateThreadTest < GemTestCase

# Wait for the thread to acquire the lock:
sleep(0.1) until @mutex.synchronize { @t1_acquired_lock }
ApplicationRecord.connection.reconnect!
Label.connection.reconnect!
end

teardown do
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
Loading