Prettified
This commit is contained in:
parent
4d0a0a3570
commit
5fb7eaaeab
|
@ -724,7 +724,9 @@ class Sanic(
|
||||||
if self.config.EVENT_AUTOREGISTER:
|
if self.config.EVENT_AUTOREGISTER:
|
||||||
self.signal_router.reset()
|
self.signal_router.reset()
|
||||||
self.add_signal(None, event)
|
self.add_signal(None, event)
|
||||||
waiter = self.signal_router.get_waiter(event, condition, exclusive)
|
waiter = self.signal_router.get_waiter(
|
||||||
|
event, condition, exclusive
|
||||||
|
)
|
||||||
self.signal_router.finalize()
|
self.signal_router.finalize()
|
||||||
else:
|
else:
|
||||||
raise NotFound("Could not find signal %s" % event)
|
raise NotFound("Could not find signal %s" % event)
|
||||||
|
|
|
@ -517,11 +517,11 @@ class Blueprint(BaseSanic):
|
||||||
)
|
)
|
||||||
|
|
||||||
def event(
|
def event(
|
||||||
self,
|
self,
|
||||||
event: str,
|
event: str,
|
||||||
timeout: Optional[Union[int, float]] = None,
|
timeout: Optional[Union[int, float]] = None,
|
||||||
*,
|
*,
|
||||||
condition: Optional[Dict[str, Any]] = None,
|
condition: Optional[Dict[str, Any]] = None,
|
||||||
):
|
):
|
||||||
"""Wait for a signal event to be dispatched.
|
"""Wait for a signal event to be dispatched.
|
||||||
|
|
||||||
|
@ -541,7 +541,9 @@ class Blueprint(BaseSanic):
|
||||||
|
|
||||||
waiters = []
|
waiters = []
|
||||||
for app in self.apps:
|
for app in self.apps:
|
||||||
waiter = app.signal_router.get_waiter(event, condition, exclusive=False)
|
waiter = app.signal_router.get_waiter(
|
||||||
|
event, condition, exclusive=False
|
||||||
|
)
|
||||||
if not waiter:
|
if not waiter:
|
||||||
raise NotFound("Could not find signal %s" % event)
|
raise NotFound("Could not find signal %s" % event)
|
||||||
waiters.append(waiter)
|
waiters.append(waiter)
|
||||||
|
@ -558,7 +560,7 @@ class Blueprint(BaseSanic):
|
||||||
task.cancel()
|
task.cancel()
|
||||||
if not done:
|
if not done:
|
||||||
raise TimeoutError()
|
raise TimeoutError()
|
||||||
finished_task, = done
|
(finished_task,) = done
|
||||||
return finished_task.result()
|
return finished_task.result()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
@ -104,9 +104,10 @@ class SignalWaiter:
|
||||||
self.signal.ctx.waiters.remove(self)
|
self.signal.ctx.waiters.remove(self)
|
||||||
|
|
||||||
def matches(self, event, condition):
|
def matches(self, event, condition):
|
||||||
return ((condition is None and not self.exclusive)
|
return (
|
||||||
or (condition is None and not self.requirements)
|
(condition is None and not self.exclusive)
|
||||||
or condition == self.requirements
|
or (condition is None and not self.requirements)
|
||||||
|
or condition == self.requirements
|
||||||
) and (self.trigger or event == self.event_definition)
|
) and (self.trigger or event == self.event_definition)
|
||||||
|
|
||||||
|
|
||||||
|
@ -278,12 +279,14 @@ class SignalRouter(BaseRouter):
|
||||||
return task
|
return task
|
||||||
|
|
||||||
def get_waiter(
|
def get_waiter(
|
||||||
self,
|
self,
|
||||||
event: Union[str, Enum],
|
event: Union[str, Enum],
|
||||||
condition: Optional[Dict[str, Any]],
|
condition: Optional[Dict[str, Any]],
|
||||||
exclusive: bool,
|
exclusive: bool,
|
||||||
):
|
):
|
||||||
event_definition = str(event.value) if isinstance(event, Enum) else event
|
event_definition = (
|
||||||
|
str(event.value) if isinstance(event, Enum) else event
|
||||||
|
)
|
||||||
name, trigger, _ = self._get_event_parts(event_definition)
|
name, trigger, _ = self._get_event_parts(event_definition)
|
||||||
signal = cast(Signal, self.name_index.get(name))
|
signal = cast(Signal, self.name_index.get(name))
|
||||||
if not signal:
|
if not signal:
|
||||||
|
|
|
@ -80,7 +80,6 @@ def test_invalid_signal(app, signal):
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_dispatch_signal_triggers_event(app):
|
async def test_dispatch_signal_triggers_event(app):
|
||||||
|
|
||||||
@app.signal("foo.bar.baz")
|
@app.signal("foo.bar.baz")
|
||||||
def sync_signal(*args):
|
def sync_signal(*args):
|
||||||
pass
|
pass
|
||||||
|
@ -92,7 +91,7 @@ async def test_dispatch_signal_triggers_event(app):
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
|
|
||||||
assert event_task.done()
|
assert event_task.done()
|
||||||
event_task.result() # Will raise if there was an exception
|
event_task.result() # Will raise if there was an exception
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
|
@ -142,7 +141,6 @@ async def test_dispatch_signal_with_enum_event(app):
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_dispatch_signal_with_enum_event_to_event(app):
|
async def test_dispatch_signal_with_enum_event_to_event(app):
|
||||||
|
|
||||||
class FooEnum(Enum):
|
class FooEnum(Enum):
|
||||||
FOO_BAR_BAZ = "foo.bar.baz"
|
FOO_BAR_BAZ = "foo.bar.baz"
|
||||||
|
|
||||||
|
@ -157,7 +155,7 @@ async def test_dispatch_signal_with_enum_event_to_event(app):
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
|
|
||||||
assert event_task.done()
|
assert event_task.done()
|
||||||
event_task.result() # Will raise if there was an exception
|
event_task.result() # Will raise if there was an exception
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
|
@ -185,7 +183,6 @@ async def test_dispatch_signal_triggers_multiple_handlers(app):
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_dispatch_signal_triggers_multiple_events(app):
|
async def test_dispatch_signal_triggers_multiple_events(app):
|
||||||
|
|
||||||
@app.signal("foo.bar.baz")
|
@app.signal("foo.bar.baz")
|
||||||
def sync_signal(*_):
|
def sync_signal(*_):
|
||||||
pass
|
pass
|
||||||
|
@ -200,13 +197,12 @@ async def test_dispatch_signal_triggers_multiple_events(app):
|
||||||
|
|
||||||
assert event_task1.done()
|
assert event_task1.done()
|
||||||
assert event_task2.done()
|
assert event_task2.done()
|
||||||
event_task1.result() # Will raise if there was an exception
|
event_task1.result() # Will raise if there was an exception
|
||||||
event_task2.result() # Will raise if there was an exception
|
event_task2.result() # Will raise if there was an exception
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_dispatch_signal_with_multiple_handlers_triggers_event_once(app):
|
async def test_dispatch_signal_with_multiple_handlers_triggers_event_once(app):
|
||||||
|
|
||||||
@app.signal("foo.bar.baz")
|
@app.signal("foo.bar.baz")
|
||||||
def sync_signal(*_):
|
def sync_signal(*_):
|
||||||
pass
|
pass
|
||||||
|
@ -222,7 +218,7 @@ async def test_dispatch_signal_with_multiple_handlers_triggers_event_once(app):
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
|
|
||||||
assert event_task.done()
|
assert event_task.done()
|
||||||
event_task.result() # Will raise if there was an exception
|
event_task.result() # Will raise if there was an exception
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
|
@ -243,7 +239,6 @@ async def test_dispatch_signal_triggers_dynamic_route(app):
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_dispatch_signal_triggers_parameterized_dynamic_route_event(app):
|
async def test_dispatch_signal_triggers_parameterized_dynamic_route_event(app):
|
||||||
|
|
||||||
@app.signal("foo.bar.<baz:int>")
|
@app.signal("foo.bar.<baz:int>")
|
||||||
def sync_signal(baz):
|
def sync_signal(baz):
|
||||||
pass
|
pass
|
||||||
|
@ -255,12 +250,11 @@ async def test_dispatch_signal_triggers_parameterized_dynamic_route_event(app):
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
|
|
||||||
assert event_task.done()
|
assert event_task.done()
|
||||||
event_task.result() # Will raise if there was an exception
|
event_task.result() # Will raise if there was an exception
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_dispatch_signal_triggers_starred_dynamic_route_event(app):
|
async def test_dispatch_signal_triggers_starred_dynamic_route_event(app):
|
||||||
|
|
||||||
@app.signal("foo.bar.<baz:int>")
|
@app.signal("foo.bar.<baz:int>")
|
||||||
def sync_signal(baz):
|
def sync_signal(baz):
|
||||||
pass
|
pass
|
||||||
|
@ -272,7 +266,7 @@ async def test_dispatch_signal_triggers_starred_dynamic_route_event(app):
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
|
|
||||||
assert event_task.done()
|
assert event_task.done()
|
||||||
event_task.result() # Will raise if there was an exception
|
event_task.result() # Will raise if there was an exception
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
|
@ -294,14 +288,15 @@ async def test_dispatch_signal_triggers_with_requirements(app):
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_dispatch_signal_to_event_with_requirements(app):
|
async def test_dispatch_signal_to_event_with_requirements(app):
|
||||||
|
|
||||||
@app.signal("foo.bar.baz")
|
@app.signal("foo.bar.baz")
|
||||||
def sync_signal(*_):
|
def sync_signal(*_):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
app.signal_router.finalize()
|
app.signal_router.finalize()
|
||||||
|
|
||||||
event_task = asyncio.create_task(app.event("foo.bar.baz", condition={"one": "two"}))
|
event_task = asyncio.create_task(
|
||||||
|
app.event("foo.bar.baz", condition={"one": "two"})
|
||||||
|
)
|
||||||
await app.dispatch("foo.bar.baz")
|
await app.dispatch("foo.bar.baz")
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
assert not event_task.done()
|
assert not event_task.done()
|
||||||
|
@ -309,7 +304,7 @@ async def test_dispatch_signal_to_event_with_requirements(app):
|
||||||
await app.dispatch("foo.bar.baz", condition={"one": "two"})
|
await app.dispatch("foo.bar.baz", condition={"one": "two"})
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
assert event_task.done()
|
assert event_task.done()
|
||||||
event_task.result() # Will raise if there was an exception
|
event_task.result() # Will raise if there was an exception
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
|
@ -331,24 +326,27 @@ async def test_dispatch_signal_triggers_with_requirements_exclusive(app):
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_dispatch_signal_to_event_with_requirements_exclusive(app):
|
async def test_dispatch_signal_to_event_with_requirements_exclusive(app):
|
||||||
|
|
||||||
@app.signal("foo.bar.baz")
|
@app.signal("foo.bar.baz")
|
||||||
def sync_signal(*_):
|
def sync_signal(*_):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
app.signal_router.finalize()
|
app.signal_router.finalize()
|
||||||
|
|
||||||
event_task = asyncio.create_task(app.event("foo.bar.baz", condition={"one": "two"}, exclusive=False))
|
event_task = asyncio.create_task(
|
||||||
|
app.event("foo.bar.baz", condition={"one": "two"}, exclusive=False)
|
||||||
|
)
|
||||||
await app.dispatch("foo.bar.baz")
|
await app.dispatch("foo.bar.baz")
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
assert event_task.done()
|
assert event_task.done()
|
||||||
event_task.result() # Will raise if there was an exception
|
event_task.result() # Will raise if there was an exception
|
||||||
|
|
||||||
event_task = asyncio.create_task(app.event("foo.bar.baz", condition={"one": "two"}, exclusive=False))
|
event_task = asyncio.create_task(
|
||||||
|
app.event("foo.bar.baz", condition={"one": "two"}, exclusive=False)
|
||||||
|
)
|
||||||
await app.dispatch("foo.bar.baz", condition={"one": "two"})
|
await app.dispatch("foo.bar.baz", condition={"one": "two"})
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
assert event_task.done()
|
assert event_task.done()
|
||||||
event_task.result() # Will raise if there was an exception
|
event_task.result() # Will raise if there was an exception
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
|
@ -368,7 +366,6 @@ async def test_dispatch_signal_triggers_with_context(app):
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_dispatch_signal_to_event_with_context(app):
|
async def test_dispatch_signal_to_event_with_context(app):
|
||||||
|
|
||||||
@app.signal("foo.bar.baz")
|
@app.signal("foo.bar.baz")
|
||||||
def sync_signal(**context):
|
def sync_signal(**context):
|
||||||
pass
|
pass
|
||||||
|
@ -379,7 +376,7 @@ async def test_dispatch_signal_to_event_with_context(app):
|
||||||
await app.dispatch("foo.bar.baz", context={"amount": 9})
|
await app.dispatch("foo.bar.baz", context={"amount": 9})
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
assert event_task.done()
|
assert event_task.done()
|
||||||
assert event_task.result()['amount'] == 9
|
assert event_task.result()["amount"] == 9
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
|
@ -399,7 +396,6 @@ async def test_dispatch_signal_triggers_with_context_fail(app):
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_dispatch_signal_to_dynamic_route_event(app):
|
async def test_dispatch_signal_to_dynamic_route_event(app):
|
||||||
|
|
||||||
@app.signal("foo.bar.<something>")
|
@app.signal("foo.bar.<something>")
|
||||||
def sync_signal(**context):
|
def sync_signal(**context):
|
||||||
pass
|
pass
|
||||||
|
@ -410,7 +406,7 @@ async def test_dispatch_signal_to_dynamic_route_event(app):
|
||||||
await app.dispatch("foo.bar.baz")
|
await app.dispatch("foo.bar.baz")
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
assert event_task.done()
|
assert event_task.done()
|
||||||
assert event_task.result()['something'] == "baz"
|
assert event_task.result()["something"] == "baz"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
|
@ -521,7 +517,7 @@ async def test_dispatch_signal_triggers_event_on_bp_with_context(app):
|
||||||
for _ in range(5):
|
for _ in range(5):
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
assert event_task.done()
|
assert event_task.done()
|
||||||
assert event_task.result()['amount'] == 9
|
assert event_task.result()["amount"] == 9
|
||||||
|
|
||||||
|
|
||||||
def test_bad_finalize(app):
|
def test_bad_finalize(app):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user