Exemplo n.º 1
0
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"))
Exemplo n.º 2
0
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,
    )
Exemplo n.º 3
0
def test_init_tables(postgres_service_with_fake_data):
    dsn = postgres_service_with_fake_data
    init_pg_tables(dsn, metadata)