Testing interlaced long requests in django

I am trying to write tests for django requests that can take a lot of time to process and thus can interleave other requests. My plan was to issue a long request and enter statements into places where he could pause.

But trying to use threads in my unit test does not 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 call to printer () works the way I expected the first time, but when the call from Thread cannot 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 would like to understand what is happening. Also, I would like to know if there is a better strategy for testing concurrent queries.

+7
source share
2 answers

You cannot use threads with memory databases (in this case sqlite3) in Django, see this error . Your test will probably work with PostgreSQL or MySQL.

+1
source

According to Rob, at the time the question was asked, this was not possible with SQLite. However, with Django 1.8 (see https://docs.djangoproject.com/en/1.8/topics/testing/overview/ ):

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

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

+1
source

All Articles