fix(bench): eliminate sanitizeExit overhead
};
}
- function assertExitSync(fn, isTest) {
- return function exitSanitizer(...params) {
- setExitHandler((exitCode) => {
- assert(
- false,
- `${
- isTest ? "Test case" : "Bench"
- } attempted to exit with exit code: ${exitCode}`,
- );
- });
-
- try {
- fn(...new SafeArrayIterator(params));
- } catch (err) {
- throw err;
- } finally {
- setExitHandler(null);
- }
- };
- }
-
function assertTestStepScopes(fn) {
/** @param step {TestStep} */
return async function testStepSanitizer(step) {
};
}
- function withPermissions(fn, permissions) {
function pledgePermissions(permissions) {
return core.opSync(
"op_pledge_test_permissions",
core.opSync("op_restore_test_permissions", token);
}
+ function withPermissions(fn, permissions) {
return async function applyPermissions(...params) {
const token = pledgePermissions(permissions);
const AsyncFunction = (async () => {}).constructor;
benchDef.async = AsyncFunction === benchDef.fn.constructor;
- benchDef.fn = wrapBenchFnWithSanitizers(
- benchDef.fn,
- benchDef,
- );
-
ArrayPrototypePush(benches, benchDef);
}
try {
if (bench.permissions) {
- token = core.opSync(
- "op_pledge_test_permissions",
- serializePermissions(bench.permissions),
+ token = pledgePermissions(bench.permissions);
+ }
+
+ if (bench.sanitizeExit) {
+ setExitHandler((exitCode) => {
+ assert(
+ false,
+ `Bench attempted to exit with exit code: ${exitCode}`,
);
+ });
}
const benchTimeInMs = 500;
} catch (error) {
return { failed: { ...bench, error: formatError(error) } };
} finally {
- if (token !== null) core.opSync("op_restore_test_permissions", token);
+ if (bench.sanitizeExit) setExitHandler(null);
+ if (token !== null) restorePermissions(token);
}
}
return testFn;
}
- /**
- * @template T {Function}
- * @param fn {T}
- * @param opts {{
- * sanitizeExit: boolean,
- * }}
- * @returns {T}
- */
- function wrapBenchFnWithSanitizers(fn, opts) {
- if (opts.sanitizeExit) {
- fn = opts.async ? assertExit(fn, false) : assertExitSync(fn, false);
- }
- return fn;
- }
-
/**
* @template T
* @param value {T | undefined}
