有没有办法使用多线程运行 django 测试并强制竞争条件?我想确保处理事务错误的代码路径被执行。更具体地说,我希望能够产生 2 个线程,这些线程将尝试对数据库执行相同的操作,其中一个成功,另一个失败。我正在使用 django 中的测试框架。
Python伪代码:
def some_method():
try
with transaction.atomic():
objectA = get_object_from_db()
objectA.delete()
except Error:
# error handling code to be run
class TestClass(TransactionalTestCase):
def test_some_method():
# run two threads and make sure that the race condition was present and some_method recovered successfully
Run Code Online (Sandbox Code Playgroud)