小编Bar*_*yes的帖子

在django中测试交错的长时间运行请求

我正在尝试为可能需要很长时间处理的django请求编写测试,因此可能会与其他请求交错.我的计划是发出一个长时间运行的请求,并将断言注入可能暂停的地方.

但尝试在我的单元测试中使用线程似乎不能很好地工作:

class ThreadTest(test.TestCase):
    def thread_test(self):
        def printer():
            print models.Daemon.objects.count()

        d = models.Daemon(url='http://lockss.notadomain:8088')
        d.save()
        printer()
        t = threading.Thread(target=printer)
        t.start()
        t.join()
Run Code Online (Sandbox Code Playgroud)

printer()调用的工作方式与我第一次的预期相同,但是当从Thread调用时无法找到该表:

1
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.6/threading.py", line 532, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.6/threading.py", line 484, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/home/bhayes/lockss-code/hare/lockss-django/autest/tests.py", line 247, in printer
    print models.Daemon.objects.count()
  File "/usr/lib/pymodules/python2.6/django/db/models/manager.py", line 120, in count
    return self.get_query_set().count()
  File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 326, in count
    return self.query.get_count(using=self.db)
  File "/usr/lib/pymodules/python2.6/django/db/models/sql/query.py", line 394, in get_count
    number = obj.get_aggregation(using=using)[None]
  File …
Run Code Online (Sandbox Code Playgroud)

django multithreading unit-testing

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

标签 统计

django ×1

multithreading ×1

unit-testing ×1