Example #1
0
    def test_peek_last_item_ttl(self):
        l = TTLRU(2)
        l.set_with_ttl(0, 0, int(80e6))
        l.set_with_ttl(1, 1, int(20e6))
        self.assertEqual(l.peek_first_item(), (1, 1))
        self.assertEqual(l.peek_last_item(), (0, 0))

        time.sleep(0.01)  # approximately 0.01s
        self.assertEqual(l.peek_last_item(), (0, 0))

        time.sleep(0.01)  # approximately 0.02s
        self.assertEqual(l.peek_last_item(), (0, 0))

        time.sleep(0.05)  # approximately 0.07s
        self.assertEqual(l.peek_last_item(), (0, 0))

        time.sleep(0.01)  # approximately 0.08s
        self.assertEqual(l.peek_first_item(), None)

        l.set_with_ttl(0, 0, int(10e6))
        l.set_with_ttl(1, 1, int(20e6))

        self.assertEqual(l.peek_last_item(), (0, 0))
        time.sleep(0.01)  # approximately 0.01s
        self.assertEqual(l.peek_last_item(), (1, 1))
        time.sleep(0.01)  # approximately 0.02s
        self.assertEqual(l.peek_first_item(), None)
Example #2
0
    def test_ref_count(self):
        l = TTLRU(2, ttl=int(20e6))
        x = {1: 2}
        self.assertEqual(sys.getrefcount(x), 2)
        l[1] = x
        self.assertEqual(sys.getrefcount(x), 3)
        time.sleep(0.02)
        self.assertEqual(sys.getrefcount(x), 3)
        l.get(1)
        self.assertEqual(sys.getrefcount(x), 2)

        # ====================
        l = TTLRU(2, ttl=int(20e6))
        x = {1: 2}
        self.assertEqual(sys.getrefcount(x), 2)
        l[1] = x
        self.assertEqual(sys.getrefcount(x), 3)
        time.sleep(0.02)
        self.assertEqual(sys.getrefcount(x), 3)
        l.peek_first_item()
        self.assertEqual(sys.getrefcount(x), 2)

        # ====================
        l = TTLRU(2, ttl=int(20e6))
        x = {1: 2}
        self.assertEqual(sys.getrefcount(x), 2)
        l[1] = x
        self.assertEqual(sys.getrefcount(x), 3)
        time.sleep(0.02)
        self.assertEqual(sys.getrefcount(x), 3)
        l.peek_last_item()
        self.assertEqual(sys.getrefcount(x), 2)

        # ====================
        l = TTLRU(2, ttl=int(20e6))
        x = {1: 2}
        self.assertEqual(sys.getrefcount(x), 2)
        l[1] = x
        self.assertEqual(sys.getrefcount(x), 3)
        time.sleep(0.02)
        self.assertEqual(sys.getrefcount(x), 3)
        l.keys()
        self.assertEqual(sys.getrefcount(x), 2)

        # ====================
        l = TTLRU(2, ttl=int(20e6))
        x = {1: 2}
        self.assertEqual(sys.getrefcount(x), 2)
        l[1] = x
        self.assertEqual(sys.getrefcount(x), 3)
        time.sleep(0.02)
        self.assertEqual(sys.getrefcount(x), 3)
        l[1] = 2
        self.assertEqual(sys.getrefcount(x), 2)

        # ====================
        l = TTLRU(2, ttl=int(20e6))
        x = {1: 2}
        self.assertEqual(sys.getrefcount(x), 2)
        l[1] = x
        self.assertEqual(sys.getrefcount(x), 3)
        time.sleep(0.02)
        self.assertEqual(sys.getrefcount(x), 3)
        1 in l
        self.assertEqual(sys.getrefcount(x), 2)

        l = TTLRU(2, ttl=int(20e6))
        x = {1: 2}
        self.assertEqual(sys.getrefcount(x), 2)
        l[1] = x
        self.assertEqual(sys.getrefcount(x), 3)
        time.sleep(0.02)
        self.assertEqual(sys.getrefcount(x), 3)
        try:
            l[1]
        except:
            pass
        self.assertEqual(sys.getrefcount(x), 2)

        # ====================
        l = TTLRU(2, ttl=int(20e6))
        x = {1: 2}
        self.assertEqual(sys.getrefcount(x), 2)
        l[1] = x
        self.assertEqual(sys.getrefcount(x), 3)
        l[1] = 2
        self.assertEqual(sys.getrefcount(x), 2)
Example #3
0
 def test_peek_last_item(self):
     l = TTLRU(2)
     self.assertEqual(None, l.peek_last_item())
     l[1] = '1'
     l[2] = '2'
     self.assertEqual((1, '1'), l.peek_last_item())