Django unit testing: how to test concurrent database operations?

I am creating a Django library that uses an optimistic concurrency control to prevent concurrent recording due to inconsistent data. I would like to be able to write unit tests for this function, but I'm not sure how to do this.

I know that Django unit tests are single-threaded, so the only way I can imagine working with validation is to open two separate database connections (to the same database) at the same time and switch the connection that Django uses ORM on query execution, although I'm not sure if connection joining is possible even in Django.

What are some test methods for concurrent database operations with Django?

+7
source share
4 answers

What are some test methods for concurrent database operations with Django?

Actually, Django is not a problem here.

Your library for optimistic concurrency management should be tested on its own standalone device.

Beyond Django; using only unittest.

You need to test using multi-threaded (and multi-processor) test drivers. Beyond Django.

Once you make sure that this works, you can test inside Django to make sure the API works.

, , urllib2, Django. , Django, urllib2, Django.

, - , . .

+3

, , , . . - , , ;)

+2

- Django- , . unit test /, , concurrency. .

0
source

use gevent:

import gevent

def foo():
    print('Running in foo')
    gevent.sleep(0)
    print('Explicit context switch to foo again')

def bar():
    print('Explicit context to bar')
    gevent.sleep(0)
    print('Implicit context switch back to bar')

gevent.joinall([
    gevent.spawn(foo),
    gevent.spawn(bar),
])


Running in foo
Explicit context to bar
Explicit context switch to foo again
Implicit context switch back to bar

http://sdiehl.imtqy.com/gevent-tutorial/

0
source

All Articles