Skip to content

Commit

Permalink
test: πŸ’ use fake timers in TimedItemBuffer tests (#61225)
Browse files Browse the repository at this point in the history
βœ… Closes: #58662

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
streamich and elasticmachine authored Mar 25, 2020
1 parent 8eaed26 commit 2255256
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/plugins/bfetch/common/buffer/tests/timed_item_buffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@
import { TimedItemBuffer } from '../timed_item_buffer';
import { runItemBufferTests } from './run_item_buffer_tests';

// FLAKY: https://github.com/elastic/kibana/issues/58662
describe.skip('TimedItemBuffer', () => {
jest.useFakeTimers();

beforeEach(() => {
jest.clearAllTimers();
});

describe('TimedItemBuffer', () => {
runItemBufferTests(TimedItemBuffer);

test('does not do unnecessary flushes', async () => {
test('does not do unnecessary flushes', () => {
const onFlush = jest.fn();
const buf = new TimedItemBuffer({
onFlush,
Expand All @@ -38,22 +43,22 @@ describe.skip('TimedItemBuffer', () => {
expect(onFlush).toHaveBeenCalledTimes(1);
});

test('does not do extra flush after timeout if buffer was flushed during timeout wait', async () => {
test('does not do extra flush after timeout if buffer was flushed during timeout wait', () => {
const onFlush = jest.fn();
const buf = new TimedItemBuffer({
onFlush,
maxItemAge: 10,
});

buf.write(0);
await new Promise(r => setTimeout(r, 3));
jest.advanceTimersByTime(3);
buf.flush();
await new Promise(r => setTimeout(r, 11));
jest.advanceTimersByTime(11);

expect(onFlush).toHaveBeenCalledTimes(1);
});

test('flushes buffer automatically after timeout reached', async () => {
test('flushes buffer automatically after timeout reached', () => {
const onFlush = jest.fn();
const buf = new TimedItemBuffer({
onFlush,
Expand All @@ -64,12 +69,12 @@ describe.skip('TimedItemBuffer', () => {
buf.write(2);
expect(onFlush).toHaveBeenCalledTimes(0);

await new Promise(r => setTimeout(r, 3));
jest.advanceTimersByTime(3);
expect(onFlush).toHaveBeenCalledTimes(1);
expect(onFlush).toHaveBeenCalledWith([1, 2]);
});

test('does not call flush after timeout if flush was triggered because buffer size reached', async () => {
test('does not call flush after timeout if flush was triggered because buffer size reached', () => {
const onFlush = jest.fn();
const buf = new TimedItemBuffer({
onFlush,
Expand All @@ -81,11 +86,11 @@ describe.skip('TimedItemBuffer', () => {
buf.write(2);

expect(onFlush).toHaveBeenCalledTimes(1);
await new Promise(r => setTimeout(r, 3));
jest.advanceTimersByTime(3);
expect(onFlush).toHaveBeenCalledTimes(1);
});

test('does not automatically flush if `.clear()` was called', async () => {
test('does not automatically flush if `.clear()` was called', () => {
const onFlush = jest.fn();
const buf = new TimedItemBuffer({
onFlush,
Expand All @@ -95,11 +100,11 @@ describe.skip('TimedItemBuffer', () => {

buf.write(1);
buf.write(2);
await new Promise(r => setImmediate(r));
jest.advanceTimersByTime(1);
buf.clear();

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

0 comments on commit 2255256

Please sign in to comment.