async def test_cache_group_multi_get(): caches = [ CacheGroupItem(SimpleMaxTTLMemoryCache(), True, True, SpeedTier.FAST), CacheGroupItem(SimpleMemoryCache2(), True, True, SpeedTier.FAST), CacheGroupItem(SimpleMemoryCache3(), True, True, SpeedTier.FAST), CacheGroupItem(SimpleMemoryCache4(), True, True, SpeedTier.FAST) ] for cache_item in caches: await cache_item.cache.clear() cache_group = CacheGroup(caches) keys = [str(i) for i in range(len(caches))] values = [str(i) for i in range(len(caches))] await cache_group.multi_set(zip(keys, values, [None for key in keys])) assert await cache_group.multi_get(keys) == values assert await caches[0].cache.multi_get(keys) == values assert await caches[1].cache.multi_get(keys) == values assert await caches[2].cache.multi_get(keys) == values assert await caches[3].cache.multi_get(keys) == values await cache_group._read_caches[0].clear() assert await cache_group.multi_get(keys) == values assert await cache_group._read_caches[1].multi_get(keys) == values assert await cache_group._read_caches[2].multi_get(keys) == values assert await cache_group._read_caches[3].multi_get(keys) == values await cache_group._read_caches[1].clear() assert await cache_group.multi_get(keys) == values assert await cache_group._read_caches[2].multi_get(keys) == values assert await cache_group._read_caches[3].multi_get(keys) == values await cache_group._read_caches[2].clear() assert await cache_group.multi_get(keys) == values assert await cache_group._read_caches[3].multi_get(keys) == values
async def test_cache_group_multi_get(): caches = [ SimpleMaxTTLMemoryCache(), SimpleMemoryCache2(), SimpleMemoryCache3(), SimpleMemoryCache4() ] for cache in caches: await cache.clear() cache_group = CacheGroup(caches) keys = [str(i) for i in range(len(caches))] values = [str(i) for i in range(len(caches))] await cache_group.multi_set(zip(keys, values, [None for key in keys])) assert await cache_group.multi_get(keys) == values assert await caches[0].multi_get(keys) == values assert await caches[1].multi_get(keys) == values assert await caches[2].multi_get(keys) == values assert await caches[3].multi_get(keys) == values await cache_group._caches[0].clear() assert await cache_group.multi_get(keys) == values assert await cache_group._caches[1].multi_get(keys) == values assert await cache_group._caches[2].multi_get(keys) == values assert await cache_group._caches[3].multi_get(keys) == values await cache_group._caches[1].clear() assert await cache_group.multi_get(keys) == values assert await cache_group._caches[2].multi_get(keys) == values assert await cache_group._caches[3].multi_get(keys) == values await cache_group._caches[2].clear() assert await cache_group.multi_get(keys) == values assert await cache_group._caches[3].multi_get(keys) == values
async def test_cache_group_get(): caches = [ CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST), CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST), CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST) ] cache_group = CacheGroup(caches) assert caches[0] is not caches[1] await cache_group._read_caches[0].set('key', 0, 180) assert await cache_group._read_caches[0].get('key') == 0 await cache_group._read_caches[1].set('key', 1, 180) assert await cache_group._read_caches[0].get('key') == 0 assert await cache_group._read_caches[1].get('key') == 1 await cache_group._read_caches[2].set('key', 2, 180) assert await cache_group._read_caches[0].get('key') == 0 assert await cache_group._read_caches[1].get('key') == 1 assert await cache_group._read_caches[2].get('key') == 2 assert await cache_group.get('key') == 0 await cache_group._read_caches[0].delete('key') assert await cache_group.get('key') == 1 await cache_group._read_caches[1].delete('key') assert await cache_group.get('key') == 2 await cache_group._read_caches[2].delete('key') assert await cache_group.get('key') is None
async def test_cache_group_get_batch_jsonrpc_responses( steemd_requests_and_responses): caches = [ SimpleMaxTTLMemoryCache(), SimpleMemoryCache2(), SimpleMemoryCache3(), SimpleMemoryCache4() ] for cache in caches: await cache.clear() cache_group = CacheGroup(caches) req, resp = steemd_requests_and_responses req = JussiJSONRPCRequest.from_request(dummy_request, 0, req) resp['jsonrpc'] = '2.0' batch_req = [req, req, req] batch_resp = [resp, resp, resp] key = jsonrpc_cache_key(req) assert await cache_group.get(key) is None await cache_group.set('last_irreversible_block_num', 15_000_000) await cache_group.cache_jsonrpc_response(batch_req, batch_resp) assert await cache_group.get(key) == resp assert await cache_group.get_batch_jsonrpc_responses(batch_req ) == batch_resp
async def test_cache_group_cache_jsonrpc_response( steemd_requests_and_responses): caches = [ CacheGroupItem(SimpleMaxTTLMemoryCache(), True, True, SpeedTier.FAST), CacheGroupItem(SimpleMemoryCache2(), True, True, SpeedTier.FAST), CacheGroupItem(SimpleMemoryCache3(), True, True, SpeedTier.FAST), CacheGroupItem(SimpleMemoryCache4(), True, True, SpeedTier.FAST) ] for cache_item in caches: await cache_item.cache.clear() cache_group = CacheGroup(caches) req, resp = steemd_requests_and_responses req = JussiJSONRPCRequest.from_request(dummy_request, 0, req) resp['jsonrpc'] = '2.0' key = jsonrpc_cache_key(req) assert await cache_group.get(key) is None await cache_group.set('last_irreversible_block_num', 15_000_000) await cache_group.cache_jsonrpc_response(req, resp) for cache_item in caches: assert await cache_item.cache.get(key ) == resp, f'key:{key} urn:{req.urn}' assert await cache_group.get(key) == resp, f'key:{key} urn:{req.urn}'
async def test_cache_group_set(): caches = [ CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST), CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST), CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST) ] cache_group = CacheGroup(caches) await cache_group.set('key', 1, 180) for i, cache_item in enumerate(caches): assert await cache_item.cache.get('key') == 1
async def test_cache_group_set(): caches = [ SimpleMaxTTLMemoryCache(), SimpleMemoryCache2(), SimpleMemoryCache3(), SimpleMemoryCache4() ] for cache in caches: await cache.clear() cache_group = CacheGroup(caches) await cache_group.set('key', 1) for i, cache in enumerate(caches): assert await cache.get('key', i)
async def test_cache_group_set(): caches = [ CacheGroupItem(SimpleMaxTTLMemoryCache(), True, True, SpeedTier.FAST), CacheGroupItem(SimpleMemoryCache2(), True, True, SpeedTier.FAST), CacheGroupItem(SimpleMemoryCache3(), True, True, SpeedTier.FAST), CacheGroupItem(SimpleMemoryCache4(), True, True, SpeedTier.FAST) ] for cache_item in caches: await cache_item.cache.clear() cache_group = CacheGroup(caches) await cache_group.set('key', 1) for i, cache_item in enumerate(caches): assert await cache_item.cache.get('key', i)
async def test_cache_group_get_single_jsonrpc_response( steemd_request_and_response): caches = [ CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST), CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST), CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST) ] cache_group = CacheGroup(caches) cache_group = CacheGroup(caches) req, resp = steemd_request_and_response req = jsonrpc_from_request(dummy_request, 0, req) resp['jsonrpc'] = '2.0' key = jsonrpc_cache_key(req) assert await cache_group.get(key) is None await cache_group.set('last_irreversible_block_num', 15_000_000, 180) await cache_group.cache_single_jsonrpc_response(req, resp) assert await cache_group.get(key) == resp assert await cache_group.get_single_jsonrpc_response(req) == resp cache_group._memory_cache.clears() assert await cache_group.get_single_jsonrpc_response(req) == resp for cache_item in caches: assert await cache_item.cache.get(key) == resp
async def test_cache_group_clear(): caches = [ CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST), CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST), CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST) ] cache_group = CacheGroup(caches) for cache_item in caches: await cache_item.cache.set('key', 'value', 180) await cache_group.clear() for cache_item in caches: assert await cache_item.cache.get('key') is None assert await cache_group.get('key') is None
async def test_cache_group_clear(): caches = [ SimpleMaxTTLMemoryCache(), SimpleMemoryCache2(), SimpleMemoryCache3(), SimpleMemoryCache4() ] for cache in caches: await cache.clear() cache_group = CacheGroup(caches) for cache in caches: await cache.set('key', 'value') await cache_group.clear() assert await cache_group.get('key') is None
async def test_cache_group_cache_batch_jsonrpc_responses(): batch_req = [ jsonrpc_from_request(dummy_request, _id, { "id": _id, "jsonrpc": "2.0", "method": "get_block", "params": [_id] }) for _id in range(1, 10) ] batch_resp = [{ 'id': _id, "jsonrpc": "2.0", 'result': { "previous": "000003e7c4fd3221cf407efcf7c1730e2ca54b05", "timestamp": "2016-03-24T16:55:30", "witness": "initminer", "transaction_merkle_root": "0000000000000000000000000000000000000000", "extensions": [], "witness_signature": "207f15578cac20ac0e8af1ebb8f463106b8849577e21cca9fc60da146d1d95df88072dedc6ffb7f7f44a9185bbf9bf8139a5b4285c9f423843720296a44d428856", "transactions": [], "block_id": "000003e8b922f4906a45af8e99d86b3511acd7a5", "signing_key": "STM8GC13uCZbP44HzMLV6zPZGwVQ8Nt4Kji8PapsPiNq1BK153XTX", "transaction_ids": [] } } for _id in range(1, 10)] caches = [ CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST), CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST), CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST) ] cache_group = CacheGroup(caches) keys = [jsonrpc_cache_key(req) for req in batch_req] await cache_group.set('last_irreversible_block_num', 15_000_000, 180) await cache_group.cache_batch_jsonrpc_response(batch_req, batch_resp) for i, key in enumerate(keys): assert cache_group._memory_cache.gets(key) == batch_resp[i] assert await caches[0].cache.get(key) == batch_resp[i] assert await caches[1].cache.get(key) == batch_resp[i] assert await caches[2].cache.get(key) == batch_resp[i] assert await cache_group.get(key) == batch_resp[i]
async def test_cache_group_set_many(): caches = [ CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST), CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST), CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST) ] cache_group = CacheGroup(caches) keys = [str(i) for i in range(len(caches))] values = [str(i) for i in range(len(caches))] ttls = [None for i in range(len(caches))] pairs = {k: v for k, v in zip(keys, values)} await cache_group.set_many(pairs, 180) for cache_item in caches: assert await cache_item.cache.mget(keys) == values assert await cache_group.mget(keys) == values await cache_item.cache.clear()
async def test_cache_group_clear(): caches = [ CacheGroupItem(SimpleMaxTTLMemoryCache(), True, True, SpeedTier.FAST), CacheGroupItem(SimpleMemoryCache2(), True, True, SpeedTier.FAST), CacheGroupItem(SimpleMemoryCache3(), True, True, SpeedTier.FAST), CacheGroupItem(SimpleMemoryCache4(), True, True, SpeedTier.FAST) ] for cache_item in caches: await cache_item.cache.clear() cache_group = CacheGroup(caches) for cache_item in caches: await cache_item.cache.set('key', 'value') await cache_group.clear() for cache_item in caches: assert await cache_item.cache.get('key') is None assert await cache_group.get('key') is None
async def test_cache_group_mget(): caches = [ CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST), CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST), CacheGroupItem(build_mocked_cache(), True, True, SpeedTier.FAST) ] cache_group = CacheGroup(caches) keys = [str(i) for i in range(len(caches))] values = [str(i) for i in range(len(caches))] pairs = {k: v for k, v in zip(keys, values)} await cache_group.set_many(pairs, 180) assert await cache_group.mget(keys) == values assert await caches[0].cache.mget(keys) == values assert await caches[1].cache.mget(keys) == values assert await caches[2].cache.mget(keys) == values await cache_group._read_caches[0].clear() assert await cache_group.mget(keys) == values assert await caches[0].cache.mget(keys) == [None for key in keys] assert await cache_group._read_caches[1].mget(keys) == values assert await cache_group._read_caches[2].mget(keys) == values await cache_group._read_caches[1].clear() assert await cache_group.mget(keys) == values assert await caches[0].cache.mget(keys) == [None for key in keys] assert await caches[1].cache.mget(keys) == [None for key in keys] assert await cache_group._read_caches[2].mget(keys) == values await cache_group._read_caches[2].clear() assert await cache_group.mget(keys) == values assert await caches[0].cache.mget(keys) == [None for key in keys] assert await caches[1].cache.mget(keys) == [None for key in keys] assert await caches[2].cache.mget(keys) == [None for key in keys] await cache_group.clear() assert await caches[0].cache.mget(keys) == [None for key in keys] assert await caches[1].cache.mget(keys) == [None for key in keys] assert await caches[2].cache.mget(keys) == [None for key in keys] assert await cache_group.mget(keys) == [None for key in keys]
async def test_cache_group_get_single_jsonrpc_response( steemd_requests_and_responses): caches = [ SimpleMaxTTLMemoryCache(), SimpleMemoryCache2(), SimpleMemoryCache3(), SimpleMemoryCache4() ] for cache in caches: await cache.clear() cache_group = CacheGroup(caches) req, resp = steemd_requests_and_responses resp['jsonrpc'] = '2.0' key = jsonrpc_cache_key(req) assert await cache_group.get(key) is None await cache_group.cache_jsonrpc_response(req, resp, 15_000_000) assert await cache_group.get(key) == resp assert await cache_group.get_single_jsonrpc_response(req) == resp for cache in caches: assert await cache.get(key) == resp
async def test_cache_group_multi_set(): caches = [ SimpleMaxTTLMemoryCache(), SimpleMemoryCache2(), SimpleMemoryCache3(), SimpleMemoryCache4() ] for cache in caches: await cache.clear() cache_group = CacheGroup(caches) keys = [str(i) for i in range(len(caches))] values = [str(i) for i in range(len(caches))] ttls = [None for i in range(len(caches))] triplets = list(zip(keys, values, ttls)) await cache_group.multi_set(triplets) for cache in caches: assert await cache.multi_get(keys) == values assert await cache_group.multi_get(keys) == values await cache.clear()
async def test_cache_group_get(): caches = [ CacheGroupItem(SimpleMaxTTLMemoryCache(), True, True, SpeedTier.FAST), CacheGroupItem(SimpleMemoryCache2(), True, True, SpeedTier.FAST), CacheGroupItem(SimpleMemoryCache3(), True, True, SpeedTier.FAST), CacheGroupItem(SimpleMemoryCache4(), True, True, SpeedTier.FAST) ] for cache_item in caches: await cache_item.cache.clear() cache_group = CacheGroup(caches) assert caches[0] is not caches[1] await cache_group._read_caches[0].set('key', 0) assert await cache_group._read_caches[0].get('key') == 0 await cache_group._read_caches[1].set('key', 1) assert await cache_group._read_caches[0].get('key') == 0 assert await cache_group._read_caches[1].get('key') == 1 await cache_group._read_caches[2].set('key', 2) assert await cache_group._read_caches[0].get('key') == 0 assert await cache_group._read_caches[1].get('key') == 1 assert await cache_group._read_caches[2].get('key') == 2 await cache_group._read_caches[3].set('key', 3) assert await cache_group._read_caches[0].get('key') == 0 assert await cache_group._read_caches[1].get('key') == 1 assert await cache_group._read_caches[2].get('key') == 2 assert await cache_group._read_caches[3].get('key') == 3 assert await cache_group.get('key') == 0 await cache_group._read_caches[0].delete('key') assert await cache_group.get('key') == 1 await cache_group._read_caches[1].delete('key') assert await cache_group.get('key') == 2 await cache_group._read_caches[2].delete('key') assert await cache_group.get('key') == 3
async def test_cache_group_get(): caches = [ SimpleMaxTTLMemoryCache(), SimpleMemoryCache2(), SimpleMemoryCache3(), SimpleMemoryCache4() ] for cache in caches: await cache.clear() cache_group = CacheGroup(caches) assert caches[0] is not caches[1] await cache_group._caches[0].set('key', 0) assert await cache_group._caches[0].get('key') == 0 await cache_group._caches[1].set('key', 1) assert await cache_group._caches[0].get('key') == 0 assert await cache_group._caches[1].get('key') == 1 await cache_group._caches[2].set('key', 2) assert await cache_group._caches[0].get('key') == 0 assert await cache_group._caches[1].get('key') == 1 assert await cache_group._caches[2].get('key') == 2 await cache_group._caches[3].set('key', 3) assert await cache_group._caches[0].get('key') == 0 assert await cache_group._caches[1].get('key') == 1 assert await cache_group._caches[2].get('key') == 2 assert await cache_group._caches[3].get('key') == 3 assert await cache_group.get('key') == 0 await cache_group._caches[0].delete('key') assert await cache_group.get('key') == 1 await cache_group._caches[1].delete('key') assert await cache_group.get('key') == 2 await cache_group._caches[2].delete('key') assert await cache_group.get('key') == 3
def test_cache_group_is_complete_response(steemd_requests_and_responses): req, resp = steemd_requests_and_responses req = JussiJSONRPCRequest.from_request(dummy_request, 0, req) assert CacheGroup.is_complete_response(req, resp) is True
def test_cache_group_is_complete_response_bad_responses(req, resp, expected): assert CacheGroup.is_complete_response(req, resp) is expected
def test_cache_group_x_jussi_cache_key(steemd_requests_and_responses): req, resp = steemd_requests_and_responses batch_req = [req, req, req] assert jsonrpc_cache_key(req) == CacheGroup.x_jussi_cache_key(req) assert CacheGroup.x_jussi_cache_key(batch_req) == 'batch'
def test_cache_group_is_complete_response(steemd_requests_and_responses): req, resp = steemd_requests_and_responses assert CacheGroup.is_complete_response(req, resp) is True
def test_cache_group_x_jussi_cache_key(steemd_requests_and_responses): req, resp = steemd_requests_and_responses req = JussiJSONRPCRequest.from_request(dummy_request, 0, req) batch_req = [req, req, req] assert jsonrpc_cache_key(req) == CacheGroup.x_jussi_cache_key(req) assert CacheGroup.x_jussi_cache_key(batch_req) == 'batch'
def test_cache_group_is_complete_response(steemd_request_and_response): req, resp = steemd_request_and_response req = jsonrpc_from_request(dummy_request, 0, req) assert CacheGroup.is_complete_response(req, resp) is True