gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:49:31 26 4
gpt4 key购买 nike

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

但是尝试在我的单元测试中使用线程似乎效果不佳:

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() 调用按我第一次的预期工作,但是当从 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 "/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

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

最佳答案

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

关于django - 在 django 中测试交错的长时间运行的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6078930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com