Heisenbug to do with self.client losing its sessionstore

35 views
Skip to first unread message

Peter Bengtsson

unread,
Mar 31, 2015, 4:27:32 PM3/31/15
to django...@googlegroups.com
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?


mlpi...@qdqmedia.com

unread,
Jun 5, 2015, 11:10:52 AM6/5/15
to django...@googlegroups.com
Hi, I am facing a similar issue and have not found any solution yet. I was wondering if you had been able to fix this?

Peter Bengtsson

unread,
Jun 5, 2015, 2:42:26 PM6/5/15
to django...@googlegroups.com
No :(

It's still a mystery. It's really annoying. It resurfaced again about 2 weeks ago and it caused several test failures that I could never reproduce locally. 
I have some tests that look like this:

class TestCase(DjangoTestCase):
  def setUp(self):
      super(TestCase, self).setUp()
      User.objects.create(...)
      assert self.client.login(...)

  def test_something(self):
      response = self.client.get('/some/url/for/signed/in/people')
      assert response.status_code == 200
   
But sometimes, for no reason I can understand, that fails because at the point of doing the self.client.get the self.client.session is empty! :(
Reply all
Reply to author
Forward
0 new messages