HEX
Server: Apache/2.4.65 (Ubuntu)
System: Linux ielts-store-v2 6.8.0-1036-gcp #38~22.04.1-Ubuntu SMP Thu Aug 14 01:19:18 UTC 2025 x86_64
User: root (0)
PHP: 7.2.34-54+ubuntu20.04.1+deb.sury.org+1
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,
Upload Files
File: //snap/google-cloud-cli/394/platform/gsutil/third_party/cachetools/tests/test_mru.py
import unittest
import warnings

from cachetools import MRUCache

from . import CacheTestMixin


class MRUCacheTest(unittest.TestCase, CacheTestMixin):
    # TODO: method to create cache that can be overridden
    Cache = MRUCache

    def test_evict__writes_only(self):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            cache = MRUCache(maxsize=2)
        self.assertEqual(len(w), 1)
        self.assertIs(w[0].category, DeprecationWarning)

        cache[1] = 1
        cache[2] = 2
        cache[3] = 3  # Evicts 1 because nothing's been used yet

        assert len(cache) == 2
        assert 1 not in cache, "Wrong key was evicted. Should have been '1'."
        assert 2 in cache
        assert 3 in cache

    def test_evict__with_access(self):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            cache = MRUCache(maxsize=2)
        self.assertEqual(len(w), 1)
        self.assertIs(w[0].category, DeprecationWarning)

        cache[1] = 1
        cache[2] = 2
        cache[1]
        cache[2]
        cache[3] = 3  # Evicts 2
        assert 2 not in cache, "Wrong key was evicted. Should have been '2'."
        assert 1 in cache
        assert 3 in cache

    def test_evict__with_delete(self):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            cache = MRUCache(maxsize=2)
        self.assertEqual(len(w), 1)
        self.assertIs(w[0].category, DeprecationWarning)

        cache[1] = 1
        cache[2] = 2
        del cache[2]
        cache[3] = 3  # Doesn't evict anything because we just deleted 2

        assert 2 not in cache
        assert 1 in cache

        cache[4] = 4  # Should evict 1 as we just accessed it with __contains__
        assert 1 not in cache
        assert 3 in cache
        assert 4 in cache