1#!/usr/bin/python3 2 3# This program is free software; you can redistribute it and/or modify it under 4# the terms of the GNU Lesser General Public License as published by the Free 5# Software Foundation; either version 3 of the License, or (at your option) any 6# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text 7# of the license. 8 9__author__ = 'Bastien Nocera' 10__email__ = 'hadess@hadess.net' 11__copyright__ = '(c) 2019 Red Hat Inc.' 12__license__ = 'LGPL 3+' 13 14import unittest 15import sys 16import subprocess 17import fcntl 18import os 19import time 20 21import taptestrunner 22 23try: 24 # Do all non-standard imports here so we can skip the tests if any 25 # needed packages are not available. 26 import dbus 27 import dbus.mainloop.glib 28 import dbusmock 29 from gi.repository import GLib 30 from gi.repository import Gio 31 32 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 33 34 # XDG_DESKTOP_PORTAL_PATH = os.path.expanduser("~/.cache/jhbuild/build/xdg-desktop-portal/xdg-desktop-portal") 35 XDG_DESKTOP_PORTAL_PATH = "@libexecdir@/xdg-desktop-portal" 36 37 class TestLowMemoryMonitor(dbusmock.DBusTestCase): 38 '''Test GMemoryMonitorDBus''' 39 40 @classmethod 41 def setUpClass(klass): 42 klass.start_system_bus() 43 klass.dbus_con = klass.get_dbus(True) 44 45 def setUp(self): 46 try: 47 Gio.MemoryMonitor 48 except AttributeError: 49 raise unittest.SkipTest('Low memory monitor not in ' 50 'introspection data. Requires ' 51 'GObject-Introspection ≥ 1.63.2') 52 try: 53 (self.p_mock, self.obj_lmm) = self.spawn_server_template( 54 'low_memory_monitor', {}, stdout=subprocess.PIPE) 55 except ModuleNotFoundError: 56 raise unittest.SkipTest("Low memory monitor dbusmock template not " 57 "found. Requires dbusmock ≥ 0.18.4.") 58 # set log to nonblocking 59 flags = fcntl.fcntl(self.p_mock.stdout, fcntl.F_GETFL) 60 fcntl.fcntl(self.p_mock.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK) 61 self.last_warning = -1 62 self.dbusmock = dbus.Interface(self.obj_lmm, dbusmock.MOCK_IFACE) 63 self.memory_monitor = Gio.MemoryMonitor.dup_default() 64 self.memory_monitor.connect("low-memory-warning", self.memory_warning_cb) 65 self.mainloop = GLib.MainLoop() 66 self.main_context = self.mainloop.get_context() 67 68 def tearDown(self): 69 self.p_mock.terminate() 70 self.p_mock.wait() 71 72 def memory_warning_cb(self, monitor, level): 73 self.last_warning = level 74 self.main_context.wakeup() 75 76 def test_low_memory_warning_signal(self): 77 '''LowMemoryWarning signal''' 78 79 # Wait 2 seconds 80 timeout = 2 81 while timeout > 0: 82 time.sleep(0.5) 83 timeout -= 0.5 84 self.main_context.iteration(False) 85 86 self.dbusmock.EmitWarning(100) 87 # Wait 2 seconds or until warning 88 timeout = 2 89 while timeout > 0 and self.last_warning != 100: 90 time.sleep(0.5) 91 timeout -= 0.5 92 self.main_context.iteration(False) 93 self.assertEqual(self.last_warning, 100) 94 95 self.dbusmock.EmitWarning(255) 96 # Wait 2 seconds or until warning 97 timeout = 2 98 while timeout > 0 and self.last_warning != 255: 99 time.sleep(0.5) 100 timeout -= 0.5 101 self.main_context.iteration(False) 102 self.assertEqual(self.last_warning, 255) 103 104except ImportError as e: 105 @unittest.skip("Cannot import %s" % e.name) 106 class TestLowMemoryMonitor(unittest.TestCase): 107 def test_low_memory_warning_signal(self): 108 pass 109 110if __name__ == '__main__': 111 unittest.main(testRunner=taptestrunner.TAPTestRunner()) 112