Try making stream timeout tests more reliable
This commit is contained in:
parent
d446aa9e6b
commit
994e4606ba
1 changed files with 17 additions and 6 deletions
|
@ -1,10 +1,11 @@
|
||||||
// Copyright 2021 Signal Messenger, LLC
|
// Copyright 2021-2022 Signal Messenger, LLC
|
||||||
// SPDX-License-Identifier: AGPL-3.0-only
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
import { assert } from 'chai';
|
import { assert } from 'chai';
|
||||||
import { Readable } from 'stream';
|
import { Readable } from 'stream';
|
||||||
import * as sinon from 'sinon';
|
import * as sinon from 'sinon';
|
||||||
import { noop } from 'lodash';
|
import { noop } from 'lodash';
|
||||||
|
import { once } from 'events';
|
||||||
|
|
||||||
import { getStreamWithTimeout } from '../../util/getStreamWithTimeout';
|
import { getStreamWithTimeout } from '../../util/getStreamWithTimeout';
|
||||||
|
|
||||||
|
@ -12,6 +13,16 @@ describe('getStreamWithTimeout', () => {
|
||||||
let sandbox: sinon.SinonSandbox;
|
let sandbox: sinon.SinonSandbox;
|
||||||
let clock: sinon.SinonFakeTimers;
|
let clock: sinon.SinonFakeTimers;
|
||||||
|
|
||||||
|
// This helps tests preserve ordering.
|
||||||
|
const pushAndWait = (
|
||||||
|
stream: Readable,
|
||||||
|
chunk: string | null
|
||||||
|
): Promise<unknown> => {
|
||||||
|
const promise = once(stream, chunk === null ? 'end' : 'data');
|
||||||
|
stream.push(chunk);
|
||||||
|
return promise;
|
||||||
|
};
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
sandbox = sinon.createSandbox();
|
sandbox = sinon.createSandbox();
|
||||||
clock = sandbox.useFakeTimers();
|
clock = sandbox.useFakeTimers();
|
||||||
|
@ -55,11 +66,11 @@ describe('getStreamWithTimeout', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
await clock.tickAsync(500);
|
await clock.tickAsync(500);
|
||||||
stream.push('hello ');
|
await pushAndWait(stream, 'hello ');
|
||||||
await clock.tickAsync(500);
|
await clock.tickAsync(500);
|
||||||
stream.push('world');
|
await pushAndWait(stream, 'world');
|
||||||
await clock.tickAsync(500);
|
await clock.tickAsync(500);
|
||||||
stream.push(null);
|
await pushAndWait(stream, null);
|
||||||
await clock.nextAsync();
|
await clock.nextAsync();
|
||||||
|
|
||||||
assert.strictEqual(Buffer.from(await data).toString(), 'hello world');
|
assert.strictEqual(Buffer.from(await data).toString(), 'hello world');
|
||||||
|
@ -79,9 +90,9 @@ describe('getStreamWithTimeout', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
await clock.tickAsync(500);
|
await clock.tickAsync(500);
|
||||||
stream.push('hello ');
|
await pushAndWait(stream, 'hello ');
|
||||||
await clock.tickAsync(500);
|
await clock.tickAsync(500);
|
||||||
stream.push('world');
|
await pushAndWait(stream, 'world');
|
||||||
|
|
||||||
const promise = assert.isRejected(
|
const promise = assert.isRejected(
|
||||||
data,
|
data,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue