[Patchew-devel] [PATCH v2] testing: Fix invalidation of prefetch cache upon set_property

Fam Zheng posted 1 patch 6 years, 3 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/patchew-ci tags/patchew/20180322020536.30263-1-famz@redhat.com
api/models.py         | 14 ++++++++++----
tests/test_testing.py | 13 +++++++++++++
2 files changed, 23 insertions(+), 4 deletions(-)
[Patchew-devel] [PATCH v2] testing: Fix invalidation of prefetch cache upon set_property
Posted by Fam Zheng 6 years, 3 months ago
Because we have "prefetch_related('properties', 'project')" in
MessageManager.series_heads(), "testing.ready" didn't work due to the
stale cache of Message._properties, which is built from the stale
self.properties, which in turn cannot reflect the .set_property()
updates.

Fix it with a harder invalidating code path.

Signed-off-by: Fam Zheng <famz@redhat.com>
---
 api/models.py         | 14 ++++++++++----
 tests/test_testing.py | 13 +++++++++++++
 2 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/api/models.py b/api/models.py
index de01a61..a672bd4 100644
--- a/api/models.py
+++ b/api/models.py
@@ -408,9 +408,15 @@ class Message(models.Model):
 
     def get_properties(self):
         if hasattr(self, '_properties'):
-            return self._properties
+            if self._properties is not None:
+                return self._properties
+            else:
+                # The prefetch cache is invalidated, query again
+                all_props = MessageProperty.objects.filter(message=self)
+        else:
+            all_props = self.properties.all()
         r = {}
-        for m in self.properties.all():
+        for m in all_props:
             if m.blob:
                 r[m.name] = load_blob_json(m.value)
             else:
@@ -434,8 +440,8 @@ class Message(models.Model):
         mp.value = value
         mp.blob = blob
         mp.save()
-        if hasattr(self, '_properties'):
-            del(self._properties)
+        # Invalidate cache
+        self._properties = None
 
     def set_property(self, prop, value):
         old_val = self.get_property(prop)
diff --git a/tests/test_testing.py b/tests/test_testing.py
index 0897bbc..f26da2f 100755
--- a/tests/test_testing.py
+++ b/tests/test_testing.py
@@ -43,6 +43,19 @@ class TestingTest(PatchewTestCase):
         self.msg.set_property("git.tag", "dummy tag")
         self.msg.set_property("git.base", "dummy base")
 
+    def test_testing_ready(self):
+        self.assertTrue(self.msg.get_property("testing.ready"))
+        # Set property through series_heads elements must be handled the same
+        self.msg.set_property("git.repo", None)
+        self.msg.set_property("git.tag", None)
+        self.msg.set_property("testing.ready", None)
+        msg = Message.objects.series_heads()[0]
+        self.assertEqual(self.msg.message_id, msg.message_id)
+        msg.set_property("git.repo", "dummy repo")
+        msg.set_property("git.tag", "dummy tag")
+        msg.set_property("git.base", "dummy base")
+        self.assertTrue(msg.get_property("testing.ready"))
+
     def msg_testing_done(self, log=None, **report):
         if not 'passed' in report:
             report['passed'] = True
-- 
2.14.3

_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://www.redhat.com/mailman/listinfo/patchew-devel