ruby-on-railsrubypostgresqldeadlockminitest

How can I fix Postgres deadlock errors in Minitest?


I am using Minitest and Postgres to test my Rails website. When I added tests for model and controller, I started getting Postgresql deadlock errors in different tests.

UPDATE

I found the problem. I get these errors when there are too many tests. I have 50 tests and everything works well. When I add a few more tests I get errors. How can I fix them?

Error:

Minitest::UnexpectedError: ActiveRecord::Deadlocked: PG::TRDeadlockDetected: ERROR:  deadlock detected LINE 7: DELETE FROM "videos";                     ^ DETAIL:  Process 11456 waits for RowExclusiveLock on relation 42608 of database 42434; blocked by process 10460. Process 10460 waits for ShareRowExclusiveLock on relation 42564 of database 42434; blocked by process 11456. HINT:  See server log for query details. 

Other errors:

Minitest::UnexpectedError: ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "accounts_pkey"
DETAIL:  Key (id)=(825136781) already exists.
Minitest::UnexpectedError: ActiveRecord::ConnectionNotEstablished: connection is closed

Solution

  • Comment this line in test_helper.rb

    parallelize(workers: :number_of_processors, with: :threads)