[geary/mjog/lru-cache-intermittent-test-failure] Util.Cache.Lru: Fix intermitted unit test failure



commit 6da19c5ba9b902a3f4ea0507e346bf63ed554fe5
Author: Michael Gratton <mike vee net>
Date:   Tue Mar 17 14:45:54 2020 +1100

    Util.Cache.Lru: Fix intermitted unit test failure
    
    Cache behaviour was undefined if two entries were added in the same
    microsecond. This adds a case to the comparison to stablise the sort
    order when that occurs.
    
    Fixes #739

 src/client/util/util-cache.vala | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
---
diff --git a/src/client/util/util-cache.vala b/src/client/util/util-cache.vala
index 44097d89..f054e32e 100644
--- a/src/client/util/util-cache.vala
+++ b/src/client/util/util-cache.vala
@@ -13,8 +13,15 @@ public class Util.Cache.Lru<T> : Geary.BaseObject {
 
 
         public static int lru_compare(CacheEntry<T> a, CacheEntry<T> b) {
-            return (a.key == b.key)
-                ? 0 : (int) (a.last_used - b.last_used);
+            if (a.key == b.key) {
+                return 0;
+            }
+            if (a.last_used != b.last_used) {
+                return (int) (a.last_used - b.last_used);
+            }
+            // If all else is equal, use the keys themselves to
+            // stabilise the sorting order
+            return GLib.strcmp(a.key, b.key);
         }
 
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]