summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorpgjones <philip.graham.jones@googlemail.com>2022-07-23 11:56:12 +0100
committerPhil Jones <philip.graham.jones@googlemail.com>2023-01-24 20:19:57 +0000
commit5ed9c956aaf68a8e2defa9722109aa2c7bcf7ae1 (patch)
treef103622c530a5a4fa121319e4b35d040ebcf80dc /tests
parent0d4ca6e72c155e30aedd4315e8678ee9cada32b4 (diff)
downloadblinker-5ed9c956aaf68a8e2defa9722109aa2c7bcf7ae1.tar.gz
Add a send_async method to the Signal
This allows for signals to send to coroutine receivers by awaiting them. The _async_wrapper and _sync_wrapper allows for conversion of sync and async receivers as required if defined. If not defined a runtime error is raised. The wrappers are used to avoid any direct tie into asyncio, trio, greenbacks, asgiref, or other specific async implementation.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_signals.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/test_signals.py b/tests/test_signals.py
index 4f1114d..1fca3e5 100644
--- a/tests/test_signals.py
+++ b/tests/test_signals.py
@@ -332,6 +332,33 @@ def test_strong_receiver():
assert [id(fn) for fn in sig.receivers.values()] == [fn_id]
+async def test_async_receiver():
+ sentinel = []
+
+ async def received_async(sender):
+ sentinel.append(sender)
+
+ def received(sender):
+ sentinel.append(sender)
+
+ def wrapper(func):
+
+ async def inner(*args, **kwargs):
+ func(*args, **kwargs)
+
+ return inner
+
+ sig = blinker.Signal()
+ sig.connect(received)
+ sig.connect(received_async)
+
+ await sig.send_async(_sync_wrapper=wrapper)
+ assert len(sentinel) == 2
+
+ with pytest.raises(RuntimeError):
+ sig.send()
+
+
def test_instancemethod_receiver():
sentinel = []