Esempio n. 1
0
    def test_remove_nothing(self):
        cache = GenerationalCache()
        cache.add(self.obj1)

        present = cache.remove(self.obj2)
        self.assertFalse(present)
        self.assertEqual(cache.get_cached(), [self.obj1])
Esempio n. 2
0
def test_generational_cache_two_generations(obj1, obj2):
    """
    Inserting more objects than the cache's size causes the cache
    to contain two generations, each holding up to <size> objects.
    """
    cache = GenerationalCache(1)
    cache.add(obj1)
    cache.add(obj2)

    assert sorted(cache.get_cached()) == [obj1, obj2]
Esempio n. 3
0
    def test_two_generations(self):
        """
        Inserting more objects than the cache's size causes the cache
        to contain two generations, each holding up to <size> objects.
        """
        cache = GenerationalCache(1)
        cache.add(self.obj1)
        cache.add(self.obj2)

        self.assertEqual(sorted(cache.get_cached()), [self.obj1, self.obj2])
Esempio n. 4
0
    def test_two_generations(self):
        """
        Inserting more objects than the cache's size causes the cache
        to contain two generations, each holding up to <size> objects.
        """
        cache = GenerationalCache(1)
        cache.add(self.obj1)
        cache.add(self.obj2)

        self.assertEqual(sorted(cache.get_cached()), [self.obj1, self.obj2])
Esempio n. 5
0
 def test_size_limit(self):
     """
     A cache will never hold more than twice its size in objects.  The
     generational system is what prevents it from holding exactly the
     requested number of objects.
     """
     size = 10
     cache = GenerationalCache(size)
     for value in xrange(5 * size):
         cache.add(StubObjectInfo(value))
     self.assertEquals(len(cache.get_cached()), size * 2)
Esempio n. 6
0
 def test_size_limit(self):
     """
     A cache will never hold more than twice its size in objects.  The
     generational system is what prevents it from holding exactly the
     requested number of objects.
     """
     size = 10
     cache = GenerationalCache(size)
     for value in range(5 * size):
         cache.add(StubObjectInfo(value))
     self.assertEquals(len(cache.get_cached()), size * 2)
Esempio n. 7
0
    def test_remove_object(self):
        cache = GenerationalCache()
        cache.add(self.obj1)
        cache.add(self.obj2)
        cache.add(self.obj3)

        present = cache.remove(self.obj2)
        self.assertTrue(present)
        self.assertEqual(sorted(cache.get_cached()), [self.obj1, self.obj3])
Esempio n. 8
0
def test_generational_cache_remove_object(obj1, obj2, obj3):
    cache = GenerationalCache()
    cache.add(obj1)
    cache.add(obj2)
    cache.add(obj3)

    present = cache.remove(obj2)
    assert present
    assert sorted(cache.get_cached()) == [obj1, obj3]
Esempio n. 9
0
def test_generational_cache_clear_cache_clears_the_second_generation(
        obj1, obj2):
    cache = GenerationalCache(1)
    cache.add(obj1)
    cache.add(obj2)
    cache.clear()
    assert cache.get_cached() == []
Esempio n. 10
0
 def test_set_size_larger_than_current_size(self):
     """
     Setting the cache size to something more than the number of
     objects in the cache does not affect its current contents,
     and will merge any elements from the second generation into
     the first one.
     """
     cache = GenerationalCache(1)
     cache.add(self.obj1) # new=[1]    old=[]
     cache.add(self.obj2) # new=[2]    old=[1]
     cache.set_size(2)    # new=[1, 2] old=[]
     cache.add(self.obj3) # new=[3]    old=[1, 2]
     self.assertEqual(sorted(cache.get_cached()),
                      [self.obj1, self.obj2, self.obj3])
Esempio n. 11
0
def test_generational_cache_evict_oldest(obj1, obj2, obj3):
    """The "oldest" object is the first to be evicted."""
    cache = GenerationalCache(1)
    cache.add(obj1)
    cache.add(obj2)
    cache.add(obj3)

    assert sorted(cache.get_cached()) == [obj2, obj3]
Esempio n. 12
0
    def test_evict_oldest(self):
        """The "oldest" object is the first to be evicted."""
        cache = GenerationalCache(1)
        cache.add(self.obj1)
        cache.add(self.obj2)
        cache.add(self.obj3)

        self.assertEqual(sorted(cache.get_cached()), [self.obj2, self.obj3])
