-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor config, add some jest tests
- Loading branch information
Showing
13 changed files
with
155 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { range } from 'lodash'; | ||
import { startTaskTimer, startTaskTimerWithEventLoopMonitoring } from './task_events'; | ||
|
||
const DelayIterations = 4; | ||
const DelayMillis = 250; | ||
const DelayTotal = DelayIterations * DelayMillis; | ||
|
||
async function nonBlockingDelay(millis: number) { | ||
await new Promise((resolve) => setTimeout(resolve, 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) {} | ||
} | ||
|
||
async function nonBlockingTask() { | ||
for (let i = 0; i < DelayIterations; i++) { | ||
await nonBlockingDelay(DelayMillis); | ||
} | ||
} | ||
|
||
async function blockingTask() { | ||
for (let i = 0; i < DelayIterations; i++) { | ||
await blockingDelay(DelayMillis); | ||
} | ||
} | ||
|
||
describe('task_events', () => { | ||
test('startTaskTimer', async () => { | ||
const stopTaskTimer = startTaskTimer(); | ||
await nonBlockingTask(); | ||
const result = stopTaskTimer(); | ||
expect(result.stop - result.start).not.toBeLessThan(DelayTotal); | ||
expect(result.eventLoopBlockMs).toBe(undefined); | ||
}); | ||
|
||
describe('startTaskTimerWithEventLoopMonitoring', () => { | ||
test('non-blocking', async () => { | ||
const stopTaskTimer = startTaskTimerWithEventLoopMonitoring({ | ||
monitor: true, | ||
warn_on_delay: 5000, | ||
}); | ||
await nonBlockingTask(); | ||
const result = stopTaskTimer(); | ||
expect(result.stop - result.start).not.toBeLessThan(DelayTotal); | ||
expect(result.eventLoopBlockMs).toBeLessThan(DelayMillis); | ||
}); | ||
|
||
test('blocking', async () => { | ||
const stopTaskTimer = startTaskTimerWithEventLoopMonitoring({ | ||
monitor: true, | ||
warn_on_delay: 5000, | ||
}); | ||
await blockingTask(); | ||
const result = stopTaskTimer(); | ||
expect(result.stop - result.start).not.toBeLessThan(DelayTotal); | ||
expect(result.eventLoopBlockMs).not.toBeLessThan(DelayMillis); | ||
}); | ||
|
||
test('not monitoring', async () => { | ||
const stopTaskTimer = startTaskTimerWithEventLoopMonitoring({ | ||
monitor: false, | ||
warn_on_delay: 5000, | ||
}); | ||
await blockingTask(); | ||
const result = stopTaskTimer(); | ||
expect(result.stop - result.start).not.toBeLessThan(DelayTotal); | ||
expect(result.eventLoopBlockMs).toBe(0); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters