db: Give the Idle Queue a way to handle high priority tasks

If we have a high priority task, then we want to push it to the front of
the idle queue so it runs as soon as possible.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2023-06-25 09:32:00 -04:00
parent 73019d8eb4
commit 6607e5b0ad
2 changed files with 14 additions and 2 deletions

View File

@ -65,12 +65,13 @@ class Queue(GObject.GObject):
self.cancel()
def push(self, func: typing.Callable, *args,
now: bool = False) -> bool | None:
now: bool = False, first: bool = False) -> bool | None:
"""Add a task to the Idle Queue."""
if not self.enabled or now:
return func(*args)
self._tasks.append((func, *args))
pos = 0 if first else len(self._tasks)
self._tasks.insert(pos, (func, *args))
self.total += 1
self.__start()

View File

@ -139,6 +139,17 @@ class TestIdleQueue(unittest.TestCase):
mock_idle_add.assert_not_called()
func.assert_called_with(1)
def test_push_first(self, mock_idle_add: unittest.mock.Mock,
mock_source_removed: unittest.mock.Mock):
"""Test pushing an idle task with first=True."""
self.queue.push(1)
self.queue.push(0, first=True)
self.assertListEqual(self.queue._tasks, [(0,), (1,)])
self.queue.push(2, first=False)
self.assertListEqual(self.queue._tasks, [(0,), (1,), (2,)])
self.queue.push(3)
self.assertListEqual(self.queue._tasks, [(0,), (1,), (2,), (3,)])
def test_push_many_enabled(self, mock_idle_add: unittest.mock.Mock,
mock_source_removed: unittest.mock.Mock):
"""Test adding several calls to one function at one time."""