Skip to content

Commit

Permalink
pyportaltest: Allow configurable mainloop timeouts
Browse files Browse the repository at this point in the history
For now we keep the mainloop property with a default timeout of 2000ms
but future tests can make this more specific.
  • Loading branch information
whot committed Sep 10, 2024
1 parent 6939ad6 commit 6c9f664
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
41 changes: 31 additions & 10 deletions tests/pyportaltest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,36 @@ def tearDown(self):
self.dbus_monitor.terminate()
self.dbus_monitor.wait()

def setup_mainloop_with_timeout(self, timeout_ms: int):
"""
Set our mainloop for this test. This mainloop automatically quits after
the given timeout (if the timeout is nonzero), but only on the first
call to run():
>>> loop = self.setup_mainloop_with_timeout(timeout_ms=1000)
>>> loop.run() # exits after 1000ms
>>> loop.run() # loops forever now
>>> loop2 = self.setup_mainloop_with_timeout(timeout_ms=2000)
>>> loop2.run() # exits after 2000ms
>>> assert loop == loop2
That's usually enough for tests, if you need to call mainloop.run()
repeatedly ensure that a timeout handler is set to ensure quick test
case failure in case of error.
"""
if self._mainloop is None:

self._mainloop = GLib.MainLoop()

def quit():
self._mainloop.quit()
self._mainloop = None

if timeout_ms:
GLib.timeout_add(timeout_ms, quit)

return self._mainloop

@property
def mainloop(self):
"""
Expand All @@ -152,16 +182,7 @@ def mainloop(self):
timeout handler is set to ensure quick test case failure in case of
error.
"""
if self._mainloop is None:

def quit():
self._mainloop.quit()
self._mainloop = None

self._mainloop = GLib.MainLoop()
GLib.timeout_add(2000, quit)

return self._mainloop
return self.setup_mainloop_with_timeout(timeout_ms=2000)

def assert_version_eq(self, version: int):
"""Assert the given version number is the one our portal exports"""
Expand Down
3 changes: 1 addition & 2 deletions tests/pyportaltest/test_remotedesktop.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ def short_mainloop(self):
Run a short iteration of our mainloop. Useful to for pure method calls
that still require dbusmock etc. to update after the invocation.
"""
GLib.timeout_add(200, self.mainloop.quit)
self.mainloop.run()
self.setup_mainloop_with_timeout(timeout_ms=200).run()

def create_session(
self,
Expand Down

0 comments on commit 6c9f664

Please sign in to comment.