summaryrefslogtreecommitdiffstats
path: root/meta/recipes-core/glib-2.0/glib-2.0/memory-monitor.patch
blob: 4f38509da6fc1ecb9fbe6972d62c05497da52b09 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
From ce840b6b111e1e109e511f6833d6aa419e2b723a Mon Sep 17 00:00:00 2001
From: Philip Withnall <philip@tecnocode.co.uk>
Date: Tue, 23 Jan 2024 11:16:52 +0000
Subject: [PATCH] Merge branch '2887-memory-monitor-tests' into 'main'

tests: Fix race condition in memory-monitor-dbus.test

Closes #2887

See merge request GNOME/glib!3844

Hopefully these commits fix the occasional failures we've been seeing:
https://bugzilla.yoctoproject.org/show_bug.cgi?id=15362

Upstream-Status: Backport
Signed-off-by: Ross Burton <ross.burton@arm.com>
---
 gio/tests/memory-monitor-dbus.py.in          | 64 +++++++++++++-------
 gio/tests/memory-monitor-portal.py.in        | 54 ++++++++++-------
 gio/tests/power-profile-monitor-dbus.py.in   | 35 ++++++-----
 gio/tests/power-profile-monitor-portal.py.in | 34 ++++++-----
 4 files changed, 113 insertions(+), 74 deletions(-)

diff --git a/gio/tests/memory-monitor-dbus.py.in b/gio/tests/memory-monitor-dbus.py.in
index bf32918..7aae01e 100755
--- a/gio/tests/memory-monitor-dbus.py.in
+++ b/gio/tests/memory-monitor-dbus.py.in
@@ -16,7 +16,6 @@ import sys
 import subprocess
 import fcntl
 import os
-import time
 
 import taptestrunner
 
@@ -57,53 +56,74 @@ try:
             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)
+
+            try:
+                self.wait_for_bus_object('org.freedesktop.LowMemoryMonitor',
+                                        '/org/freedesktop/LowMemoryMonitor',
+                                        system_bus=True)
+            except:
+                raise
+
             self.memory_monitor = Gio.MemoryMonitor.dup_default()
+            assert("GMemoryMonitorDBus" in str(self.memory_monitor))
             self.memory_monitor.connect("low-memory-warning", self.memory_warning_cb)
             self.mainloop = GLib.MainLoop()
             self.main_context = self.mainloop.get_context()
 
+            # The LowMemoryMonitor API is stateless: it doesn’t expose any
+            # properties, just a warning signal. Emit the signal in a loop until
+            # the GMemoryMonitor instance has initialised and synchronised to
+            # the right state.
+            def emit_warning(level):
+                self.dbusmock.EmitWarning(level)
+                return GLib.SOURCE_CONTINUE
+
+            idle_id = GLib.idle_add(emit_warning, 0)
+            while self.last_warning != 0:
+                self.main_context.iteration(True)
+            GLib.source_remove(idle_id)
+
         def tearDown(self):
             self.p_mock.terminate()
             self.p_mock.wait()
 
-        def assertEventually(self, condition, message=None, timeout=50):
+        def assertEventually(self, condition, message=None, timeout=5):
             '''Assert that condition function eventually returns True.
 
-            Timeout is in deciseconds, defaulting to 50 (5 seconds). message is
+            Timeout is in seconds, defaulting to 5 seconds. message is
             printed on failure.
             '''
-            while timeout >= 0:
-                context = GLib.MainContext.default()
-                while context.iteration(False):
-                    pass
-                if condition():
-                    break
-                timeout -= 1
-                time.sleep(0.1)
-            else:
-                self.fail(message or 'timed out waiting for ' + str(condition))
+            if not message:
+                message = 'timed out waiting for ' + str(condition)
+
+            def timed_out_cb(message):
+                self.fail(message)
+                return GLib.SOURCE_REMOVE
+
+            timeout_source = GLib.timeout_source_new_seconds(timeout)
+            timeout_source.set_callback(timed_out_cb, message)
+            timeout_source.attach(self.main_context)
+
+            while not condition():
+                self.main_context.iteration(True)
+
+            timeout_source.destroy()
 
         def memory_warning_cb(self, monitor, level):
+            print("Received memory warning signal, level", 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:
-                time.sleep(0.5)
-                timeout -= 0.5
-                self.main_context.iteration(False)
-
             self.dbusmock.EmitWarning(100)
             # Wait 2 seconds or until warning
-            self.assertEventually(lambda: self.last_warning == 100, "'100' low-memory warning not received", 20)
+            self.assertEventually(lambda: self.last_warning == 100, "'100' low-memory warning not received", 2)
 
             self.dbusmock.EmitWarning(255)
             # Wait 2 seconds or until warning
-            self.assertEventually(lambda: self.last_warning == 255, "'255' low-memory warning not received", 20)
+            self.assertEventually(lambda: self.last_warning == 255, "'255' low-memory warning not received", 2)
 
 except ImportError as e:
     @unittest.skip("Cannot import %s" % e.name)
diff --git a/gio/tests/memory-monitor-portal.py.in b/gio/tests/memory-monitor-portal.py.in
index 748cee8..f570508 100755
--- a/gio/tests/memory-monitor-portal.py.in
+++ b/gio/tests/memory-monitor-portal.py.in
@@ -16,7 +16,6 @@ import sys
 import subprocess
 import fcntl
 import os
-import time
 
 import taptestrunner
 
@@ -80,26 +79,44 @@ try:
             self.mainloop = GLib.MainLoop()
             self.main_context = self.mainloop.get_context()
 
+            # The LowMemoryMonitor API is stateless: it doesn’t expose any
+            # properties, just a warning signal. Emit the signal in a loop until
+            # the GMemoryMonitor instance has initialised and synchronised to
+            # the right state.
+            def emit_warning(level):
+                self.dbusmock.EmitWarning(level)
+                return GLib.SOURCE_CONTINUE
+
+            idle_id = GLib.idle_add(self.emit_warning, 0)
+            while self.last_warning != 0:
+                self.main_context.iteration(True)
+            GLib.source_remove(idle_id)
+
         def tearDown(self):
             self.p_mock.terminate()
             self.p_mock.wait()
 
-        def assertEventually(self, condition, message=None, timeout=50):
+        def assertEventually(self, condition, message=None, timeout=5):
             '''Assert that condition function eventually returns True.
 
-            Timeout is in deciseconds, defaulting to 50 (5 seconds). message is
+            Timeout is in seconds, defaulting to 5 seconds. message is
             printed on failure.
             '''
-            while timeout >= 0:
-                context = GLib.MainContext.default()
-                while context.iteration(False):
-                    pass
-                if condition():
-                    break
-                timeout -= 1
-                time.sleep(0.1)
-            else:
-                self.fail(message or 'timed out waiting for ' + str(condition))
+            if not message:
+                message = 'timed out waiting for ' + str(condition)
+
+            def timed_out_cb(message):
+                self.fail(message)
+                return GLib.SOURCE_REMOVE
+
+            timeout_source = GLib.timeout_source_new_seconds(timeout)
+            timeout_source.set_callback(timed_out_cb, message)
+            timeout_source.attach(self.main_context)
+
+            while not condition():
+                self.main_context.iteration(True)
+
+            timeout_source.destroy()
 
         def portal_memory_warning_cb(self, monitor, level):
             self.last_warning = level
@@ -108,20 +125,13 @@ try:
         def test_low_memory_warning_portal_signal(self):
             '''LowMemoryWarning signal'''
 
-            # Wait 2 seconds
-            timeout = 2
-            while timeout > 0:
-                time.sleep(0.5)
-                timeout -= 0.5
-                self.main_context.iteration(False)
-
             self.dbusmock.EmitWarning(100)
             # Wait 2 seconds or until warning
-            self.assertEventually(lambda: self.last_warning == 100, "'100' low-memory warning not received", 20)
+            self.assertEventually(lambda: self.last_warning == 100, "'100' low-memory warning not received", 2)
 
             self.dbusmock.EmitWarning(255)
             # Wait 2 seconds or until warning
-            self.assertEventually(lambda: self.last_warning == 255, "'255' low-memory warning not received", 20)
+            self.assertEventually(lambda: self.last_warning == 255, "'255' low-memory warning not received", 2)
 
 except ImportError as e:
     @unittest.skip("Cannot import %s" % e.name)
diff --git a/gio/tests/power-profile-monitor-dbus.py.in b/gio/tests/power-profile-monitor-dbus.py.in
index 06e594f..f955afc 100755
--- a/gio/tests/power-profile-monitor-dbus.py.in
+++ b/gio/tests/power-profile-monitor-dbus.py.in
@@ -16,7 +16,6 @@ import sys
 import subprocess
 import fcntl
 import os
-import time
 
 import taptestrunner
 
@@ -58,6 +57,7 @@ try:
             self.power_saver_enabled = False
             self.dbus_props = dbus.Interface(self.obj_ppd, dbus.PROPERTIES_IFACE)
             self.power_profile_monitor = Gio.PowerProfileMonitor.dup_default()
+            assert("GPowerProfileMonitorDBus" in str(self.power_profile_monitor))
             self.power_profile_monitor.connect("notify::power-saver-enabled", self.power_saver_enabled_cb)
             self.mainloop = GLib.MainLoop()
             self.main_context = self.mainloop.get_context()
@@ -66,22 +66,27 @@ try:
             self.p_mock.terminate()
             self.p_mock.wait()
 
-        def assertEventually(self, condition, message=None, timeout=50):
+        def assertEventually(self, condition, message=None, timeout=5):
             '''Assert that condition function eventually returns True.
 
-            Timeout is in deciseconds, defaulting to 50 (5 seconds). message is
+            Timeout is in seconds, defaulting to 5 seconds. message is
             printed on failure.
             '''
-            while timeout >= 0:
-                context = GLib.MainContext.default()
-                while context.iteration(False):
-                    pass
-                if condition():
-                    break
-                timeout -= 1
-                time.sleep(0.1)
-            else:
-                self.fail(message or 'timed out waiting for ' + str(condition))
+            if not message:
+                message = 'timed out waiting for ' + str(condition)
+
+            def timed_out_cb(message):
+                self.fail(message)
+                return GLib.SOURCE_REMOVE
+
+            timeout_source = GLib.timeout_source_new_seconds(timeout)
+            timeout_source.set_callback(timed_out_cb, message)
+            timeout_source.attach(self.main_context)
+
+            while not condition():
+                self.main_context.iteration(True)
+
+            timeout_source.destroy()
 
         def power_saver_enabled_cb(self, spec, data):
             self.power_saver_enabled = self.power_profile_monitor.get_power_saver_enabled()
@@ -92,10 +97,10 @@ try:
 
             self.assertEqual(self.power_profile_monitor.get_power_saver_enabled(), False)
             self.dbus_props.Set('net.hadess.PowerProfiles', 'ActiveProfile', dbus.String('power-saver', variant_level=1))
-            self.assertEventually(lambda: self.power_saver_enabled == True, "power-saver didn't become enabled", 10)
+            self.assertEventually(lambda: self.power_saver_enabled == True, "power-saver didn't become enabled", 1)
 
             self.dbus_props.Set('net.hadess.PowerProfiles', 'ActiveProfile', dbus.String('balanced', variant_level=1))
-            self.assertEventually(lambda: self.power_saver_enabled == False, "power-saver didn't become disabled", 10)
+            self.assertEventually(lambda: self.power_saver_enabled == False, "power-saver didn't become disabled", 1)
 
 except ImportError as e:
     @unittest.skip("Cannot import %s" % e.name)
diff --git a/gio/tests/power-profile-monitor-portal.py.in b/gio/tests/power-profile-monitor-portal.py.in
index 09e9a45..ad2abf6 100755
--- a/gio/tests/power-profile-monitor-portal.py.in
+++ b/gio/tests/power-profile-monitor-portal.py.in
@@ -16,7 +16,6 @@ import sys
 import subprocess
 import fcntl
 import os
-import time
 
 import taptestrunner
 
@@ -90,22 +89,27 @@ try:
             self.p_mock.terminate()
             self.p_mock.wait()
 
-        def assertEventually(self, condition, message=None, timeout=50):
+        def assertEventually(self, condition, message=None, timeout=5):
             '''Assert that condition function eventually returns True.
 
-            Timeout is in deciseconds, defaulting to 50 (5 seconds). message is
+            Timeout is in seconds, defaulting to 5 seconds. message is
             printed on failure.
             '''
-            while timeout >= 0:
-                context = GLib.MainContext.default()
-                while context.iteration(False):
-                    pass
-                if condition():
-                    break
-                timeout -= 1
-                time.sleep(0.1)
-            else:
-                self.fail(message or 'timed out waiting for ' + str(condition))
+            if not message:
+                message = 'timed out waiting for ' + str(condition)
+
+            def timed_out_cb(message):
+                self.fail(message)
+                return GLib.SOURCE_REMOVE
+
+            timeout_source = GLib.timeout_source_new_seconds(timeout)
+            timeout_source.set_callback(timed_out_cb, message)
+            timeout_source.attach(self.main_context)
+
+            while not condition():
+                self.main_context.iteration(True)
+
+            timeout_source.destroy()
 
         def power_saver_enabled_cb(self, spec, data):
             self.power_saver_enabled = self.power_profile_monitor.get_power_saver_enabled()
@@ -116,10 +120,10 @@ try:
 
             self.assertEqual(self.power_profile_monitor.get_power_saver_enabled(), False)
             self.dbus_props.Set('net.hadess.PowerProfiles', 'ActiveProfile', dbus.String('power-saver', variant_level=1))
-            self.assertEventually(lambda: self.power_saver_enabled == True, "power-saver didn't become enabled", 10)
+            self.assertEventually(lambda: self.power_saver_enabled == True, "power-saver didn't become enabled", 1)
 
             self.dbus_props.Set('net.hadess.PowerProfiles', 'ActiveProfile', dbus.String('balanced', variant_level=1))
-            self.assertEventually(lambda: self.power_saver_enabled == False, "power-saver didn't become disabled", 10)
+            self.assertEventually(lambda: self.power_saver_enabled == False, "power-saver didn't become disabled", 1)
 
         def test_power_profile_power_saver_enabled_portal_default(self):
             '''power-saver-enabled property default value'''