How to use the aiomisc.Service function in aiomisc

To help you get started, we’ve selected a few aiomisc examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github aiokitchen / aiomisc / tests / test_entrypoint.py View on Github external
def test_service_start_event():
    class Sleeper(aiomisc.Service):
        result = False

        async def start(self):
            self.start_event.set()

            await asyncio.sleep(86400)
            Sleeper.result = True

    with aiomisc.entrypoint(Sleeper()):
        pass

    assert not Sleeper.result
github aiokitchen / aiomisc / tests / test_entrypoint.py View on Github external
def test_required_kwargs():
    class Svc(aiomisc.Service):
        __required__ = 'foo',

        async def start(self):
            pass

    with pytest.raises(AttributeError):
        Svc()

    assert Svc(foo='bar').foo == 'bar'
github aiokitchen / aiomisc / tests / test_entrypoint.py View on Github external
def test_wrong_sublclass():
    with pytest.raises(TypeError):
        class _(aiomisc.Service):
            def start(self):
                return True

    class MyService(aiomisc.Service):
        async def start(self):
            return

    with pytest.raises(TypeError):
        class _(MyService):
            def stop(self):
                return True

    class _(MyService):
        async def stop(self):
            return True
github aiokitchen / aiomisc / tests / test_entrypoint.py View on Github external
def test_service_events_2():
    class Initialization(aiomisc.Service):
        async def start(self):
            self.context['test'] = True

    class Awaiter(aiomisc.Service):
        result = None

        async def start(self):
            context = aiomisc.get_context()

            await asyncio.sleep(0.1)

            await context['test']
            Awaiter.result = await context['test']

    services = (
        Initialization(),
github aiokitchen / aiomisc / tests / pytest_plugin / test_loop_fixture.py View on Github external
import asyncio
import socket

import pytest

from aiomisc import Service


class _TestService(Service):
    loop_on_init = None  # type: asyncio.AbstractEventLoop

    async def start(self):
        assert self.loop is asyncio.get_event_loop()


@pytest.fixture()
async def async_sleep(loop):
    f = loop.create_future()
    loop.call_soon(f.set_result, True)
    return await f


@pytest.fixture()
def service(loop: asyncio.AbstractEventLoop):
    return _TestService(loop_on_init=loop)
github aiokitchen / aiomisc / tests / test_entrypoint.py View on Github external
def test_service_class():
    with pytest.raises(NotImplementedError):
        services = (
            aiomisc.Service(running=False, stopped=False),
            aiomisc.Service(running=False, stopped=False),
        )

        with aiomisc.entrypoint(*services):
            pass
github aiokitchen / aiomisc / tests / test_signal.py View on Github external
from aiomisc import entrypoint, Service, Signal, receiver


@pytest.fixture
def signal():
    return Signal()


@pytest.fixture(autouse=True, scope="module")
def clear_entrypoint_signals():
    entrypoint.PRE_START = Signal()
    entrypoint.POST_STOP = Signal()


class FooService(Service):

    async def start(self):
        ...


def test_pre_start_signal(loop):
    expected_services = tuple(FooService() for _ in range(2))
    ep = entrypoint(*expected_services, loop=loop)
    received_services = None

    async def pre_start_callback(services, entrypoint):
        nonlocal received_services
        received_services = services

    ep.pre_start.connect(pre_start_callback)
github aiokitchen / aiomisc / tests / test_entrypoint.py View on Github external
def test_simple():
    class StartingService(aiomisc.Service):
        async def start(self):
            self.running = True

    class DummyService(StartingService):
        async def stop(self, err: Exception = None):
            self.stopped = True

    services = (
        DummyService(running=False, stopped=False),
        DummyService(running=False, stopped=False),
    )

    with aiomisc.entrypoint(*services):
        pass

    for svc in services:
github aiokitchen / aiomisc / aiomisc / service / periodic.py View on Github external
import logging

from aiomisc import Service, PeriodicCallback


log = logging.getLogger(__name__)


class PeriodicService(Service):

    __required__ = ('interval',)

    interval = None  # type: float # in seconds
    delay = 0  # type: float # in seconds

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.periodic = PeriodicCallback(self.callback)

    async def start(self):
        self.periodic.start(self.interval, delay=self.delay, loop=self.loop)
        log.info('Periodic service %s started', self)

    async def stop(self, err):
        if self.periodic.task: