[folks] tests: Add an assertion that the main context is empty on tear_down



commit 5c0c1c6801ab43adacfd53d46d7cc856092c2501
Author: Philip Withnall <philip withnall collabora co uk>
Date:   Thu Apr 3 14:39:03 2014 +0100

    tests: Add an assertion that the main context is empty on tear_down
    
    Leaving the global default main context with pending events is a common
    cause of cross-test interference which can cause spurious test failures.
    Try and ensure there are no pending events at the end of each test’s
    tear_down function.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=726973

 tests/lib/test-case.vala  |    2 ++
 tests/lib/test-utils.vala |   16 ++++++++++++++++
 2 files changed, 18 insertions(+), 0 deletions(-)
---
diff --git a/tests/lib/test-case.vala b/tests/lib/test-case.vala
index 1eeb430..cbda675 100644
--- a/tests/lib/test-case.vala
+++ b/tests/lib/test-case.vala
@@ -401,6 +401,8 @@ public abstract class Folks.TestCase : Object
    */
   public virtual void tear_down ()
     {
+      /* Assert there are no events left on the main context. */
+      assert (TestUtils.main_context_is_empty ());
     }
 
   internal extern static void _dbus_1_set_no_exit_on_disconnect ();
diff --git a/tests/lib/test-utils.vala b/tests/lib/test-utils.vala
index 67f1248..fa41778 100644
--- a/tests/lib/test-utils.vala
+++ b/tests/lib/test-utils.vala
@@ -587,4 +587,20 @@ public class Folks.TestUtils
           return BuildConf.ABS_TOP_BUILDDIR + "/tests/" + filename;
         }
     }
+
+  /**
+   * Check that there are no pending events on the given ``context``, and return
+   * ``true`` if there are none.
+   *
+   * @param context A main context, or ``null`` to use the default main context.
+   * @returns Whether there are no events pending on the context.
+   * @since UNRELEASED
+   */
+  public static bool main_context_is_empty (MainContext? context = null)
+    {
+      if (context == null)
+          context = MainContext.default ();
+
+      return !context.pending ();
+    }
 }


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