feat(task): with_statement_timeout helper for long running collection or process query
This commit is contained in:
parent
c0ae02f458
commit
d0f77d0aab
5 changed files with 82 additions and 0 deletions
|
@ -0,0 +1,31 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Maintenance::StatementsHelpersConcern do
|
||||
let(:dummy_class) do
|
||||
Class.new do
|
||||
include Maintenance::StatementsHelpersConcern
|
||||
end
|
||||
end
|
||||
|
||||
let(:instance) { dummy_class.new }
|
||||
|
||||
describe '#with_statement_timeout' do
|
||||
it 'applies the statement timeout and raises an error for long-running queries' do
|
||||
expect {
|
||||
instance.with_statement_timeout('1ms') do
|
||||
# Cette requête devrait prendre plus de 1ms et donc déclencher un timeout
|
||||
ActiveRecord::Base.connection.execute("SELECT pg_sleep(1)")
|
||||
end
|
||||
}.to raise_error(ActiveRecord::StatementInvalid, /canceling statement due to statement timeout/i)
|
||||
end
|
||||
|
||||
it 'allows queries to complete within the timeout and returns the result' do
|
||||
result = instance.with_statement_timeout('1s') do
|
||||
ActiveRecord::Base.connection.execute("SELECT 42 AS answer").first['answer']
|
||||
end
|
||||
expect(result).to eq 42
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue