RSpec: how to test methods using Parallel (error PG :: ConnectionBad)

In my application, I have several Builder classes that are responsible for receiving data received from an external API request and creating / saving resources in the database. I deal with a lot of data and implemented a Parallel gem to speed this up using several processes.

However, I found that any test for a method that uses Parallel fails with the same error:

ActiveRecord::StatementInvalid: PG::ConnectionBad: PQconsumeInput() server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. 

Here is an example of test code:

 class AirportBuilder < Resource def build_from_collection Parallel.each(object_producer, in_processes: 24) do |params| instance = Airport.find_or_initialize_by(fsid: params[:fs]) build!(instance, params) end end end 

I did a few searches on this, but all of the results on Google use multiple threads / processes to make the test suite faster, which is another problem.

Any ideas on how I can test this efficiently without causing PG errors? I understand that I might need to drown something, but I'm not quite sure what to drown and still have a meaningful test.

Thanks in advance to everyone who can help!

+6
source share
1 answer

Are you using too many database connections than are configured for your test database? Perhaps try setting it to the pool size equal to the needs of your script (which looks like 24)?

test: adapter: whatever host: whatever username: whatever password: whatever database: whatever pool: 24

Mentioned that you can also do some math in the default ActiveRecord connection pool . Some good information in this Heroku dev center article .

+1
source

All Articles