mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-01-12 07:26:15 +01:00
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:
parent
e0276a3a17
commit
67a9fbf1fa
@ -16,7 +16,6 @@ import sys
|
|||||||
import subprocess
|
import subprocess
|
||||||
import fcntl
|
import fcntl
|
||||||
import os
|
import os
|
||||||
import time
|
|
||||||
|
|
||||||
import taptestrunner
|
import taptestrunner
|
||||||
|
|
||||||
@ -71,6 +70,19 @@ try:
|
|||||||
self.mainloop = GLib.MainLoop()
|
self.mainloop = GLib.MainLoop()
|
||||||
self.main_context = self.mainloop.get_context()
|
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):
|
def tearDown(self):
|
||||||
self.p_mock.terminate()
|
self.p_mock.terminate()
|
||||||
self.p_mock.wait()
|
self.p_mock.wait()
|
||||||
@ -105,13 +117,6 @@ try:
|
|||||||
def test_low_memory_warning_signal(self):
|
def test_low_memory_warning_signal(self):
|
||||||
'''LowMemoryWarning signal'''
|
'''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)
|
self.dbusmock.EmitWarning(100)
|
||||||
# Wait 2 seconds or until warning
|
# Wait 2 seconds or until warning
|
||||||
self.assertEventually(lambda: self.last_warning == 100, "'100' low-memory warning not received", 2)
|
self.assertEventually(lambda: self.last_warning == 100, "'100' low-memory warning not received", 2)
|
||||||
|
@ -16,7 +16,6 @@ import sys
|
|||||||
import subprocess
|
import subprocess
|
||||||
import fcntl
|
import fcntl
|
||||||
import os
|
import os
|
||||||
import time
|
|
||||||
|
|
||||||
import taptestrunner
|
import taptestrunner
|
||||||
|
|
||||||
@ -80,6 +79,19 @@ try:
|
|||||||
self.mainloop = GLib.MainLoop()
|
self.mainloop = GLib.MainLoop()
|
||||||
self.main_context = self.mainloop.get_context()
|
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):
|
def tearDown(self):
|
||||||
self.p_mock.terminate()
|
self.p_mock.terminate()
|
||||||
self.p_mock.wait()
|
self.p_mock.wait()
|
||||||
@ -113,13 +125,6 @@ try:
|
|||||||
def test_low_memory_warning_portal_signal(self):
|
def test_low_memory_warning_portal_signal(self):
|
||||||
'''LowMemoryWarning signal'''
|
'''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)
|
self.dbusmock.EmitWarning(100)
|
||||||
# Wait 2 seconds or until warning
|
# Wait 2 seconds or until warning
|
||||||
self.assertEventually(lambda: self.last_warning == 100, "'100' low-memory warning not received", 2)
|
self.assertEventually(lambda: self.last_warning == 100, "'100' low-memory warning not received", 2)
|
||||||
|
Loading…
Reference in New Issue
Block a user