Skip to content

Commit 4b276e0

Browse files
vuanhphungIsaac
andcommitted
Test late CloudFetch body errors after listener cleanup
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com> Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 0a652d2 commit 4b276e0

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

‎tests/unit/result/CloudFetchResultHandler.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect, AssertionError } from 'chai';
22
import sinon, { SinonStub } from 'sinon';
3+
import { execFileSync } from 'child_process';
34
import http from 'http';
45
import { PassThrough } from 'stream';
56
import { AddressInfo } from 'net';
@@ -808,6 +809,41 @@ describe('CloudFetchResultHandler', () => {
808809
expect(body.listenerCount('end')).to.eq(0);
809810
expect(body.listeners('error')).to.deep.eq(errorsBefore);
810811
});
812+
813+
it('handles late body errors without application error handlers or unhandled rejections', function () {
814+
this.timeout(15000);
815+
const script = `
816+
const assert = require('assert').strict;
817+
const { PassThrough } = require('stream');
818+
const { Response } = require('node-fetch');
819+
const Handler = require('./lib/result/CloudFetchResultHandler').default;
820+
(async () => {
821+
const body = new PassThrough();
822+
const response = new Response(body);
823+
const originalErrors = body.listeners('error');
824+
assert.equal(originalErrors.length, 1);
825+
const reading = Handler.prototype.readBody.call({}, response);
826+
body.end(Buffer.from('result bytes'));
827+
assert.equal((await reading).toString(), 'result bytes');
828+
assert.equal(response.bodyUsed, true);
829+
assert.equal(body.listenerCount('data'), 0);
830+
assert.equal(body.listenerCount('end'), 0);
831+
await new Promise((resolve) => setImmediate(() => {
832+
body.emit('error', new Error('late body error'));
833+
resolve();
834+
}));
835+
await new Promise((resolve) => setImmediate(resolve));
836+
assert.deepEqual(body.listeners('error'), originalErrors);
837+
process.stdout.write('late body error handled\\n');
838+
})().catch((error) => { console.error(error); process.exitCode = 1; });
839+
`;
840+
const output = execFileSync(
841+
process.execPath,
842+
['--unhandled-rejections=strict', '--require', 'ts-node/register', '-e', script],
843+
{ cwd: process.cwd(), encoding: 'utf8', timeout: 10000, stdio: 'pipe' },
844+
);
845+
expect(output).to.eq('late body error handled\n');
846+
});
811847
});
812848

813849
describe('download deadline', () => {

0 commit comments

Comments
 (0)