compute pressure: Fix while loop to end earlier

On some testing bot, the while loop in testing seems
to take most of the time and the callback of the observer
is not invoked on time, which leads to timeout.

A step_timeout is also added to give a time to the event loop
to invoke the callback from the observer.

Bug: 1501324
Change-Id: I33e2253067e537e036b484e7e3fb62fe567346b9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5126032
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Reviewed-by: Raphael Kubo Da Costa <raphael.kubo.da.costa@intel.com>
Commit-Queue: Arnaud Mandy <arnaud.mandy@intel.com>
Cr-Commit-Position: refs/heads/main@{#1250091}
diff --git a/compute-pressure/compute_pressure_rate_obfuscation_mitigation_not_triggered.tentative.https.window.js b/compute-pressure/compute_pressure_rate_obfuscation_mitigation_not_triggered.tentative.https.window.js
index 9fd549d..cb1aa43 100644
--- a/compute-pressure/compute_pressure_rate_obfuscation_mitigation_not_triggered.tentative.https.window.js
+++ b/compute-pressure/compute_pressure_rate_obfuscation_mitigation_not_triggered.tentative.https.window.js
@@ -29,6 +29,8 @@
     while (observerChanges.length < minChangesThreshold) {
       mockPressureService.setPressureUpdate(
           'cpu', readings[i++ % readings.length]);
+      // Allow tasks to run (avoid a micro-task loop).
+      await new Promise((resolve) => t.step_timeout(resolve, 0));
       await t.step_wait(
           () => mockPressureService.updatesDelivered() >= i,
           `At least ${i} readings have been delivered`);
diff --git a/compute-pressure/compute_pressure_rate_obfuscation_mitigation_triggered.tentative.https.window.js b/compute-pressure/compute_pressure_rate_obfuscation_mitigation_triggered.tentative.https.window.js
index 0ae20fd..11dcc3c 100644
--- a/compute-pressure/compute_pressure_rate_obfuscation_mitigation_triggered.tentative.https.window.js
+++ b/compute-pressure/compute_pressure_rate_obfuscation_mitigation_triggered.tentative.https.window.js
@@ -17,18 +17,12 @@
   await new Promise(async resolve => {
     const observerChanges = [];
     const observer = new PressureObserver(changes => {
-      if (observerChanges.length >= (minChangesThreshold - 1) && !gotPenalty) {
-        // Add an assert to the maximum threshold possible.
-        t.step(() => {
-          assert_less_than_equal(observerChanges.length, maxChangesThreshold,
-                                 "Sample count reaching maxChangesThreshold.");
-        });
-
+      if (observerChanges.length >= (minChangesThreshold - 1)) {
         const lastSample = observerChanges.at(-1);
         if ((changes[0].time - lastSample[0].time) >= minPenaltyTimeInMs) {
           // The update delivery might still be working even if
           // maxChangesThreshold have been reached and before disconnect() is
-          // processed. This will corrupt the result for the above t.step().
+          // processed.
           // Therefore we are adding a flag to dismiss any updates after the
           // penalty is detected, which is the condition for the test to pass.
           gotPenalty = true;
@@ -46,12 +40,17 @@
     // pressureChanges.length, as system load and browser optimizations can
     // cause the actual timer used by mockPressureService to deliver readings
     // to be a bit slower or faster than requested.
-    while (true) {
+    while (observerChanges.length <= maxChangesThreshold || !gotPenalty) {
       mockPressureService.setPressureUpdate(
           'cpu', readings[i++ % readings.length]);
+      // Allow tasks to run (avoid a micro-task loop).
+      await new Promise((resolve) => t.step_timeout(resolve, 0));
       await t.step_wait(
           () => mockPressureService.updatesDelivered() >= i,
           `At least ${i} readings have been delivered`);
     }
+
+    assert_true(gotPenalty, 'Penalty not triggered');
+
   });
 }, 'Rate obfuscation mitigation should have been triggered, when changes is higher than minimum changes before penalty');