示例#1
0
    def test_flush(self, mock_utc_now):
        now = utc_now()
        now_plus_10 = now + datetime.timedelta(seconds=10)
        now_plus_20 = now + datetime.timedelta(seconds=20)

        mock_utc_now.return_value = now
        cache = ExpiringCache(default_ttl=100)

        # At time now
        cache["foo"] = "bar"

        # At time now + 10
        mock_utc_now.return_value = now_plus_10
        cache["foo10"] = "bar"

        # At time now + 20
        mock_utc_now.return_value = now_plus_20
        cache["foo20"] = "bar"

        assert cache._data == {
            "foo": [now + cache._default_ttl, "bar"],
            "foo10": [now_plus_10 + cache._default_ttl, "bar"],
            "foo20": [now_plus_20 + cache._default_ttl, "bar"],
        }

        # Set to now + 105 which expires the first, but not the other two
        mock_utc_now.return_value = now + datetime.timedelta(seconds=105)
        cache.flush()

        # We don't want to trigger eviction or anything like that, so check
        # the contents of the internal data structure directly
        assert cache._data == {
            "foo10": [now_plus_10 + cache._default_ttl, "bar"],
            "foo20": [now_plus_20 + cache._default_ttl, "bar"],
        }
示例#2
0
    def test_flush(self, mock_utc_now):
        now = utc_now()
        now_plus_10 = now + datetime.timedelta(seconds=10)
        now_plus_20 = now + datetime.timedelta(seconds=20)

        mock_utc_now.return_value = now
        cache = ExpiringCache(default_ttl=100)

        # At time now
        cache['foo'] = 'bar'

        # At time now + 10
        mock_utc_now.return_value = now_plus_10
        cache['foo10'] = 'bar'

        # At time now + 20
        mock_utc_now.return_value = now_plus_20
        cache['foo20'] = 'bar'

        assert (cache._data == {
            'foo': [now + cache._default_ttl, 'bar'],
            'foo10': [now_plus_10 + cache._default_ttl, 'bar'],
            'foo20': [now_plus_20 + cache._default_ttl, 'bar'],
        })

        # Set to now + 105 which expires the first, but not the other two
        mock_utc_now.return_value = now + datetime.timedelta(seconds=105)
        cache.flush()

        assert (cache._data == {
            'foo10': [now_plus_10 + cache._default_ttl, 'bar'],
            'foo20': [now_plus_20 + cache._default_ttl, 'bar'],
        })