-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add workerIdleMemoryLimit to support worker recycling in the ev…
…ent of node >16.11.0 memory leaks (#13056)
- Loading branch information
Showing
25 changed files
with
1,358 additions
and
38 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
127 changes: 127 additions & 0 deletions
127
packages/jest-config/src/__tests__/stringToBytes.test.ts
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,127 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import stringToBytes from '../stringToBytes'; | ||
|
||
describe('numeric input', () => { | ||
test('> 1 represents bytes', () => { | ||
expect(stringToBytes(50.8)).toEqual(50); | ||
}); | ||
|
||
test('1.1 should be a 1', () => { | ||
expect(stringToBytes(1.1, 54)).toEqual(1); | ||
}); | ||
|
||
test('< 1 represents a %', () => { | ||
expect(stringToBytes(0.3, 51)).toEqual(15); | ||
}); | ||
|
||
test('should throw when no reference supplied', () => { | ||
expect(() => stringToBytes(0.3)).toThrowError(); | ||
}); | ||
|
||
test('should throw on a bad input', () => { | ||
expect(() => stringToBytes(-0.3, 51)).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('string input', () => { | ||
describe('numeric passthrough', () => { | ||
test('> 1 represents bytes', () => { | ||
expect(stringToBytes('50.8')).toEqual(50); | ||
}); | ||
|
||
test('< 1 represents a %', () => { | ||
expect(stringToBytes('0.3', 51)).toEqual(15); | ||
}); | ||
|
||
test('should throw when no reference supplied', () => { | ||
expect(() => stringToBytes('0.3')).toThrowError(); | ||
}); | ||
|
||
test('should throw on a bad input', () => { | ||
expect(() => stringToBytes('-0.3', 51)).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('parsing', () => { | ||
test('0% should throw an error', () => { | ||
expect(() => stringToBytes('0%', 51)).toThrowError(); | ||
}); | ||
|
||
test('30%', () => { | ||
expect(stringToBytes('30%', 51)).toEqual(15); | ||
}); | ||
|
||
test('80%', () => { | ||
expect(stringToBytes('80%', 51)).toEqual(40); | ||
}); | ||
|
||
test('100%', () => { | ||
expect(stringToBytes('100%', 51)).toEqual(51); | ||
}); | ||
|
||
// The units caps is intentionally janky to test for forgiving string parsing. | ||
describe('k', () => { | ||
test('30k', () => { | ||
expect(stringToBytes('30K')).toEqual(30000); | ||
}); | ||
|
||
test('30KB', () => { | ||
expect(stringToBytes('30kB')).toEqual(30000); | ||
}); | ||
|
||
test('30KiB', () => { | ||
expect(stringToBytes('30kIb')).toEqual(30720); | ||
}); | ||
}); | ||
|
||
describe('m', () => { | ||
test('30M', () => { | ||
expect(stringToBytes('30M')).toEqual(30000000); | ||
}); | ||
|
||
test('30MB', () => { | ||
expect(stringToBytes('30MB')).toEqual(30000000); | ||
}); | ||
|
||
test('30MiB', () => { | ||
expect(stringToBytes('30MiB')).toEqual(31457280); | ||
}); | ||
}); | ||
|
||
describe('g', () => { | ||
test('30G', () => { | ||
expect(stringToBytes('30G')).toEqual(30000000000); | ||
}); | ||
|
||
test('30GB', () => { | ||
expect(stringToBytes('30gB')).toEqual(30000000000); | ||
}); | ||
|
||
test('30GiB', () => { | ||
expect(stringToBytes('30GIB')).toEqual(32212254720); | ||
}); | ||
}); | ||
|
||
test('unknown unit', () => { | ||
expect(() => stringToBytes('50XX')).toThrowError(); | ||
}); | ||
}); | ||
}); | ||
|
||
test('nesting', () => { | ||
expect(stringToBytes(stringToBytes(stringToBytes('30%', 51)))).toEqual(15); | ||
}); | ||
|
||
test('null', () => { | ||
expect(stringToBytes(null)).toEqual(null); | ||
}); | ||
|
||
test('undefined', () => { | ||
expect(stringToBytes(undefined)).toEqual(undefined); | ||
}); |
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,90 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
function stringToBytes( | ||
input: undefined, | ||
percentageReference?: number, | ||
): undefined; | ||
function stringToBytes(input: null, percentageReference?: number): null; | ||
function stringToBytes( | ||
input: string | number, | ||
percentageReference?: number, | ||
): number; | ||
|
||
/** | ||
* Converts a string representing an amount of memory to bytes. | ||
* | ||
* @param input The value to convert to bytes. | ||
* @param percentageReference The reference value to use when a '%' value is supplied. | ||
*/ | ||
function stringToBytes( | ||
input: string | number | null | undefined, | ||
percentageReference?: number, | ||
): number | null | undefined { | ||
if (input === null || input === undefined) { | ||
return input; | ||
} | ||
|
||
if (typeof input === 'string') { | ||
if (isNaN(Number.parseFloat(input.slice(-1)))) { | ||
// eslint-disable-next-line prefer-const | ||
let [, numericString, trailingChars] = | ||
input.match(/(.*?)([^0-9.-]+)$/i) || []; | ||
|
||
if (trailingChars && numericString) { | ||
const numericValue = Number.parseFloat(numericString); | ||
trailingChars = trailingChars.toLowerCase(); | ||
|
||
switch (trailingChars) { | ||
case '%': | ||
input = numericValue / 100; | ||
break; | ||
case 'kb': | ||
case 'k': | ||
return numericValue * 1000; | ||
case 'kib': | ||
return numericValue * 1024; | ||
case 'mb': | ||
case 'm': | ||
return numericValue * 1000 * 1000; | ||
case 'mib': | ||
return numericValue * 1024 * 1024; | ||
case 'gb': | ||
case 'g': | ||
return numericValue * 1000 * 1000 * 1000; | ||
case 'gib': | ||
return numericValue * 1024 * 1024 * 1024; | ||
} | ||
} | ||
|
||
// It ends in some kind of char so we need to do some parsing | ||
} else { | ||
input = Number.parseFloat(input); | ||
} | ||
} | ||
|
||
if (typeof input === 'number') { | ||
if (input <= 1 && input > 0) { | ||
if (percentageReference) { | ||
return Math.floor(input * percentageReference); | ||
} else { | ||
throw new Error( | ||
'For a percentage based memory limit a percentageReference must be supplied', | ||
); | ||
} | ||
} else if (input > 1) { | ||
return Math.floor(input); | ||
} else { | ||
throw new Error('Unexpected numerical input'); | ||
} | ||
} | ||
|
||
throw new Error('Unexpected input'); | ||
} | ||
|
||
// https://github.com/import-js/eslint-plugin-import/issues/1590 | ||
export default stringToBytes; |
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
Oops, something went wrong.