scratch-vm/test/integration/clone-cleanup.js
Christopher Willis-Ford 840ffb5df0 test: don't use process.exit to end tests
Newer versions of `tap` run more asynchronously, so sometimes using `process.nextTick(process.exit)`
to end a test would prevent the test from completing correctly. Removing all instances of
`process.nextTick(process.exit)` put tests into three categories:
* the test still worked correctly -- no fixup needed.
* the test would hang because the VM's `_steppingInterval` was keeping
  Node alive. These tests call a new `quit()` method which ends the
  stepping interval.
* the `load-extensions` test needed special attention because the "Video
  Sensing" extension starts its own loop using `setTimeout`. I added a
  `_stopLoop()` method on the extension and directly call that from the
  test. I'm not completely happy with this solution but anything more
  general would likely require a change to the extension spec, so I'm
  leaving that as a followup task.
2022-06-07 11:44:06 -07:00

92 lines
2.9 KiB
JavaScript

const path = require('path');
const test = require('tap').test;
const makeTestStorage = require('../fixtures/make-test-storage');
const readFileToBuffer = require('../fixtures/readProjectFile').readFileToBuffer;
const VirtualMachine = require('../../src/index');
const projectUri = path.resolve(__dirname, '../fixtures/clone-cleanup.sb3');
const project = readFileToBuffer(projectUri);
test('clone-cleanup', t => {
const vm = new VirtualMachine();
vm.attachStorage(makeTestStorage());
/**
* Track which step of the project is currently under test.
* @type {number}
*/
let testStep = -1;
const verifyCounts = (expectedClones, extraThreads) => {
// stage plus one sprite, plus clones
t.strictEqual(vm.runtime.targets.length, 2 + expectedClones,
`target count at step ${testStep}`);
// the stage should never have any clones
t.strictEqual(vm.runtime.targets[0].sprite.clones.length, 1,
`stage clone count at step ${testStep}`);
// check sprite clone count (+1 for original)
t.strictEqual(vm.runtime.targets[1].sprite.clones.length, 1 + expectedClones,
`sprite clone count at step ${testStep}`);
// thread count isn't directly tied to clone count since threads can end
t.strictEqual(vm.runtime.threads.length, extraThreads + (2 * expectedClones),
`thread count at step ${testStep}`);
};
const testNextStep = () => {
++testStep;
switch (testStep) {
case 0:
// Project has started, main thread running, no clones yet
verifyCounts(0, 1);
break;
case 1:
// 10 clones have been created, main thread still running
verifyCounts(10, 1);
break;
case 2:
// The first batch of clones has deleted themselves; main thread still running
verifyCounts(0, 1);
break;
case 3:
// The second batch of clones has been created and the main thread is about to end
verifyCounts(10, 1);
// After the main thread ends, do one last test step
setTimeout(() => testNextStep(), 1000);
break;
case 4:
// The second batch of clones has deleted themselves; everything is finished
verifyCounts(0, 0);
vm.quit();
t.end();
break;
}
};
// Start VM, load project, and run
t.doesNotThrow(() => {
vm.start();
vm.clear();
vm.setCompatibilityMode(false);
vm.setTurboMode(false);
vm.loadProject(project).then(() => {
// Verify initial state: no clones, nothing running ("step -1")
verifyCounts(0, 0);
vm.greenFlag();
// Let the project control the pace of the tests
vm.runtime.on('SAY', () => testNextStep());
});
});
});