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 authored and mcatanzaro committed Jan 27, 2022
1 parent 01514cf commit 63d0e97
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)

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 63d0e97

Please sign in to comment.