@@ -21,31 +21,37 @@ assert.strictEqual(typeof hdl.uid, 'number'); |
21 | 21 | assert.strictEqual(typeof hdl.triggerAsyncId, 'number'); |
22 | 22 | checkInvocations(hdl, { init: 1 }, 'when created handle'); |
23 | 23 |
|
| 24 | +// Store all buffers together so that they do not get |
| 25 | +// garbage collected. |
| 26 | +const buffers = { |
| 27 | + writeResult: new Uint32Array(2), |
| 28 | + dictionary: new Uint8Array(0), |
| 29 | + inBuf: new Uint8Array([0x78]), |
| 30 | + outBuf: new Uint8Array(1) |
| 31 | +}; |
| 32 | + |
24 | 33 | handle.init( |
25 | 34 | constants.Z_DEFAULT_WINDOWBITS, |
26 | 35 | constants.Z_MIN_LEVEL, |
27 | 36 | constants.Z_DEFAULT_MEMLEVEL, |
28 | 37 | constants.Z_DEFAULT_STRATEGY, |
29 | | - new Uint32Array(2), |
| 38 | + buffers.writeResult, |
30 | 39 | function processCallback() { this.cb(); }, |
31 | | - Buffer.from('') |
| 40 | + buffers.dictionary |
32 | 41 | ); |
33 | 42 | checkInvocations(hdl, { init: 1 }, 'when initialized handle'); |
34 | 43 |
|
35 | | -const inBuf = Buffer.from('x'); |
36 | | -const outBuf = Buffer.allocUnsafe(1); |
37 | | - |
38 | 44 | let count = 2; |
39 | 45 | handle.cb = common.mustCall(onwritten, 2); |
40 | | -handle.write(true, inBuf, 0, 1, outBuf, 0, 1); |
| 46 | +handle.write(true, buffers.inBuf, 0, 1, buffers.outBuf, 0, 1); |
41 | 47 | checkInvocations(hdl, { init: 1 }, 'when invoked write() on handle'); |
42 | 48 |
|
43 | 49 | function onwritten() { |
44 | 50 | if (--count) { |
45 | 51 | // first write |
46 | 52 | checkInvocations(hdl, { init: 1, before: 1 }, |
47 | 53 | 'when wrote to handle the first time'); |
48 | | - handle.write(true, inBuf, 0, 1, outBuf, 0, 1); |
| 54 | + handle.write(true, buffers.inBuf, 0, 1, buffers.outBuf, 0, 1); |
49 | 55 | } else { |
50 | 56 | // second write |
51 | 57 | checkInvocations(hdl, { init: 1, before: 2, after: 1 }, |
@@ -61,4 +67,7 @@ function onexit() { |
61 | 67 | // TODO: destroy never called here even with large amounts of ticks |
62 | 68 | // is that correct? |
63 | 69 | checkInvocations(hdl, { init: 1, before: 2, after: 2 }, 'when process exits'); |
| 70 | + |
| 71 | + // Do something with `buffers` to keep them alive until here. |
| 72 | + buffers.buffers = buffers; |
64 | 73 | } |