def test_new_level(self): # 3-level cache, max 2 entries each level cache = PnrpCache(1000, 2, 0, 1) cache.add(201, 1) cache.add(301, 1) # should create a new level, containing (0, 1) self.assertEquals(2, len(cache._PnrpCache__levels)) cache.get(0) # make sure these are all still cached cache.get(201) cache.get(301) cache.add(21, 1) cache.add(31, 1) # should create a new level, containing (0, 1) self.assertEquals(3, len(cache._PnrpCache__levels)) cache.get(0) # make sure these are all still cached cache.get(21) cache.get(31) cache.get(201) cache.get(301) cache.add(2, 1) self.sleep(.1) cache.add(3, 1) # should evict (2, 1) self.assertUncached(cache, 2)
def test_add(self): cache = PnrpCache(10, 10, 0, 1) try: cache.add(-1, 0) self.fail('added a negative peer id') except: pass try: cache.add(100, 0) self.fail('added a peer id outside of the cloud namespace') except: pass cache.add(2, 3) self.assertEqual(3, cache.get(2)) cache.add(4, 5) cache.add(6, 7) cache.add(8, 9) self.assertEqual(5, cache.get(4)) self.assertEqual(7, cache.get(6)) cache.add(0, 9) self.assertEqual(9, cache.get(0))
def test_add(self): cache = PnrpCache(10, 10, 0, 1) try: cache.add(-1, 0) self.fail('added a negative peer id') except: pass try: cache.add(100, 0) self.fail('added a peer id outside of the cloud namespace') except: pass cache.add(2, 3) self.assertEqual(3, cache.get(2)) cache.add(4, 5) cache.add(6, 7) cache.add(8, 9) self.assertEqual(5, cache.get(4)) self.assertEqual(7, cache.get(6)) cache.add(0, 9) self.assertEqual(9, cache.get(0))
def test_new_level(self): # 3-level cache, max 2 entries each level cache = PnrpCache(1000, 2, 0, 1) cache.add(201, 1) cache.add(301, 1) # should create a new level, containing (0, 1) self.assertEquals(2, len(cache._PnrpCache__levels)) cache.get(0) # make sure these are all still cached cache.get(201) cache.get(301) cache.add(21, 1) cache.add(31, 1) # should create a new level, containing (0, 1) self.assertEquals(3, len(cache._PnrpCache__levels)) cache.get(0) # make sure these are all still cached cache.get(21) cache.get(31) cache.get(201) cache.get(301) cache.add(2, 1) self.sleep(.1) cache.add(3, 1) # should evict (2, 1) self.assertUncached(cache, 2)
def test_seed_not_evicted(self): cache = PnrpCache(10, 2, 0, 1) cache.add(2, 3) cache.add(4, 5) cache.get(0) cache.get(4) self.assertUncached(cache, 2)
def test_seed_not_evicted(self): cache = PnrpCache(10, 2, 0, 1) cache.add(2, 3) cache.add(4, 5) cache.get(0) cache.get(4) self.assertUncached(cache, 2)
def test_eviction(self): """ NOTE: This uses select([], [], [], ...) as a poor-man's sleep() function. This may not work on Windows! """ cache = PnrpCache(10, 3, 0, 1) # only one level cache.add(2, 3) self.sleep(.1) cache.add(4, 5) # cache is now full. next addition should evict id 2 (not 0; it's the seed) self.sleep(.1) cache.add(6, 7) self.assertUncached(cache, 2) # check that get() updates timestamp for LRU cache.get(4) cache.add(8, 9) self.assertUncached(cache, 6)
def test_eviction(self): """ NOTE: This uses select([], [], [], ...) as a poor-man's sleep() function. This may not work on Windows! """ cache = PnrpCache(10, 3, 0, 1) # only one level cache.add(2, 3) self.sleep(.1) cache.add(4, 5) # cache is now full. next addition should evict id 2 (not 0; it's the seed) self.sleep(.1) cache.add(6, 7) self.assertUncached(cache, 2) # check that get() updates timestamp for LRU cache.get(4) cache.add(8, 9) self.assertUncached(cache, 6)
def test_create(self): # try creating a cache with level size 0 self.assertRaises(AssertionError, PnrpCache, 5, 0, 3, 4) # try create a cache with cloud size -1 self.assertRaises(AssertionError, PnrpCache, -1, 3, 3, 4) # try creating a cache with seed > cloud size self.assertRaises(AssertionError, PnrpCache, 5, 3, 10, 4) # try creating a cache with seed -2 self.assertRaises(AssertionError, PnrpCache, 5, 3, -2, 4) # create a cache with only one level cache = PnrpCache(10, 10, 0, 1) self.assertEqual(1, cache.get(0)) self.assertUncached(cache, 2)
def test_create(self): # try creating a cache with level size 0 self.assertRaises(AssertionError, PnrpCache, 5, 0, 3, 4) # try create a cache with cloud size -1 self.assertRaises(AssertionError, PnrpCache, -1, 3, 3, 4) # try creating a cache with seed > cloud size self.assertRaises(AssertionError, PnrpCache, 5, 3, 10, 4) # try creating a cache with seed -2 self.assertRaises(AssertionError, PnrpCache, 5, 3, -2, 4) # create a cache with only one level cache = PnrpCache(10, 10, 0, 1) self.assertEqual(1, cache.get(0)) self.assertUncached(cache, 2)