added app.register_listener method w/test

This commit is contained in:
Stephan Fitzpatrick 2018-02-09 14:01:17 -08:00
parent 6d37ef7256
commit 571b5b544d
2 changed files with 32 additions and 0 deletions

View File

@ -117,6 +117,21 @@ class Sanic:
return decorator
def register_listener(self, listener, event):
"""
Register the listener for a given event.
Args:
listener: callable i.e. setup_db(app, loop)
event: when to register listener i.e. 'before_server_start'
Returns: listener
"""
return self.listener(event)(listener)
# Decorator
def route(self, uri, methods=frozenset({'GET'}), host=None,
strict_slashes=None, stream=False, version=None, name=None):

View File

@ -49,6 +49,23 @@ def test_single_listener(listener_name):
assert random_name_app.name + listener_name == output.pop()
@pytest.mark.parametrize('listener_name', AVAILABLE_LISTENERS)
def test_register_listener(listener_name):
"""
Test that listeners on their own work with
app.register_listener method
"""
random_name_app = Sanic(''.join(
[choice(ascii_letters) for _ in range(choice(range(5, 10)))]))
output = list()
# Register listener
listener = create_listener(listener_name, output)
random_name_app.register_listener(listener,
event=listener_name)
start_stop_app(random_name_app)
assert random_name_app.name + listener_name == output.pop()
def test_all_listeners():
random_name_app = Sanic(''.join(
[choice(ascii_letters) for _ in range(choice(range(5, 10)))]))