def test_constructor(self): for i in range(5): name = self.generate_string() cache = Cache(name) self.assertEqual(name, cache.name) self.assertEqual(cache.allow_type_override, True) for j in range(5): cache = Cache() cache.allow_type_override = False self.assertEqual(cache.name, None) self.assertEqual(cache.allow_type_override, False)
def __init__(self, command_prefix: str = "!", **options): __intents = discord.Intents.default() __intents.members = True __intents.presences = True super().__init__(command_prefix=command_prefix, intents=__intents, activity=discord.Game(name="Say !help"), **options) self.remove_command("help") self.on_ready_called = False self.aiohttp_session = aiohttp.ClientSession(loop=self.loop) self.mwiki_cache = Cache("mwiki") self.wiki_cache = Cache("wiki")
def test_lfu(tmpdir, storage): filepath = None if storage == 'memory' else f'{tmpdir}/cache' cache = Cache(filepath=filepath, maxsize=2, ttl=-1, policy='LFU') @cache def func(a): return a def keys(): return [arg for (fn_name, arg), v in cache.items()] assert func(1) == 1 assert func(2) == 2 the_keys = keys() assert len(the_keys) == 2 assert 1 in the_keys and 2 in the_keys assert func(1) == 1 assert func(1) == 1 assert func(2) == 2 assert func(2) == 2 assert func(3) == 3 the_keys = keys() assert len(the_keys) == 2 assert 1 in the_keys and 2 in the_keys assert 3 not in the_keys
def setUp(self): self.fake_bus = FakeBus() self.fake_tracker = FakeTracker() self.default_cache = Cache( 0, # Cache id. CachingTest._NUMBER_OF_CACHE_LINES, CachingTest._SIZE_OF_CACHE_LINE, self.fake_bus, self.fake_tracker, debug_mode=False) # Simulations for the test_trace tests. self.sc_simulation = SimulationEnvironment( CachingTest._NUMBER_OF_PROCESSORS, CachingTest._NUMBER_OF_CACHE_LINES, CachingTest._SIZE_OF_CACHE_LINE, "SC", debug_mode=True, # Turn on to force consistency checks. ) self.tso_simulation = SimulationEnvironment( CachingTest._NUMBER_OF_PROCESSORS, CachingTest._NUMBER_OF_CACHE_LINES, CachingTest._SIZE_OF_CACHE_LINE, "TSO", debug_mode=True, # Turn on to force consistency checks. write_buffer_size=32, retire_at_count=1)
def test_len(self): n = 3000 cache = Cache(self.generate_string()) for i in range(n): cache.add(self.generate_string()) self.assertEqual(len(cache), n)
def setUp(self): # backup real settings self.copy_LogFile = C.LogFile self.copy_CacheFile = C.CacheFile self.copy_CacheType = C.CacheType # create test settings C.CacheFile = tempfile.NamedTemporaryFile("w") C.LogFile = tempfile.NamedTemporaryFile("w") C.CacheType = "file" self.cache = Cache() self.cache.load() self.testentries = { "testkey1": { "title": "TESTKEY_TITLE 1", "date_last_update": int(time.time()) - 86400, "cache_type": "negative", "cache_version": 0.1 }, "testkey2": { "title": "TESTKEY_TITLE 2", "date_last_update": int(time.time()) - 43200, "cache_type": "permanent", "cache_version": 0.1 }, "testkey3": { "title": "TESTKEY_TITLE 3", "date_last_update": int(time.time()), "cache_type": "positive", "cache_version": 0.1 } }
def test_sc_read_latency(self): """Tests the measurements of read-latency in an SC cache.""" # Cache with 16 lines, 8 words/line. cache = Cache(1, 16, 8, self.fake_bus, self.fake_tracker, False) # Test object in cache. cache.read(1) # Prime cache. cache.latency = 0 cache.read(1) self.assertEqual(cache.latency, 2) cache.latency = 0 # Test line returned by other processor. cache.read(9) self.assertEqual(cache.latency, 22) cache.latency = 0 # Test line returned by main memory. # Duck-punch the fake bus to claim that it read from memory. # (Don't you love Python? Such abuse! :D) old_read_miss = FakeBus.read_miss def new_read_miss(self, cache_id, address): return True FakeBus.read_miss = new_read_miss cache.read(18) self.assertEqual(cache.latency, 222) FakeBus.read_miss = old_read_miss
class TestRedisProxy(unittest.TestCase): cache = Cache() cache.setDebug(False) cache.setRedis(redisHost, redisPort, redisDB) cache.setExpiry(expiryTime) cache.setMaxKeys(maxKeys) def test_cache(self): i = 0 while i <= 2 * maxKeys: data = self.cache.get("testkey-%s" % (i)).data self.assertEqual(int(data), i) i = i + 1 def test_missing(self): self.assertEqual(self.cache.get("nosuchkey"), False) def test_lru(self): data = self.cache.get("lrutest") lruDate = data.created i = 0 while i <= 2 * maxKeys: self.cache.get("testkey-%s" % (i)) i = i + 1 time.sleep(1) data = self.cache.get("lrutest") self.assertNotEqual(data.created, lruDate) def test_expiry(self): entry = self.cache.get("testkey-%s" % (1)) time.sleep(expiryTime + 1) newentry = self.cache.get("testkey-%s" % (1)) self.assertNotEqual(entry.created, newentry.created)
def test_clear(self): cache = Cache() for i in range(100): cache[str(i)] = i self.assertTrue(len(cache) == 100) cache.clear() self.assertTrue(len(cache) == 0)
def test_contains(self): cache_a = Cache(self.generate_string()) cache_b = Cache(self.generate_string()) key1 = self.generate_string() key2 = self.generate_string() cache_a[key1] = random.randint(0, 100) cache_a[key2] = random.randint(0, 100) key3 = self.generate_string() cache_b[key3] = random.randint(0, 100) self.assertTrue(key1 in cache_a) self.assertTrue(key2 in cache_a) self.assertFalse(key3 in cache_a) self.assertTrue(key3 in cache_b) self.assertFalse(key1 in cache_b) self.assertFalse("afdsgfjhgfgfa" in cache_b)
def test_getters_setters(self): cache = Cache() cache["a"] = "Single string" random_list = [self.generate_string() for _ in range(5)] cache["b"] = random_list cache["b"].append(50) cache["c"] = "" cache["d"] = None multilevel_dict = { "first": self.generate_string(), "second": { "list": random_list, "dict": { "cache1": Cache(), "adsfada": self.generate_string() }, "cache2": Cache() } } cache["e"] = multilevel_dict cache["e"]["second"]["dict"]["cache1"]["testval"] = random_list cache[123] = True func_key = self.generate_string cache[func_key] = "Functions and builtin objects are allowed too" self.assertEqual(cache["a"], "Single string") self.assertEqual(cache["b"], random_list) self.assertTrue(50 in cache["b"]) self.assertEqual(cache["c"], "") self.assertEqual(cache["d"], None) self.assertEqual(cache["e"], multilevel_dict) self.assertTrue("testval" in cache["e"]["second"]["dict"]["cache1"]) self.assertEqual(cache["e"]["second"]["dict"]["cache1"]["testval"], random_list) self.assertEqual(cache.get("d"), None) self.assertEqual(cache.get("sfdsa", -1), -1) self.assertEqual(cache.get("e"), cache["e"]) self.assertEqual(cache[123], True) self.assertEqual(cache[func_key], "Functions and builtin objects are allowed too")
def test_repr(): c = Cache(maxsize=1, ttl=1, filepath=None, policy='FIFO', only_on_errors=False, x='y') expected = ("Cache(maxsize=1, ttl=1, filepath=None, policy='FIFO', " f"key={make_key}, only_on_errors=False, x='y')") assert repr(c) == expected
def test_iter(self): cache = Cache() d = {} for key in range(50): val = self.generate_string() cache[str(key)] = val d[str(key)] = val for cache_item in cache: self.assertEqual(cache_item[1].value, d[cache_item[0]])
def setUp(self): self.fake_bus = FakeBus() self.fake_tracker = FakeTracker() self.default_cache = Cache( 0, # Cache id. CachingTest._NUMBER_OF_CACHE_LINES, CachingTest._SIZE_OF_CACHE_LINE, self.fake_bus, self.fake_tracker, debug_mode=False)
def main(): args = parser.parse_args() if args.load: cache = CsvCacheManager.load_cache_from(args.load) else: cache = Cache() server = Server(cache) try: server.run() except KeyboardInterrupt: print('Server shutdown') finally: CsvCacheManager.save_cache_to(server.cache, args.save)
def test_poppers_del(self): cache = Cache() list_ = [1, 2, 3, 4, 5] cache[1.4564] = "a" cache["val0"] = 65465 cache["val1"] = "aaaaa" cache["val2"] = list_ cache["val3"] = list_ + [6, 7, 8] self.assertEqual(cache.popitem()[1], [1, 2, 3, 4, 5, 6, 7, 8]) self.assertEqual(cache.popitem()[1], list_) self.assertEqual(cache.pop("val0"), 65465) self.assertEqual(cache.pop(1.4564), "a") self.assertTrue(len(cache) == 1)
def test_delete_unpopular(self): cache = Cache() for i in range(10): cache.add(i) asdf = cache["3"] sfdg = cache["4"] srgd = cache.get("3") jhkh = cache.get("4") deleted = cache.delete_unpopular(2) self.assertEqual(deleted, 8) self.assertTrue(len(cache) == 2)
def setUp(self): self.tracker = SimulationStatisticsTracker() self.bus = Bus() self.caches = [] for i in range(4): cache = Cache( i, # Cache id StatisticsTest._NUMBER_OF_CACHE_LINES, StatisticsTest._SIZE_OF_CACHE_LINE, self.bus, self.tracker, debug_mode=False) self.caches.append(cache)
def testListArgs(self): cache = Cache() invocations = [] # args of actual invocations @cache.keep def sum_(numbers): invocations.append(True) return sum(numbers) v1 = sum_(list(range(10))) v2 = sum_([0, 1, 2, 3, 4] + [5, 6, 7, 8, 9]) self.assertEquals(45, v1) self.assertEquals(45, v2) self.assertEquals(1, len(invocations))
def setUp(self): # backup real settings self.copy_LogFile = C.LogFile self.copy_CacheFile = C.CacheFile self.copy_CacheType = C.CacheType # create test settings C.CacheFile = tempfile.NamedTemporaryFile("w") C.LogFile = tempfile.NamedTemporaryFile("w") C.CacheType = "file" self.cache = Cache() self.cache.load()
def test_remove(tmpdir, cache): tmpdir = str(tmpdir) cache.remove() filepath = f'{tmpdir}/cache' cache = Cache(filepath=filepath) assert os.path.isfile(filepath) assert os.listdir(tmpdir) == ['cache'] cache[1] = 'one' cache[2] = 'two' assert os.path.isfile(filepath) assert os.listdir(tmpdir) == ['cache'] cache.remove() assert not os.path.isfile(filepath) assert os.listdir(tmpdir) == []
def testFactorial(self): cache = Cache() invocations = [] # args of actual invocations @cache.keep def fact(n): invocations.append(n) if n == 1: return 1 return n * fact(n - 1) fact(5) f6 = fact(6) self.assertEquals(720, f6) self.assertEquals([5, 4, 3, 2, 1, 6], invocations)
def test_delete_deprecated(self): cache = Cache() cache.set_item_lifetime(seconds=1) for i in range(10): cache.add(i) time.sleep(0.5) deleted = cache.delete_deprecated() self.assertTrue(len(cache) == 1) self.assertEqual(deleted, 9) cache.reset_item_lifetime() self.assertEqual(cache.item_lifetime, None) cache.delete_deprecated() self.assertTrue(len(cache) == 1)
def test_delete_delegated(self): cache = Cache() def delete_item(cache_item: CacheItem): return cache_item.key % 10 == 0 or cache_item.value % 25 == 0 for i in range(1, 101): cache[i] = i num_deleted = cache.delete_delegated(delete_item) # Should be deleted: 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 25, 75 (10 based on keys, 2 based on values) self.assertEqual(num_deleted, 10 + 2) for key, value in cache.items(): cond = key % 10 != 0 and value.value % 25 != 0 self.assertTrue(cond)
def main(): loop = asyncio.get_event_loop() args = parser.parse_args() if args.load: cache = CsvCacheManager.load_cache_from(args.load) else: cache = Cache() server = AsyncServer(loop, cache) try: loop.create_task(server.run()) loop.run_forever() #loop.run_until_complete(server.run()) except KeyboardInterrupt: print('Server shutdown') finally: CsvCacheManager.save_cache_to(server.cache, args.save)
def testDictListArgs(self): cache = Cache() invocations = [] # args of actual invocations @cache.keep def sum_(num_dict): invocations.append(True) num_lists = num_dict.values() nums = [] for nl in num_lists: nums.extend(nl) # can't call mu directly! :) return sum(nums) v1 = sum_({'a': [1, 2, 3], 'b': [4, 5, 6]}) v2 = sum_({'b': ([4, 5] + [6]), 'a': [1, 2, 3]}) self.assertEquals(21, v1) self.assertEquals(21, v2) self.assertEquals(1, len(invocations))
def test_add_delete(self): cache = Cache() cache.allow_type_override = False cache.add(1234) cache.add([x for x in range(10)]) cache.add([random.randint(0, 100) for _ in range(10)], "randList") self.assertTrue(len(cache) == 3) cache.delete("1234") self.assertTrue(len(cache) == 2) del cache["randList"] self.assertTrue(len(cache) == 1) cache.add(123, "123") cache.add(123) cache.add("Does not raise", 123) self.assertRaises(TypeError, cache.add, "Should raise", "123") self.assertRaises(TypeError, cache.add, [], "123") self.assertRaises(TypeError, cache.add, 0, 123)
def test_readFile(self): name = "./test_file.txt" cache = Cache(chunk_size = 1) result = cache.readFile(name) expected = [b'a', b'b', b'c', b'd', b'\r', b'\n'] cm = cache.client.get("{}:hash".format(name)) file = open(name, 'rb') content = file.read() file.close() cm2 = hashlib.md5(content).digest() assert result == expected assert cm == cm2 assert cache.client.get("{}:0".format(name)) == b'a' assert cache.client.get("{}:1".format(name)) == b'b' assert cache.client.get("{}:2".format(name)) == b'c' assert cache.client.get("{}:3".format(name)) == b'd' assert cache.client.get("{}:4".format(name)) == b'\r' assert cache.client.get("{}:5".format(name)) == b'\n'
def test_sc_write_latency(self): """Tests the measurements of write-latency in an SC cache. All writes in SC take 222 cycles, due to write-through cache.""" # Cache with 16 lines, 8 words/line. cache = Cache(1, 16, 8, self.fake_bus, self.fake_tracker, False) # Test writing to something in cache. cache.write(5) # Prime cache. cache.latency = 0 cache.write(5) self.assertEqual(cache.latency, 222) cache.latency = 0 # Test writing to something that another processor can return. cache.write(9) self.assertEqual(cache.latency, 222) cache.latency = 0 # Test value returned by main memory. # Duck-punch the fake bus to claim that it read from memory. # (Don't you love Python? Such abuse! :D) old_read_miss = FakeBus.read_miss def new_read_miss(self, cache_id, address): return True FakeBus.read_miss = new_read_miss cache.write(20) self.assertEqual(cache.latency, 222) # Restore the FakeBus class. FakeBus.read_miss = old_read_miss
def cache(tmpdir, request): filepath = request.param and f'{tmpdir}/cache' or None with Cache(filepath=filepath) as c: yield c