Skip to content

Commit

Permalink
Fix flaky primary key test
Browse files Browse the repository at this point in the history
I noticed in a build that this test was flaky and causing subsequent
tests to fail with a no connection pool error. I don't entirely get why
this test was failing, it's not the only one where we remove a
connection. When this test fails, `@automatic_reconnect` is `false`.
After spending far too much time trying to figure out the combination of
tests that break this I decided to fix it by making a brand new
connection that we can remove. It's also a more accurate test because
we're asserting the connection is actually not `connected?`. Note that I
had to use the connection handler to remove the connection because there
is an inconsitency in remove_connection on the model. Unfortunately,
`NoConnection.remove_connection` will cause `connected?` to fall back to
`ActiveRecord::Base` which is contrary to the behavior of all other
methods on the active record models. I'm going to send a PR to fix that
but for now this should fix the (rare) flaky test for a primary key with
no connection.

To reproduce I used this seed: `SEED=29535 bundle exec rake test:mysql2`
  • Loading branch information
eileencodes committed Jun 14, 2023
1 parent 01363bd commit 7cfcf53
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions activerecord/test/cases/primary_keys_test.rb
Expand Up @@ -271,20 +271,26 @@ def test_serial_with_unquoted_sequence_name
end

class PrimaryKeyWithNoConnectionTest < ActiveRecord::TestCase
class NoConnection < ActiveRecord::Base
self.abstract_class = true
self.primary_key = "foo"
end

self.use_transactional_tests = false

unless in_memory_db?
def test_set_primary_key_with_no_connection
connection = ActiveRecord::Base.remove_connection

model = Class.new(ActiveRecord::Base)
model.primary_key = "foo"
NoConnection.establish_connection :arunit
# execute to fully establish a connection
NoConnection.connection.execute("SELECT 1")

assert_equal "foo", model.primary_key
assert NoConnection.connected?
assert_equal "foo", NoConnection.primary_key

ActiveRecord::Base.establish_connection(connection)
ActiveRecord::Base.connection_handler.remove_connection_pool("PrimaryKeyWithNoConnectionTest::NoConnection")
assert_nil NoConnection.connected?

assert_equal "foo", model.primary_key
assert_equal "foo", NoConnection.primary_key
end
end
end
Expand Down

0 comments on commit 7cfcf53

Please sign in to comment.