2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
2021-11-15 22:54:59 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
|
|
|
import { Readable } from 'stream';
|
|
|
|
import * as sinon from 'sinon';
|
|
|
|
import { noop } from 'lodash';
|
2022-05-31 20:02:48 +00:00
|
|
|
import { once } from 'events';
|
2021-11-15 22:54:59 +00:00
|
|
|
|
|
|
|
import { getStreamWithTimeout } from '../../util/getStreamWithTimeout';
|
|
|
|
|
|
|
|
describe('getStreamWithTimeout', () => {
|
|
|
|
let sandbox: sinon.SinonSandbox;
|
|
|
|
let clock: sinon.SinonFakeTimers;
|
|
|
|
|
2022-05-31 20:02:48 +00:00
|
|
|
// This helps tests preserve ordering.
|
|
|
|
const pushAndWait = (
|
|
|
|
stream: Readable,
|
|
|
|
chunk: string | null
|
|
|
|
): Promise<unknown> => {
|
2022-09-14 21:40:44 +00:00
|
|
|
const promise = once(stream, chunk == null ? 'end' : 'data');
|
2022-05-31 20:02:48 +00:00
|
|
|
stream.push(chunk);
|
|
|
|
return promise;
|
|
|
|
};
|
|
|
|
|
2021-11-15 22:54:59 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
sandbox = sinon.createSandbox();
|
|
|
|
clock = sandbox.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
sandbox.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('resolves on finished stream', async () => {
|
|
|
|
const stream = new Readable({
|
|
|
|
read: noop,
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.push('hello');
|
|
|
|
stream.push(' ');
|
|
|
|
stream.push('world');
|
|
|
|
stream.push(null);
|
|
|
|
|
|
|
|
const abort = sinon.stub();
|
|
|
|
const data = await getStreamWithTimeout(stream, {
|
|
|
|
name: 'test',
|
|
|
|
timeout: 1000,
|
|
|
|
abortController: { abort },
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.strictEqual(Buffer.from(data).toString(), 'hello world');
|
|
|
|
sinon.assert.notCalled(abort);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not timeout on slow but steady stream', async () => {
|
|
|
|
const stream = new Readable({
|
|
|
|
read: noop,
|
|
|
|
});
|
|
|
|
|
|
|
|
const abort = sinon.stub();
|
|
|
|
const data = getStreamWithTimeout(stream, {
|
|
|
|
name: 'test',
|
|
|
|
timeout: 1000,
|
|
|
|
abortController: { abort },
|
|
|
|
});
|
|
|
|
|
|
|
|
await clock.tickAsync(500);
|
2022-05-31 20:02:48 +00:00
|
|
|
await pushAndWait(stream, 'hello ');
|
2021-11-15 22:54:59 +00:00
|
|
|
await clock.tickAsync(500);
|
2022-05-31 20:02:48 +00:00
|
|
|
await pushAndWait(stream, 'world');
|
2021-11-15 22:54:59 +00:00
|
|
|
await clock.tickAsync(500);
|
2022-05-31 20:02:48 +00:00
|
|
|
await pushAndWait(stream, null);
|
2021-11-15 22:54:59 +00:00
|
|
|
await clock.nextAsync();
|
|
|
|
|
|
|
|
assert.strictEqual(Buffer.from(await data).toString(), 'hello world');
|
|
|
|
sinon.assert.notCalled(abort);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does timeout on slow but unsteady stream', async () => {
|
|
|
|
const stream = new Readable({
|
|
|
|
read: noop,
|
|
|
|
});
|
|
|
|
|
|
|
|
const abort = sinon.stub();
|
|
|
|
const data = getStreamWithTimeout(stream, {
|
|
|
|
name: 'test',
|
|
|
|
timeout: 1000,
|
|
|
|
abortController: { abort },
|
|
|
|
});
|
|
|
|
|
|
|
|
await clock.tickAsync(500);
|
2022-05-31 20:02:48 +00:00
|
|
|
await pushAndWait(stream, 'hello ');
|
2021-11-15 22:54:59 +00:00
|
|
|
await clock.tickAsync(500);
|
2022-05-31 20:02:48 +00:00
|
|
|
await pushAndWait(stream, 'world');
|
2021-11-15 22:54:59 +00:00
|
|
|
|
|
|
|
const promise = assert.isRejected(
|
|
|
|
data,
|
|
|
|
'getStreamWithTimeout(test) timed out'
|
|
|
|
);
|
|
|
|
|
|
|
|
await clock.tickAsync(1000);
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
sinon.assert.called(abort);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects on timeout', async () => {
|
|
|
|
const stream = new Readable({
|
|
|
|
read: noop,
|
|
|
|
});
|
|
|
|
|
|
|
|
const abort = sinon.stub();
|
|
|
|
const promise = assert.isRejected(
|
|
|
|
getStreamWithTimeout(stream, {
|
|
|
|
name: 'test',
|
|
|
|
timeout: 1000,
|
|
|
|
abortController: { abort },
|
|
|
|
}),
|
|
|
|
'getStreamWithTimeout(test) timed out'
|
|
|
|
);
|
|
|
|
|
|
|
|
await clock.tickAsync(1000);
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
|
|
|
|
sinon.assert.called(abort);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects on stream error', async () => {
|
|
|
|
const stream = new Readable({
|
|
|
|
read: noop,
|
|
|
|
});
|
|
|
|
|
|
|
|
const abort = sinon.stub();
|
|
|
|
const promise = assert.isRejected(
|
|
|
|
getStreamWithTimeout(stream, {
|
|
|
|
name: 'test',
|
|
|
|
timeout: 1000,
|
|
|
|
abortController: { abort },
|
|
|
|
}),
|
|
|
|
'welp'
|
|
|
|
);
|
|
|
|
|
|
|
|
stream.emit('error', new Error('welp'));
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
sinon.assert.notCalled(abort);
|
|
|
|
});
|
|
|
|
});
|