如何在 Rails 中使用 Active Record 进行慢速 SQL 查询以进行测试

Jua*_*tas 2 postgresql activerecord ruby-on-rails

我想测试一个超过 5 秒的 sql 查询超时处理程序:

module ActiveRecord
  module ConnectionAdapters
    class PostgreSQLAdapter

      def configure_connection_with_statement_timeout
        configure_connection_without_statement_timeout
        ActiveRecord::Base.logger.silence do
          execute('SET statement_timeout = 5000')
        end
      end
      alias_method_chain :configure_connection, :statement_timeout

    end
  end
end
Run Code Online (Sandbox Code Playgroud)

但我不知道如何使用 Active Record 进行如此慢的 sql 查询。

MrY*_*iji 6

可能重复:延迟或等待声明


pg_sleep似乎就是这样做的(睡眠 10 秒):

SELECT pg_sleep(10); 
Run Code Online (Sandbox Code Playgroud)

另请参阅:http://blog.endpoint.com/2012/11/how-to-make-postgresql-query-slow.html