Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autospawn decorator #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions biloba/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .service import Service, ConfigurableService
from .service import * # noqa
from .service import __all__ as service_all
from .config import parse_address
from .util import waitany

Expand All @@ -10,8 +11,10 @@


__all__ = [
'ConfigurableService',
'Service',
'parse_address',
'waitany',
]

__all__.extend(service_all)

del service_all
30 changes: 30 additions & 0 deletions biloba/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

from . import config as biloba_config, events

__all__ = [
'Service',
'ConfigurableService',
'autospawn',
]


class Service(events.EventEmitter):
"""
Expand Down Expand Up @@ -291,6 +297,14 @@ def watch_service(self, child):
finally:
self.stop()

def __enter__(self):
self.start()

return self

def __exit__(self, *exc_args):
self.stop()


class ConfigurableService(Service):
"""
Expand Down Expand Up @@ -328,3 +342,19 @@ def apply_default_config(self, config):
config.setdefault(key, value)

return config


def autospawn(func):
"""
Decorator that will spawn the call in a service's greenlet pool

The function will only be called once the service has started.
"""
@functools.wraps(func)
def wrapped(self, *args, **kwargs):
if self.started:
self.spawn(func, self, *args, **kwargs)
else:
self.once('start', lambda: self.spawn(self, *args, **kwargs))

return wrapped
50 changes: 50 additions & 0 deletions biloba/test/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,53 @@ def test_biloba_config(self):
svc = service.ConfigurableService(cfg)

self.assertIs(svc.config, cfg)


class AutospawnTestCase(unittest.TestCase):
"""
Tests for `service.autospawn`.
"""

def test_started(self):
class MyService(service.Service):
@service.autospawn
def my_func(self):
self.emit('autospawn')

my_service = MyService()
self.executed = False

@my_service.on('autospawn')
def on_autospawn():
self.assertTrue(my_service.started)
self.executed = True

with my_service:
ret = my_service.my_func()

self.assertIsNone(ret)

self.assertTrue(self.executed)

def test_not_started(self):
class MyService(service.Service):
@service.autospawn
def my_func(self):
self.emit('autospawn')

my_service = MyService()
self.executed = False

@my_service.on('autospawn')
def on_autospawn():
self.assertTrue(my_service.started)
self.executed = True

ret = my_service.my_func()

self.assertIsNone(ret)

self.assertFalse(self.executed)

with my_service:
self.assertTrue(self.executed)