Bug 1378577 - Fix await race condition which causes intermittent timeouts in test_stylesheet_clone_import_rule.html. r=emilio, a=test-only
authorBrad Werth <bwerth@mozilla.com>
Mon, 28 Aug 2017 17:02:06 -0700
changeset 421470 f105366cc45d84bfe9d41a1c411d2f6b8fc47cdc
parent 421469 5ef3deb43f7d523b975e48c268ddf46d18191345
child 421471 6664fab5dea98a05b834b75583dd9029c279dab9
push id7691
push userryanvm@gmail.com
push dateTue, 29 Aug 2017 23:43:59 +0000
treeherdermozilla-beta@1fd4f4337aae [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersemilio, test-only
bugs1378577
milestone56.0
Bug 1378577 - Fix await race condition which causes intermittent timeouts in test_stylesheet_clone_import_rule.html. r=emilio, a=test-only 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();