1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/test/cases/transaction_isolation_test.rb
Edouard CHIN 30e4c199d2 Don't run concurrent transaction test on sqlite3:
- #37798 had to be reverted because a new flakyness appeared ([see
  CI build](https://buildkite.com/rails/rails/builds/65467#efaa1dd5-aaf4-43a1-a204-d1c42abf614d))

  This failure isn't related to the change itself.
  If you apply this diff, the test will some time to time fail.
  Even without my changes.

  ```diff
  diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb
index b5c1cac3d9..09fe0ddd7f 100644
--- a/activerecord/test/cases/transactions_test.rb
+++ b/activerecord/test/cases/transactions_test.rb
@@ -1127,7 +1127,6 @@ def test_no_automatic_savepoint_for_inner_transaction
   end
 end if Topic.connection.supports_savepoints?

-if ActiveRecord::Base.connection.supports_transaction_isolation?
   class ConcurrentTransactionTest < TransactionTest
     # This will cause transactions to overlap and fail unless they are performed on
     # separate database connections.
@@ -1198,4 +1197,3 @@ def test_transaction_isolation__read_committed
       assert_equal original_salary, Developer.find(1).salary
     end
   end
-end
  ```

  SQlite3 isn't safe to run in a multi threaded environment unless
  sqlite is compiled with a special flag which isn't our case.
  Ref https://www.sqlite.org/threadsafe.html
2019-12-17 17:15:23 +01:00

106 lines
3.1 KiB
Ruby

# frozen_string_literal: true
require "cases/helper"
unless ActiveRecord::Base.connection.supports_transaction_isolation? && !current_adapter?(:SQLite3Adapter)
class TransactionIsolationUnsupportedTest < ActiveRecord::TestCase
self.use_transactional_tests = false
class Tag < ActiveRecord::Base
end
test "setting the isolation level raises an error" do
assert_raises(ActiveRecord::TransactionIsolationError) do
Tag.transaction(isolation: :serializable) { Tag.connection.materialize_transactions }
end
end
end
else
class TransactionIsolationTest < ActiveRecord::TestCase
self.use_transactional_tests = false
class Tag < ActiveRecord::Base
self.table_name = "tags"
end
class Tag2 < ActiveRecord::Base
self.table_name = "tags"
end
setup do
Tag.establish_connection :arunit
Tag2.establish_connection :arunit
Tag.destroy_all
end
# 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
# We are testing that a dirty read does not happen
test "read committed" do
Tag.transaction(isolation: :read_committed) do
assert_equal 0, Tag.count
Tag2.transaction do
Tag2.create
assert_equal 0, Tag.count
end
end
assert_equal 1, Tag.count
end
# We are testing that a nonrepeatable read does not happen
if ActiveRecord::Base.connection.transaction_isolation_levels.include?(:repeatable_read)
test "repeatable read" do
tag = Tag.create(name: "jon")
Tag.transaction(isolation: :repeatable_read) do
tag.reload
Tag2.find(tag.id).update(name: "emily")
tag.reload
assert_equal "jon", tag.name
end
tag.reload
assert_equal "emily", tag.name
end
end
# 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
end
test "setting isolation when joining a transaction raises an error" do
Tag.transaction do
assert_raises(ActiveRecord::TransactionIsolationError) do
Tag.transaction(isolation: :serializable) { }
end
end
end
test "setting isolation when starting a nested transaction raises error" do
Tag.transaction do
assert_raises(ActiveRecord::TransactionIsolationError) do
Tag.transaction(requires_new: true, isolation: :serializable) { }
end
end
end
end
end