def test_rr_behavior(self): random.seed(0) cache = RRCache('unittest', max_entries=10, client=self.client) for i in range(20): key = 'key_%2.2d' % i yield md(cache.set, key, 'test') yield md(cache.get, key) keys = yield md(self.client.keys, 'key_*') keys.sort() expected = ['key_00', 'key_01', 'key_04', 'key_07', 'key_09', 'key_11', 'key_13', 'key_16', 'key_17', 'key_19'] self.assertEquals(keys, expected)
def test_lfu_behavior(self): cache = LfuCache('unittest', max_entries=10, client=self.client) for i in range(18): key = 'key_%2.2d' % i yield md(cache.set, key, 'test') for (index, loops) in enumerate(range(17, -1, -1)): key = 'key_%2.2d' % index for i in range(loops): op, args = random.choice([(cache.get, (key,)), (cache.set, (key, 'test',))]) yield md(op, *args) yield md(cache.set, 'key_18', 'test') yield md(cache.set, 'key_19', 'test') keys = yield md(self.client.keys, 'key_*') keys.sort() expected = ['key_00', 'key_01', 'key_02', 'key_03', 'key_04', 'key_05', 'key_06', 'key_07', 'key_08', 'key_09'] self.assertEquals(keys, expected)
def test_happened(self): counter = self.event_count_class('a', client=self.client) for event in (0, 1, 3): yield md(counter.record, event) self.assert_((yield md(counter.happened, 0))) self.assert_((yield md(counter.happened, 1))) self.failIf((yield md(counter.happened, 2))) self.assert_((yield md(counter.happened, 3))) self.failIf((yield md(counter.happened, 4)))
def test_lru_behavior(self): cache = LruCache('unittest', max_entries=10, client=self.client) for i in range(18): key = 'key_%2.2d' % i yield md(cache.set, key, 'test') yield md(cache.get, 'key_00') yield md(cache.set, 'key_01', 'apple') yield md(cache.set, 'key_18', 'test') yield md(cache.set, 'key_19', 'test') keys = yield md(self.client.keys, 'key_*') keys.sort() expected = ['key_00', 'key_01', 'key_12', 'key_13', 'key_14', 'key_15', 'key_16', 'key_17', 'key_18', 'key_19'] self.assertEquals(keys, expected)
def test_lru_cache_set_and_get(self): cache = self.cache_class('unittest', client=self.client) yield md(cache.set, 'a', 'hello') self.assertEquals((yield md(cache.get, 'a')), 'hello')
def test_count(self): counter = self.event_count_class('a', client=self.client) for event in (1, 2, 5, 1, 7, 2, 1): yield md(counter.record, event) self.assertEquals((yield md(counter.count)), 4)
def test_max_event_initializes_key(self): eventcount = self.event_count_class('counter', client=self.client) self.failIf((yield md(self.client.exists, 'counter'))) yield md(eventcount.initialize, 1000) self.assert_((yield md(self.client.exists, 'counter')))