tests: Wait for memory monitor to be able to handle signals before testing

This fixes a race condition which frequently caused the
`memory-monitor-dbus.py` test to fail.

The registration of the `LowMemoryMonitor` object on the bus, and the
`GMemoryMonitorDBus`’s connection to the warning signal raced, such that
it was possible for the mock `LowMemoryMonitor` to emit a warning signal
before the `GMemoryMonitorDBus` proxy was listening, and hence the proxy
would never see the signal.

Fix this by explicitly synchronising the two before proceeding to the
tests.

Make the same changes in the `memory-monitor-portal.py` test too, even
though that one was not failing. This should remove the need for a 2s
wait on every test run.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Fixes: #2887
This commit is contained in:
Philip Withnall 2024-01-22 14:03:50 +00:00
parent e0276a3a17
commit 67a9fbf1fa
2 changed files with 26 additions and 16 deletions

View File

@ -16,7 +16,6 @@ import sys
import subprocess
import fcntl
import os
import time
import taptestrunner
@ -71,6 +70,19 @@ try:
self.mainloop = GLib.MainLoop()
self.main_context = self.mainloop.get_context()
# The LowMemoryMonitor API is stateless: it doesnt 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()
@ -105,13 +117,6 @@ try:
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", 2)

View File

@ -16,7 +16,6 @@ import sys
import subprocess
import fcntl
import os
import time
import taptestrunner
@ -80,6 +79,19 @@ try:
self.mainloop = GLib.MainLoop()
self.main_context = self.mainloop.get_context()
# The LowMemoryMonitor API is stateless: it doesnt 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()
@ -113,13 +125,6 @@ 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", 2)