[gnome-settings-daemon/benzea/ci-fixes] tests: Use OutputChecker for notification daemon



commit 3f8a60be335cdf398fd49452337768737536ea5b
Author: Benjamin Berg <bberg redhat com>
Date:   Tue Jul 20 17:45:20 2021 +0200

    tests: Use OutputChecker for notification daemon
    
    Using a pipe without guaranteing that it is actually read could
    potentially get us into really weird deadlock situations. Switch to use
    OutputChecker to avoid the issue (and log the notifications).

 plugins/power/test.py     | 59 +++++++++--------------------------------------
 plugins/xsettings/test.py |  5 +---
 tests/gsdtestcase.py      |  7 +++---
 3 files changed, 16 insertions(+), 55 deletions(-)
---
diff --git a/plugins/power/test.py b/plugins/power/test.py
index a1024fcd..3da17a82 100755
--- a/plugins/power/test.py
+++ b/plugins/power/test.py
@@ -149,8 +149,7 @@ class PowerPluginBase(gsdtestcase.GSDTestCase):
         # always start with zero idle time
         self.reset_idle_timer()
 
-        # flush notification log
-        self.p_notify.stdout.read()
+        self.p_notify_log.clear()
 
     def stop_daemon(self):
 
@@ -779,13 +778,7 @@ class PowerPluginTest6(PowerPluginBase):
         # Check that it was picked up
         self.check_plugin_log('EMIT: charge-critical', 2)
 
-        # Wait a bit longer to ensure event has been fired
-        time.sleep(0.5)
-        # we should have gotten a notification now
-        notify_log = self.p_notify.stdout.read()
-
-        # verify notification
-        self.assertRegex(notify_log, b'[0-9.]+ Notify "Power" .* "battery-caution-symbolic" ".*battery 
critical.*"')
+        self.p_notify_log.check_line_re(b'[0-9.]+ Notify "Power" .* "battery-caution-symbolic" ".*battery 
critical.*"', timeout=0.5)
 
     def test_notify_critical_battery_on_start(self):
         '''action on critical battery on startup'''
@@ -795,13 +788,7 @@ class PowerPluginTest6(PowerPluginBase):
         # Check that it was picked up
         self.check_plugin_log('EMIT: charge-critical', 2)
 
-        time.sleep(0.5)
-
-        # we should have gotten a notification by now
-        notify_log = self.p_notify.stdout.read()
-
-        # verify notification
-        self.assertRegex(notify_log, b'[0-9.]+ Notify "Power" .* "battery-caution-symbolic" ".*battery 
critical.*"')
+        self.p_notify_log.check_line_re(b'[0-9.]+ Notify "Power" .* "battery-caution-symbolic" ".*battery 
critical.*"', timeout=0.5)
 
     def test_notify_device_battery(self):
         '''critical power level notification for device batteries'''
@@ -846,13 +833,8 @@ class PowerPluginTest6(PowerPluginBase):
                                    dbus_interface='org.freedesktop.DBus.Mock')
 
         self.check_plugin_log('EMIT: charge-critical', 2)
-        time.sleep(0.5)
-
-        # we should have gotten a notification by now
-        notify_log = self.p_notify.stdout.read()
 
-        # verify notification
-        self.assertRegex(notify_log, b'[0-9.]+ Notify "Power" .* ".*" ".*Wireless mouse .*low.* 
power.*\([0-9.]+%\).*"')
+        self.p_notify_log.check_line_re(b'[0-9.]+ Notify "Power" .* ".*" ".*Wireless mouse .*low.* 
power.*\([0-9.]+%\).*"', timeout=0.5)
 
     def test_notify_device_spam(self):
         '''no repeat notifications for device batteries'''
@@ -881,13 +863,8 @@ class PowerPluginTest6(PowerPluginBase):
         time.sleep(1)
 
         self.check_plugin_log('EMIT: charge-low', 2)
-        time.sleep(0.5)
-
-        # we should have gotten a notification by now
-        notify_log = self.p_notify.stdout.read()
 
-        # verify notification
-        self.assertRegex(notify_log, b'[0-9.]+ Notify "Power" .* ".*" ".*Wireless mouse .*low.* 
power.*\([0-9.]+%\).*"')
+        self.p_notify_log.check_line_re(b'[0-9.]+ Notify "Power" .* ".*" ".*Wireless mouse .*low.* 
power.*\([0-9.]+%\).*"', timeout=0.5)
 
         # Disconnect mouse
         self.obj_upower.RemoveObject(bat2_path)
@@ -910,11 +887,8 @@ class PowerPluginTest6(PowerPluginBase):
         obj_bat2 = self.system_bus_con.get_object('org.freedesktop.UPower', bat2_path)
         self.obj_upower.EmitSignal('', 'DeviceAdded', 'o', [bat2_path],
                                    dbus_interface='org.freedesktop.DBus.Mock')
-        time.sleep(1)
 
-        # we shouldn't have gotten a notification by now
-        notify_log = self.p_notify.stdout.read()
-        self.assertIsNone(notify_log)
+        self.p_notify_log.check_no_line(b'', wait=1)
 
         # Disconnect mouse
         self.obj_upower.RemoveObject(bat2_path)
@@ -941,13 +915,8 @@ class PowerPluginTest6(PowerPluginBase):
 
         # Verify new warning
         self.check_plugin_log('EMIT: charge-critical', 2)
-        time.sleep(0.5)
 
-        # we should have gotten a notification by now
-        notify_log = self.p_notify.stdout.read()
-
-        # verify notification
-        self.assertRegex(notify_log, b'[0-9.]+ Notify "Power" .* ".*" ".*Wireless mouse .*very low.* 
power.*\([0-9.]+%\).*"')
+        self.p_notify_log.check_line_re(b'[0-9.]+ Notify "Power" .* ".*" ".*Wireless mouse .*very low.* 
power.*\([0-9.]+%\).*"', timeout=0.5)
 
     def test_notify_device_battery_coarse_level(self):
         '''critical power level notification for device batteries with coarse level'''
@@ -993,14 +962,10 @@ class PowerPluginTest6(PowerPluginBase):
                                    dbus_interface='org.freedesktop.DBus.Mock')
 
         self.check_plugin_log('EMIT: charge-critical', 2)
-        time.sleep(0.5)
-
-        # we should have gotten a notification by now
-        notify_log = self.p_notify.stdout.read()
 
-        # verify notification
-        self.assertRegex(notify_log, b'[0-9.]+ Notify "Power" .* ".*" ".*Wireless mouse .*low.* power.*"')
-        self.assertNotRegex(notify_log, b'[0-9.]+ Notify "Power" .* ".*" ".*\([0-9.]+%\).*"')
+        self.p_notify_log.check_no_line_re(b'[0-9.]+ Notify "Power" .* ".*" ".*\([0-9.]+%\).*"', wait=0.5)
+        self.p_notify_log.check_line_re(b'[0-9.]+ Notify "Power" .* ".*" ".*Wireless mouse .*low.* power.*"')
+        self.p_notify_log.check_no_line_re(b'[0-9.]+ Notify "Power" .* ".*" ".*\([0-9.]+%\).*"')
 
     def test_forced_logout(self):
         '''Test forced logout'''
@@ -1014,9 +979,7 @@ class PowerPluginTest6(PowerPluginBase):
 
         self.check_for_logout(idle_delay + 2)
 
-        # The notification should have been received before the logout, but it's saved anyway
-        notify_log = self.p_notify.stdout.read()
-        self.assertTrue(b'You will soon log out because of inactivity.' in notify_log)
+        self.p_notify_log.check_line(b'You will soon log out because of inactivity.')
 
     def test_forced_logout_inhibition(self):
         '''Test we don't force logout when inhibited'''
diff --git a/plugins/xsettings/test.py b/plugins/xsettings/test.py
index 8e5bfda7..3657b588 100755
--- a/plugins/xsettings/test.py
+++ b/plugins/xsettings/test.py
@@ -98,10 +98,7 @@ class XsettingsPluginTest(gsdtestcase.GSDTestCase):
         self.plugin_log = open(self.plugin_log_write.name, 'rb', buffering=0)
 
         # flush notification log
-        try:
-            self.p_notify.stdout.read()
-        except IOError:
-            pass
+        self.p_notify_log.clear()
 
         time.sleep(3)
         obj_xsettings = self.session_bus_con.get_object(
diff --git a/tests/gsdtestcase.py b/tests/gsdtestcase.py
index e0704128..a45e5125 100644
--- a/tests/gsdtestcase.py
+++ b/tests/gsdtestcase.py
@@ -94,10 +94,11 @@ class GSDTestCase(X11SessionTestCase):
         klass.addClassCleanup(klass.session_bus_con.close)
 
         # we never want to cause notifications on the actual GUI
+        klass.p_notify_log = OutputChecker()
         klass.p_notify = klass.spawn_server_template(
-            'notification_daemon', {}, stdout=subprocess.PIPE)[0]
-        set_nonblock(klass.p_notify.stdout)
-        klass.addClassCleanup(lambda : (klass.p_notify.terminate(), klass.p_notify.wait()))
+            'notification_daemon', {}, stdout=klass.p_notify_log.fd)[0]
+        klass.p_notify_log.writer_attached()
+        klass.addClassCleanup(klass.stop_process, klass.p_notify)
 
         klass.start_session()
         klass.start_monitor()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]