async def test_capped_cache(): cache = utils.create_cache("internal", {"cache_size": 2}) await cache.setup() stored = base_cache.CacheEntry(0, "pol_id", "pol_body") await cache.set("test1", stored) await cache.set("test2", stored) await cache.set("test3", stored) assert await cache.get("test2") == stored assert await cache.get("test3") == stored
async def setup_cache(cache_type, cache_opts): tmpfile = None if cache_type == 'sqlite': tmpfile = tempfile.NamedTemporaryFile() cache_opts["filename"] = tmpfile.name cache = utils.create_cache(cache_type, cache_opts) await cache.setup() if cache_type == 'redis': cache._pool.flushdb() return cache, tmpfile
async def test_responder_expiration(event_loop): async def query(host, port, domain): reader, writer = await asyncio.open_connection(host, port) stream_reader = netstring.StreamReader() string_reader = stream_reader.next_string() writer.write(netstring.encode(b'test ' + domain.encode('ascii'))) try: res = b'' while True: try: part = string_reader.read() except netstring.WantRead: data = await reader.read(4096) assert data stream_reader.feed(data) else: if not part: break res += part return res finally: writer.close() with tempfile.NamedTemporaryFile() as cachedb: cfg = {} cfg["port"] = 18461 cfg["cache_grace"] = 0 cfg["shutdown_timeout"] = 1 cfg["cache"] = { "type": "sqlite", "options": { "filename": cachedb.name, }, } cfg = utils.populate_cfg_defaults(cfg) cache = utils.create_cache(cfg['cache']['type'], cfg['cache']['options']) await cache.setup() pol_body = { "version": "STSv1", "mode": "enforce", "mx": ["mail.loc"], "max_age": 1, } await cache.set("no-record.loc", base_cache.CacheEntry(0, "0", pol_body)) resp = STSSocketmapResponder(cfg, event_loop, cache) await resp.start() try: result = await query(cfg['host'], cfg['port'], 'no-record.loc') assert result == b'NOTFOUND ' finally: await resp.stop() await cache.teardown()
async def responder(event_loop): import postfix_mta_sts_resolver.utils as utils cfg = utils.populate_cfg_defaults(None) cfg["zones"]["test2"] = cfg["default_zone"] cache = utils.create_cache(cfg['cache']['type'], cfg['cache']['options']) await cache.setup() resp = STSSocketmapResponder(cfg, event_loop, cache) await resp.start() result = resp, cfg['host'], cfg['port'] yield result await resp.stop() await cache.teardown()
async def responder(event_loop): import postfix_mta_sts_resolver.utils as utils cfg = utils.populate_cfg_defaults({"default_zone": {"require_sni": False}}) cfg["zones"]["test2"] = cfg["default_zone"] cfg["port"] = 28461 cache = utils.create_cache(cfg['cache']['type'], cfg['cache']['options']) await cache.setup() resp = STSSocketmapResponder(cfg, event_loop, cache) await resp.start() result = resp, cfg['host'], cfg['port'] await yield_(result) await resp.stop() await cache.teardown()
async def unix_responder(event_loop): import postfix_mta_sts_resolver.utils as utils cfg = utils.populate_cfg_defaults({'path': '/tmp/mta-sts.sock', 'mode': 0o666}) cfg["zones"]["test2"] = cfg["default_zone"] cache = utils.create_cache(cfg['cache']['type'], cfg['cache']['options']) await cache.setup() resp = STSSocketmapResponder(cfg, event_loop, cache) await resp.start() result = resp, cfg['path'] await yield_(result) await resp.stop() await cache.teardown()
async def test_cache_lifecycle(cache_type, cache_opts): if cache_type == 'sqlite': tmpfile = tempfile.NamedTemporaryFile() cache_opts["filename"] = tmpfile.name cache = utils.create_cache(cache_type, cache_opts) await cache.setup() assert await cache.get("nonexistent") == None stored = base_cache.CacheEntry(0, "pol_id", "pol_body") await cache.set("test", stored) await cache.set("test", stored) # second time for testing conflicting insert assert await cache.get("test") == stored await cache.teardown() if cache_type == 'sqlite': tmpfile.close()
def test_unknown_cache_lifecycle(): with pytest.raises(NotImplementedError): cache = utils.create_cache("void", {})
async def module_cache_fixture(): cfg = populate_cfg_defaults(None) cache = create_cache(cfg['cache']['type'], cfg['cache']['options']) await cache.setup() yield cache await cache.teardown()
async def cache(): cfg = populate_cfg_defaults(None) cache = create_cache(cfg['cache']['type'], cfg['cache']['options']) await cache.setup() await yield_(cache) await cache.teardown()