File: memory-monitor-dbus.py.in

package info (click to toggle)
glib2.0 2.78.4-6
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,048 kB
  • sloc: ansic: 495,608; xml: 17,404; python: 9,572; sh: 1,260; perl: 1,144; cpp: 487; makefile: 225
file content (131 lines) | stat: -rwxr-xr-x 5,078 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/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 fcntl
import os
import time

import taptestrunner

try:
    # Do all non-standard imports here so we can skip the tests if any
    # needed packages are not available.
    import dbus
    import dbus.mainloop.glib
    import dbusmock
    from gi.repository import GLib
    from gi.repository import Gio

    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

    class TestLowMemoryMonitor(dbusmock.DBusTestCase):
        '''Test GMemoryMonitorDBus'''

        @classmethod
        def setUpClass(klass):
            klass.start_system_bus()
            klass.dbus_con = klass.get_dbus(True)

        @unittest.skipIf('DEB_ALLOW_FLAKY_TESTS' not in os.environ,
                         'https://bugs.debian.org/995178')
        def setUp(self):
            try:
                Gio.MemoryMonitor
            except AttributeError:
                raise unittest.SkipTest('Low memory monitor not in '
                                        'introspection data. Requires '
                                        'GObject-Introspection ≥ 1.63.2')
            try:
                (self.p_mock, self.obj_lmm) = self.spawn_server_template(
                    'low_memory_monitor', {}, stdout=subprocess.PIPE)
            except ModuleNotFoundError:
                raise unittest.SkipTest("Low memory monitor dbusmock template not "
                                        "found. Requires dbusmock ≥ 0.18.4.")
            # 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()
            self.main_context = self.mainloop.get_context()

        def tearDown(self):
            self.p_mock.terminate()
            self.p_mock.wait()

        def assertEventually(self, condition, message=None, timeout=50):
            '''Assert that condition function eventually returns True.

            Timeout is in deciseconds, defaulting to 50 (5 seconds). message is
            printed on failure.
            '''
            print('Waiting up to {}/10 seconds for {!r} to be true'.format(timeout, condition))
            orig_timeout = timeout

            while timeout >= 0:
                print('Loop iteration')
                context = GLib.MainContext.default()
                while context.iteration(False):
                    print('Iterating main loop')
                    pass
                if condition():
                    print('Condition reached')
                    break
                timeout -= 1
                time.sleep(0.1)
            else:
                print('Condition not reached in {}/10 seconds'.format(orig_timeout))
                self.fail(message or 'timed out waiting for ' + str(condition))

        def memory_warning_cb(self, monitor, level):
            print('low-memory-warning: monitor {!r} reports level {}'.format(
                monitor,
                level,
            ))
            self.last_warning = level
            self.main_context.wakeup()

        def test_low_memory_warning_signal(self):
            '''LowMemoryWarning signal'''

            # Wait 2 seconds
            timeout = 2
            while timeout > 0:
                print('Waiting 0.5s for stray events...')
                time.sleep(0.5)
                timeout -= 0.5
                self.main_context.iteration(False)

            print('Emitting mock low-memory warning, level 100: try to free memory')
            self.dbusmock.EmitWarning(100)
            # Wait 2 seconds or until warning
            self.assertEventually(lambda: self.last_warning == 100, "'100' low-memory warning not received", 100)

            print('Emitting mock low-memory warning, level 255: OOM imminent')
            self.dbusmock.EmitWarning(255)
            # Wait 2 seconds or until warning
            self.assertEventually(lambda: self.last_warning == 255, "'255' low-memory warning not received", 100)

except ImportError as e:
    @unittest.skip("Cannot import %s" % e.name)
    class TestLowMemoryMonitor(unittest.TestCase):
        def test_low_memory_warning_signal(self):
            pass

if __name__ == '__main__':
    unittest.main(testRunner=taptestrunner.TAPTestRunner())