2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
require "cases/helper"
|
2012-09-21 11:14:42 -04:00
|
|
|
|
2019-12-16 06:40:58 -05:00
|
|
|
unless ActiveRecord::Base.connection.supports_transaction_isolation? && !current_adapter?(:SQLite3Adapter)
|
2013-11-08 10:57:51 -05:00
|
|
|
class TransactionIsolationUnsupportedTest < ActiveRecord::TestCase
|
2015-03-10 22:21:19 -04:00
|
|
|
self.use_transactional_tests = false
|
2012-09-21 11:14:42 -04:00
|
|
|
|
2013-11-08 10:57:51 -05:00
|
|
|
class Tag < ActiveRecord::Base
|
2012-09-21 11:14:42 -04:00
|
|
|
end
|
|
|
|
|
2013-11-08 10:57:51 -05:00
|
|
|
test "setting the isolation level raises an error" do
|
|
|
|
assert_raises(ActiveRecord::TransactionIsolationError) do
|
2018-08-23 08:24:35 -04:00
|
|
|
Tag.transaction(isolation: :serializable) { Tag.connection.materialize_transactions }
|
2013-11-08 10:57:51 -05:00
|
|
|
end
|
2012-09-21 11:14:42 -04:00
|
|
|
end
|
|
|
|
end
|
2017-11-08 15:08:26 -05:00
|
|
|
else
|
2013-11-08 10:57:51 -05:00
|
|
|
class TransactionIsolationTest < ActiveRecord::TestCase
|
2015-03-10 22:21:19 -04:00
|
|
|
self.use_transactional_tests = false
|
2012-09-21 11:14:42 -04:00
|
|
|
|
2013-11-08 10:57:51 -05:00
|
|
|
class Tag < ActiveRecord::Base
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "tags"
|
2012-09-21 11:14:42 -04:00
|
|
|
end
|
|
|
|
|
2013-11-08 10:57:51 -05:00
|
|
|
class Tag2 < ActiveRecord::Base
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "tags"
|
2012-09-25 15:29:25 -04:00
|
|
|
end
|
2013-11-08 10:57:51 -05:00
|
|
|
|
|
|
|
setup do
|
2013-12-24 04:18:54 -05:00
|
|
|
Tag.establish_connection :arunit
|
|
|
|
Tag2.establish_connection :arunit
|
2013-11-08 10:57:51 -05:00
|
|
|
Tag.destroy_all
|
2012-09-21 11:14:42 -04:00
|
|
|
end
|
|
|
|
|
2013-11-08 10:57:51 -05:00
|
|
|
# It is impossible to properly test read uncommitted. The SQL standard only
|
|
|
|
# specifies what must not happen at a certain level, not what must happen. At
|
|
|
|
# the read uncommitted level, there is nothing that must not happen.
|
|
|
|
if ActiveRecord::Base.connection.transaction_isolation_levels.include?(:read_uncommitted)
|
|
|
|
test "read uncommitted" do
|
|
|
|
Tag.transaction(isolation: :read_uncommitted) do
|
|
|
|
assert_equal 0, Tag.count
|
|
|
|
Tag2.create
|
|
|
|
assert_equal 1, Tag.count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-09-21 11:14:42 -04:00
|
|
|
|
2013-11-08 10:57:51 -05:00
|
|
|
# We are testing that a dirty read does not happen
|
|
|
|
test "read committed" do
|
|
|
|
Tag.transaction(isolation: :read_committed) do
|
2012-09-21 11:14:42 -04:00
|
|
|
assert_equal 0, Tag.count
|
2013-11-08 10:57:51 -05:00
|
|
|
|
|
|
|
Tag2.transaction do
|
|
|
|
Tag2.create
|
|
|
|
assert_equal 0, Tag.count
|
|
|
|
end
|
2012-09-21 11:14:42 -04:00
|
|
|
end
|
2013-11-08 10:57:51 -05:00
|
|
|
|
|
|
|
assert_equal 1, Tag.count
|
2012-09-21 11:14:42 -04:00
|
|
|
end
|
|
|
|
|
2013-11-08 10:57:51 -05:00
|
|
|
# We are testing that a nonrepeatable read does not happen
|
|
|
|
if ActiveRecord::Base.connection.transaction_isolation_levels.include?(:repeatable_read)
|
|
|
|
test "repeatable read" do
|
2016-08-06 12:26:20 -04:00
|
|
|
tag = Tag.create(name: "jon")
|
2012-09-21 11:14:42 -04:00
|
|
|
|
2013-11-08 10:57:51 -05:00
|
|
|
Tag.transaction(isolation: :repeatable_read) do
|
|
|
|
tag.reload
|
2016-08-06 12:26:20 -04:00
|
|
|
Tag2.find(tag.id).update(name: "emily")
|
2012-09-21 11:14:42 -04:00
|
|
|
|
2013-11-08 10:57:51 -05:00
|
|
|
tag.reload
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "jon", tag.name
|
2013-11-08 10:57:51 -05:00
|
|
|
end
|
2012-09-21 11:14:42 -04:00
|
|
|
|
2013-11-08 10:57:51 -05:00
|
|
|
tag.reload
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "emily", tag.name
|
2013-11-08 10:57:51 -05:00
|
|
|
end
|
2012-09-21 11:14:42 -04:00
|
|
|
end
|
|
|
|
|
2013-11-08 10:57:51 -05:00
|
|
|
# We are only testing that there are no errors because it's too hard to
|
|
|
|
# test serializable. Databases behave differently to enforce the serializability
|
|
|
|
# constraint.
|
|
|
|
test "serializable" do
|
|
|
|
Tag.transaction(isolation: :serializable) do
|
|
|
|
Tag.create
|
|
|
|
end
|
2012-09-21 11:14:42 -04:00
|
|
|
end
|
|
|
|
|
2013-11-08 10:57:51 -05:00
|
|
|
test "setting isolation when joining a transaction raises an error" do
|
|
|
|
Tag.transaction do
|
|
|
|
assert_raises(ActiveRecord::TransactionIsolationError) do
|
2018-09-25 13:18:20 -04:00
|
|
|
Tag.transaction(isolation: :serializable) { }
|
2013-11-08 10:57:51 -05:00
|
|
|
end
|
2012-09-21 11:14:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-08 10:57:51 -05:00
|
|
|
test "setting isolation when starting a nested transaction raises error" do
|
|
|
|
Tag.transaction do
|
|
|
|
assert_raises(ActiveRecord::TransactionIsolationError) do
|
2018-09-25 13:18:20 -04:00
|
|
|
Tag.transaction(requires_new: true, isolation: :serializable) { }
|
2013-11-08 10:57:51 -05:00
|
|
|
end
|
2012-09-21 11:14:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|