小编bia*_*pio的帖子

如何在 Django 测试中强制事务中的竞争条件?

有没有办法使用多线程运行 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)

python testing django transactions race-condition

7
推荐指数
1
解决办法
1501
查看次数

标签 统计

django ×1

python ×1

race-condition ×1

testing ×1

transactions ×1