Skip to content

Commit

Permalink
use fake timers to avoid flakiness (#101254)
Browse files Browse the repository at this point in the history
  • Loading branch information
lizozom authored Jun 3, 2021
1 parent 1d39d36 commit c30dc1e
Showing 1 changed file with 40 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,14 @@ const setup = () => {
};
};

// FLAKY: https://github.com/elastic/kibana/issues/101126
describe.skip('createStreamingBatchedFunction()', () => {
describe('createStreamingBatchedFunction()', () => {
beforeAll(() => {
jest.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
});
test('returns a function', () => {
const { fetchStreaming } = setup();
const fn = createStreamingBatchedFunction({
Expand Down Expand Up @@ -87,8 +93,8 @@ describe.skip('createStreamingBatchedFunction()', () => {
expect(fetchStreaming).toHaveBeenCalledTimes(0);
fn({ baz: 'quix' });
expect(fetchStreaming).toHaveBeenCalledTimes(0);
jest.advanceTimersByTime(6);

await new Promise((r) => setTimeout(r, 6));
expect(fetchStreaming).toHaveBeenCalledTimes(1);
});

Expand All @@ -103,7 +109,7 @@ describe.skip('createStreamingBatchedFunction()', () => {
});

expect(fetchStreaming).toHaveBeenCalledTimes(0);
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);
expect(fetchStreaming).toHaveBeenCalledTimes(0);
});

Expand All @@ -118,7 +124,7 @@ describe.skip('createStreamingBatchedFunction()', () => {
});

fn({ foo: 'bar' });
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

expect(fetchStreaming.mock.calls[0][0]).toMatchObject({
url: '/test',
Expand All @@ -139,7 +145,7 @@ describe.skip('createStreamingBatchedFunction()', () => {
fn({ foo: 'bar' });
fn({ baz: 'quix' });

await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);
const { body } = fetchStreaming.mock.calls[0][0];
expect(JSON.parse(body)).toEqual({
batch: [{ foo: 'bar' }, { baz: 'quix' }],
Expand All @@ -160,13 +166,10 @@ describe.skip('createStreamingBatchedFunction()', () => {

expect(fetchStreaming).toHaveBeenCalledTimes(0);
fn({ foo: 'bar' });
await flushPromises();
expect(fetchStreaming).toHaveBeenCalledTimes(0);
fn({ baz: 'quix' });
await flushPromises();
expect(fetchStreaming).toHaveBeenCalledTimes(0);
fn({ full: 'yep' });
await flushPromises();
expect(fetchStreaming).toHaveBeenCalledTimes(1);
});

Expand All @@ -186,7 +189,7 @@ describe.skip('createStreamingBatchedFunction()', () => {
of(fn({ foo: 'bar' }, abortController.signal));
fn({ baz: 'quix' });

await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);
const { body } = fetchStreaming.mock.calls[0][0];
expect(JSON.parse(body)).toEqual({
batch: [{ baz: 'quix' }],
Expand All @@ -206,7 +209,6 @@ describe.skip('createStreamingBatchedFunction()', () => {
fn({ a: '1' });
fn({ b: '2' });
fn({ c: '3' });
await flushPromises();

expect(fetchStreaming.mock.calls[0][0]).toMatchObject({
url: '/test',
Expand All @@ -231,11 +233,9 @@ describe.skip('createStreamingBatchedFunction()', () => {
fn({ a: '1' });
fn({ b: '2' });
fn({ c: '3' });
await flushPromises();
expect(fetchStreaming).toHaveBeenCalledTimes(1);
fn({ d: '4' });
await flushPromises();
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);
expect(fetchStreaming).toHaveBeenCalledTimes(2);
});
});
Expand All @@ -253,7 +253,7 @@ describe.skip('createStreamingBatchedFunction()', () => {

const promise1 = fn({ a: '1' });
const promise2 = fn({ b: '2' });
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

expect(await isPending(promise1)).toBe(true);
expect(await isPending(promise2)).toBe(true);
Expand All @@ -274,7 +274,7 @@ describe.skip('createStreamingBatchedFunction()', () => {
const promise1 = fn({ a: '1' });
const promise2 = fn({ b: '2' });
const promise3 = fn({ c: '3' });
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

expect(await isPending(promise1)).toBe(true);
expect(await isPending(promise2)).toBe(true);
Expand Down Expand Up @@ -316,7 +316,7 @@ describe.skip('createStreamingBatchedFunction()', () => {
const promise1 = fn({ a: '1' });
const promise2 = fn({ b: '2' });
const promise3 = fn({ c: '3' });
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

stream.next(
JSON.stringify({
Expand Down Expand Up @@ -365,7 +365,7 @@ describe.skip('createStreamingBatchedFunction()', () => {
const promise1 = fn({ a: '1' });
const promise2 = fn({ b: '2' });
const promise3 = fn({ c: '3' });
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

stream.next(
JSON.stringify({
Expand Down Expand Up @@ -405,7 +405,7 @@ describe.skip('createStreamingBatchedFunction()', () => {
});

const promise = fn({ a: '1' });
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

expect(await isPending(promise)).toBe(true);

Expand Down Expand Up @@ -437,7 +437,7 @@ describe.skip('createStreamingBatchedFunction()', () => {
const promise2 = of(fn({ a: '2' }));
const promise3 = of(fn({ a: '3' }));

await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

stream.next(
JSON.stringify({
Expand All @@ -446,7 +446,7 @@ describe.skip('createStreamingBatchedFunction()', () => {
}) + '\n'
);

await new Promise((r) => setTimeout(r, 1));
jest.advanceTimersByTime(1);

stream.next(
JSON.stringify({
Expand All @@ -455,7 +455,7 @@ describe.skip('createStreamingBatchedFunction()', () => {
}) + '\n'
);

await new Promise((r) => setTimeout(r, 1));
jest.advanceTimersByTime(1);

stream.next(
JSON.stringify({
Expand All @@ -464,7 +464,7 @@ describe.skip('createStreamingBatchedFunction()', () => {
}) + '\n'
);

await new Promise((r) => setTimeout(r, 1));
jest.advanceTimersByTime(1);

const [result1] = await promise1;
const [, error2] = await promise2;
Expand All @@ -489,13 +489,14 @@ describe.skip('createStreamingBatchedFunction()', () => {
const abortController = new AbortController();
const promise = fn({ a: '1' }, abortController.signal);
const promise2 = fn({ a: '2' }, abortController.signal);
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

expect(await isPending(promise)).toBe(true);
expect(await isPending(promise2)).toBe(true);

abortController.abort();
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);
await flushPromises();

expect(await isPending(promise)).toBe(false);
expect(await isPending(promise2)).toBe(false);
Expand All @@ -519,12 +520,13 @@ describe.skip('createStreamingBatchedFunction()', () => {
const abortController = new AbortController();
const promise = fn({ a: '1' }, abortController.signal);
const promise2 = fn({ a: '2' });
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

expect(await isPending(promise)).toBe(true);

abortController.abort();
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);
await flushPromises();

expect(await isPending(promise)).toBe(false);
const [, error] = await of(promise);
Expand All @@ -537,7 +539,7 @@ describe.skip('createStreamingBatchedFunction()', () => {
}) + '\n'
);

await new Promise((r) => setTimeout(r, 1));
jest.advanceTimersByTime(1);

const [result2] = await of(promise2);
expect(result2).toEqual({ b: '2' });
Expand All @@ -558,11 +560,11 @@ describe.skip('createStreamingBatchedFunction()', () => {
const promise1 = of(fn({ a: '1' }));
const promise2 = of(fn({ a: '2' }));

await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

stream.complete();

await new Promise((r) => setTimeout(r, 1));
jest.advanceTimersByTime(1);

const [, error1] = await promise1;
const [, error2] = await promise2;
Expand All @@ -589,7 +591,7 @@ describe.skip('createStreamingBatchedFunction()', () => {
const promise1 = of(fn({ a: '1' }));
const promise2 = of(fn({ a: '2' }));

await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

stream.next(
JSON.stringify({
Expand All @@ -599,7 +601,7 @@ describe.skip('createStreamingBatchedFunction()', () => {
);
stream.complete();

await new Promise((r) => setTimeout(r, 1));
jest.advanceTimersByTime(1);

const [, error1] = await promise1;
const [result1] = await promise2;
Expand Down Expand Up @@ -627,13 +629,13 @@ describe.skip('createStreamingBatchedFunction()', () => {
const promise1 = of(fn({ a: '1' }));
const promise2 = of(fn({ a: '2' }));

await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

stream.error({
message: 'something went wrong',
});

await new Promise((r) => setTimeout(r, 1));
jest.advanceTimersByTime(1);

const [, error1] = await promise1;
const [, error2] = await promise2;
Expand All @@ -660,7 +662,7 @@ describe.skip('createStreamingBatchedFunction()', () => {
const promise1 = of(fn({ a: '1' }));
const promise2 = of(fn({ a: '2' }));

await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

stream.next(
JSON.stringify({
Expand All @@ -670,7 +672,7 @@ describe.skip('createStreamingBatchedFunction()', () => {
);
stream.error('oops');

await new Promise((r) => setTimeout(r, 1));
jest.advanceTimersByTime(1);

const [, error1] = await promise1;
const [result1] = await promise2;
Expand Down Expand Up @@ -698,7 +700,7 @@ describe.skip('createStreamingBatchedFunction()', () => {
const promise1 = of(fn({ a: '1' }));
const promise2 = of(fn({ a: '2' }));

await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

stream.next(
JSON.stringify({
Expand All @@ -709,7 +711,7 @@ describe.skip('createStreamingBatchedFunction()', () => {

stream.next('Not a JSON\n');

await new Promise((r) => setTimeout(r, 1));
jest.advanceTimersByTime(1);

const [, error1] = await promise1;
const [result1] = await promise2;
Expand Down

0 comments on commit c30dc1e

Please sign in to comment.