Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
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'
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
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(),
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)
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
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)
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:
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: