Opened 12 years ago

Closed 11 years ago

#20075 closed Bug (fixed)

Cache session test fails when using a cache other than LocMemCache

Reported by: Martin Larente Owned by: nobody
Component: contrib.sessions Version: 1.5
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Whenever the default cache is set to a different backend than the default LocMemCache, the cache session tests fail. To reproduce it, put this in your settings file:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
        'LOCATION': '/path/to/your/cache',  # change this to an actual folder on your system
    }
}

When the tests are run with this non-default cache, we get:

FAIL: test_non_default_cache (django.contrib.sessions.tests.CacheSessionTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\Ryo\venv\esi-acctman\lib\site-packages\django\test\utils.py", line 220, in inner
    return test_func(*args, **kwargs)
  File "C:\Users\Ryo\venv\esi-acctman\lib\site-packages\django\contrib\sessions\tests.py", line 473, in test_non_default_cache
    self.assertNotEqual(get_cache('sessions').get(self.session.cache_key), None)
AssertionError: None == None

The reason for this is that, although the test is run with overridden settings that sets LocMemCache as the cache to use for the session backend:

    @override_settings(CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
        },
        'sessions': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        },
    }, SESSION_CACHE_ALIAS='sessions')
    def test_non_default_cache(self):
        self.session.save()
        self.assertEqual(get_cache('default').get(self.session.cache_key), None)
        self.assertNotEqual(get_cache('sessions').get(self.session.cache_key), None)

The actual session instance (self.session) is created in the setup method of SessionTestsMixin, which doesn't override these settings. It works with the default cache settings because it just so happens that the default cache system is also the one used in the test, but as soon as the default cache backend is changed, the test fails.

Change History (4)

comment:1 by Jacob, 12 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Joeri Bekker, 11 years ago

I added some documentation and a warning message when users try to override settings that are showing unexpected behaviour. More in: #19031

comment:3 by Tim Graham, 11 years ago

This was fixed in master by 8c1cc4b3b04f639ae40bc806380bc4a9dd568eb0 (re-initializing the session backend). I will backport the fix to 1.5.x.

comment:4 by Tim Graham <timograham@…>, 11 years ago

Resolution: fixed
Status: newclosed

In 2f9378223d0c92447e2529d5eeaad7d301b78b84:

[1.5.x] Fixed #20075 -- Fixed session test fail when using a cache other than LocMemCache.

As override_settings was used after the initialization of the session backend,
we need to use a new session backend here.

Backport of 8c1cc4b3b0 from master

Note: See TracTickets for help on using tickets.
Back to Top