[glib/wip/hadess/add-memory-monitor: 6/6] tests: Add GMemoryMonitor installed-tests



commit 4680a93d516f5b61d5c865378c3a43e9ba9b0598
Author: Bastien Nocera <hadess hadess net>
Date:   Fri Nov 15 12:24:57 2019 +0100

    tests: Add GMemoryMonitor installed-tests
    
    Those tests require Python, gobject-introspection and python-dbusmock
    making them unsuitable to be run within the uninstalled test suite.
    
    There are no restrictions of dependencies when it comes to installed
    tests so use those to exercise GMemoryMonitor.

 gio/tests/memory-monitor-dbus.py   | 85 +++++++++++++++++++++++++++++++++
 gio/tests/memory-monitor-portal.py | 98 ++++++++++++++++++++++++++++++++++++++
 gio/tests/meson.build              | 18 +++++++
 3 files changed, 201 insertions(+)
---
diff --git a/gio/tests/memory-monitor-dbus.py b/gio/tests/memory-monitor-dbus.py
new file mode 100755
index 000000000..bd527c8ba
--- /dev/null
+++ b/gio/tests/memory-monitor-dbus.py
@@ -0,0 +1,85 @@
+#!/usr/bin/python3
+
+# This program is free software; you can redistribute it and/or modify it under
+# the terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation; either version 3 of the License, or (at your option) any
+# later version.  See http://www.gnu.org/copyleft/lgpl.html for the full text
+# of the license.
+
+__author__ = 'Bastien Nocera'
+__email__ = 'hadess hadess net'
+__copyright__ = '(c) 2019 Red Hat Inc.'
+__license__ = 'LGPL 3+'
+
+import unittest
+import sys
+import subprocess
+import dbus
+import dbus.mainloop.glib
+import dbusmock
+import fcntl
+import os
+import time
+
+from gi.repository import GLib
+from gi.repository import Gio
+
+dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
+
+# XDG_DESKTOP_PORTAL_PATH = 
os.path.expanduser("~/.cache/jhbuild/build/xdg-desktop-portal/xdg-desktop-portal")
+XDG_DESKTOP_PORTAL_PATH = "/usr/libexec/xdg-desktop-portal"
+
+class TestLowMemoryMonitor(dbusmock.DBusTestCase):
+    '''Test GMemoryMonitorDBus'''
+
+    @classmethod
+    def setUpClass(klass):
+        klass.start_system_bus()
+        klass.dbus_con = klass.get_dbus(True)
+
+    def setUp(self):
+        (self.p_mock, self.obj_lmm) = self.spawn_server_template(
+            'low_memory_monitor', {}, stdout=subprocess.PIPE)
+        # set log to nonblocking
+        flags = fcntl.fcntl(self.p_mock.stdout, fcntl.F_GETFL)
+        fcntl.fcntl(self.p_mock.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)
+        self.last_warning = -1
+        self.dbusmock = dbus.Interface(self.obj_lmm, dbusmock.MOCK_IFACE)
+        self.memory_monitor = Gio.MemoryMonitor.dup_default()
+        self.memory_monitor.connect("low-memory-warning", self.memory_warning_cb)
+        self.mainloop = GLib.MainLoop()
+        main_context = self.mainloop.get_context()
+
+    def tearDown(self):
+        self.p_mock.terminate()
+        self.p_mock.wait()
+
+    def wait_loop(self):
+        main_context = self.mainloop.get_context()
+        timeout = 2
+        while timeout > 0:
+            time.sleep(0.5)
+            timeout -= 0.5
+            main_context.iteration(False)
+
+    def memory_warning_cb(self, monitor, level):
+        # print ("memory_warning_cb %d" % level)
+        self.last_warning = level
+
+    def test_low_memory_warning_signal(self):
+        '''LowMemoryWarning signal'''
+
+        self.wait_loop()
+
+        self.dbusmock.EmitWarning(100)
+        self.wait_loop()
+        self.assertEqual (self.last_warning, 100)
+
+        self.dbusmock.EmitWarning(255)
+        self.wait_loop()
+        self.assertEqual (self.last_warning, 255)
+
+
+if __name__ == '__main__':
+    # avoid writing to stderr
+    unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout, verbosity=2))
diff --git a/gio/tests/memory-monitor-portal.py b/gio/tests/memory-monitor-portal.py
new file mode 100755
index 000000000..88b4912bb
--- /dev/null
+++ b/gio/tests/memory-monitor-portal.py
@@ -0,0 +1,98 @@
+#!/usr/bin/python3
+
+# This program is free software; you can redistribute it and/or modify it under
+# the terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation; either version 3 of the License, or (at your option) any
+# later version.  See http://www.gnu.org/copyleft/lgpl.html for the full text
+# of the license.
+
+__author__ = 'Bastien Nocera'
+__email__ = 'hadess hadess net'
+__copyright__ = '(c) 2019 Red Hat Inc.'
+__license__ = 'LGPL 3+'
+
+import unittest
+import sys
+import subprocess
+import dbus
+import dbus.mainloop.glib
+import dbusmock
+import fcntl
+import os
+import time
+
+from gi.repository import GLib
+from gi.repository import Gio
+
+dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
+
+# XDG_DESKTOP_PORTAL_PATH = 
os.path.expanduser("~/.cache/jhbuild/build/xdg-desktop-portal/xdg-desktop-portal")
+XDG_DESKTOP_PORTAL_PATH = "/usr/libexec/xdg-desktop-portal"
+
+class TestLowMemoryMonitorPortal(dbusmock.DBusTestCase):
+    '''Test GMemoryMonitorPortal'''
+
+    @classmethod
+    def setUpClass(klass):
+        klass.start_system_bus()
+        klass.dbus_con = klass.get_dbus(True)
+
+    def setUp(self):
+        (self.p_mock, self.obj_lmm) = self.spawn_server_template(
+            'low_memory_monitor', {}, stdout=subprocess.PIPE)
+        # set log to nonblocking
+        flags = fcntl.fcntl(self.p_mock.stdout, fcntl.F_GETFL)
+        fcntl.fcntl(self.p_mock.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)
+        self.last_warning = -1
+        self.dbusmock = dbus.Interface(self.obj_lmm, dbusmock.MOCK_IFACE)
+        self.xdp = subprocess.Popen([XDG_DESKTOP_PORTAL_PATH])
+        try:
+            self.wait_for_bus_object('org.freedesktop.portal.Desktop',
+                                     '/org/freedesktop/portal/desktop')
+        except:
+            # on failure, print log
+            with open(self.session_log_write.name) as f:
+                print('----- session log -----\n%s\n------' % f.read())
+            raise
+        # subprocess.Popen(['gdbus', 'monitor', '--session', '--dest', 'org.freedesktop.portal.Desktop'])
+
+        os.environ['GTK_USE_PORTAL'] = "1"
+        self.memory_monitor = Gio.MemoryMonitor.dup_default()
+        assert("GMemoryMonitorPortal" in str(self.memory_monitor))
+        self.memory_monitor.connect("low-memory-warning", self.portal_memory_warning_cb)
+        self.mainloop = GLib.MainLoop()
+        main_context = self.mainloop.get_context()
+
+    def tearDown(self):
+        self.p_mock.terminate()
+        self.p_mock.wait()
+
+    def wait_loop(self):
+        main_context = self.mainloop.get_context()
+        timeout = 2
+        while timeout > 0:
+            time.sleep(0.5)
+            timeout -= 0.5
+            main_context.iteration(False)
+
+    def portal_memory_warning_cb(self, monitor, level):
+        print ("portal_memory_warning_cb %d" % level)
+        self.last_warning = level
+
+    def test_low_memory_warning_portal_signal(self):
+        '''LowMemoryWarning signal'''
+
+        self.wait_loop()
+
+        self.dbusmock.EmitWarning(100)
+        self.wait_loop()
+        self.assertEqual (self.last_warning, 100)
+
+        self.dbusmock.EmitWarning(255)
+        self.wait_loop()
+        self.assertEqual (self.last_warning, 255)
+
+
+if __name__ == '__main__':
+    # avoid writing to stderr
+    unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout, verbosity=2))
diff --git a/gio/tests/meson.build b/gio/tests/meson.build
index 0f493e521..6c38c0b0a 100644
--- a/gio/tests/meson.build
+++ b/gio/tests/meson.build
@@ -474,6 +474,24 @@ if installed_tests_enabled
   install_data('static-link.py', install_dir : installed_tests_execdir)
 endif
 
+memory_monitor_tests = [
+  'memory-monitor-dbus',
+  'memory-monitor-portal',
+]
+
+foreach memory_monitor_test : memory_monitor_tests
+  cdata = configuration_data()
+  cdata.set('installed_tests_dir', installed_tests_execdir)
+  cdata.set('program', memory_monitor_test + '.py')
+  configure_file(
+    input: installed_tests_template,
+    output: memory_monitor_test + '.test',
+    install_dir: installed_tests_metadir,
+    configuration: cdata
+  )
+  install_data(memory_monitor_test + '.py', install_dir : installed_tests_execdir)
+endforeach
+
 if not meson.is_cross_build() or meson.has_exe_wrapper()
 
   plugin_resources_c = custom_target('plugin-resources.c',


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