mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-02 07:23:41 +02:00
tests: Rework assertEventually() to use GLib main context
Rather than sleeping and blocking everything, use a timeout source on the main context so that inputs can continue to be handled while waiting for a timeout. This introduces no functional changes to the test. Signed-off-by: Philip Withnall <pwithnall@gnome.org> Helps: #2887
This commit is contained in:
@@ -16,7 +16,6 @@ import sys
|
||||
import subprocess
|
||||
import fcntl
|
||||
import os
|
||||
import time
|
||||
|
||||
import taptestrunner
|
||||
|
||||
@@ -66,21 +65,27 @@ try:
|
||||
self.p_mock.terminate()
|
||||
self.p_mock.wait()
|
||||
|
||||
def assertEventually(self, condition, message=None, timeout=50):
|
||||
def assertEventually(self, condition, message=None, timeout=5):
|
||||
'''Assert that condition function eventually returns True.
|
||||
|
||||
Timeout is in deciseconds, defaulting to 50 (5 seconds). message is
|
||||
Timeout is in seconds, defaulting to 5 seconds. message is
|
||||
printed on failure.
|
||||
'''
|
||||
while timeout >= 0:
|
||||
while self.main_context.iteration(False):
|
||||
pass
|
||||
if condition():
|
||||
break
|
||||
timeout -= 1
|
||||
time.sleep(0.1)
|
||||
else:
|
||||
self.fail(message or 'timed out waiting for ' + str(condition))
|
||||
if not message:
|
||||
message = 'timed out waiting for ' + str(condition)
|
||||
|
||||
def timed_out_cb(message):
|
||||
self.fail(message)
|
||||
return GLib.SOURCE_REMOVE
|
||||
|
||||
timeout_source = GLib.timeout_source_new_seconds(timeout)
|
||||
timeout_source.set_callback(timed_out_cb, message)
|
||||
timeout_source.attach(self.main_context)
|
||||
|
||||
while not condition():
|
||||
self.main_context.iteration(True)
|
||||
|
||||
timeout_source.destroy()
|
||||
|
||||
def power_saver_enabled_cb(self, spec, data):
|
||||
self.power_saver_enabled = self.power_profile_monitor.get_power_saver_enabled()
|
||||
@@ -91,10 +96,10 @@ try:
|
||||
|
||||
self.assertEqual(self.power_profile_monitor.get_power_saver_enabled(), False)
|
||||
self.dbus_props.Set('net.hadess.PowerProfiles', 'ActiveProfile', dbus.String('power-saver', variant_level=1))
|
||||
self.assertEventually(lambda: self.power_saver_enabled == True, "power-saver didn't become enabled", 10)
|
||||
self.assertEventually(lambda: self.power_saver_enabled == True, "power-saver didn't become enabled", 1)
|
||||
|
||||
self.dbus_props.Set('net.hadess.PowerProfiles', 'ActiveProfile', dbus.String('balanced', variant_level=1))
|
||||
self.assertEventually(lambda: self.power_saver_enabled == False, "power-saver didn't become disabled", 10)
|
||||
self.assertEventually(lambda: self.power_saver_enabled == False, "power-saver didn't become disabled", 1)
|
||||
|
||||
except ImportError as e:
|
||||
@unittest.skip("Cannot import %s" % e.name)
|
||||
|
Reference in New Issue
Block a user