[responseOps][task manager] fix timing issue in event loop delay test (#128531)

resolves https://github.com/elastic/kibana/issues/128441

Looks like perhaps there is some timing / rounding error in the tests,
where a non-blocking delay of 1000ms seems to have only taken 999ms,
causing the test failure. Smells very much like this node bug/feachur: 
https://github.com/nodejs/node/issues/26578

The fix is to not depend on setTimeout() to match expectations of
Date.now() math (per referenced issue ^^^). We force it by using
setTimeout() and then also validating the Date.now() expectation,
and if not, wait some more.
This commit is contained in:
Patrick Mueller 2022-05-03 00:07:35 -04:00 committed by GitHub
parent e3476a168b
commit 4d99fff4ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -11,17 +11,28 @@ const DelayIterations = 4;
const DelayMillis = 250;
const DelayTotal = DelayIterations * DelayMillis;
async function nonBlockingDelay(millis: number) {
async function basicNonBlockingDelay(millis: number) {
await new Promise((resolve) => setTimeout(resolve, millis));
}
async function nonBlockingDelay(millis: number) {
// can't just use basicNonBlockingDelay because:
// https://github.com/nodejs/node/issues/26578
const end = Date.now() + millis;
while (Date.now() <= end) {
await basicNonBlockingDelay(millis);
}
}
async function blockingDelay(millis: number) {
// get task in async queue
await nonBlockingDelay(0);
const end = Date.now() + millis;
// eslint-disable-next-line no-empty
while (Date.now() < end) {}
while (Date.now() <= end) {}
}
async function nonBlockingTask() {
@ -45,8 +56,7 @@ describe('task_events', () => {
expect(result.eventLoopBlockMs).toBe(undefined);
});
// FLAKY: https://github.com/elastic/kibana/issues/128441
describe.skip('startTaskTimerWithEventLoopMonitoring', () => {
describe('startTaskTimerWithEventLoopMonitoring', () => {
test('non-blocking', async () => {
const stopTaskTimer = startTaskTimerWithEventLoopMonitoring({
monitor: true,