test: improve multiple zlib tests · nodejs/node@68cf7f0
@@ -6,124 +6,119 @@ if (!common.hasCrypto)
66const assert = require('assert');
77const crypto = require('crypto');
88const stream = require('stream');
9-const util = require('util');
109const zlib = require('zlib');
11101211const Stream = stream.Stream;
13121413// emit random bytes, and keep a shasum
15-function RandomReadStream(opt) {
16-Stream.call(this);
14+class RandomReadStream extends Stream {
15+constructor(opt) {
16+super();
171718-this.readable = true;
19-this._paused = false;
20-this._processing = false;
21-22-this._hasher = crypto.createHash('sha1');
23-opt = opt || {};
24-25-// base block size.
26-opt.block = opt.block || 256 * 1024;
18+this.readable = true;
19+this._paused = false;
20+this._processing = false;
272128-// total number of bytes to emit
29-opt.total = opt.total || 256 * 1024 * 1024;
30-this._remaining = opt.total;
22+this._hasher = crypto.createHash('sha1');
23+opt = opt || {};
312432-// how variable to make the block sizes
33-opt.jitter = opt.jitter || 1024;
25+ // base block size.
26+ opt.block = opt.block || 256 * 1024;
342735-this._opt = opt;
28+// total number of bytes to emit
29+opt.total = opt.total || 256 * 1024 * 1024;
30+this._remaining = opt.total;
363137-this._process = this._process.bind(this);
32+// how variable to make the block sizes
33+opt.jitter = opt.jitter || 1024;
383439-process.nextTick(this._process);
40-}
35+this._opt = opt;
413642-util.inherits(RandomReadStream, Stream);
37+ this._process = this._process.bind(this);
433844-RandomReadStream.prototype.pause = function() {
45-this._paused = true;
46-this.emit('pause');
47-};
39+process.nextTick(this._process);
40+}
484149-RandomReadStream.prototype.resume = function() {
50-// console.error("rrs resume");
51-this._paused = false;
52-this.emit('resume');
53-this._process();
54-};
42+pause() {
43+this._paused = true;
44+this.emit('pause');
45+}
554656-RandomReadStream.prototype._process = function() {
57-if (this._processing) return;
58-if (this._paused) return;
47+resume() {
48+// console.error("rrs resume");
49+this._paused = false;
50+this.emit('resume');
51+this._process();
52+}
595360-this._processing = true;
54+_process() {
55+if (this._processing) return;
56+if (this._paused) return;
615762-if (!this._remaining) {
63-this._hash = this._hasher.digest('hex').toLowerCase().trim();
64-this._processing = false;
58+this._processing = true;
655966-this.emit('end');
67-return;
68-}
60+if (!this._remaining) {
61+ this._hash = this._hasher.digest('hex').toLowerCase().trim();
62+ this._processing = false;
696370-// figure out how many bytes to output
71-// if finished, then just emit end.
72-let block = this._opt.block;
73-const jitter = this._opt.jitter;
74-if (jitter) {
75-block += Math.ceil(Math.random() * jitter - (jitter / 2));
76-}
77-block = Math.min(block, this._remaining);
78-const buf = Buffer.allocUnsafe(block);
79-for (let i = 0; i < block; i++) {
80-buf[i] = Math.random() * 256;
81-}
64+this.emit('end');
65+return;
66+}
826783-this._hasher.update(buf);
68+// figure out how many bytes to output
69+// if finished, then just emit end.
70+let block = this._opt.block;
71+const jitter = this._opt.jitter;
72+if (jitter) {
73+block += Math.ceil(Math.random() * jitter - (jitter / 2));
74+}
75+block = Math.min(block, this._remaining);
76+const buf = Buffer.allocUnsafe(block);
77+for (let i = 0; i < block; i++) {
78+buf[i] = Math.random() * 256;
79+}
848085-this._remaining -= block;
81+ this._hasher.update(buf);
868287-console.error('block=%d\nremain=%d\n', block, this._remaining);
88-this._processing = false;
83+this._remaining -= block;
898490-this.emit('data', buf);
91-process.nextTick(this._process);
92-};
85+this._processing = false;
938687+this.emit('data', buf);
88+process.nextTick(this._process);
89+}
90+}
94919592// a filter that just verifies a shasum
96-function HashStream() {
97-Stream.call(this);
93+class HashStream extends Stream {
94+constructor() {
95+super();
96+this.readable = this.writable = true;
97+this._hasher = crypto.createHash('sha1');
98+}
989999-this.readable = this.writable = true;
100-this._hasher = crypto.createHash('sha1');
101-}
100+write(c) {
101+// Simulate the way that an fs.ReadStream returns false
102+// on *every* write, only to resume a moment later.
103+this._hasher.update(c);
104+process.nextTick(() => this.resume());
105+return false;
106+}
107+108+resume() {
109+this.emit('resume');
110+process.nextTick(() => this.emit('drain'));
111+}
102112103-util.inherits(HashStream, Stream);
104-105-HashStream.prototype.write = function(c) {
106-// Simulate the way that an fs.ReadStream returns false
107-// on *every* write like a jerk, only to resume a
108-// moment later.
109-this._hasher.update(c);
110-process.nextTick(this.resume.bind(this));
111-return false;
112-};
113-114-HashStream.prototype.resume = function() {
115-this.emit('resume');
116-process.nextTick(this.emit.bind(this, 'drain'));
117-};
118-119-HashStream.prototype.end = function(c) {
120-if (c) {
121-this.write(c);
113+end(c) {
114+if (c) {
115+this.write(c);
116+}
117+this._hash = this._hasher.digest('hex').toLowerCase().trim();
118+this.emit('data', this._hash);
119+this.emit('end');
122120}
123-this._hash = this._hasher.digest('hex').toLowerCase().trim();
124-this.emit('data', this._hash);
125-this.emit('end');
126-};
121+}
127122128123129124const inp = new RandomReadStream({ total: 1024, block: 256, jitter: 16 });
@@ -133,23 +128,6 @@ const gunz = zlib.createGunzip();
133128134129inp.pipe(gzip).pipe(gunz).pipe(out);
135130136-inp.on('data', function(c) {
137-console.error('inp data', c.length);
138-});
139-140-gzip.on('data', function(c) {
141-console.error('gzip data', c.length);
142-});
143-144-gunz.on('data', function(c) {
145-console.error('gunz data', c.length);
146-});
147-148-out.on('data', function(c) {
149-console.error('out data', c.length);
150-});
151-152-out.on('data', common.mustCall(function(c) {
153-console.error('hash=%s', c);
131+out.on('data', common.mustCall((c) => {
154132assert.strictEqual(c, inp._hash, 'hashes should match');
155133}));