Why not use shared ActiveRecord connections for Rspec + Selenium?

后端 未结 6 1852
傲寒
傲寒 2020-11-30 23:12

It seems the most commonly accepted way to deal with Selenium and tests is to avoid using transactional fixtures and then using something like database_cleaner between tests

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 23:21

    There's a good thing at the end of this post. It may explain why I get a MALLOC error when I attempt to a very simple threading script.

    http://apidock.com/rails/ActiveRecord/Base/connection

    leente - March 15, 2011 0 thanks
    Don't cache it!
    
    Don’t store a connection in a variable, because another thread might try to use it when it’s already checked back in into the connection pool. See: ActiveRecord::ConnectionAdapters::ConnectionPool
    
    connection = ActiveRecord::Base.connection
    
    threads = (1..100).map do
      Thread.new do
        begin
          10.times do
            connection.execute("SELECT SLEEP(1)")  # WRONG
            ActiveRecord::Base.connection.execute("SELECT SLEEP(1)")  # CORRECT
          end
          puts "success"
        rescue => e
          puts e.message
        end
      end
    end
    
    threads.each(&:join)
    

提交回复
热议问题