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

发布于 2024-11-08 15:09:06 字数 1848 浏览 0 评论 0原文

我正在尝试为 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()

printer() 调用第一次按我的预期工作,但是当从线程调用时无法找到表:

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 "/usr/lib/pymodules/python2.6/django/db/models/sql/query.py", line 366, in get_aggregation
    result = query.get_compiler(using).execute_sql(SINGLE)
  File "/usr/lib/pymodules/python2.6/django/db/models/sql/compiler.py", line 727, in execute_sql
    cursor.execute(sql, params)
  File "/usr/lib/pymodules/python2.6/django/db/backends/sqlite3/base.py", line 200, in execute
    return Database.Cursor.execute(self, query, params)
DatabaseError: no such table: autest_daemon

我想了解发生了什么事。另外,我想知道是否有更好的策略来测试并行请求。

I am trying to write tests for django requests which might take a long time to process, and thus might interleave with other requests. My plan was to issue a long-running request, and inject assertions into places where it might suspend.

But trying to use threading in my unit test doesn't seem to work well:

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()

The printer() call works as I'd expect the first time, but then when called from a Thread is unable to find the table:

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 "/usr/lib/pymodules/python2.6/django/db/models/sql/query.py", line 366, in get_aggregation
    result = query.get_compiler(using).execute_sql(SINGLE)
  File "/usr/lib/pymodules/python2.6/django/db/models/sql/compiler.py", line 727, in execute_sql
    cursor.execute(sql, params)
  File "/usr/lib/pymodules/python2.6/django/db/backends/sqlite3/base.py", line 200, in execute
    return Database.Cursor.execute(self, query, params)
DatabaseError: no such table: autest_daemon

I'd like to understand what's going on. Also, I'd like to know if there is a better strategy for testing parallel requests.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩发小公举 2024-11-15 15:09:06

您不能在 Django 中的内存数据库(本例中为 sqlite3)中使用线程,请参阅此 bug。您的测试可能适用于 PostgreSQL 或 MySQL。

You can not use threading with in memory databases (sqlite3 in this case) in Django see this bug. Your test would probably work with PostgreSQL or MySQL.

别靠近我心 2024-11-15 15:09:06

正如 Rob 所说,在提出问题时,SQLite 无法做到这一点。但是,从 Django 1.8 开始(请参阅 https://docs.djangoproject.com/ en/1.8/topics/testing/overview/):

如果使用 Python 3.4+ 和 SQLite 3.7.13+ 的 SQLite 内存数据库,将启用共享缓存,因此您可以编写能够在线程之间共享数据库的测试。

因此,如果您可以选择升级,那么它应该可以工作。

As Rob says, at the time the question was asked, this wasn't possible with SQLite. However, as of Django 1.8 (see https://docs.djangoproject.com/en/1.8/topics/testing/overview/):

If using a SQLite in-memory database with Python 3.4+ and SQLite 3.7.13+, shared cache will be enabled, so you can write tests with ability to share the database between threads.

So if you have the option to upgrade, it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文