Skip to content

Commit

Permalink
Disable transactions in locking examples
Browse files Browse the repository at this point in the history
These tests are creating threads and explicit transactions to verify
when we're locked or not locked in the database.  The Rails 5.1 release
notes mention that this type of behavior can cause a deadlock, which
we've seen with rails 5.1, and to instead disable transactional tests.

Why isn't there a cleaner way to flag examples as transactional or not?
Although not elegant, disabling transactional examples before and
enabling them after works.

From the rails 5.1 release notes:

"Transactional tests now wrap all Active Record connections in database
transactions.

When a test spawns additional threads, and those threads obtain database
connections, those connections are now handled specially:

The threads will share a single connection, which is inside the managed
transaction. This ensures all threads see the database in the same
state, ignoring the outermost transaction. Previously, such additional
connections were unable to see the fixture rows, for example.

When a thread enters a nested transaction, it will temporarily obtain
exclusive use of the connection, to maintain isolation.

If your tests currently rely on obtaining a separate,
outside-of-transaction, connection in a spawned thread, you'll need to
switch to more explicit connection management.

If your tests spawn threads and those threads interact while also using
explicit database transactions, this change may introduce a deadlock.

The easy way to opt out of this new behavior is to disable transactional
tests on any test cases it affects."

https://guides.rubyonrails.org/5_1_release_notes.html
  • Loading branch information
jrafanie committed Oct 11, 2018
1 parent a8f357d commit 32c7c4a
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions spec/models/vmdb_database_connection_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
require "concurrent/atomic/event"

describe VmdbDatabaseConnection do
before do
@db = FactoryGirl.create(:vmdb_database)
end
RSpec.configure do |c|
c.use_transactional_examples = false
end

describe VmdbDatabaseConnection do
after :all do
# HACK: Some other tests (around Automate) rely on the fact there's
# only one connection in the pool. It's totally unfair to blame this
# spec for using the API in a perfectly ordinary way.. but it solves
# the immediate problem.

VmdbDatabaseConnection.connection_pool.disconnect!
end

Expand Down Expand Up @@ -165,3 +164,7 @@ class << @buffer
end
end
end

RSpec.configure do |c|
c.use_transactional_examples = true
end

0 comments on commit 32c7c4a

Please sign in to comment.