summaryrefslogtreecommitdiff
path: root/tests/eventmock.py
diff options
context:
space:
mode:
authorDaniel P. Berrangé <berrange@redhat.com>2022-05-17 15:01:19 +0100
committerDaniel P. Berrangé <berrange@redhat.com>2022-06-08 16:43:52 +0100
commite3fab09382d4a6c439820ed5e6930e57716dcb50 (patch)
tree75bd89dbae775c6e0b255453a67dbde3a824f879 /tests/eventmock.py
parentb9f79758c973db088b5c687425c6613796fdb250 (diff)
downloadlibvirt-python-e3fab09382d4a6c439820ed5e6930e57716dcb50.tar.gz
tests: use mocks to allow calling virEventRegisterImpl many times
We currently have to run each of the test_aio.py test cases in a separate process, because libvirt.virEventRegisterImpl can only be called once per process. This leads to quite unpleasant console output when running tests. By introducing a mock for libvirt.virEventRegisterImpl we can regain the ability to run everything in a single process. The only caveat is that it relies on tests to fully cleanup, but in practice this is ok for our current tests. Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
Diffstat (limited to 'tests/eventmock.py')
-rw-r--r--tests/eventmock.py82
1 files changed, 82 insertions, 0 deletions
diff --git a/tests/eventmock.py b/tests/eventmock.py
new file mode 100644
index 0000000..7298086
--- /dev/null
+++ b/tests/eventmock.py
@@ -0,0 +1,82 @@
+
+import libvirt
+import libvirtmod
+
+_add_handle_impl = None
+_update_handle_impl = None
+_remove_handle_impl = None
+
+_add_timeout_impl = None
+_update_timeout_impl = None
+_remove_timeout_impl = None
+
+_registered = False
+
+def _add_handle(fd: int, event: int, cb: libvirt._EventCB, opaque: libvirt._T) -> int:
+ global _add_handle_impl
+ assert _add_handle_impl != None
+ return _add_handle_impl(fd, event, cb, opaque)
+
+def _update_handle(watch: int, event: int) -> None:
+ global _update_handle_impl
+ assert _update_handle_impl != None
+ _update_handle_impl(watch, event)
+
+def _remove_handle(watch: int) -> int:
+ global _remove_handle_impl
+ assert _remove_handle_impl != None
+ return _remove_handle_impl(watch)
+
+def _add_timeout(timeout: int, cb: libvirt._TimerCB, opaque: libvirt._T) -> int:
+ global _add_timeout_impl
+ assert _add_timeout_impl != None
+ return _add_timeout_impl(timeout, cb, opaque)
+
+def _update_timeout(timer: int, timeout: int) -> None:
+ global _update_timeout_impl
+ assert _update_timeout_impl != None
+ _update_timeout_impl(timer, timeout)
+
+def _remove_timeout(timer: int) -> int:
+ global _remove_timeout_impl
+ assert _remove_timeout_impl != None
+ return _remove_timeout_impl(timer)
+
+# libvirt.virEventRegisterImpl() is a one time call per process
+# This method is intended to be used with mock patching, so that
+# tests can get the appearance of being able to call
+# virEventRegisterImpl many times.
+#
+# Note, this relies on the tests closing all connection objects
+# and not leaving any handles/timers pending when they stop
+# running their event loop impl.
+
+def virEventRegisterImplMock(add_handle_impl,
+ update_handle_impl,
+ remove_handle_impl,
+ add_timeout_impl,
+ update_timeout_impl,
+ remove_timeout_impl):
+ global _add_handle_impl
+ global _update_handle_impl
+ global _remove_handle_impl
+ global _add_timeout_impl
+ global _update_timeout_impl
+ global _remove_timeout_impl
+
+ _add_handle_impl = add_handle_impl
+ _update_handle_impl = update_handle_impl
+ _remove_handle_impl = remove_handle_impl
+ _add_timeout_impl = add_timeout_impl
+ _update_timeout_impl = update_timeout_impl
+ _remove_timeout_impl = remove_timeout_impl
+
+ global _registered
+ if not _registered:
+ libvirtmod.virEventRegisterImpl(_add_handle,
+ _update_handle,
+ _remove_timeout,
+ _add_timeout,
+ _update_timeout,
+ _remove_timeout)
+ _registered = True