Esempio n. 13
0
def storm_cache_factory():
    """Return a Storm Cache of the type and size specified in dbconfig."""
    if dbconfig.storm_cache == 'generational':
        return GenerationalCache(int(dbconfig.storm_cache_size))
    elif dbconfig.storm_cache == 'stupid':
        return StupidCache(int(dbconfig.storm_cache_size))
    elif dbconfig.storm_cache == 'default':
        return Cache(int(dbconfig.storm_cache_size))
    else:
        assert False, "Unknown storm_cache %s." % dbconfig.storm_cache
Esempio n. 14
0
    def test_evict_LRU(self):
        """
        Actually, it's not the oldest but the LRU object that is first
        to be evicted.  Re-adding the oldest object makes it not be
        the LRU.
        """
        cache = GenerationalCache(1)
        cache.add(self.obj1)
        cache.add(self.obj2)

        # This "refreshes" the oldest object in the cache.
        cache.add(self.obj1)

        cache.add(self.obj3)

        self.assertEqual(sorted(cache.get_cached()), [self.obj1, self.obj3])
Esempio n. 15
0
def test_generational_cache_evict_lru(obj1, obj2, obj3):
    """
    Actually, it's not the oldest but the LRU object that is first
    to be evicted.  Re-adding the oldest object makes it not be
    the LRU.
    """
    cache = GenerationalCache(1)
    cache.add(obj1)
    cache.add(obj2)

    # This "refreshes" the oldest object in the cache.
    cache.add(obj1)

    cache.add(obj3)

    assert sorted(cache.get_cached()) == [obj1, obj3]
Esempio n. 16
0
    def test_remove_object(self):
        cache = GenerationalCache()
        cache.add(self.obj1)
        cache.add(self.obj2)
        cache.add(self.obj3)

        present = cache.remove(self.obj2)
        self.assertTrue(present)
        self.assertEqual(sorted(cache.get_cached()), [self.obj1, self.obj3])
Esempio n. 17
0
    def test_remove_from_overlap(self):
        """
        Removing an object from the cache removes it from both its
        primary and secondary generations.
        """
        cache = GenerationalCache(2)
        cache.add(self.obj1) # new=[1]    old=[]
        cache.add(self.obj2) # new=[1, 2] old=[]
        cache.add(self.obj3) # new=[3]    old=[1, 2]
        cache.add(self.obj1) # new=[3, 1] old=[1, 2]

        present = cache.remove(self.obj1)
        self.assertTrue(present)
        self.assertEqual(sorted(cache.get_cached()), [self.obj2, self.obj3])
Esempio n. 18
0
def test_generational_cache_remove_nothing(obj1, obj2):
    cache = GenerationalCache()
    cache.add(obj1)

    present = cache.remove(obj2)
    assert not present
    assert cache.get_cached() == [obj1]
Esempio n. 19
0
    def test_remove_nothing(self):
        cache = GenerationalCache()
        cache.add(self.obj1)

        present = cache.remove(self.obj2)
        self.assertFalse(present)
        self.assertEqual(cache.get_cached(), [self.obj1])
Esempio n. 20
0
 def test_set_size_larger_than_current_size(self):
     """
     Setting the cache size to something more than the number of
     objects in the cache does not affect its current contents,
     and will merge any elements from the second generation into
     the first one.
     """
     cache = GenerationalCache(1)
     cache.add(self.obj1) # new=[1]    old=[]
     cache.add(self.obj2) # new=[2]    old=[1]
     cache.set_size(2)    # new=[1, 2] old=[]
     cache.add(self.obj3) # new=[3]    old=[1, 2]
     self.assertEqual(sorted(cache.get_cached()),
                      [self.obj1, self.obj2, self.obj3])
Esempio n. 21
0
def test_generational_cache_remove_from_overlap(obj1, obj2, obj3):
    """
    Removing an object from the cache removes it from both its
    primary and secondary generations.
    """
    cache = GenerationalCache(2)
    cache.add(obj1)  # new=[1]    old=[]
    cache.add(obj2)  # new=[1, 2] old=[]
    cache.add(obj3)  # new=[3]    old=[1, 2]
    cache.add(obj1)  # new=[3, 1] old=[1, 2]

    present = cache.remove(obj1)
    assert present
    assert sorted(cache.get_cached()) == [obj2, obj3]
Esempio n. 22
0
def test_generational_cache_set_size_limit():
    """
    Setting the size limits the cache's size just like passing an
    initial size would.
    """
    size = 10
    cache = GenerationalCache(size * 100)
    cache.set_size(size)
    for value in iter_range(size * 10):
        cache.add(StubObjectInfo(value))
    assert len(cache.get_cached()) == size * 2
