def test_local_last_updated(self): cache.clear() mydict = CachedDict(timeout=100) mydict.remote_cache.set_many({ mydict.remote_cache_key: {'MYFLAG': 'value1'}, mydict.remote_cache_last_updated_key: 12345 }) # load the local cache from remote cache # this sets: mydict._local_last_updated = time.time() mydict._populate() local_last_updated = mydict._local_last_updated assert mydict._local_cache == {'MYFLAG': 'value1'} with mock.patch('time.time', mock.Mock(return_value=time.time() + 101)): mydict.remote_cache.set_many({ mydict.remote_cache_key: {'MYFLAG': 'value2'}, mydict.remote_cache_last_updated_key: time.time() }) assert mydict.local_cache_has_expired() assert mydict.local_cache_is_invalid() mydict._populate() assert mydict._local_cache == {'MYFLAG': 'value2'} assert mydict._local_last_updated != local_last_updated
def test_does_not_expire_by_default(self, _update_cache_data): mydict = CachedDict(timeout=100) mydict._cache = {} mydict._last_updated = time.time() mydict._populate() self.assertFalse(_update_cache_data.called)
def test_populate_timeout(self): cache.clear() mydict = CachedDict(timeout=100) now = time.time() mydict.remote_cache.set_many({ mydict.remote_cache_key: {'MYFLAG': 'value1'}, mydict.remote_cache_last_updated_key: now }) # load the local cache from remote cache mydict._populate() mydict.remote_cache.set_many({ mydict.remote_cache_key: {'MYFLAG': 'value2'}, mydict.remote_cache_last_updated_key: now + 1 }) # before timeout: local cache should not be updated with mock.patch('time.time', mock.Mock(return_value=now + mydict.timeout - 1)): mydict._populate() mydict._populate() mydict._populate() assert mydict._local_cache == {'MYFLAG': 'value1'} # after timeout: local cache should be updated with mock.patch('time.time', mock.Mock(return_value=now + mydict.timeout + 1)): mydict._populate() assert mydict._local_cache == {'MYFLAG': 'value2'}
def test_is_invalid_if_remote_cache_updated_right_after_local_last_updated( self): cache.clear() mydict = CachedDict(timeout=100) mydict.remote_cache.set_many({ mydict.remote_cache_key: { 'MYFLAG': 'value1' }, mydict.remote_cache_last_updated_key: 12345 }) # load the local cache from remote cache # this sets: mydict._local_last_updated = time.time() mydict._populate() # simulate remote cache updated by external process # remote_cache[remote_cache_last_updated_key] = time.time() mydict.remote_cache.set_many({ mydict.remote_cache_key: { 'MYFLAG': 'value2' }, mydict.remote_cache_last_updated_key: time.time() }) assert mydict.local_cache_is_invalid()
def test_reset_does_expire(self, _update_cache_data): mydict = CachedDict(timeout=100) mydict._cache = {} mydict._last_updated = time.time() mydict._populate(reset=True) _update_cache_data.assert_called_once_with()
def test_expired_does_update_data(self, _update_cache_data): mydict = CachedDict() mydict._cache = {} mydict._last_updated = time.time() mydict._populate() _update_cache_data.assert_called_once_with()
def setUp(self): self.cache = mock.Mock() self.mydict = CachedDict(timeout=100, cache=self.cache)
def test_setitem_not_implemented(self): d = CachedDict() with pytest.raises(NotImplementedError): d['x'] = 'foo'
def test_delitem_not_implemented(self): d = CachedDict() with pytest.raises(NotImplementedError): del d['x']
def test_is_expired_within_bounds(self): mydict = CachedDict(timeout=100) mydict._last_updated = time.time() self.assertFalse(mydict.is_expired())
def test_is_expired_last_updated_beyond_timeout(self): mydict = CachedDict(timeout=100) mydict._last_updated = time.time() - 101 self.assertTrue(mydict.is_expired())
def test_is_expired_missing_last_updated(self): mydict = CachedDict(timeout=100) mydict._last_updated = None self.assertTrue(mydict.is_expired())