| 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__ = '[email protected]' |
| 11 |
__copyright__ = '(c) 2019 Red Hat Inc.' |
| 12 |
__license__ = 'LGPL 3+' |
| 13 |
|
| 14 |
import unittest |
| 15 |
import sys |
| 16 |
import subprocess |
| 17 |
import fcntl |
| 18 |
import os |
| 19 |
import time |
| 20 |
|
| 21 |
import taptestrunner |
| 22 |
|
| 23 |
try: |
| 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 |
class TestLowMemoryMonitor(dbusmock.DBusTestCase): |
| 35 |
'''Test GMemoryMonitorDBus''' |
| 36 |
|
| 37 |
@classmethod |
| 38 |
def setUpClass(klass): |
| 39 |
klass.start_system_bus() |
| 40 |
klass.dbus_con = klass.get_dbus(True) |
| 41 |
|
| 42 |
def setUp(self): |
| 43 |
try: |
| 44 |
Gio.MemoryMonitor |
| 45 |
except AttributeError: |
| 46 |
raise unittest.SkipTest('Low memory monitor not in ' |
| 47 |
'introspection data. Requires ' |
| 48 |
'GObject-Introspection ≥ 1.63.2') |
| 49 |
try: |
| 50 |
(self.p_mock, self.obj_lmm) = self.spawn_server_template( |
| 51 |
'low_memory_monitor', {}, stdout=subprocess.PIPE) |
| 52 |
except ModuleNotFoundError: |
| 53 |
raise unittest.SkipTest("Low memory monitor dbusmock template not " |
| 54 |
"found. Requires dbusmock ≥ 0.18.4.") |
| 55 |
# set log to nonblocking |
| 56 |
flags = fcntl.fcntl(self.p_mock.stdout, fcntl.F_GETFL) |
| 57 |
fcntl.fcntl(self.p_mock.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK) |
| 58 |
self.last_warning = -1 |
| 59 |
self.dbusmock = dbus.Interface(self.obj_lmm, dbusmock.MOCK_IFACE) |
| 60 |
self.memory_monitor = Gio.MemoryMonitor.dup_default() |
| 61 |
self.memory_monitor.connect("low-memory-warning", self.memory_warning_cb) |
| 62 |
self.mainloop = GLib.MainLoop() |
| 63 |
self.main_context = self.mainloop.get_context() |
| 64 |
|
| 65 |
def tearDown(self): |
| 66 |
self.p_mock.terminate() |
| 67 |
self.p_mock.wait() |
| 68 |
|
| 69 |
def assertEventually(self, condition, message=None, timeout=50): |
| 70 |
'''Assert that condition function eventually returns True. |
| 71 |
|
| 72 |
Timeout is in deciseconds, defaulting to 50 (5 seconds). message is |
| 73 |
printed on failure. |
| 74 |
''' |
| 75 |
while timeout >= 0: |
| 76 |
context = GLib.MainContext.default() |
| 77 |
while context.iteration(False): |
| 78 |
pass |
| 79 |
if condition(): |
| 80 |
break |
| 81 |
timeout -= 1 |
| 82 |
time.sleep(0.1) |
| 83 |
else: |
| 84 |
self.fail(message or 'timed out waiting for ' + str(condition)) |
| 85 |
|
| 86 |
def memory_warning_cb(self, monitor, level): |
| 87 |
self.last_warning = level |
| 88 |
self.main_context.wakeup() |
| 89 |
|
| 90 |
def test_low_memory_warning_signal(self): |
| 91 |
'''LowMemoryWarning signal''' |
| 92 |
|
| 93 |
# Wait 2 seconds |
| 94 |
timeout = 2 |
| 95 |
while timeout > 0: |
| 96 |
time.sleep(0.5) |
| 97 |
timeout -= 0.5 |
| 98 |
self.main_context.iteration(False) |
| 99 |
|
| 100 |
self.dbusmock.EmitWarning(100) |
| 101 |
# Wait 2 seconds or until warning |
| 102 |
self.assertEventually(lambda: self.last_warning == 100, "'100' low-memory warning not received", 20) |
| 103 |
|
| 104 |
self.dbusmock.EmitWarning(255) |
| 105 |
# Wait 2 seconds or until warning |
| 106 |
self.assertEventually(lambda: self.last_warning == 255, "'255' low-memory warning not received", 20) |
| 107 |
|
| 108 |
except ImportError as e: |
| 109 |
@unittest.skip("Cannot import %s" % e.name) |
| 110 |
class TestLowMemoryMonitor(unittest.TestCase): |
| 111 |
def test_low_memory_warning_signal(self): |
| 112 |
pass |
| 113 |
|
| 114 |
if __name__ == '__main__': |
| 115 |
unittest.main(testRunner=taptestrunner.TAPTestRunner()) |