Skip to content

Commit

Permalink
[Flaky Test #111821] Mock moment to avoid midnight TZ issues (#163157)
Browse files Browse the repository at this point in the history
  • Loading branch information
afharo authored Aug 11, 2023
1 parent 1f79ff4 commit d5c4f46
Showing 1 changed file with 17 additions and 15 deletions.
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');
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

0 comments on commit d5c4f46

Please sign in to comment.