def test_logging(): confs = ( {'logging.disable_existing_loggers': True}, {'logging': { 'handlers': {'a': {'level': 'DEBUG'}, 'b': {'level': 'DEBUG'}}, 'loggers': {'a': {'level': 'DEBUG'}, 'a.a': {'level': 'DEBUG'}}, }}, {'logging.handlers.a': {'level': 'WARNING'}}, {'logging.handlers.b.level': 'WARNING'}, {'logging.loggers.a': {'level': 'INFO'}}, {'logging.loggers.a.a.level': 'WARNING'}, {'logging.loggers.b': {'level': 'ERROR'}}, {'logging.loggers.b.b': {'level': 'CRITICAL'}}, ) c = Config() for i in confs: c._update_logging(i) assert c.logging == { 'disable_existing_loggers': True, 'handlers': { 'a': {'level': 'WARNING'}, 'b': {'level': 'WARNING'}, }, 'loggers': { 'a': {'level': 'INFO'}, 'a.a': {'level': 'WARNING'}, 'b': {'level': 'ERROR'}, 'b.b': {'level': 'CRITICAL'}, }, }
async def test_context_create(loop): conf = Config() conf.update( MergeDict({ 'q.cls': 'aioworkers.queue.timeout.TimestampQueue', 'f.e': 1, })) c = Context(conf, loop=loop) await c.init() await c.start() assert c.config.f.e == 1 with pytest.raises(AttributeError): c.r with pytest.raises(KeyError): c['r'] async def handler(context): pass c.on_stop.append(handler) async def handler(): raise ValueError c.on_stop.append(handler) c.on_stop.append(handler()) c.on_stop.append(1) await c.stop()
async def test_q(loop): conf = Config() conf.update({'q.cls': utils.import_uri(proxy.ProxyQueue)}) async with Context(conf, loop=loop) as ctx: ctx.q.set_queue(Queue()) await ctx.q.put(1) assert 1 == await ctx.q.get()
def config(config_yaml): from aioworkers.core.config import Config c = Config() if config_yaml: import yaml loader = getattr(yaml, 'CLoader', yaml.Loader) c.update(yaml.load(config_yaml, loader)) return c
async def test_plq(loop): conf = Config() conf.update({ 'q.cls': utils.import_uri(proxy.PipeLineQueue), 'q.format': 'newline:str', }) async with Context(conf, loop=loop) as ctx: fin = io.BytesIO(b'123\n') fout = io.BytesIO() ctx.q.set_reader(fin) ctx.q.set_writer(fout) assert '123' == await ctx.q.get() await ctx.q.put('1') assert b'1\n' == fout.getvalue()
async def test_autorun(loop): config = Config(w=dict(cls='aioworkers.worker.base.Worker', autorun=True)) async with Context(config, loop=loop) as context: worker = context.w await worker._future assert worker._started_at assert not worker.running()
def config(): from aioworkers.core.config import Config return Config(group={ 'cls': 'aioworkers_tg.group.TelegramGroup', 'api_token': '1234567890', 'group_id': '1', }, )
def test_from_env(): os.environ['TEST'] = 'DEBUG' c = Config() c.update({ 'env.x.y': 'TEST', 'env.x': {'z': 'TEST'}, 'env': {'z': 'TEST'}, 'env.logging.root.level': 'TEST', }) assert c._env == { 'x.y': 'TEST', 'x.z': 'TEST', 'z': 'TEST', 'logging.root.level': 'TEST', } assert c.x.y == c.x.z == c.z == 'DEBUG' assert c.logging['root']['level'] == 'DEBUG'
async def test_set_context(loop): worker = Worker() config = Config(name='') context = Context(loop=loop) worker.set_config(config) worker.set_context(context) with pytest.raises(RuntimeError): worker.set_context(context)
async def test_format(loop): config = Config(storage=dict( cls='aioworkers.storage.http.Storage', format='bytes', )) async with Context(config=config, loop=loop) as context: storage = context.storage assert isinstance(storage.raw_key('test'), URL)
def config(aioworkers, config_yaml): from aioworkers.core.config import Config c = Config() if aioworkers.plugins.default: c.load_plugins() if aioworkers.plugins: c.load_plugins(*aioworkers.plugins) for data in yaml_load_all(config_yaml): c.update(data) return c
async def test_crontab(loop): config = Config(w=dict( cls='aioworkers.worker.base.Worker', persist=True, crontab='*/1 * * * *', )) async with Context(config, loop=loop) as context: worker = context.w await asyncio.sleep(0.1, loop=loop) await worker.stop() assert not worker.running()
async def test_close_connector(loop): from aioworkers.core.config import Config from aioworkers.core.context import Context c = Config() c.update( { "mongo": { "cls": "aioworkers_mongo.base.Connector", "uri": "mongodb://localhost:27017/", } } ) async with Context(c, loop=loop) as ctx: assert ctx.mongo._client # Check that client has been stopped. # TODO Find better way to check that MongoClient has been closed. assert not ctx.mongo._client.delegate._topology._opened
async def test_pool_close(loop, dsn): conf = Config( db={ "cls": "aioworkers_pg.base.Connector", "dsn": dsn, }, ) async with Context(conf, loop=loop) as c: assert c.db._pool is not None assert not c.db._pool._closed assert c.db._pool is None
async def test_stop(loop): config = Config(w=dict( cls='aioworkers.worker.base.Worker', autorun=True, persist=True, sleep=0.01, sleep_start=0.01, )) async with Context(config, loop=loop) as context: worker = context.w await asyncio.sleep(0.1, loop=loop) await worker.stop() assert not worker.running() assert isinstance(await worker.status(), dict)
def config(): db_uri = os.getenv('DATABASE_URL') if db_uri: uri, db_name = db_uri.rsplit('/', maxsplit=1) # overcoming difficulties if not db_name.startswith('test_'): os.environ['DATABASE_URL'] = uri + '/' + 'test_' + db_name plugin_config = Path(aioworkers_aiohttp.__file__).with_name('plugin.ini') import confinder config = Config().load(plugin_config, *confinder.configs) # config.checker.autorun = False return config
async def test_coro_run(loop, mocker): f = loop.create_future() async def myrun(*args, **kwargs): f.set_result(10) mocker.patch('aioworkers.worker.base.import_name', lambda x: myrun) config = Config(w=dict( cls='aioworkers.worker.base.Worker', autorun=True, run='mocked.run', )) async with Context(config, loop=loop) as context: worker = context.w assert worker._started_at await worker._future assert worker._stopped_at assert f.result() == 10
async def test_set_get(loop, aiohttp_client): app = web.Application() app.router.add_get( '/test/1', lambda x: web.json_response(["Python"])) client = await aiohttp_client(app) url = client.make_url('/') data = 'Python' config = Config( storage=dict( cls='aioworkers.storage.http.Storage', prefix=str(url), headers=[['A', 'b']], format='json', )) async with Context(config=config, loop=loop) as context: storage = context.storage assert data in await storage.get('test/1') with pytest.raises(StorageError): await storage.set('test/1', data)
def test_dump_config(): fd = io.BytesIO() utils.dump_to_fd(fd, Config()) fd.seek(0) c = utils.load_from_fd(fd) assert c
from pathlib import Path from aioworkers.core.config import Config from aioworkers.core.context import Context config = Config() config.load(Path(__file__).with_suffix('.yaml')) async def test_autorun(loop): async with Context(config.autorun, loop=loop) as ctx: await ctx.sv._future assert ctx.sv._started_at assert not ctx.sv.running() await ctx.sv.stop() assert await ctx.sv.status() assert 2 == await ctx.sv(2) async def run(w, value): return value async def test_super_queue(loop): async with Context(config.super.queue, loop=loop) as ctx: await ctx.q1.put(1) result = await ctx.q2.get() assert result == 1 await ctx.sv(2) result = await ctx.q2.get() assert result == 2
def test_load_config(): p = Path(__file__).parent / 'data' conf = Config(search_dirs=[p]) config = conf.load(p / 'conf1.json', 'conf2.json') assert config assert repr(config)
def config(): plugin_config = Path(aioworkers_aiohttp.__file__).with_name('plugin.ini') app_config = Path(__file__).parent.with_name('config.yaml') config = Config().load(plugin_config, app_config) return config
def test_load_plugins(): conf = Config() plugins = conf.load_plugins('time') assert isinstance(plugins, list)
def config(): c = Config() p = Path(__file__).parent / 'q.ini' c.load(p) return c