Exemple #1
0
    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()
Exemple #2
0
    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'}
Exemple #3
0
    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
Exemple #4
0
    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()
Exemple #5
0
    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)
Exemple #6
0
    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()
Exemple #7
0
    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()
Exemple #8
0
 def setUp(self):
     self.cache = mock.Mock()
     self.mydict = CachedDict(timeout=100, cache=self.cache)
Exemple #9
0
class CachedDictTest(TestCase):
    def setUp(self):
        self.cache = mock.Mock()
        self.mydict = CachedDict(timeout=100, cache=self.cache)

    @mock.patch('modeldict.base.CachedDict._update_cache_data')
    @mock.patch('modeldict.base.CachedDict.local_cache_has_expired', mock.Mock(return_value=True))
    @mock.patch('modeldict.base.CachedDict.local_cache_is_invalid', mock.Mock(return_value=False))
    def test_expired_does_update_data(self, _update_cache_data):
        self.mydict._local_cache = {}
        self.mydict._local_last_updated = time.time()
        self.mydict._last_checked_for_remote_changes = time.time()
        self.mydict._populate()

        assert not _update_cache_data.called

    @mock.patch('modeldict.base.CachedDict._update_cache_data')
    @mock.patch('modeldict.base.CachedDict.local_cache_has_expired', mock.Mock(return_value=False))
    @mock.patch('modeldict.base.CachedDict.local_cache_is_invalid', mock.Mock(return_value=True))
    def test_reset_does_expire(self, _update_cache_data):
        self.mydict._local_cache = {}
        self.mydict._local_last_updated = time.time()
        self.mydict._last_checked_for_remote_changes = time.time()
        self.mydict._populate(reset=True)

        _update_cache_data.assert_called_once_with()

    @mock.patch('modeldict.base.CachedDict._update_cache_data')
    @mock.patch('modeldict.base.CachedDict.local_cache_has_expired', mock.Mock(return_value=False))
    @mock.patch('modeldict.base.CachedDict.local_cache_is_invalid', mock.Mock(return_value=True))
    def test_does_not_expire_by_default(self, _update_cache_data):
        self.mydict._local_cache = {}
        self.mydict._local_last_updated = time.time()
        self.mydict._last_checked_for_remote_changes = time.time()
        self.mydict._populate()

        assert not _update_cache_data.called

    def test_is_expired_missing_last_checked_for_remote_changes(self):
        self.mydict._last_checked_for_remote_changes = 0.0
        assert self.mydict.local_cache_has_expired()
        assert not self.cache.get.called

    def test_is_expired_last_updated_beyond_timeout(self):
        self.mydict._local_last_updated = time.time() - 101
        assert self.mydict.local_cache_has_expired()

    def test_is_expired_within_bounds(self):
        self.mydict._last_checked_for_remote_changes = time.time()

    def test_is_not_expired_if_remote_cache_is_old(self):
        # set it to an expired time
        self.mydict._local_cache = {'a': 1}
        self.mydict._local_last_updated = time.time() - 100
        self.cache.get.return_value = self.mydict._local_last_updated - 1

        result = self.mydict.local_cache_is_invalid()

        self.cache.get.assert_called_once_with(self.mydict.remote_cache_last_updated_key)
        assert not result

    def test_is_expired_if_remote_cache_is_new(self):
        # set it to an expired time, but with a local cache
        self.mydict._local_cache = dict(a=1)
        last_update = time.time() - 101
        self.mydict._local_last_updated = last_update
        self.mydict._last_checked_for_remote_changes = last_update
        self.cache.get.return_value = time.time()

        result = self.mydict.local_cache_is_invalid()

        assert result
        self.cache.get.assert_called_once_with(
            self.mydict.remote_cache_last_updated_key
        )

    def test_is_invalid_if_local_cache_is_none(self):
        self.mydict._local_cache = None
        assert self.mydict.local_cache_is_invalid()

    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_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_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_local_last_updated_not_updated_if_not_needed(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

        with mock.patch('time.time', mock.Mock(return_value=time.time() + 101)):
            assert mydict.local_cache_has_expired()
            assert not mydict.local_cache_is_invalid()

            mydict._populate()

            assert mydict._local_last_updated == local_last_updated
class CachedDictTest(TestCase):
    def setUp(self):
        self.cache = mock.Mock()
        self.mydict = CachedDict(timeout=100, cache=self.cache)

    @mock.patch('modeldict.base.CachedDict._update_cache_data')
    @mock.patch('modeldict.base.CachedDict.local_cache_has_expired', mock.Mock(return_value=True))
    @mock.patch('modeldict.base.CachedDict.local_cache_is_invalid', mock.Mock(return_value=False))
    def test_expired_does_update_data(self, _update_cache_data):
        self.mydict._local_cache = {}
        self.mydict._last_checked_for_remote_changes = time.time()
        self.mydict._populate()

        assert not _update_cache_data.called

    @mock.patch('modeldict.base.CachedDict._update_cache_data')
    @mock.patch('modeldict.base.CachedDict.local_cache_has_expired', mock.Mock(return_value=False))
    @mock.patch('modeldict.base.CachedDict.local_cache_is_invalid', mock.Mock(return_value=True))
    def test_reset_does_expire(self, _update_cache_data):
        self.mydict._local_cache = {}
        self.mydict._last_checked_for_remote_changes = time.time()
        self.mydict._populate(reset=True)

        _update_cache_data.assert_called_once_with()

    @mock.patch('modeldict.base.CachedDict._update_cache_data')
    @mock.patch('modeldict.base.CachedDict.local_cache_has_expired', mock.Mock(return_value=False))
    @mock.patch('modeldict.base.CachedDict.local_cache_is_invalid', mock.Mock(return_value=True))
    def test_does_not_expire_by_default(self, _update_cache_data):
        self.mydict._local_cache = {}
        self.mydict._last_checked_for_remote_changes = time.time()
        self.mydict._populate()

        assert not _update_cache_data.called

    def test_is_expired_missing_last_checked_for_remote_changes(self):
        self.mydict._last_checked_for_remote_changes = 0.0
        assert self.mydict.local_cache_has_expired()
        assert not self.cache.get.called

    def test_is_expired_last_updated_beyond_timeout(self):
        self.mydict._local_last_updated = time.time() - 101
        assert self.mydict.local_cache_has_expired()

    def test_is_expired_within_bounds(self):
        self.mydict._last_checked_for_remote_changes = time.time()

    def test_is_not_expired_if_remote_cache_is_old(self):
        # set it to an expired time
        self.mydict._local_cache = dict(a=1)
        self.mydict._local_last_updated = time.time() - 101
        self.cache.get.return_value = self.mydict._local_last_updated

        result = self.mydict.local_cache_is_invalid()

        self.cache.get.assert_called_once_with(self.mydict.remote_cache_last_updated_key)
        assert not result

    def test_is_expired_if_remote_cache_is_new(self):
        # set it to an expired time, but with a local cache
        self.mydict._local_cache = dict(a=1)
        self.mydict._last_checked_for_remote_changes = time.time() - 101
        self.cache.get.return_value = time.time()

        result = self.mydict.local_cache_is_invalid()

        self.cache.get.assert_called_once_with(
            self.mydict.remote_cache_last_updated_key
        )
        assert result

    def test_populate_timeout(self):
        cache.clear()
        mydict = CachedDict(timeout=100)

        now = int(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(mydict.remote_cache_key, {'MYFLAG': 'value2'})
        mydict.remote_cache.set(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'}
Exemple #11
0
    def test_delitem_not_implemented(self):
        d = CachedDict()

        with pytest.raises(NotImplementedError):
            del d['x']
Exemple #12
0
 def test_is_expired_within_bounds(self):
     mydict = CachedDict(timeout=100)
     mydict._last_updated = time.time()
     self.assertFalse(mydict.is_expired())
Exemple #13
0
 def test_is_expired_last_updated_beyond_timeout(self):
     mydict = CachedDict(timeout=100)
     mydict._last_updated = time.time() - 101
     self.assertTrue(mydict.is_expired())
Exemple #14
0
 def test_is_expired_missing_last_updated(self):
     mydict = CachedDict(timeout=100)
     mydict._last_updated = None
     self.assertTrue(mydict.is_expired())
Exemple #15
0
class CachedDictTest(TestCase):
    def setUp(self):
        self.cache = mock.Mock()
        self.mydict = CachedDict(timeout=100, cache=self.cache)

    @mock.patch('modeldict.base.CachedDict._update_cache_data')
    @mock.patch('modeldict.base.CachedDict.local_cache_has_expired', mock.Mock(return_value=True))
    @mock.patch('modeldict.base.CachedDict.local_cache_is_invalid', mock.Mock(return_value=False))
    def test_expired_does_update_data(self, _update_cache_data):
        self.mydict._local_cache = {}
        self.mydict._last_checked_for_remote_changes = time.time()
        self.mydict._populate()

        self.assertFalse(_update_cache_data.called)

    @mock.patch('modeldict.base.CachedDict._update_cache_data')
    @mock.patch('modeldict.base.CachedDict.local_cache_has_expired', mock.Mock(return_value=False))
    @mock.patch('modeldict.base.CachedDict.local_cache_is_invalid', mock.Mock(return_value=True))
    def test_reset_does_expire(self, _update_cache_data):
        self.mydict._local_cache = {}
        self.mydict._last_checked_for_remote_changes = time.time()
        self.mydict._populate(reset=True)

        _update_cache_data.assert_called_once_with()

    @mock.patch('modeldict.base.CachedDict._update_cache_data')
    @mock.patch('modeldict.base.CachedDict.local_cache_has_expired', mock.Mock(return_value=False))
    @mock.patch('modeldict.base.CachedDict.local_cache_is_invalid', mock.Mock(return_value=True))
    def test_does_not_expire_by_default(self, _update_cache_data):
        self.mydict._local_cache = {}
        self.mydict._last_checked_for_remote_changes = time.time()
        self.mydict._populate()

        self.assertFalse(_update_cache_data.called)

    def test_is_expired_missing_last_checked_for_remote_changes(self):
        self.mydict._last_checked_for_remote_changes = None
        self.assertTrue(self.mydict.local_cache_has_expired())
        self.assertFalse(self.cache.get.called)

    def test_is_expired_last_updated_beyond_timeout(self):
        self.mydict._local_last_updated = time.time() - 101
        self.assertTrue(self.mydict.local_cache_has_expired())

    def test_is_expired_within_bounds(self):
        self.mydict._last_checked_for_remote_changes = time.time()

    def test_is_not_expired_if_remote_cache_is_old(self):
        # set it to an expired time
        self.mydict._local_cache = dict(a=1)
        self.mydict._local_last_updated = time.time() - 101
        self.cache.get.return_value = self.mydict._local_last_updated

        result = self.mydict.local_cache_is_invalid()

        self.cache.get.assert_called_once_with(self.mydict.remote_cache_last_updated_key)
        self.assertFalse(result)

    def test_is_expired_if_remote_cache_is_new(self):
        # set it to an expired time, but with a local cache
        self.mydict._local_cache = dict(a=1)
        self.mydict._last_checked_for_remote_changes = time.time() - 101
        self.cache.get.return_value = time.time()

        result = self.mydict.local_cache_is_invalid()

        self.cache.get.assert_called_once_with(
            self.mydict.remote_cache_last_updated_key
        )
        self.assertEquals(result, True)
Exemple #16
0
    def test_setitem_not_implemented(self):
        d = CachedDict()

        with pytest.raises(NotImplementedError):
            d['x'] = 'foo'
Exemple #17
0
 def setUp(self):
     self.cache = mock.Mock()
     self.mydict = CachedDict(timeout=100, cache=self.cache)
Exemple #18
0
class CachedDictTest(TestCase):
    def setUp(self):
        self.cache = mock.Mock()
        self.mydict = CachedDict(timeout=100, cache=self.cache)

    @mock.patch('modeldict.base.CachedDict._update_cache_data')
    @mock.patch('modeldict.base.CachedDict.local_cache_has_expired',
                mock.Mock(return_value=True))
    @mock.patch('modeldict.base.CachedDict.local_cache_is_invalid',
                mock.Mock(return_value=False))
    def test_expired_does_update_data(self, _update_cache_data):
        self.mydict._local_cache = {}
        self.mydict._last_checked_for_remote_changes = time.time()
        self.mydict._populate()

        self.assertFalse(_update_cache_data.called)

    @mock.patch('modeldict.base.CachedDict._update_cache_data')
    @mock.patch('modeldict.base.CachedDict.local_cache_has_expired',
                mock.Mock(return_value=False))
    @mock.patch('modeldict.base.CachedDict.local_cache_is_invalid',
                mock.Mock(return_value=True))
    def test_reset_does_expire(self, _update_cache_data):
        self.mydict._local_cache = {}
        self.mydict._last_checked_for_remote_changes = time.time()
        self.mydict._populate(reset=True)

        _update_cache_data.assert_called_once_with()

    @mock.patch('modeldict.base.CachedDict._update_cache_data')
    @mock.patch('modeldict.base.CachedDict.local_cache_has_expired',
                mock.Mock(return_value=False))
    @mock.patch('modeldict.base.CachedDict.local_cache_is_invalid',
                mock.Mock(return_value=True))
    def test_does_not_expire_by_default(self, _update_cache_data):
        self.mydict._local_cache = {}
        self.mydict._last_checked_for_remote_changes = time.time()
        self.mydict._populate()

        self.assertFalse(_update_cache_data.called)

    def test_is_expired_missing_last_checked_for_remote_changes(self):
        self.mydict._last_checked_for_remote_changes = None
        self.assertTrue(self.mydict.local_cache_has_expired())
        self.assertFalse(self.cache.get.called)

    def test_is_expired_last_updated_beyond_timeout(self):
        self.mydict._local_last_updated = time.time() - 101
        self.assertTrue(self.mydict.local_cache_has_expired())

    def test_is_expired_within_bounds(self):
        self.mydict._last_checked_for_remote_changes = time.time()

    def test_is_not_expired_if_remote_cache_is_old(self):
        # set it to an expired time
        self.mydict._local_cache = dict(a=1)
        self.mydict._local_last_updated = time.time() - 101
        self.cache.get.return_value = self.mydict._local_last_updated

        result = self.mydict.local_cache_is_invalid()

        self.cache.get.assert_called_once_with(
            self.mydict.remote_cache_last_updated_key)
        self.assertFalse(result)

    def test_is_expired_if_remote_cache_is_new(self):
        # set it to an expired time, but with a local cache
        self.mydict._local_cache = dict(a=1)
        self.mydict._last_checked_for_remote_changes = time.time() - 101
        self.cache.get.return_value = time.time()

        result = self.mydict.local_cache_is_invalid()

        self.cache.get.assert_called_once_with(
            self.mydict.remote_cache_last_updated_key)
        self.assertEquals(result, True)