async def test_engine_when_pg_not_reachable(): dsn = DataSourceName( database="db", user="******", password="******", host="localhost", port=123 ) with pytest.raises(psycopg2.OperationalError): await create_pg_engine(dsn)
async def test_engine_when_idle_for_some_time(): # NOTE: this test needs a docker swarm and a running postgres service dsn = DataSourceName( user="******", password="******", host="127.0.0.1", port=5432, database="db", application_name="test-app", ) engine = await create_pg_engine(dsn, minsize=1, maxsize=1) init_pg_tables(dsn, metadata) # import pdb; pdb.set_trace() assert not engine.closed # does not mean anything!!! # pylint: disable=no-value-for-parameter async with engine.acquire() as conn: # writes await conn.execute(tbl.insert().values(val="first")) # by default docker swarm kills connections that are idle for more than 15 minutes await asyncio.sleep(901) # import pdb; pdb.set_trace() async with engine.acquire() as conn: await conn.execute(tbl.insert().values(val="third"))
async def _create_db_engine(self) -> aiopg.sa.Engine: dsn = DataSourceName( application_name=f"{__name__}_{id(socket.gethostname())}", database=config.POSTGRES_DB, user=config.POSTGRES_USER, password=config.POSTGRES_PW, host=config.POSTGRES_ENDPOINT.split(":")[0], port=config.POSTGRES_ENDPOINT.split(":")[1], ) await wait_till_postgres_responsive(dsn) engine = await create_pg_engine(dsn, minsize=1, maxsize=4) return engine
async def __aenter__(self): dsn = DataSourceName( application_name=f"{__name__}_{id(socket.gethostname())}", database=POSTGRES_DB, user=POSTGRES_USER, password=POSTGRES_PW, host=POSTGRES_ENDPOINT.split(":")[0], port=POSTGRES_ENDPOINT.split(":")[1], ) log.info("Creating pg engine for %s", dsn) await wait_till_postgres_responsive(dsn) engine = await create_pg_engine(dsn, minsize=1, maxsize=4) self._db_engine = engine return self._db_engine
async def pg_engine(app: web.Application): cfg = app[APP_CONFIG_KEY][CONFIG_SECTION_NAME] pg_cfg = cfg["postgres"] app[f"{__name__}.dsn"] = dsn = DataSourceName( application_name=f"{__name__}_{id(app)}", database=pg_cfg["database"], user=pg_cfg["user"], password=pg_cfg["password"], host=pg_cfg["host"], port=pg_cfg["port"], ) log.info("Creating pg engine for %s", dsn) for attempt in Retrying( **PostgresRetryPolicyUponInitialization(log).kwargs): with attempt: engine = await create_pg_engine(dsn, minsize=pg_cfg["minsize"], maxsize=pg_cfg["maxsize"]) await raise_if_not_responsive(engine) assert engine # nosec app[APP_DB_ENGINE_KEY] = engine if cfg["init_tables"]: log.info("Initializing tables for %s", dsn) init_pg_tables(dsn, schema=metadata) yield # ------------------- if engine is not app.get(APP_DB_ENGINE_KEY): log.critical( "app does not hold right db engine. Somebody has changed it??") engine.close() await engine.wait_closed() log.debug( "engine '%s' after shutdown: closed=%s, size=%d", engine.dsn, engine.closed, engine.size, )
def postgres_service(docker_services, docker_ip, docker_compose_file) -> DataSourceName: # container environment with open(docker_compose_file) as fh: config = yaml.safe_load(fh) environ = config["services"]["postgres"]["environment"] dsn = DataSourceName( user=environ["POSTGRES_USER"], password=environ["POSTGRES_PASSWORD"], host=docker_ip, port=docker_services.port_for("postgres", 5432), database=environ["POSTGRES_DB"], application_name="test-app", ) # Wait until service is responsive. docker_services.wait_until_responsive( check=lambda: is_postgres_responsive(dsn), timeout=30.0, pause=0.1, ) return dsn