Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/library/asyncio-eventloop.rst
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,12 @@ Server
The server is closed asynchronously, use the :meth:`wait_closed`
coroutine to wait until the server is closed.

.. method:: get_loop()

Gives the event loop associated with the server object.

.. versionadded:: 3.7

.. coroutinemethod:: wait_closed()

Wait until the :meth:`close` method completes.
Expand Down
3 changes: 3 additions & 0 deletions Lib/asyncio/base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ def close(self):
if self._active_count == 0:
self._wakeup()

def get_loop(self):
return self._loop

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for the method.

@srinivasreddy srinivasreddy Dec 23, 2017

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot find tests for Server class. Could you help me please? Or it looks like i need to write test cases for Server class.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loop.create_server returns Server objects.


def _wakeup(self):
waiters = self._waiters
self._waiters = None
Expand Down
4 changes: 4 additions & 0 deletions Lib/asyncio/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ async def wait_closed(self):
"""Coroutine to wait until service is closed."""
return NotImplemented

def get_loop(self):
""" Get the event loop the Server object is attached to."""
return NotImplemented

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for make sure that AbstractServer().get_loop() returns NotImplemented.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asvetlov It should raise NotImplementedError, please fix. NotImplemened singleton is for comparisons in Python.

@asvetlov asvetlov Dec 30, 2017

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh, my bad.



class AbstractEventLoop:
"""Abstract event loop."""
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_asyncio/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2865,6 +2865,19 @@ class TestCGetEventLoop(GetEventLoopTestsMixin, unittest.TestCase):
get_running_loop_impl = events._c_get_running_loop
get_event_loop_impl = events._c_get_event_loop

class TestServer(unittest.TestCase):

def test_get_loop(self):
loop = asyncio.new_event_loop()
proto = MyProto(loop)
server = loop.run_until_complete(loop.create_server(lambda: proto, '0.0.0.0', 0))
self.assertEqual(server.get_loop(), loop)
loop.close()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test will leak an open server object. Add server.close(); loop.run_until_complete(server.wait_closed())


class TestAbstractServer(unittest.TestCase):

def test_get_loop(self):
self.assertEqual(events.AbstractServer().get_loop(), NotImplemented)

if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add get_loop() method to Server and AbstractServer classes.