Esempio n. 23
0
def test_generational_cache_three_generations(obj1, obj2, obj3):
    """
    If more than 2*<size> objects come along, only 2*<size>
    objects are retained.
    """
    cache = GenerationalCache(1)
    cache.add(obj1)
    cache.add(obj2)
    cache.add(obj3)

    assert sorted(cache.get_cached()) == [obj2, obj3]
Esempio n. 24
0
 def test_set_size_limit(self):
     """
     Setting the size limits the cache's size just like passing an
     initial size would.
     """
     size = 10
     cache = GenerationalCache(size * 100)
     cache.set_size(size)
     for value in range(size * 10):
         cache.add(StubObjectInfo(value))
     self.assertEquals(len(cache.get_cached()), size * 2)
Esempio n. 25
0
    def test_three_generations(self):
        """
        If more than 2*<size> objects come along, only 2*<size>
        objects are retained.
        """
        cache = GenerationalCache(1)
        cache.add(self.obj1)
        cache.add(self.obj2)
        cache.add(self.obj3)

        self.assertEqual(sorted(cache.get_cached()), [self.obj2, self.obj3])
Esempio n. 26
0
 def test_set_size_smaller_than_current_size(self):
     """
     Setting the size to a smaller size than the number of objects
     currently cached will drop some of the extra content.  Note that
     because of the generation system, it can actually hold two times
     the size requested in edge cases.
     """
     cache = GenerationalCache(150)
     for i in range(250):
         cache.add(StubObjectInfo(i))
     cache.set_size(100)
     cached = cache.get_cached()
     self.assertEquals(len(cached), 100)
     for obj_info in cache.get_cached():
         self.assertTrue(obj_info.id >= 100)
Esempio n. 27
0
    def test_evict_oldest(self):
        """The "oldest" object is the first to be evicted."""
        cache = GenerationalCache(1)
        cache.add(self.obj1)
        cache.add(self.obj2)
        cache.add(self.obj3)

        self.assertEqual(sorted(cache.get_cached()), [self.obj2, self.obj3])
Esempio n. 28
0
def test_generational_cache_generational_overlap(obj1, obj2, obj3):
    """
    An object that is both in the primary and the secondary
    generation is listed only once in the cache's contents.
    """
    cache = GenerationalCache(2)
    cache.add(obj1)  # new=[1]    old=[]
    cache.add(obj2)  # new=[1, 2] old=[]
    cache.add(obj3)  # new=[3]    old=[1, 2]
    cache.add(obj1)  # new=[3, 1] old=[1, 2]

    assert sorted(cache.get_cached()) == [obj1, obj2, obj3]
Esempio n. 29
0
    def test_evict_LRU(self):
        """
        Actually, it's not the oldest but the LRU object that is first
        to be evicted.  Re-adding the oldest object makes it not be
        the LRU.
        """
        cache = GenerationalCache(1)
        cache.add(self.obj1)
        cache.add(self.obj2)

        # This "refreshes" the oldest object in the cache.
        cache.add(self.obj1)

        cache.add(self.obj3)

        self.assertEqual(sorted(cache.get_cached()), [self.obj1, self.obj3])
Esempio n. 30
0
    def test_generational_overlap(self):
        """
        An object that is both in the primary and the secondary
        generation is listed only once in the cache's contents.
        """
        cache = GenerationalCache(2)
        cache.add(self.obj1) # new=[1]    old=[]
        cache.add(self.obj2) # new=[1, 2] old=[]
        cache.add(self.obj3) # new=[3]    old=[1, 2]
        cache.add(self.obj1) # new=[3, 1] old=[1, 2]

        self.assertEqual(sorted(cache.get_cached()),
                         [self.obj1, self.obj2, self.obj3])
Esempio n. 31
0
    def test_remove_from_overlap(self):
        """
        Removing an object from the cache removes it from both its
        primary and secondary generations.
        """
        cache = GenerationalCache(2)
        cache.add(self.obj1) # new=[1]    old=[]
        cache.add(self.obj2) # new=[1, 2] old=[]
        cache.add(self.obj3) # new=[3]    old=[1, 2]
        cache.add(self.obj1) # new=[3, 1] old=[1, 2]

        present = cache.remove(self.obj1)
        self.assertTrue(present)
        self.assertEqual(sorted(cache.get_cached()), [self.obj2, self.obj3])
