-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathtransaction_test.rb
104 lines (88 loc) · 3.23 KB
/
transaction_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# frozen_string_literal: true
require 'test_helper'
class TransactionScopingTest < GemTestCase
def supported?
%i[postgresql jdbcpostgresql].include?(env_db)
end
test 'raises an error when attempting to use transaction level locks if not supported' do
skip if supported?
Tag.transaction do
exception = assert_raises(ArgumentError) do
Tag.with_advisory_lock 'test', transaction: true do
raise 'should not get here'
end
end
assert_match(/#{Regexp.escape('not supported')}/, exception.message)
end
end
class PostgresqlTest < TransactionScopingTest
setup do
skip unless env_db == :postgresql
@pg_lock_count = lambda do
ActiveRecord::Base.connection.select_value("SELECT COUNT(*) FROM pg_locks WHERE locktype = 'advisory';").to_i
end
end
test 'without timeout, the session locks release after the block executes' do
Tag.transaction do
assert_equal(0, @pg_lock_count.call)
Tag.with_advisory_lock 'test' do
assert_equal(1, @pg_lock_count.call)
end
assert_equal(0, @pg_lock_count.call)
end
end
test 'with timeout, the session locks release after the block executes' do
Tag.transaction do
assert_equal(0, @pg_lock_count.call)
Tag.with_advisory_lock 'test', timeout_seconds: 1 do
assert_equal(1, @pg_lock_count.call)
end
assert_equal(0, @pg_lock_count.call)
end
end
test 'without timeout, the session locks release when transaction fails inside block' do
Tag.transaction do
assert_equal(0, @pg_lock_count.call)
exception = assert_raises(ActiveRecord::StatementInvalid) do
Tag.with_advisory_lock 'test' do
Tag.connection.execute 'SELECT 1/0;'
end
end
assert_match(/#{Regexp.escape('division by zero')}/, exception.message)
assert_equal(0, @pg_lock_count.call)
end
end
test 'with timeout, the session locks release when transaction fails inside block' do
Tag.transaction do
assert_equal(0, @pg_lock_count.call)
exception = assert_raises(ActiveRecord::StatementInvalid) do
Tag.with_advisory_lock 'test', timeout_seconds: 1 do
Tag.connection.execute 'SELECT 1/0;'
end
end
assert_match(/#{Regexp.escape('division by zero')}/, exception.message)
assert_equal(0, @pg_lock_count.call)
end
end
test 'without timeout, the transaction level locks hold until the transaction completes' do
Tag.transaction do
assert_equal(0, @pg_lock_count.call)
Tag.with_advisory_lock 'test', transaction: true do
assert_equal(1, @pg_lock_count.call)
end
assert_equal(1, @pg_lock_count.call)
end
assert_equal(0, @pg_lock_count.call)
end
test 'with timeout, the transaction level locks hold until the transaction completes' do
Tag.transaction do
assert_equal(0, @pg_lock_count.call)
Tag.with_advisory_lock 'test', timeout_seconds: 1, transaction: true do
assert_equal(1, @pg_lock_count.call)
end
assert_equal(1, @pg_lock_count.call)
end
assert_equal(0, @pg_lock_count.call)
end
end
end