Try making stream timeout tests more reliable

This commit is contained in:
Evan Hahn 2022-05-31 20:02:48 +00:00 committed by GitHub
parent d446aa9e6b
commit 994e4606ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 6 deletions

View File

@ -1,10 +1,11 @@
// Copyright 2021 Signal Messenger, LLC
// Copyright 2021-2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import { Readable } from 'stream';
import * as sinon from 'sinon';
import { noop } from 'lodash';
import { once } from 'events';
import { getStreamWithTimeout } from '../../util/getStreamWithTimeout';
@ -12,6 +13,16 @@ describe('getStreamWithTimeout', () => {
let sandbox: sinon.SinonSandbox;
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(() => {
sandbox = sinon.createSandbox();
clock = sandbox.useFakeTimers();
@ -55,11 +66,11 @@ describe('getStreamWithTimeout', () => {
});
await clock.tickAsync(500);
stream.push('hello ');
await pushAndWait(stream, 'hello ');
await clock.tickAsync(500);
stream.push('world');
await pushAndWait(stream, 'world');
await clock.tickAsync(500);
stream.push(null);
await pushAndWait(stream, null);
await clock.nextAsync();
assert.strictEqual(Buffer.from(await data).toString(), 'hello world');
@ -79,9 +90,9 @@ describe('getStreamWithTimeout', () => {
});
await clock.tickAsync(500);
stream.push('hello ');
await pushAndWait(stream, 'hello ');
await clock.tickAsync(500);
stream.push('world');
await pushAndWait(stream, 'world');
const promise = assert.isRejected(
data,