Exemplo n.º 1
0
async def test_successful_task_interval(timemachine):
    app = Pyncette(
        repository_factory=wrap_factory(sqlite_repository, timemachine))
    use_prometheus(app)

    counter = MagicMock()

    @app.dynamic_task()
    async def dynamic_task_1(context: Context) -> None:
        counter.execute()

    @app.task(interval=datetime.timedelta(seconds=2))
    async def task_1(context: Context) -> None:
        counter.execute()

    async with app.create() as ctx:
        await ctx.schedule_task(dynamic_task_1,
                                "1",
                                interval=datetime.timedelta(seconds=2))
        task = asyncio.create_task(ctx.run())
        await timemachine.step(datetime.timedelta(seconds=10))
        await ctx.unschedule_task(dynamic_task_1, "1")

        ctx.shutdown()
        await task
        await timemachine.unwind()

    metrics = generate_latest().decode("ascii").splitlines()

    assert (
        'pyncette_repository_ops_total{operation="commit_task",task_name="dynamic_task_1"} 5.0'
        in metrics)
    assert (
        'pyncette_repository_ops_total{operation="commit_task",task_name="task_1"} 5.0'
        in metrics)
    assert (
        'pyncette_repository_ops_total{operation="poll_dynamic_task",task_name="dynamic_task_1"} 11.0'
        in metrics)
    assert (
        'pyncette_repository_ops_total{operation="poll_task",task_name="dynamic_task_1"} 5.0'
        in metrics)
    assert (
        'pyncette_repository_ops_total{operation="poll_task",task_name="task_1"} 11.0'
        in metrics)
    assert (
        'pyncette_repository_ops_total{operation="register_task",task_name="dynamic_task_1"} 1.0'
        in metrics)
    assert (
        'pyncette_repository_ops_total{operation="unregister_task",task_name="dynamic_task_1"} 1.0'
        in metrics)
    assert 'pyncette_tasks_total{task_name="dynamic_task_1"} 5.0' in metrics
    assert 'pyncette_tasks_total{task_name="task_1"} 5.0' in metrics
Exemplo n.º 2
0
async def test_default_healthcheck_handler_healthy(timemachine):
    app = Pyncette(
        repository_factory=wrap_factory(sqlite_repository, timemachine))

    async with app.create() as ctx:
        task = asyncio.create_task(ctx.run())
        await timemachine.step(datetime.timedelta(seconds=1.5))
        is_healthy = await default_healthcheck(ctx)
        ctx.shutdown()
        await task
        await timemachine.unwind()

    assert is_healthy
Exemplo n.º 3
0
async def test_default_healthcheck_handler_unhealthy(timemachine):
    app = Pyncette(
        repository_factory=wrap_factory(sqlite_repository, timemachine))

    async with app.create() as ctx:
        task = asyncio.create_task(ctx.run())
        # Advance time without executing calbacks
        timemachine._update_offset(timemachine.offset +
                                   datetime.timedelta(hours=1))
        is_healthy = await default_healthcheck(ctx)
        ctx.shutdown()
        await task
        await timemachine.unwind()

    assert not is_healthy
Exemplo n.º 4
0
async def test_healthcheck_server_failure(timemachine):
    app = Pyncette(
        repository_factory=wrap_factory(sqlite_repository, timemachine))

    async def healthcheck_handler(app_context):
        return False

    # Bind on random port to avoid conflict
    use_healthcheck_server(app,
                           port=0,
                           bind_address="127.0.0.1",
                           healthcheck_handler=healthcheck_handler)

    async with app.create() as ctx, aiohttp.ClientSession() as session:
        task = asyncio.create_task(ctx.run())
        async with session.get(
                f"http://127.0.0.1:{get_healthcheck_port(ctx)}/health"
        ) as resp:
            assert resp.status == 500
        ctx.shutdown()
        await task
        await timemachine.unwind()
Exemplo n.º 5
0
async def test_healthcheck_server_invalid_verb(timemachine):
    app = Pyncette(
        repository_factory=wrap_factory(sqlite_repository, timemachine))

    async def healthcheck_handler(app_context):
        pass  # pragma: no cover

    # Bind on random port to avoid conflict
    use_healthcheck_server(app,
                           port=0,
                           bind_address="127.0.0.1",
                           healthcheck_handler=healthcheck_handler)

    async with app.create() as ctx, aiohttp.ClientSession() as session:
        task = asyncio.create_task(ctx.run())
        async with session.post(
                f"http://127.0.0.1:{get_healthcheck_port(ctx)}/health",
                json={"test": "object"},
        ) as resp:
            assert resp.status == 405
        ctx.shutdown()
        await task
        await timemachine.unwind()