Bug 1378577: Fix await race condition which causes intermittent timeouts in test_stylesheet_clone_import_rule.html. draft
authorBrad Werth <bwerth@mozilla.com>
Mon, 28 Aug 2017 17:02:06 -0700
changeset 654630 881304bcbe744b3dcef5167add84b20440a93759
parent 654351 e2efa420beb1a578c7350ba925c82230da6b1267
child 728606 ecf0abcba7eca68ba4c536cbf36984f2115da9c3
push id76619
push userbwerth@mozilla.com
push dateTue, 29 Aug 2017 01:28:48 +0000
bugs1378577
milestone57.0a1
Bug 1378577: Fix await race condition which causes intermittent timeouts in test_stylesheet_clone_import_rule.html. MozReview-Commit-ID: 2vWsCcnsmwF
layout/style/test/chrome/test_stylesheet_clone_import_rule.html
--- a/layout/style/test/chrome/test_stylesheet_clone_import_rule.html
+++ b/layout/style/test/chrome/test_stylesheet_clone_import_rule.html
@@ -28,22 +28,32 @@
     return Array.from(cssRules).map(rule => rule.cssText).join('');
   }
 
   async function runTest() {
 
     // Test that the div is initially red (from base.css)
     is(getComputedStyle(theOnlyDiv).color, "rgb(0, 128, 0)", "div begins as green.");
 
+    // Create a Promise to watch for some number of events.
+    let generateCountingListenerCallback = function(numberOfEventsToWaitFor) {
+      let eventsReceived = 0;
+      return function(event) {
+        return (++eventsReceived >= numberOfEventsToWaitFor);
+      }
+    };
+    let gotAllOurStyleRuleAddedEvents = ContentTaskUtils.waitForEvent(document,
+      "StyleRuleAdded", true, generateCountingListenerCallback(2));
+
     stylesheet.insertRule('@import url("import_useless2.css")', 0);
     stylesheet.insertRule('@import url("import_useless2.css")', 1);
 
-    // Wait for two StyleRuleAdded events to be fired.
-    await ContentTaskUtils.waitForEvent(document, "StyleRuleAdded", true);
-    await ContentTaskUtils.waitForEvent(document, "StyleRuleAdded", true);
+    // Wait for the StyleRuleAdded events to be fired. This function returns the last event,
+    // but we don't care what it contains -- only that the correct number of events fired.
+    await gotAllOurStyleRuleAddedEvents;
 
     // Get the imported sheets and confirm they are non-null and have rules.
     let importSheet1 = stylesheet.cssRules[0].styleSheet;
     let importSheet2 = stylesheet.cssRules[1].styleSheet;
 
     ok(importSheet1 && importSheet2, "Imported sheets exist.");
     if (!(importSheet1 && importSheet2)) {
       SimpleTest.finish();