Skip to content

Commit

Permalink
Fix flakiness in request lib tests (elastic#42647)
Browse files Browse the repository at this point in the history
* Remove flakiness from 'pollIntervalMs' test by making timing irrelevant.
* Remove flakiness from 'state' tests by increasing wait time.
* Fix flakiness with 'resets the pollIntervalMs' test by increasing the wait time.
  • Loading branch information
cjcenizal committed Aug 6, 2019
1 parent 8998980 commit 2161b00
Showing 1 changed file with 37 additions and 34 deletions.
71 changes: 37 additions & 34 deletions src/plugins/es_ui_shared/public/request/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,19 @@ describe('request lib', () => {
describe('path, method, body', () => {
it('is used to send the request', async () => {
initUseRequest({ ...successRequest });
await wait(10);
await wait(50);
expect(hook.data).toBe(successResponse.data);
});
});

// FLAKY: https://github.com/elastic/kibana/issues/42561
describe.skip('pollIntervalMs', () => {
describe('pollIntervalMs', () => {
it('sends another request after the specified time has elapsed', async () => {
initUseRequest({ ...successRequest, pollIntervalMs: 30 });
await wait(5);
sinon.assert.calledOnce(sendPost);

await wait(40);
sinon.assert.calledTwice(sendPost);
initUseRequest({ ...successRequest, pollIntervalMs: 10 });
await wait(50);
// We just care that multiple requests have been sent out. We don't check the specific
// timing because that risks introducing flakiness into the tests, and it's unlikely
// we could break the implementation by getting the exact timing wrong.
expect(sendPost.callCount).toBeGreaterThan(1);

// We have to manually clean up or else the interval will continue to fire requests,
// interfering with other tests.
Expand All @@ -132,14 +131,14 @@ describe('request lib', () => {
initUseRequest({ ...successRequest, deserializer });
sinon.assert.notCalled(deserializer);

await wait(5);
await wait(50);
sinon.assert.calledOnce(deserializer);
sinon.assert.calledWith(deserializer, successResponse.data);
});

it('processes data', async () => {
initUseRequest({ ...successRequest, deserializer: () => 'intercepted' });
await wait(5);
await wait(50);
expect(hook.data).toBe('intercepted');
});
});
Expand All @@ -152,7 +151,7 @@ describe('request lib', () => {
expect(hook.isInitialRequest).toBe(true);

hook.sendRequest();
await wait(5);
await wait(50);
expect(hook.isInitialRequest).toBe(false);
});
});
Expand All @@ -162,30 +161,29 @@ describe('request lib', () => {
initUseRequest({ ...successRequest });
expect(hook.isLoading).toBe(true);

await wait(5);
await wait(50);
expect(hook.isLoading).toBe(false);
});
});

describe('error', () => {
it('surfaces errors from requests', async () => {
initUseRequest({ ...errorRequest });
await wait(10);
await wait(50);
expect(hook.error).toBe(errorResponse);
});

// FLAKY: https://github.com/elastic/kibana/issues/42563
it.skip('persists while a request is in-flight', async () => {
it('persists while a request is in-flight', async () => {
initUseRequest({ ...errorRequest });
await wait(5);
await wait(50);
hook.sendRequest();
expect(hook.isLoading).toBe(true);
expect(hook.error).toBe(errorResponse);
});

it('is undefined when the request is successful', async () => {
initUseRequest({ ...successRequest });
await wait(10);
await wait(50);
expect(hook.isLoading).toBe(false);
expect(hook.error).toBeUndefined();
});
Expand All @@ -194,30 +192,28 @@ describe('request lib', () => {
describe('data', () => {
it('surfaces payloads from requests', async () => {
initUseRequest({ ...successRequest });
await wait(10);
await wait(50);
expect(hook.data).toBe(successResponse.data);
});

it('persists while a request is in-flight', async () => {
initUseRequest({ ...successRequest });
await wait(5);
await wait(50);
hook.sendRequest();
expect(hook.isLoading).toBe(true);
expect(hook.data).toBe(successResponse.data);
});

// FLAKY: https://github.com/elastic/kibana/issues/42562
it.skip('is undefined when the request fails', async () => {
it('is undefined when the request fails', async () => {
initUseRequest({ ...errorRequest });
await wait(10);
await wait(50);
expect(hook.isLoading).toBe(false);
expect(hook.data).toBeUndefined();
});
});
});

// FLAKY: https://github.com/elastic/kibana/issues/42225
describe.skip('callbacks', () => {
describe('callbacks', () => {
describe('sendRequest', () => {
it('sends the request', () => {
initUseRequest({ ...successRequest });
Expand All @@ -227,19 +223,26 @@ describe('request lib', () => {
});

it('resets the pollIntervalMs', async () => {
initUseRequest({ ...successRequest, pollIntervalMs: 30 });
await wait(5);
sinon.assert.calledOnce(sendPost);
initUseRequest({ ...successRequest, pollIntervalMs: 800 });
await wait(200); // 200ms
hook.sendRequest();
expect(sendPost.callCount).toBe(2);

await wait(20);
await wait(200); // 400ms
hook.sendRequest();

// If the request didn't reset the interval, there would have been three requests sent by now.
await wait(20);
sinon.assert.calledTwice(sendPost);
await wait(200); // 600ms
hook.sendRequest();

await wait(200); // 800ms
hook.sendRequest();

await wait(200); // 1000ms
hook.sendRequest();

await wait(20);
sinon.assert.calledThrice(sendPost);
// If sendRequest didn't reset the interval, the interval would have triggered another
// request by now, and the callCount would be 7.
expect(sendPost.callCount).toBe(6);

// We have to manually clean up or else the interval will continue to fire requests,
// interfering with other tests.
Expand Down

0 comments on commit 2161b00

Please sign in to comment.