示例#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)
示例#2
0
 def test_no_ttl(self):
     l = TTLRU(2)
     l.set_with_ttl(0, 0, -1)
     l.set_with_ttl(1, 1, int(20e6))
     self.assertEqual(l.items(), [(1, 1), (0, 0)])
     time.sleep(0.02)  # approximately 0.02s
     self.assertEqual(l.items(), [(0, 0)])
     time.sleep(0.02)  # approximately 0.04s
     self.assertEqual(l.items(), [(0, 0)])
示例#3
0
 def test_set_with_ttl(self):
     l = TTLRU(2)
     l.set_with_ttl(0, 0, int(20e6))
     l.set_with_ttl(1, 1, int(80e6))
     self.assertTrue(0 in l)
     self.assertTrue(1 in l)
     time.sleep(0.01)  # approximately 0.01s
     self.assertTrue(0 in l)
     self.assertTrue(1 in l)
     time.sleep(0.01)  # approximately 0.02s
     self.assertTrue(0 not in l)
     self.assertTrue(1 in l)
     time.sleep(0.01)  # approximately 0.03s
     self.assertTrue(0 not in l)
     self.assertTrue(1 in l)
     time.sleep(0.04)  # approximately 0.07s
     self.assertTrue(0 not in l)
     self.assertTrue(1 in l)
     time.sleep(0.01)  # approximately 0.08s
     self.assertTrue(0 not in l)
     self.assertTrue(1 not in l)