deno.land / std@0.166.0 / node / _tools / test / parallel / test-stream-writable-destroy.js

test-stream-writable-destroy.js
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// deno-fmt-ignore-file// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.// Taken from Node 18.12.0// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually
'use strict';
const common = require('../common');const { Writable, addAbortSignal } = require('stream');const assert = require('assert');
{ const write = new Writable({ write(chunk, enc, cb) { cb(); } });
write.on('finish', common.mustNotCall()); write.on('close', common.mustCall());
write.destroy(); assert.strictEqual(write.destroyed, true);}
{ const write = new Writable({ write(chunk, enc, cb) { this.destroy(new Error('asd')); cb(); } });
write.on('error', common.mustCall()); write.on('finish', common.mustNotCall()); write.end('asd'); assert.strictEqual(write.destroyed, true);}
{ const write = new Writable({ write(chunk, enc, cb) { cb(); } });
const expected = new Error('kaboom');
write.on('finish', common.mustNotCall()); write.on('close', common.mustCall()); write.on('error', common.mustCall((err) => { assert.strictEqual(err, expected); }));
write.destroy(expected); assert.strictEqual(write.destroyed, true);}
{ const write = new Writable({ write(chunk, enc, cb) { cb(); } });
write._destroy = function(err, cb) { assert.strictEqual(err, expected); cb(err); };
const expected = new Error('kaboom');
write.on('finish', common.mustNotCall('no finish event')); write.on('close', common.mustCall()); write.on('error', common.mustCall((err) => { assert.strictEqual(err, expected); }));
write.destroy(expected); assert.strictEqual(write.destroyed, true);}
{ const write = new Writable({ write(chunk, enc, cb) { cb(); }, destroy: common.mustCall(function(err, cb) { assert.strictEqual(err, expected); cb(); }) });
const expected = new Error('kaboom');
write.on('finish', common.mustNotCall('no finish event')); write.on('close', common.mustCall());
// Error is swallowed by the custom _destroy write.on('error', common.mustNotCall('no error event'));
write.destroy(expected); assert.strictEqual(write.destroyed, true);}
{ const write = new Writable({ write(chunk, enc, cb) { cb(); } });
write._destroy = common.mustCall(function(err, cb) { assert.strictEqual(err, null); cb(); });
write.destroy(); assert.strictEqual(write.destroyed, true);}
{ const write = new Writable({ write(chunk, enc, cb) { cb(); } });
write._destroy = common.mustCall(function(err, cb) { assert.strictEqual(err, null); process.nextTick(() => { this.end(); cb(); }); });
const fail = common.mustNotCall('no finish event');
write.on('finish', fail); write.on('close', common.mustCall());
write.destroy();
assert.strictEqual(write.destroyed, true);}
{ const write = new Writable({ write(chunk, enc, cb) { cb(); } });
const expected = new Error('kaboom');
write._destroy = common.mustCall(function(err, cb) { assert.strictEqual(err, null); cb(expected); });
write.on('close', common.mustCall()); write.on('finish', common.mustNotCall('no finish event')); write.on('error', common.mustCall((err) => { assert.strictEqual(err, expected); }));
write.destroy(); assert.strictEqual(write.destroyed, true);}
{ // double error case const write = new Writable({ write(chunk, enc, cb) { cb(); } });
let ticked = false; write.on('close', common.mustCall(() => { assert.strictEqual(ticked, true); })); write.on('error', common.mustCall((err) => { assert.strictEqual(ticked, true); assert.strictEqual(err.message, 'kaboom 1'); assert.strictEqual(write._writableState.errorEmitted, true); }));
const expected = new Error('kaboom 1'); write.destroy(expected); write.destroy(new Error('kaboom 2')); assert.strictEqual(write._writableState.errored, expected); assert.strictEqual(write._writableState.errorEmitted, false); assert.strictEqual(write.destroyed, true); ticked = true;}
{ const writable = new Writable({ destroy: common.mustCall(function(err, cb) { process.nextTick(cb, new Error('kaboom 1')); }), write(chunk, enc, cb) { cb(); } });
let ticked = false; writable.on('close', common.mustCall(() => { writable.on('error', common.mustNotCall()); writable.destroy(new Error('hello')); assert.strictEqual(ticked, true); assert.strictEqual(writable._writableState.errorEmitted, true); })); writable.on('error', common.mustCall((err) => { assert.strictEqual(ticked, true); assert.strictEqual(err.message, 'kaboom 1'); assert.strictEqual(writable._writableState.errorEmitted, true); }));
writable.destroy(); assert.strictEqual(writable.destroyed, true); assert.strictEqual(writable._writableState.errored, null); assert.strictEqual(writable._writableState.errorEmitted, false);
// Test case where `writable.destroy()` is called again with an error before // the `_destroy()` callback is called. writable.destroy(new Error('kaboom 2')); assert.strictEqual(writable._writableState.errorEmitted, false); assert.strictEqual(writable._writableState.errored, null);
ticked = true;}
{ const write = new Writable({ write(chunk, enc, cb) { cb(); } });
write.destroyed = true; assert.strictEqual(write.destroyed, true);
// The internal destroy() mechanism should not be triggered write.on('close', common.mustNotCall()); write.destroy();}
{ function MyWritable() { assert.strictEqual(this.destroyed, false); this.destroyed = false; Writable.call(this); }
Object.setPrototypeOf(MyWritable.prototype, Writable.prototype); Object.setPrototypeOf(MyWritable, Writable);
new MyWritable();}
{ // Destroy and destroy callback const write = new Writable({ write(chunk, enc, cb) { cb(); } });
write.destroy();
const expected = new Error('kaboom');
write.destroy(expected, common.mustCall((err) => { assert.strictEqual(err, undefined); }));}
{ // Checks that `._undestroy()` restores the state so that `final` will be // called again. const write = new Writable({ write: common.mustNotCall(), final: common.mustCall((cb) => cb(), 2), autoDestroy: true });
write.end(); write.once('close', common.mustCall(() => { write._undestroy(); write.end(); }));}
{ const write = new Writable();
write.destroy(); write.on('error', common.mustNotCall()); write.write('asd', common.expectsError({ name: 'Error', code: 'ERR_STREAM_DESTROYED', message: 'Cannot call write after a stream was destroyed' }));}
{ const write = new Writable({ write(chunk, enc, cb) { cb(); } });
write.on('error', common.mustNotCall());
write.cork(); write.write('asd', common.mustCall()); write.uncork();
write.cork(); write.write('asd', common.expectsError({ name: 'Error', code: 'ERR_STREAM_DESTROYED', message: 'Cannot call write after a stream was destroyed' })); write.destroy(); write.write('asd', common.expectsError({ name: 'Error', code: 'ERR_STREAM_DESTROYED', message: 'Cannot call write after a stream was destroyed' })); write.uncork();}
{ // Call end(cb) after error & destroy
const write = new Writable({ write(chunk, enc, cb) { cb(new Error('asd')); } }); write.on('error', common.mustCall(() => { write.destroy(); let ticked = false; write.end(common.mustCall((err) => { assert.strictEqual(ticked, true); assert.strictEqual(err.code, 'ERR_STREAM_DESTROYED'); })); ticked = true; })); write.write('asd');}
{ // Call end(cb) after finish & destroy
const write = new Writable({ write(chunk, enc, cb) { cb(); } }); write.on('finish', common.mustCall(() => { write.destroy(); let ticked = false; write.end(common.mustCall((err) => { assert.strictEqual(ticked, true); assert.strictEqual(err.code, 'ERR_STREAM_ALREADY_FINISHED'); })); ticked = true; })); write.end();}
{ // Call end(cb) after error & destroy and don't trigger // unhandled exception.
const write = new Writable({ write(chunk, enc, cb) { process.nextTick(cb); } }); const _err = new Error('asd'); write.once('error', common.mustCall((err) => { assert.strictEqual(err.message, 'asd'); })); write.end('asd', common.mustCall((err) => { assert.strictEqual(err, _err); })); write.destroy(_err);}
{ // Call buffered write callback with error
const _err = new Error('asd'); const write = new Writable({ write(chunk, enc, cb) { process.nextTick(cb, _err); }, autoDestroy: false }); write.cork(); write.write('asd', common.mustCall((err) => { assert.strictEqual(err, _err); })); write.write('asd', common.mustCall((err) => { assert.strictEqual(err, _err); })); write.on('error', common.mustCall((err) => { assert.strictEqual(err, _err); })); write.uncork();}
{ // Ensure callback order.
let state = 0; const write = new Writable({ write(chunk, enc, cb) { // `setImmediate()` is used on purpose to ensure the callback is called // after `process.nextTick()` callbacks. setImmediate(cb); } }); write.write('asd', common.mustCall(() => { assert.strictEqual(state++, 0); })); write.write('asd', common.mustCall((err) => { assert.strictEqual(err.code, 'ERR_STREAM_DESTROYED'); assert.strictEqual(state++, 1); })); write.destroy();}
{ const write = new Writable({ autoDestroy: false, write(chunk, enc, cb) { cb(); cb(); } });
write.on('error', common.mustCall(() => { assert(write._writableState.errored); })); write.write('asd');}
{ const ac = new AbortController(); const write = addAbortSignal(ac.signal, new Writable({ write(chunk, enc, cb) { cb(); } }));
write.on('error', common.mustCall((e) => { assert.strictEqual(e.name, 'AbortError'); assert.strictEqual(write.destroyed, true); })); write.write('asd'); ac.abort();}
{ const ac = new AbortController(); const write = new Writable({ signal: ac.signal, write(chunk, enc, cb) { cb(); } });
write.on('error', common.mustCall((e) => { assert.strictEqual(e.name, 'AbortError'); assert.strictEqual(write.destroyed, true); })); write.write('asd'); ac.abort();}
{ const signal = AbortSignal.abort();
const write = new Writable({ signal, write(chunk, enc, cb) { cb(); } });
write.on('error', common.mustCall((e) => { assert.strictEqual(e.name, 'AbortError'); assert.strictEqual(write.destroyed, true); }));}
{ // Destroy twice const write = new Writable({ write(chunk, enc, cb) { cb(); } });
write.end(common.mustCall()); write.destroy(); write.destroy();}
{ // https://github.com/nodejs/node/issues/39356 const s = new Writable({ final() {} }); const _err = new Error('oh no'); // Remove `callback` and it works s.end(common.mustCall((err) => { assert.strictEqual(err, _err); })); s.on('error', common.mustCall((err) => { assert.strictEqual(err, _err); })); s.destroy(_err);}
std

Version Info

Tagged at
a year ago