0

What's the easiest way to simulate a "lock timeout" DatabaseError with Django using the MySQL backend?

I have a piece of code, that operates over large querysets, and occasionally it throws the exception:

 DatabaseError: (1205, 'Lock wait timeout exceeded; try restarting transaction')

I understand what causes this and how to fix it, but I'm not sure how to test my fix since the error only happens in a production environment where several users are accessing certain tables simultaneously.

I first tried to run some sample code in a separate process to make Django apply a lock in MySQL, and while it does seem to lock the target table and make all other requests wait, a lock timeout never occurs when I test on my localhost with runserver. I let it run for 20 minutes without a timeout occurring.

Cerin
  • 60,957
  • 96
  • 316
  • 522

1 Answers1

0

If you need just to have an exception of specific type why not to mock/monkeypatch your code in test to throw this exeption instead of fetching from database?

dbf
  • 6,399
  • 2
  • 38
  • 65