Tuesday, March 31, 2015

Heisenbug to do with self.client losing its sessionstore

I have this code that looks something like this (django 1.6.11):

def test_something(self):
    url = someobject.get_url()
    User.objects.create_user('a', 'a@example.com', 'secret')
    assert self.client.login(username='a', password='secret')
    r = self.client.get(url)
    assert r.status_code == 302  # because you're not allowed to view it
    someobject.privacy_setting = 'different'
    r = self.client.get(url)
    assert r.status_code == 200  # now you can view it according the business logic


This code has been working for many many months but suddenly it started to Heisenfail with the last line being 302 != 200.
It might be related to caching somewhere else because it ONLY ever fails (if it fails!) when I run the whole test suite. 
After a lot of painful debugging I concluded that sometimes, that last self.client.get(url) causes `request.user == <SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x112318f50>>`

I.e. for the second request made by that logged in client, it's all of a sudden NOT logged in!! Not always. Only sometimes. :(

I put in a debugging line just before that last test like `assert self.client.session['_auth_user_id']` and that sometimes fails. Almost as if the testclient loses its session store DURING the lifetime of the test. Sometimes. 


Anybody seen anything similar that might be able to explain it or give me a clue?


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/87a1ed74-6ce2-49fa-886c-9cb015a90555%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment