summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel P. Berrangé <berrange@redhat.com>2022-05-17 15:31:16 +0100
committerDaniel P. Berrangé <berrange@redhat.com>2022-06-08 16:43:52 +0100
commit438b86fda1b48ff3eab25b9c7ffaf156e7bc8759 (patch)
treea478bc3caf95bf6ba33b65f87ca96528b23468dc
parente3fab09382d4a6c439820ed5e6930e57716dcb50 (diff)
downloadlibvirt-python-438b86fda1b48ff3eab25b9c7ffaf156e7bc8759.tar.gz
tests: expand AIO tests and add more comments
Add a test for one more usage scenario that was possible in the past, whereby libvirt events are registered before starting the asyncio loop, but we let libvirt find the loop associated with the current thread. Skip the test relies on auto-creating an event loop with Python >= 3.10 since it now triggers a deprecation warning which will soon turn into a RuntimeError. Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
-rw-r--r--tests/test_aio.py48
1 files changed, 40 insertions, 8 deletions
diff --git a/tests/test_aio.py b/tests/test_aio.py
index c90934d..7965fa3 100644
--- a/tests/test_aio.py
+++ b/tests/test_aio.py
@@ -75,6 +75,10 @@ class TestLibvirtAio(unittest.TestCase):
@mock.patch('libvirt.virEventRegisterImpl',
side_effect=eventmock.virEventRegisterImplMock)
def testEventsWithManualLoopSetup(self, mock_event_register):
+ # Register libvirt events after starting the asyncio loop.
+ #
+ # Manually create and set the event loop against this
+ # thread.
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
@@ -88,15 +92,20 @@ class TestLibvirtAio(unittest.TestCase):
side_effect=eventmock.virEventRegisterImplMock)
@unittest.skipIf(sys.version_info < (3,7), "test requires Python 3.7+")
def testEventsWithAsyncioRun(self, mock_event_register):
+ # Register libvirt events after starting the asyncio loop.
+ #
+ # Use asyncio helper to create and set the event loop
+ # against this thread.
asyncio.run(self._run(register=True))
mock_event_register.assert_called_once()
@mock.patch('libvirt.virEventRegisterImpl',
side_effect=eventmock.virEventRegisterImplMock)
- @unittest.skipIf(sys.version_info >= (3,10), "test not compatible with Python 3.10+")
- def testEventsPreInit(self, mock_event_register):
- # Initialize libvirt events before setting the event loop. This is not recommended.
- # But is supported in older version of Python for the sake of back-compat.
+ def testEventsPreInitExplicit(self, mock_event_register):
+ # Register libvirt events before starting the asyncio loop.
+ #
+ # Tell virEventRegisterAsyncIOImpl() explicitly what loop
+ # to use before we set a loop for this thread.
loop = asyncio.new_event_loop()
libvirtaio.virEventRegisterAsyncIOImpl(loop)
asyncio.set_event_loop(loop)
@@ -109,11 +118,34 @@ class TestLibvirtAio(unittest.TestCase):
@mock.patch('libvirt.virEventRegisterImpl',
side_effect=eventmock.virEventRegisterImplMock)
+ @unittest.skipIf(sys.version_info >= (3,10), "test incompatible with 3.10")
+ def testEventsPreInitImplicit(self, mock_event_register):
+ # Register libvirt events before starting the asyncio loop.
+ #
+ # Allow virEventRegisterAsyncIOImpl() to implicitly find the
+ # loop we set for this thread.
+ loop = asyncio.new_event_loop()
+ asyncio.set_event_loop(loop)
+ libvirtaio.virEventRegisterAsyncIOImpl()
+
+ loop.run_until_complete(self._run(register=False))
+
+ loop.close()
+ asyncio.set_event_loop(None)
+ mock_event_register.assert_called_once()
+
+ @mock.patch('libvirt.virEventRegisterImpl',
+ side_effect=eventmock.virEventRegisterImplMock)
+ @unittest.skipIf(sys.version_info >= (3,10), "test incompatible with 3.10")
def testEventsImplicitLoopInit(self, mock_event_register):
- # Allow virEventRegisterAsyncIOImpl() to init the event loop by calling
- # asyncio.get_event_loop(). This is not recommended and probably only works by
- # accident. But is supported for now for the sake of back-compat. For Python
- # 3.10+, asyncio will report deprecation warnings.
+ # Register libvirt events before starting the asyncio loop.
+ #
+ # Let virEventRegisterAsyncIOImpl() auto-create a default
+ # event loop, which we then register against this thread.
+ #
+ # Historically this often worked if called from the main thead,
+ # but since Python 3.10 this triggers a deprecation warning,
+ # which will turn into a RuntimeError in a later release.
libvirtaio.virEventRegisterAsyncIOImpl()
loop = asyncio.get_event_loop()