Esempio n. 32
0
    def test_three_generations(self):
        """
        If more than 2*<size> objects come along, only 2*<size>
        objects are retained.
        """
        cache = GenerationalCache(1)
        cache.add(self.obj1)
        cache.add(self.obj2)
        cache.add(self.obj3)

        self.assertEqual(sorted(cache.get_cached()), [self.obj2, self.obj3])
Esempio n. 33
0
 def test_set_size_limit(self):
     """
     Setting the size limits the cache's size just like passing an
     initial size would.
     """
     size = 10
     cache = GenerationalCache(size * 100)
     cache.set_size(size)
     for value in xrange(size * 10):
         cache.add(StubObjectInfo(value))
     self.assertEquals(len(cache.get_cached()), size * 2)
Esempio n. 34
0
 def test_set_size_smaller_than_current_size(self):
     """
     Setting the size to a smaller size than the number of objects
     currently cached will drop some of the extra content.  Note that
     because of the generation system, it can actually hold two times
     the size requested in edge cases.
     """
     cache = GenerationalCache(150)
     for i in range(250):
         cache.add(StubObjectInfo(i))
     cache.set_size(100)
     cached = cache.get_cached()
     self.assertEquals(len(cached), 100)
     for obj_info in cache.get_cached():
         self.assertTrue(obj_info.id >= 100)
Esempio n. 35
0
    def test_generational_overlap(self):
        """
        An object that is both in the primary and the secondary
        generation is listed only once in the cache's contents.
        """
        cache = GenerationalCache(2)
        cache.add(self.obj1) # new=[1]    old=[]
        cache.add(self.obj2) # new=[1, 2] old=[]
        cache.add(self.obj3) # new=[3]    old=[1, 2]
        cache.add(self.obj1) # new=[3, 1] old=[1, 2]

        self.assertEqual(sorted(cache.get_cached()),
                         [self.obj1, self.obj2, self.obj3])
Esempio n. 36
0
 def test_clear_cache_clears_the_second_generation(self):
     cache = GenerationalCache(1)
     cache.add(self.obj1)
     cache.add(self.obj2)
     cache.clear()
     self.assertEqual(cache.get_cached(), [])
Esempio n. 37
0
def test_generational_cache_clear_cache(obj1):
    cache = GenerationalCache()
    cache.add(obj1)
    cache.clear()
    assert cache.get_cached() == []
Esempio n. 38
0
 def test_clear_cache_clears_the_second_generation(self):
     cache = GenerationalCache(1)
     cache.add(self.obj1)
     cache.add(self.obj2)
     cache.clear()
     self.assertEqual(cache.get_cached(), [])
Esempio n. 39
0
 def test_cache_one_object(self):
     cache = GenerationalCache()
     cache.add(self.obj1)
     self.assertEqual(cache.get_cached(), [self.obj1])
Esempio n. 40
0
 def test_initially_empty(self):
     cache = GenerationalCache()
     self.assertEqual(cache.get_cached(), [])
Esempio n. 41
0
 def test_clear_cache(self):
     cache = GenerationalCache()
     cache.add(self.obj1)
     cache.clear()
     self.assertEqual(cache.get_cached(), [])
Esempio n. 42
0
 def test_initially_empty(self):
     cache = GenerationalCache()
     self.assertEqual(cache.get_cached(), [])
Esempio n. 43
0
 def test_clear_cache(self):
     cache = GenerationalCache()
     cache.add(self.obj1)
     cache.clear()
     self.assertEqual(cache.get_cached(), [])
Esempio n. 44
0
 def test_cache_multiple_objects(self):
     cache = GenerationalCache()
     cache.add(self.obj1)
     cache.add(self.obj2)
     self.assertEqual(sorted(cache.get_cached()), [self.obj1, self.obj2])
Esempio n. 45
0
 def test_cache_one_object(self):
     cache = GenerationalCache()
     cache.add(self.obj1)
     self.assertEqual(cache.get_cached(), [self.obj1])
Esempio n. 46
0
 def test_cache_multiple_objects(self):
     cache = GenerationalCache()
     cache.add(self.obj1)
     cache.add(self.obj2)
     self.assertEqual(sorted(cache.get_cached()), [self.obj1, self.obj2])
Esempio n. 47
0
def test_cache_multiple_objects(obj1, obj2):
    cache = GenerationalCache()
    cache.add(obj1)
    cache.add(obj2)
    assert sorted(cache.get_cached()) == [obj1, obj2]