From 52252bd1cb886bc3930794c7d1b2e08171d0114f Mon Sep 17 00:00:00 2001
From: Christopher Willis-Ford <7019101+cwillisf@users.noreply.github.com>
Date: Mon, 6 Jun 2022 14:22:56 -0700
Subject: [PATCH] test(wait): make the wait block test reliable under CPU load

Sometimes load causes the VM to run more slowly, especially with
parallel tests. This change allows the wait block a little extra wiggle
room to account for that. Ending early is still tested with a fairly
strict threshold.
---
 test/unit/blocks_control.js | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/test/unit/blocks_control.js b/test/unit/blocks_control.js
index cbd2b1cf9..25f0c7584 100644
--- a/test/unit/blocks_control.js
+++ b/test/unit/blocks_control.js
@@ -261,7 +261,8 @@ test('wait', t => {
     const args = {DURATION: .01};
     const waitTime = args.DURATION * 1000;
     const startTest = Date.now();
-    const threshold = 1000 / 60; // 60 hz
+    const thresholdSmall = 1000 / 60; // only allow the wait to end one 60Hz frame early
+    const thresholdLarge = 1000 / 3; // be less picky about when the wait ends, in case CPU load makes the VM run slowly
     let yields = 0;
     const util = new BlockUtility();
     const mockUtil = {
@@ -280,7 +281,7 @@ test('wait', t => {
     while (timeElapsed < waitTime) {
         timeElapsed = mockUtil.stackFrame.timer.timeElapsed();
         // In case util.timer is broken - have our own "exit"
-        if (Date.now() - startTest > timeElapsed + threshold) {
+        if (Date.now() - startTest > timeElapsed + thresholdSmall) {
             break;
         }
     }
@@ -288,7 +289,9 @@ test('wait', t => {
     c.wait(args, mockUtil);
     t.equal(yields, 1, 'Second call after timeElapsed does not yield');
     t.equal(waitTime, mockUtil.stackFrame.duration);
-    t.ok(timeElapsed >= (waitTime - threshold) &&
-         timeElapsed <= (waitTime + threshold));
+    t.ok(timeElapsed >= (waitTime - thresholdSmall),
+        'Wait block ended too early: ${timeElapsed} < ${waitTime} - ${thresholdSmall}');
+    t.ok(timeElapsed <= (waitTime + thresholdLarge),
+        'Wait block ended too late: ${timeElapsed} > ${waitTime} + ${thresholdLarge}');
     t.end();
 });