lundi 24 septembre 2018

How to test PG::QueryCanceled (due to timeout) error in a Rails app?

On production server I got this error

ActiveRecord::StatementInvalid: PG::QueryCanceled: ERROR: canceling statement due to statement timeout <SQL query here>

In this line:

Contact.where(id: contact_ids_to_delete).delete_all

SQL query was DELETE command with a huge list of ids. It timed out. I came up with a solution which is to delete Contacts in batches:

Contact.where(id: contact_ids_to_delete).in_batches.delete_all

The question is, how do I test my solution? Or what is the common way to test it? Or is there any gem that would make testing it convenient?
I see two possible ways to test it:
1. (Dynamically) set the timeout in test database to a small amount of seconds and create a test in which I generate a lot of Contacts and then try to run my code to delete them.
It seems to be the right way to do it, but it could potentially slow down the tests execution, and setting the timeout dynamically (which would be the ideal way to do it) could be tricky.
2. Test that deletions are in batches.
It could be tricky, because this way I would have to monitor the queries.

Aucun commentaire:

Enregistrer un commentaire