def test_update_cache(): # Update should add data about a new block in the cache. If the cache is full, the oldest block is dropped. locator_cache = LocatorCache(config.get("LOCATOR_CACHE_SIZE")) block_hash = get_random_value_hex(32) txs = [get_random_value_hex(32) for _ in range(10)] locator_txid_map = {compute_locator(txid): txid for txid in txs} # Cache is empty assert block_hash not in locator_cache.blocks for locator in locator_txid_map.keys(): assert locator not in locator_cache.cache # The data has been added to the cache locator_cache.update(block_hash, locator_txid_map) assert block_hash in locator_cache.blocks for locator in locator_txid_map.keys(): assert locator in locator_cache.cache
def test_update_cache_full(): # Updating a full cache should be dropping the oldest block one by one locator_cache = LocatorCache(config.get("LOCATOR_CACHE_SIZE")) block_hashes = [] big_map = {} # Fill the cache first for i in range(locator_cache.cache_size): block_hash = get_random_value_hex(32) txs = [get_random_value_hex(32) for _ in range(10)] locator_txid_map = {compute_locator(txid): txid for txid in txs} locator_cache.update(block_hash, locator_txid_map) if i == 0: first_block_hash = block_hash first_locator_txid_map = locator_txid_map else: block_hashes.append(block_hash) big_map.update(locator_txid_map) # The cache is now full. assert first_block_hash in locator_cache.blocks for locator in first_locator_txid_map.keys(): assert locator in locator_cache.cache # Add one more block_hash = get_random_value_hex(32) txs = [get_random_value_hex(32) for _ in range(10)] locator_txid_map = {compute_locator(txid): txid for txid in txs} locator_cache.update(block_hash, locator_txid_map) # The first block is not there anymore, but the rest are there assert first_block_hash not in locator_cache.blocks for locator in first_locator_txid_map.keys(): assert locator not in locator_cache.cache for block_hash in block_hashes: assert block_hash in locator_cache.blocks for locator in big_map.keys(): assert locator in locator_cache.cache