Skip to content

Commit

Permalink
gio: Simplify memory monitor tests by using assertEventually() helper
Browse files Browse the repository at this point in the history
assertEventually is a helper used in a number of projects that use
dbusmock.

See martinpitt/python-dbusmock#82
  • Loading branch information
hadess committed Aug 11, 2021
1 parent 7b968c1 commit 96a8c02
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
31 changes: 19 additions & 12 deletions gio/tests/memory-monitor-dbus.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ try:
self.p_mock.terminate()
self.p_mock.wait()

def assertEventually(self, condition, message=None, timeout=50):
'''Assert that condition function eventually returns True.
Timeout is in deciseconds, defaulting to 50 (5 seconds). message is
printed on failure.
'''
while timeout >= 0:
context = GLib.MainContext.default()
while context.iteration(False):
pass
if condition():
break
timeout -= 1
time.sleep(0.1)
else:
self.fail(message or 'timed out waiting for ' + str(condition))

def memory_warning_cb(self, monitor, level):
self.last_warning = level
self.main_context.wakeup()
Expand All @@ -82,21 +99,11 @@ try:

self.dbusmock.EmitWarning(100)
# Wait 2 seconds or until warning
timeout = 2
while timeout > 0 and self.last_warning != 100:
time.sleep(0.5)
timeout -= 0.5
self.main_context.iteration(False)
self.assertEqual(self.last_warning, 100)
self.assertEventually(self.last_warning == 100, "'100' low-memory warning not received", 20)

self.dbusmock.EmitWarning(255)
# Wait 2 seconds or until warning
timeout = 2
while timeout > 0 and self.last_warning != 255:
time.sleep(0.5)
timeout -= 0.5
self.main_context.iteration(False)
self.assertEqual(self.last_warning, 255)
self.assertEventually(self.last_warning == 255, "'255' low-memory warning not received", 20)

except ImportError as e:
@unittest.skip("Cannot import %s" % e.name)
Expand Down
31 changes: 19 additions & 12 deletions gio/tests/memory-monitor-portal.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ try:
self.p_mock.terminate()
self.p_mock.wait()

def assertEventually(self, condition, message=None, timeout=50):
'''Assert that condition function eventually returns True.
Timeout is in deciseconds, defaulting to 50 (5 seconds). message is
printed on failure.
'''
while timeout >= 0:
context = GLib.MainContext.default()
while context.iteration(False):
pass
if condition():
break
timeout -= 1
time.sleep(0.1)
else:
self.fail(message or 'timed out waiting for ' + str(condition))

def portal_memory_warning_cb(self, monitor, level):
self.last_warning = level
self.main_context.wakeup()
Expand All @@ -100,21 +117,11 @@ try:

self.dbusmock.EmitWarning(100)
# Wait 2 seconds or until warning
timeout = 2
while timeout > 0 and self.last_warning != 100:
time.sleep(0.5)
timeout -= 0.5
self.main_context.iteration(False)
self.assertEqual(self.last_warning, 100)
self.assertEventually(self.last_warning == 100, "'100' low-memory warning not received", 20)

This comment has been minimized.

Copy link
@martinpitt

martinpitt Aug 12, 2021

Contributor

I wonder how that works? Shouldn't the condition be a lambda: self.last_warning == 100? Right now it gets evaluated once, immediately, and it looks like assertEventually() would try to call the boolean value as a function. Does Python magically turn this into a lambda?

This comment has been minimized.

Copy link
@hadess

hadess Aug 31, 2021

Author Contributor

No, I added the missing lambda before your comment, but pushed it after your comment. Loads to test locally...

This comment has been minimized.

Copy link
@martinpitt

This comment has been minimized.

Copy link
@hadess

hadess Sep 6, 2021

Author Contributor

You're right, I think it was only fixed in the power profile monitor. See:
https://gitlab.gnome.org/GNOME/glib/-/merge_requests/2240


self.dbusmock.EmitWarning(255)
# Wait 2 seconds or until warning
timeout = 2
while timeout > 0 and self.last_warning != 255:
time.sleep(0.5)
timeout -= 0.5
self.main_context.iteration(False)
self.assertEqual(self.last_warning, 255)
self.assertEventually(self.last_warning == 255, "'255' low-memory warning not received", 20)

except ImportError as e:
@unittest.skip("Cannot import %s" % e.name)
Expand Down

0 comments on commit 96a8c02

Please sign in to comment.