0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/async-hooks/test-zlib.zlib-binding.deflate.js
Anna Henningsen 2efe4c29fe
test: fix flaky async-hooks/test-zlib.zlib-binding.deflate
Previously, the typed arrays used in this test would not
automatically be kept alive by the native handle when
it’s using them, so the V8 garbage collector could collect
them while they are still in use by the zlib module,
leading to memory corruption.

Fixes: https://github.com/nodejs/node/issues/20907

PR-URL: https://github.com/nodejs/node/pull/21077
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Yang Guo <yangguo@chromium.org>
Reviewed-By: James M Snell <jasnell@gmail.com>
2018-06-02 16:07:16 +02:00

74 lines
2.1 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const initHooks = require('./init-hooks');
const { checkInvocations } = require('./hook-checks');
const hooks = initHooks();
hooks.enable();
const Zlib = process.binding('zlib').Zlib;
const constants = process.binding('constants').zlib;
const handle = new Zlib(constants.DEFLATE);
const as = hooks.activitiesOfTypes('ZLIB');
assert.strictEqual(as.length, 1);
const hdl = as[0];
assert.strictEqual(hdl.type, 'ZLIB');
assert.strictEqual(typeof hdl.uid, 'number');
assert.strictEqual(typeof hdl.triggerAsyncId, 'number');
checkInvocations(hdl, { init: 1 }, 'when created handle');
// Store all buffers together so that they do not get
// garbage collected.
const buffers = {
writeResult: new Uint32Array(2),
dictionary: new Uint8Array(0),
inBuf: new Uint8Array([0x78]),
outBuf: new Uint8Array(1)
};
handle.init(
constants.Z_DEFAULT_WINDOWBITS,
constants.Z_MIN_LEVEL,
constants.Z_DEFAULT_MEMLEVEL,
constants.Z_DEFAULT_STRATEGY,
buffers.writeResult,
function processCallback() { this.cb(); },
buffers.dictionary
);
checkInvocations(hdl, { init: 1 }, 'when initialized handle');
let count = 2;
handle.cb = common.mustCall(onwritten, 2);
handle.write(true, buffers.inBuf, 0, 1, buffers.outBuf, 0, 1);
checkInvocations(hdl, { init: 1 }, 'when invoked write() on handle');
function onwritten() {
if (--count) {
// first write
checkInvocations(hdl, { init: 1, before: 1 },
'when wrote to handle the first time');
handle.write(true, buffers.inBuf, 0, 1, buffers.outBuf, 0, 1);
} else {
// second write
checkInvocations(hdl, { init: 1, before: 2, after: 1 },
'when wrote to handle the second time');
}
}
process.on('exit', onexit);
function onexit() {
hooks.disable();
hooks.sanityCheck('ZLIB');
// TODO: destroy never called here even with large amounts of ticks
// is that correct?
checkInvocations(hdl, { init: 1, before: 2, after: 2 }, 'when process exits');
// Do something with `buffers` to keep them alive until here.
buffers.buffers = buffers;
}