added app.register_listener method w/test
This commit is contained in:
parent
6d37ef7256
commit
571b5b544d
15
sanic/app.py
15
sanic/app.py
|
@ -117,6 +117,21 @@ class Sanic:
|
||||||
|
|
||||||
return decorator
|
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
|
# Decorator
|
||||||
def route(self, uri, methods=frozenset({'GET'}), host=None,
|
def route(self, uri, methods=frozenset({'GET'}), host=None,
|
||||||
strict_slashes=None, stream=False, version=None, name=None):
|
strict_slashes=None, stream=False, version=None, name=None):
|
||||||
|
|
|
@ -49,6 +49,23 @@ def test_single_listener(listener_name):
|
||||||
assert random_name_app.name + listener_name == output.pop()
|
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():
|
def test_all_listeners():
|
||||||
random_name_app = Sanic(''.join(
|
random_name_app = Sanic(''.join(
|
||||||
[choice(ascii_letters) for _ in range(choice(range(5, 10)))]))
|
[choice(ascii_letters) for _ in range(choice(range(5, 10)))]))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user