Files
deb-oslo.cache/oslo_cache/tests/test_dict_backend.py
Pavel Kholkin 5881ad7cb1 Added NO_VALUE to core file
Using oslo_cache lib the only way to compare some cached_value with NO_VALUE is
to add bad import like 'from oslo_cache.backends.dictionary import NO_VALUE'.

NO_VALUE is now added to core.py file and the improved import
'oslo_cache.core import NO_VALUE' will look much better in other projects.

NO_VALUE is now public, it is added to __all__ and is used everywhere from core
instead of dogpile.cache.api.NO_VALUE.

Co-Authored-By: Sergey Nikitin <snikitin@mirantis.com>

Change-Id: I8fea4a9eb088089289ba1dd35377fb10465c313a
2015-07-23 14:12:01 +03:00

95 lines
3.3 KiB
Python

# Copyright 2015 Mirantis Inc
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from dogpile.cache import region as dp_region
from oslo_cache import core
from oslo_cache.tests import test_cache
from oslo_config import fixture as config_fixture
from oslo_utils import fixture as time_fixture
NO_VALUE = core.NO_VALUE
KEY = 'test_key'
VALUE = 'test_value'
class CacheDictBackendTest(test_cache.BaseTestCase):
def setUp(self):
super(CacheDictBackendTest, self).setUp()
self.config_fixture = self.useFixture(config_fixture.Config())
self.config_fixture.config(group='cache', backend='oslo_cache.dict')
self.time_fixture = self.useFixture(time_fixture.TimeFixture())
self.region = dp_region.make_region()
self.region.configure(
'oslo_cache.dict', arguments={'expiration_time': 0.5})
def test_dict_backend(self):
self.assertIs(NO_VALUE, self.region.get(KEY))
self.region.set(KEY, VALUE)
self.assertEqual(VALUE, self.region.get(KEY))
self.region.delete(KEY)
self.assertIs(NO_VALUE, self.region.get(KEY))
def test_dict_backend_expiration_time(self):
self.region.set(KEY, VALUE)
self.assertEqual(VALUE, self.region.get(KEY))
self.time_fixture.advance_time_seconds(1)
self.assertIs(NO_VALUE, self.region.get(KEY))
def test_dict_backend_clear_cache(self):
self.region.set(KEY, VALUE)
self.time_fixture.advance_time_seconds(1)
self.assertEqual(1, len(self.region.backend.cache))
self.region.backend._clear()
self.assertEqual(0, len(self.region.backend.cache))
def test_dict_backend_zero_expiration_time(self):
self.region = dp_region.make_region()
self.region.configure(
'oslo_cache.dict', arguments={'expiration_time': 0})
self.region.set(KEY, VALUE)
self.time_fixture.advance_time_seconds(1)
self.assertEqual(VALUE, self.region.get(KEY))
self.assertEqual(1, len(self.region.backend.cache))
self.region.backend._clear()
self.assertEqual(VALUE, self.region.get(KEY))
self.assertEqual(1, len(self.region.backend.cache))
def test_dict_backend_multi_keys(self):
self.region.set('key1', 'value1')
self.region.set('key2', 'value2')
self.time_fixture.advance_time_seconds(1)
self.region.set('key3', 'value3')
self.assertEqual(1, len(self.region.backend.cache))
self.assertIs(NO_VALUE, self.region.get('key1'))
self.assertIs(NO_VALUE, self.region.get('key2'))
self.assertEqual('value3', self.region.get('key3'))
def test_dict_backend_rewrite_value(self):
self.region.set(KEY, 'value1')
self.region.set(KEY, 'value2')
self.assertEqual('value2', self.region.get(KEY))