|
1 | 1 | 'use strict';
|
2 | 2 | const common = require('../common');
|
3 | 3 |
|
4 |
| -// This test ensures that the stream implementation correctly handles values |
5 |
| -// for highWaterMark which exceed the range of signed 32 bit integers and |
6 |
| -// rejects invalid values. |
7 |
| - |
8 | 4 | const assert = require('assert');
|
9 | 5 | const stream = require('stream');
|
10 | 6 |
|
11 |
| -// This number exceeds the range of 32 bit integer arithmetic but should still |
12 |
| -// be handled correctly. |
13 |
| -const ovfl = Number.MAX_SAFE_INTEGER; |
14 |
| - |
15 |
| -const readable = stream.Readable({ highWaterMark: ovfl }); |
16 |
| -assert.strictEqual(readable._readableState.highWaterMark, ovfl); |
17 |
| - |
18 |
| -const writable = stream.Writable({ highWaterMark: ovfl }); |
19 |
| -assert.strictEqual(writable._writableState.highWaterMark, ovfl); |
20 |
| - |
21 |
| -for (const invalidHwm of [true, false, '5', {}, -5, NaN]) { |
22 |
| - for (const type of [stream.Readable, stream.Writable]) { |
23 |
| - common.expectsError(() => { |
24 |
| - type({ highWaterMark: invalidHwm }); |
25 |
| - }, { |
26 |
| - type: TypeError, |
27 |
| - code: 'ERR_INVALID_OPT_VALUE', |
28 |
| - message: `The value "${invalidHwm}" is invalid for option "highWaterMark"` |
29 |
| - }); |
| 7 | +{ |
| 8 | + // This test ensures that the stream implementation correctly handles values |
| 9 | + // for highWaterMark which exceed the range of signed 32 bit integers and |
| 10 | + // rejects invalid values. |
| 11 | + |
| 12 | + // This number exceeds the range of 32 bit integer arithmetic but should still |
| 13 | + // be handled correctly. |
| 14 | + const ovfl = Number.MAX_SAFE_INTEGER; |
| 15 | + |
| 16 | + const readable = stream.Readable({ highWaterMark: ovfl }); |
| 17 | + assert.strictEqual(readable._readableState.highWaterMark, ovfl); |
| 18 | + |
| 19 | + const writable = stream.Writable({ highWaterMark: ovfl }); |
| 20 | + assert.strictEqual(writable._writableState.highWaterMark, ovfl); |
| 21 | + |
| 22 | + for (const invalidHwm of [true, false, '5', {}, -5, NaN]) { |
| 23 | + for (const type of [stream.Readable, stream.Writable]) { |
| 24 | + common.expectsError(() => { |
| 25 | + type({ highWaterMark: invalidHwm }); |
| 26 | + }, { |
| 27 | + type: TypeError, |
| 28 | + code: 'ERR_INVALID_OPT_VALUE', |
| 29 | + message: |
| 30 | + `The value "${invalidHwm}" is invalid for option "highWaterMark"` |
| 31 | + }); |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +{ |
| 37 | + // This test ensures that the push method's implementation |
| 38 | + // correctly handles the edge case where the highWaterMark and |
| 39 | + // the state.length are both zero |
| 40 | + |
| 41 | + const readable = stream.Readable({ highWaterMark: 0 }); |
| 42 | + |
| 43 | + for (let i = 0; i < 3; i++) { |
| 44 | + const needMoreData = readable.push(); |
| 45 | + assert.strictEqual(needMoreData, true); |
30 | 46 | }
|
31 | 47 | }
|
| 48 | + |
| 49 | +{ |
| 50 | + // This test ensures that the read(n) method's implementation |
| 51 | + // correctly handles the edge case where the highWaterMark, state.length |
| 52 | + // and n are all zero |
| 53 | + |
| 54 | + const readable = stream.Readable({ highWaterMark: 0 }); |
| 55 | + |
| 56 | + readable._read = common.mustCall(); |
| 57 | + readable.read(0); |
| 58 | +} |
0 commit comments