1// Flags: --expose-internals 2'use strict'; 3 4const common = require('../common'); 5const assert = require('assert'); 6const initHooks = require('./init-hooks'); 7const { checkInvocations } = require('./hook-checks'); 8 9const hooks = initHooks(); 10 11hooks.enable(); 12const { internalBinding } = require('internal/test/binding'); 13const { Zlib } = internalBinding('zlib'); 14const constants = require('zlib').constants; 15 16const handle = new Zlib(constants.DEFLATE); 17 18const as = hooks.activitiesOfTypes('ZLIB'); 19assert.strictEqual(as.length, 1); 20const hdl = as[0]; 21assert.strictEqual(hdl.type, 'ZLIB'); 22assert.strictEqual(typeof hdl.uid, 'number'); 23assert.strictEqual(typeof hdl.triggerAsyncId, 'number'); 24checkInvocations(hdl, { init: 1 }, 'when created handle'); 25 26// Store all buffers together so that they do not get 27// garbage collected. 28const buffers = { 29 writeResult: new Uint32Array(2), 30 dictionary: new Uint8Array(0), 31 inBuf: new Uint8Array([0x78]), 32 outBuf: new Uint8Array(1), 33}; 34 35handle.init( 36 constants.Z_DEFAULT_WINDOWBITS, 37 constants.Z_MIN_LEVEL, 38 constants.Z_DEFAULT_MEMLEVEL, 39 constants.Z_DEFAULT_STRATEGY, 40 buffers.writeResult, 41 function processCallback() { this.cb(); }, 42 buffers.dictionary, 43); 44checkInvocations(hdl, { init: 1 }, 'when initialized handle'); 45 46let count = 2; 47handle.cb = common.mustCall(onwritten, 2); 48handle.write(true, buffers.inBuf, 0, 1, buffers.outBuf, 0, 1); 49checkInvocations(hdl, { init: 1 }, 'when invoked write() on handle'); 50 51function onwritten() { 52 if (--count) { 53 // first write 54 checkInvocations(hdl, { init: 1, before: 1 }, 55 'when wrote to handle the first time'); 56 handle.write(true, buffers.inBuf, 0, 1, buffers.outBuf, 0, 1); 57 } else { 58 // second write 59 checkInvocations(hdl, { init: 1, before: 2, after: 1 }, 60 'when wrote to handle the second time'); 61 } 62} 63 64process.on('exit', onexit); 65 66function onexit() { 67 hooks.disable(); 68 hooks.sanityCheck('ZLIB'); 69 // TODO: destroy never called here even with large amounts of ticks 70 // is that correct? 71 checkInvocations(hdl, { init: 1, before: 2, after: 2 }, 'when process exits'); 72 73 // Do something with `buffers` to keep them alive until here. 74 buffers.buffers = buffers; 75} 76