Repository: rails/rails
File: activerecord/test/cases/connection_pool_test.rb (Ruby)

Code before:
      end

      def test_inspect_does_not_show_secrets
        assert_match(/#<ActiveRecord::ConnectionAdapters::ConnectionPool env_name="\w+" role=:writing>/, @pool.inspect)

        db_config = ActiveRecord::Base.connection_pool.db_config
        pool_config = ActiveRecord::ConnectionAdapters::PoolConfig.new(ActiveRecord::Base, db_config, :reading, :shard_one)
        pool = ConnectionPool.new(pool_config)

        assert_match(/#<ActiveRecord::ConnectionAdapters::ConnectionPool env_name="\w+" role=:reading shard=:shard_one>/, pool.inspect)
      ensure
        pool&.disconnect!
      end

      def test_checkout_callback_error_does_not_affect_permanent_lease_and_active_connection_state
        msg = "some error"
        proc_to_raise = -> { raise msg }
        ActiveRecord::ConnectionAdapters::AbstractAdapter.set_callback(:checkout, :after, proc_to_raise)

        assert_predicate @pool, :permanent_lease?
        assert_not_predicate @pool, :active_connection?
        begin
          @pool.lease_connection
        rescue Exception => e
          assert_equal msg, e.message
        end
        assert_predicate @pool, :permanent_lease?
        assert_not_predicate @pool, :active_connection?
      ensure
        ActiveRecord::ConnectionAdapters::AbstractAdapter.skip_callback(:checkout, :after, proc_to_raise)
      end

      private
        def active_connections(pool)
          pool.connections.find_all(&:in_use?)
        end

        def with_single_connection_pool(**options)
          config = @db_config.configuration_hash.merge(max_connections: 1, **options)
          db_config = ActiveRecord::DatabaseConfigurations::HashConfig.new("arunit", "primary", config)
          pool_config = ActiveRecord::ConnectionAdapters::PoolConfig.new(ActiveRecord::Base, db_config, :writing, :default)

          yield(pool = ConnectionPool.new(pool_config))
        ensure
          pool.disconnect! if pool
        end

        def new_pool_with_options(async: true, **options)
          config = @db_config.configuration_hash.merge(options)
          db_config = ActiveRecord::DatabaseConfigurations::HashConfig.new(@db_config.env_name, @db_config.name, config)
          pool_config = ActiveRecord::ConnectionAdapters::PoolConfig.new(ActiveRecord::Base, db_config, :writing, :default)

Review comment (suggestion):
```suggestion
        begin
          @pool.lease_connection
        rescue Exception => e
          assert_equal msg, e.message
        end
```

This should use `assert_raise`. 

Code after:
      end

      def test_inspect_does_not_show_secrets
        assert_match(/#<ActiveRecord::ConnectionAdapters::ConnectionPool env_name="\w+" role=:writing>/, @pool.inspect)

        db_config = ActiveRecord::Base.connection_pool.db_config
        pool_config = ActiveRecord::ConnectionAdapters::PoolConfig.new(ActiveRecord::Base, db_config, :reading, :shard_one)
        pool = ConnectionPool.new(pool_config)

        assert_match(/#<ActiveRecord::ConnectionAdapters::ConnectionPool env_name="\w+" role=:reading shard=:shard_one>/, pool.inspect)
      ensure
        pool&.disconnect!
      end

      def test_checkout_callback_error_does_not_affect_permanent_lease_and_active_connection_state
        checkout_error = StandardError.new("error during checkout")
        proc_to_raise = -> { raise checkout_error }
        ActiveRecord::ConnectionAdapters::AbstractAdapter.set_callback(:checkout, :after, proc_to_raise)

        assert_predicate @pool, :permanent_lease?
        assert_not_predicate @pool, :active_connection?

        error = assert_raises StandardError do
          @pool.lease_connection
        end
        assert_same checkout_error, error

        assert_predicate @pool, :permanent_lease?
        assert_not_predicate @pool, :active_connection?
      ensure
        ActiveRecord::ConnectionAdapters::AbstractAdapter.skip_callback(:checkout, :after, proc_to_raise)
      end

      private
        def active_connections(pool)
          pool.connections.find_all(&:in_use?)
        end

        def with_single_connection_pool(**options)
          config = @db_config.configuration_hash.merge(max_connections: 1, **options)
          db_config = ActiveRecord::DatabaseConfigurations::HashConfig.new("arunit", "primary", config)
          pool_config = ActiveRecord::ConnectionAdapters::PoolConfig.new(ActiveRecord::Base, db_config, :writing, :default)

          yield(pool = ConnectionPool.new(pool_config))
        ensure
          pool.disconnect! if pool
        end

        def new_pool_with_options(async: true, **options)
          config = @db_config.configuration_hash.merge(options)
          db_config = ActiveRecord::DatabaseConfigurations::HashConfig.new(@db_config.env_name, @db_config.name, config)