From 3759340353bb2c56799e17e00e08df11e11050d4 Mon Sep 17 00:00:00 2001
From: Xavier Claessens <xavier.claessens@collabora.com>
Date: Mon, 14 Sep 2015 15:38:53 -0400
Subject: [PATCH] tracker: wait for tumbler to be done

Summary:
This fixes a race where thumbnails doesn't exist yet
when we are asserting that their filename exists.

Differential Revision: https://phabricator.apertis.org/D482
---
 tracker/automated/test-tracker.py | 64 +++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)

diff --git a/tracker/automated/test-tracker.py b/tracker/automated/test-tracker.py
index 88f7996..be536dd 100755
--- a/tracker/automated/test-tracker.py
+++ b/tracker/automated/test-tracker.py
@@ -30,10 +30,60 @@ class TrackerTest(unittest.TestCase):
         self.indexer = TrackerIndexer()
         self.indexer.copy_medias(self.homedir)
 
+        # Monitor thumbnail creation to know when it's done. The DBus API
+        # doesn't have a method to query the initial state but we can be
+        # reasonably sure it's not thumbnailing anything at this point.
+        self.tumbler_queue = []
+        self.tumbler = Gio.DBusProxy.new_for_bus_sync(
+            Gio.BusType.SESSION,
+            Gio.DBusProxyFlags.DO_NOT_LOAD_PROPERTIES,
+            None,
+            'org.freedesktop.thumbnails.Thumbnailer1',
+            '/org/freedesktop/thumbnails/Thumbnailer1',
+            'org.freedesktop.thumbnails.Thumbnailer1',
+            None)
+        self.tumbler.connect('g-signal', self.tumbler_signal_cb)
+
+        # Spy on unicast signals that weren't meant for us, because
+        # the Thumbnailer API uses those, and we want to use them to
+        # determine when it has finished. GDBus doesn't have
+        # high-level API for this sort of nonsense so we do it the hard way.
+        match_rule = ("type=signal,"
+                      "sender='org.freedesktop.thumbnails.Thumbnailer1',"
+                      "eavesdrop=true")
+        conn = self.tumbler.get_connection()
+        conn.call_sync('org.freedesktop.DBus',
+                       '/org/freedesktop/DBus',
+                       'org.freedesktop.DBus',
+                       'AddMatch',
+                       GLib.Variant('(s)', (match_rule,)),
+                       None, Gio.DBusCallFlags.NONE, -1, None)
+
     def tearDown(self):
         # Keep everything in place for further manual checks
         pass
 
+    def tumbler_signal_cb(self, proxy, sender_name, signal_name, parameters):
+        print('TrackerTest: Received tumbler signal:', signal_name)
+
+        if signal_name == 'Started':
+            child = parameters.get_child_value(0)
+            self.tumbler_queue.append(child.get_uint32())
+        elif signal_name == 'Finished':
+            child = parameters.get_child_value(0)
+            self.tumbler_queue.remove(child.get_uint32())
+        elif signal_name == 'Error':
+            child = parameters.get_child_value(1)
+            uris = child.get_strv()
+            child = parameters.get_child_value(3)
+            msg = child.get_string()
+            raise Exception("Error creating thumbnail for %s: %s" %
+                            ', '.join(uris), msg)
+
+    def tumbler_get_supported_cb(self, source, result):
+        self.tumbler.call_finish(result)
+        self.loop.quit()
+
     def tracker_config_tests(self):
         print("TrackerTest: config tests")
         settings = Gio.Settings.new("org.freedesktop.Tracker.Miner.Files")
@@ -87,6 +137,20 @@ class TrackerTest(unittest.TestCase):
 
     def thumbnail_tests(self):
         print("TrackerTest: thumbnail tests")
+
+        # Drain the DBus queue to make sure we received all signals
+        self.tumbler.call('GetSupported',
+                          GLib.Variant.new_tuple(),
+                          Gio.DBusCallFlags.NONE,
+                          -1,
+                          None,
+                          self.tumbler_get_supported_cb)
+        self.loop.run()
+
+        context = self.loop.get_context()
+        while len(self.tumbler_queue) >= 0:
+            context.iteration(False)
+
         self.assert_has_thumbnail('Documents/lorem_presentation.odp')
         self.assert_has_thumbnail('Documents/lorem_spreadsheet.ods')
         self.assert_has_thumbnail('Documents/more_lorem_ipsum.odt')
-- 
GitLab