mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-07-02 17:20:32 -04:00
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.
This commit is contained in:
parent
840ffb5df0
commit
52252bd1cb
1 changed files with 7 additions and 4 deletions
|
@ -261,7 +261,8 @@ test('wait', t => {
|
||||||
const args = {DURATION: .01};
|
const args = {DURATION: .01};
|
||||||
const waitTime = args.DURATION * 1000;
|
const waitTime = args.DURATION * 1000;
|
||||||
const startTest = Date.now();
|
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;
|
let yields = 0;
|
||||||
const util = new BlockUtility();
|
const util = new BlockUtility();
|
||||||
const mockUtil = {
|
const mockUtil = {
|
||||||
|
@ -280,7 +281,7 @@ test('wait', t => {
|
||||||
while (timeElapsed < waitTime) {
|
while (timeElapsed < waitTime) {
|
||||||
timeElapsed = mockUtil.stackFrame.timer.timeElapsed();
|
timeElapsed = mockUtil.stackFrame.timer.timeElapsed();
|
||||||
// In case util.timer is broken - have our own "exit"
|
// In case util.timer is broken - have our own "exit"
|
||||||
if (Date.now() - startTest > timeElapsed + threshold) {
|
if (Date.now() - startTest > timeElapsed + thresholdSmall) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -288,7 +289,9 @@ test('wait', t => {
|
||||||
c.wait(args, mockUtil);
|
c.wait(args, mockUtil);
|
||||||
t.equal(yields, 1, 'Second call after timeElapsed does not yield');
|
t.equal(yields, 1, 'Second call after timeElapsed does not yield');
|
||||||
t.equal(waitTime, mockUtil.stackFrame.duration);
|
t.equal(waitTime, mockUtil.stackFrame.duration);
|
||||||
t.ok(timeElapsed >= (waitTime - threshold) &&
|
t.ok(timeElapsed >= (waitTime - thresholdSmall),
|
||||||
timeElapsed <= (waitTime + threshold));
|
'Wait block ended too early: ${timeElapsed} < ${waitTime} - ${thresholdSmall}');
|
||||||
|
t.ok(timeElapsed <= (waitTime + thresholdLarge),
|
||||||
|
'Wait block ended too late: ${timeElapsed} > ${waitTime} + ${thresholdLarge}');
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue