コード例 #1
0
    def test_ttl(self):
        cache = caches.DequeOutTTLCache(2, ttl=1, timer=_Timer())
        expect(cache.timer()).to(equal(0))
        expect(cache.ttl).to(equal(1))

        cache[1] = 1
        expect(set(cache)).to(equal({1}))
        expect(len(cache)).to(equal(1))
        expect(cache[1]).to(equal(1))

        cache.timer.tick()
        expect(set(cache)).to(equal({1}))
        expect(len(cache)).to(equal(1))
        expect(cache[1]).to(equal(1))

        cache[2] = 2
        expect(set(cache)).to(equal({1, 2}))
        expect(len(cache)).to(equal(2))
        expect(cache[1]).to(equal(1))
        expect(cache[2]).to(equal(2))

        cache.timer.tick()
        expect(set(cache)).to(equal({2}))
        expect(len(cache)).to(equal(1))
        expect(cache[2]).to(equal(2))
        expect(cache.get(1)).to(be_none)
コード例 #2
0
 def test_lru(self):
     lru_limit = 2
     expiry = 100
     cache = caches.DequeOutTTLCache(lru_limit, expiry)
     cache[1] = 1
     cache[2] = 2
     cache[3] = 3
     expect(len(cache)).to(equal(2))
     expect(cache[2]).to(equal(2))
     expect(cache[3]).to(equal(3))
     expect(cache.get(1)).to(be_none)
     expect(len(cache.out_deque)).to(be(1))
     cache[4] = 4
     expect(cache.get(2)).to(be_none)
     expect(len(cache.out_deque)).to(be(2))
コード例 #3
0
 def test_constructor_should_accept_deques(self):
     a_deque = collections.deque()
     c = caches.DequeOutTTLCache(3, 3, out_deque=a_deque)
     expect(c.out_deque).to(be(a_deque))
コード例 #4
0
 def test_constructor_should_fail_on_bad_deques(self):
     testf = lambda: caches.DequeOutTTLCache(
         _TEST_NUM_ENTRIES, _TEST_TTL, out_deque=object())
     expect(testf).to(raise_error(ValueError))
コード例 #5
0
 def test_constructor_should_set_up_a_default_deque(self):
     c = caches.DequeOutTTLCache(_TEST_NUM_ENTRIES, _TEST_TTL)
     expect(c.out_deque).to(be_a(collections.deque))