Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Flaky Test #111821] Mock moment to avoid midnight TZ issues #163157

Merged
merged 3 commits into from
Aug 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,37 @@
* Side Public License, v 1.
*/

import moment, { type MomentInput } from 'moment';
import type { Logger, ISavedObjectsRepository, SavedObject } from '@kbn/core/server';
import {
type TestElasticsearchUtils,
type TestKibanaUtils,
createTestServers,
createRootWithCorePlugins,
} from '@kbn/core-test-helpers-kbn-server';
import { rollDailyData } from '../daily';

import { metricsServiceMock } from '@kbn/core/server/mocks';

import {
SAVED_OBJECTS_DAILY_TYPE,
serializeSavedObjectId,
EventLoopDelaysDaily,
} from '../../saved_objects';
import moment from 'moment';
import { rollDailyData } from '../daily';

const eventLoopDelaysMonitor = metricsServiceMock.createEventLoopDelaysMonitor();

/*
* Mocking the constructor of moment, so we can control the time of the day.
* This is to avoid flaky tests when starting to run before midnight and ending the test after midnight
* because the logic might remove one extra document since we moved to the next day.
*/
jest.doMock('moment', () => {
const mockedMoment = (date?: MomentInput) => moment(date ?? '2023-07-04T10:00:00.000Z');
Copy link
Member Author

@afharo afharo Aug 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intentionally went 1 month in the past to prove the mock also applies the "prod" logic and not only the tests (it would remove all documents if it only affected the tests).

Object.setPrototypeOf(mockedMoment, moment); // inherit the prototype of `moment` so it has all the same methods.
return mockedMoment;
});

function createRawObject(date: moment.MomentInput): SavedObject<EventLoopDelaysDaily> {
const pid = Math.round(Math.random() * 10000);
const instanceUuid = 'mock_instance';
Expand All @@ -45,8 +57,8 @@ function createRawObject(date: moment.MomentInput): SavedObject<EventLoopDelaysD

function createRawEventLoopDelaysDailyDocs() {
const rawEventLoopDelaysDaily = [
createRawObject(moment.now()),
createRawObject(moment.now()),
createRawObject(moment()),
createRawObject(moment()),
createRawObject(moment().subtract(1, 'days')),
createRawObject(moment().subtract(3, 'days')),
];
Expand All @@ -59,8 +71,7 @@ function createRawEventLoopDelaysDailyDocs() {
return { rawEventLoopDelaysDaily, outdatedRawEventLoopDelaysDaily };
}

// FLAKY: https://github.com/elastic/kibana/issues/111821
describe.skip(`daily rollups integration test`, () => {
describe(`daily rollups integration test`, () => {
let esServer: TestElasticsearchUtils;
let root: TestKibanaUtils['root'];
let internalRepository: ISavedObjectsRepository;
Expand All @@ -82,15 +93,6 @@ describe.skip(`daily rollups integration test`, () => {
logger = root.logger.get('test daily rollups');
internalRepository = start.savedObjects.createInternalRepository([SAVED_OBJECTS_DAILY_TYPE]);

// If we are less than 1 second away from midnight, let's wait 1 second before creating the docs.
// Otherwise, we may receive 1 document less than the expected ones.
if (moment().endOf('day').diff(moment(), 's', true) < 1) {
logger.info(
'Delaying the creation of the docs 1s, just in case we create them before midnight and run the tests on the following day.'
);
await new Promise((resolve) => setTimeout(resolve, 1000));
}

// Create the docs now
const rawDailyDocs = createRawEventLoopDelaysDailyDocs();
rawEventLoopDelaysDaily = rawDailyDocs.rawEventLoopDelaysDaily;
Expand Down