-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Signals v2 #562
Merged
Merged
Signals v2 #562
Changes from 19 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
654a806
Initial signal implementation. Tests and documentation to follow.
alexdutton ac75361
Wrap iscoroutinefunction check in 'if __debug__', so people can optim…
alexdutton fefd2ed
Rename AsyncSignal to CoroutineSignal for clarity of purpose
alexdutton d45ff67
Add base class for signals
alexdutton cf29660
Add signal tests
alexdutton 98c418a
Documentation!
alexdutton 4d8b509
Point at FunctionSignal, not Signal in `on_response_start` docs
alexdutton 9bedbbb
Merge remote-tracking branch 'upstream/master' into signals-v2
alexdutton f04fbb3
Remove FunctionSignals in light of #525.
alexdutton 5fb868b
Move on_response_start firing to `prepare()` and treat it as a coroutine
alexdutton cc2efbd
Raise TypeError on non-coroutine functions, to match signature mismat…
alexdutton 9170057
Working tests again.
alexdutton 5825da3
Signal now based on list; still does signature checking
alexdutton dea0a1e
Merge remote-tracking branch 'upstream/master' into signals-v2
alexdutton f5b98ac
Drop requirement for signal receivers to be coroutines (but they stil…
alexdutton 322f650
Fix variable name in signature check call
alexdutton a0f10f7
Merge branch 'signals-v2' of https://github.com/alexsdutton/aiohttp i…
asvetlov dbc8393
Drop signal signature check
asvetlov b037e2b
Add more tests
asvetlov 15d815e
Allow using positional args to Signal.send
asvetlov 74413d4
Fix failed test
asvetlov 67414c8
Update docs
asvetlov 02c44a4
Merge branch 'master' into signals-v2
asvetlov 6cd8a44
Convert signal tests to pytest usage
asvetlov 1a9c0a7
Fix tests
asvetlov 0089a65
Properly mock coroutine
asvetlov 053d184
Fix signals test
asvetlov 73ad8fb
Merge branch 'master' into signals-v2
asvetlov 602b19c
Merge branch 'master' into signals-v2
asvetlov 9939f94
Fix failed test
asvetlov 0c32f47
Fix next test
asvetlov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import asyncio | ||
|
||
|
||
class Signal(list): | ||
""" | ||
Coroutine-based signal implementation | ||
|
||
To connect a callback to a signal, use any list method. If wish to pass | ||
additional arguments to your callback, use :meth:`functools.partial`. | ||
|
||
Signals are fired using the :meth:`send` coroutine, which takes named | ||
arguments. | ||
""" | ||
|
||
@asyncio.coroutine | ||
def send(self, **kwargs): | ||
""" | ||
Sends data to all registered receivers. | ||
""" | ||
for receiver in self: | ||
res = receiver(**kwargs) | ||
if asyncio.iscoroutine(res) or isinstance(res, asyncio.Future): | ||
yield from res | ||
|
||
def copy(self): | ||
raise NotImplementedError("copy() is forbidden") | ||
|
||
def sort(self): | ||
raise NotImplementedError("sort() is forbidden") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import asyncio | ||
import unittest | ||
from unittest import mock | ||
from aiohttp.multidict import CIMultiDict | ||
from aiohttp.signals import Signal | ||
from aiohttp.web import Application | ||
from aiohttp.web import Request, Response | ||
from aiohttp.protocol import HttpVersion11 | ||
from aiohttp.protocol import RawRequestMessage | ||
|
||
|
||
class TestSignals(unittest.TestCase): | ||
def setUp(self): | ||
self.loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(None) | ||
|
||
def tearDown(self): | ||
self.loop.close() | ||
|
||
def make_request(self, method, path, headers=CIMultiDict(), app=None): | ||
message = RawRequestMessage(method, path, HttpVersion11, headers, | ||
False, False) | ||
return self.request_from_message(message, app) | ||
|
||
def request_from_message(self, message, app=None): | ||
self.app = app if app is not None else mock.Mock() | ||
self.payload = mock.Mock() | ||
self.transport = mock.Mock() | ||
self.reader = mock.Mock() | ||
self.writer = mock.Mock() | ||
req = Request(self.app, message, self.payload, | ||
self.transport, self.reader, self.writer) | ||
return req | ||
|
||
def test_add_response_prepare_signal_handler(self): | ||
callback = asyncio.coroutine(lambda request, response: None) | ||
app = Application(loop=self.loop) | ||
app.on_response_prepare.append(callback) | ||
|
||
def test_add_signal_handler_not_a_callable(self): | ||
callback = True | ||
app = Application(loop=self.loop) | ||
app.on_response_prepare.append(callback) | ||
with self.assertRaises(TypeError): | ||
app.on_response_prepare(None, None) | ||
|
||
def test_function_signal_dispatch(self): | ||
signal = Signal() | ||
kwargs = {'foo': 1, 'bar': 2} | ||
|
||
callback_mock = mock.Mock() | ||
callback = asyncio.coroutine(callback_mock) | ||
|
||
signal.append(callback) | ||
|
||
self.loop.run_until_complete(signal.send(**kwargs)) | ||
callback_mock.assert_called_once_with(**kwargs) | ||
|
||
def test_response_prepare(self): | ||
callback = mock.Mock() | ||
|
||
app = Application(loop=self.loop) | ||
app.on_response_prepare.append(asyncio.coroutine(callback)) | ||
|
||
request = self.make_request('GET', '/', app=app) | ||
response = Response(body=b'') | ||
self.loop.run_until_complete(response.prepare(request)) | ||
|
||
callback.assert_called_once_with(request=request, | ||
response=response) | ||
|
||
def test_non_coroutine(self): | ||
signal = Signal() | ||
kwargs = {'foo': 1, 'bar': 2} | ||
|
||
callback = mock.Mock() | ||
|
||
signal.append(callback) | ||
|
||
self.loop.run_until_complete(signal.send(**kwargs)) | ||
callback.assert_called_once_with(**kwargs) | ||
|
||
def test_copy_forbidden(self): | ||
signal = Signal() | ||
with self.assertRaises(NotImplementedError): | ||
signal.copy() | ||
|
||
def test_sort_forbidden(self): | ||
l1 = lambda: None | ||
l2 = lambda: None | ||
l3 = lambda: None | ||
signal = Signal([l1, l2, l3]) | ||
with self.assertRaises(NotImplementedError): | ||
signal.sort() | ||
self.assertEqual(signal, [l1, l2, l3]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to ensure that all the receivers in Signal are callable? Otherwise it won't be cool to get some
TypeError: 'int' object is not callable
from the deepest aiohttp internals without any pointers about what the signal that was and where it came from.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initial version from @alexsdutton had signature check. But I quite unhappy with it.
I prefer adding good checker later but keeping no checks at all in first implementation.