File tree 6 files changed +52
-13
lines changed
6 files changed +52
-13
lines changed Original file line number Diff line number Diff line change
1
+ .PHONY : test-pg test-mysql
2
+
3
+ test-pg :
4
+ docker compose up -d pg
5
+ sleep 10 # give some time for the service to start
6
+ DATABASE_URL=postgres://with_advisory:with_advisory_pass@localhost/with_advisory_lock_test appraisal rake test
7
+
8
+ test-mysql :
9
+ docker compose up -d mysql
10
+ sleep 10 # give some time for the service to start
11
+ DATABASE_URL=mysql2://with_advisory:
[email protected] :3306/with_advisory_lock_test appraisal rake
test
12
+
13
+
14
+ test : test-pg test-mysql
Original file line number Diff line number Diff line change
1
+ version : " 3.9"
2
+ services :
3
+ pg :
4
+ image : postgres:16
5
+ environment :
6
+ POSTGRES_USER : with_advisory
7
+ POSTGRES_PASSWORD : with_advisory_pass
8
+ POSTGRES_DB : with_advisory_lock_test
9
+ ports :
10
+ - " 5432:5432"
11
+ mysql :
12
+ image : mysql:8
13
+ environment :
14
+ MYSQL_USER : with_advisory
15
+ MYSQL_PASSWORD : with_advisory_pass
16
+ MYSQL_DATABASE : with_advisory_lock_test
17
+ MYSQL_RANDOM_ROOT_PASSWORD : " yes"
18
+ MYSQL_ROOT_HOST : ' %'
19
+ ports :
20
+ - " 3306:3306"
Original file line number Diff line number Diff line change 2
2
3
3
module WithAdvisoryLock
4
4
class MySQL < Base
5
- # Caches nested lock support by MySQL reported version
6
- @@mysql_nl_cache = { }
7
- @@mysql_nl_cache_mutex = Mutex . new
8
- # See https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_get-lock
5
+ # See https://dev.mysql.com/doc/refman/5.7/en/locking-functions.html
6
+ # See https://dev.mysql.com/doc/refman/8.0/en/locking-functions.html
9
7
def try_lock
10
8
raise ArgumentError , 'shared locks are not supported on MySQL' if shared
11
9
raise ArgumentError , 'transaction level locks are not supported on MySQL' if transaction
@@ -18,8 +16,12 @@ def release_lock
18
16
end
19
17
20
18
def execute_successful? ( mysql_function )
21
- sql = "SELECT #{ mysql_function } AS #{ unique_column_name } "
22
- connection . select_value ( sql ) . to_i . positive?
19
+ execute_query ( mysql_function ) == 1
20
+ end
21
+
22
+ def execute_query ( mysql_function )
23
+ sql = "SELECT #{ mysql_function } "
24
+ connection . query_value ( sql )
23
25
end
24
26
25
27
# MySQL wants a string as the lock key.
Original file line number Diff line number Diff line change @@ -49,9 +49,11 @@ def is_postgresql_adapter?; adapter_support.postgresql?; end
49
49
50
50
setup do
51
51
ENV [ 'FLOCK_DIR' ] = Dir . mktmpdir if is_sqlite3_adapter?
52
- Tag . delete_all
53
- TagAudit . delete_all
54
- Label . delete_all
52
+ ApplicationRecord . connection . truncate_tables (
53
+ Tag . table_name ,
54
+ TagAudit . table_name ,
55
+ Label . table_name
56
+ )
55
57
end
56
58
57
59
teardown do
@@ -60,3 +62,4 @@ def is_postgresql_adapter?; adapter_support.postgresql?; end
60
62
end
61
63
62
64
puts "Testing with #{ env_db } database, ActiveRecord #{ ActiveRecord . gem_version } and #{ RUBY_ENGINE } #{ RUBY_ENGINE_VERSION } as #{ RUBY_VERSION } "
65
+ puts "Connection Pool size: #{ ActiveRecord ::Base . connection_pool . size } "
Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ def cleanup!
24
24
private
25
25
26
26
def work
27
- ApplicationRecord . connection_pool . with_connection do
27
+ Tag . connection_pool . with_connection do
28
28
Tag . with_advisory_lock ( 'test' , timeout_seconds : 0 , shared : @shared ) do
29
29
@locked = true
30
30
sleep 0.01 until @cleanup
@@ -117,7 +117,7 @@ class PostgreSQLTest < SupportedEnvironmentTest
117
117
end
118
118
119
119
def pg_lock_modes
120
- ApplicationRecord . connection . select_values ( "SELECT mode FROM pg_locks WHERE locktype = 'advisory';" )
120
+ Tag . connection . select_values ( "SELECT mode FROM pg_locks WHERE locktype = 'advisory';" )
121
121
end
122
122
123
123
test 'allows shared lock to be upgraded to an exclusive lock' do
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ class SeparateThreadTest < GemTestCase
10
10
@t1_return_value = nil
11
11
12
12
@t1 = Thread . new do
13
- ApplicationRecord . connection_pool . with_connection do
13
+ Label . connection_pool . with_connection do
14
14
@t1_return_value = Label . with_advisory_lock ( @lock_name ) do
15
15
@mutex . synchronize { @t1_acquired_lock = true }
16
16
sleep
@@ -21,7 +21,7 @@ class SeparateThreadTest < GemTestCase
21
21
22
22
# Wait for the thread to acquire the lock:
23
23
sleep ( 0.1 ) until @mutex . synchronize { @t1_acquired_lock }
24
- ApplicationRecord . connection . reconnect!
24
+ Label . connection . reconnect!
25
25
end
26
26
27
27
teardown do
You can’t perform that action at this time.
0 commit comments