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

fix: os api availability issue #764

Merged
merged 3 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
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
@@ -0,0 +1,55 @@
'use strict'

import { expect, jest, describe, it } from '@jest/globals'
import { getGlobalMetadata, getWork } from '../../../__utils__/globalTestHelper'
import DiffLineInterpreter from '../../../../src/service/diffLineInterpreter'
import { Work } from '../../../../src/types/work'
import { MetadataRepository } from '../../../../src/metadata/MetadataRepository'

jest.mock('os', () => ({
availableParallelism: null,
}))

const mockHandle = jest.fn()
jest.mock('../../../../src/service/typeHandlerFactory', () => {
return jest.fn().mockImplementation(() => {
return {
getTypeHandler: jest
.fn()
.mockImplementation(() => ({ handle: mockHandle })),
}
})
})

let work: Work
beforeEach(() => {
jest.clearAllMocks()
work = getWork()
})

describe('DiffLineInterpreter', () => {
let sut: DiffLineInterpreter
let globalMetadata: MetadataRepository
beforeAll(async () => {
// eslint-disable-next-line no-undef
globalMetadata = await getGlobalMetadata()
})

describe('compatibility test', () => {
beforeEach(() => {
sut = new DiffLineInterpreter(work, globalMetadata)
})
describe('when `availableParallelism` is not defined', () => {
it('fallback gracefully', async () => {
// Arrange
const lines = ['test']

// Act
await sut.process(lines)

// Assert
expect(mockHandle).toBeCalledTimes(lines.length)
})
})
})
})
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
"dependencies:upgrade": "shx rm -rf yarn.lock ; shx touch yarn.lock ; yarn-upgrade-all ; yarn-audit-fix"
},
"devDependencies": {
"@commitlint/cli": "^18.4.4",
"@commitlint/config-conventional": "^18.4.4",
"@commitlint/cli": "^18.5.0",
"@commitlint/config-conventional": "^18.5.0",
"@jest/globals": "^29.7.0",
"@oclif/dev-cli": "^1.26.10",
"@salesforce/cli-plugins-testkit": "^5.1.7",
Expand All @@ -80,9 +80,9 @@
"@types/async": "^3.2.24",
"@types/jest": "^29.5.11",
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.5",
"@typescript-eslint/eslint-plugin": "^6.19.0",
"@typescript-eslint/parser": "^6.19.0",
"@types/node": "^20.11.6",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
"benchmark": "^2.1.4",
"chai": "^4.3.10",
"depcheck": "^1.4.7",
Expand All @@ -98,7 +98,7 @@
"prettier": "^3.2.4",
"shx": "^0.3.4",
"sinon": "^17.0.1",
"ts-jest": "^29.1.1",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"typescript": "^5.3.3",
"yarn-audit-fix": "^10.0.7",
Expand Down
12 changes: 10 additions & 2 deletions src/service/diffLineInterpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { availableParallelism } from 'os'
import { queue } from 'async'
import StandardHandler from './standardHandler'

const MAX_PARALLELISM = Math.min(availableParallelism(), 6)

export default class DiffLineInterpreter {
constructor(
// eslint-disable-next-line no-unused-vars
Expand All @@ -18,6 +16,7 @@ export default class DiffLineInterpreter {

public async process(lines: string[]) {
const typeHandlerFactory = new TypeHandlerFactory(this.work, this.metadata)
const MAX_PARALLELISM = this.getConcurrencyThreshold()
const processor = queue(
async (handler: StandardHandler) => await handler.handle(),
MAX_PARALLELISM
Expand All @@ -32,4 +31,13 @@ export default class DiffLineInterpreter {
await processor.drain()
}
}

protected getConcurrencyThreshold() {
// This is because of this issue: https://github.com/scolladon/sfdx-git-delta/issues/762#issuecomment-1907609957
const AVAILABLE_PARALLELISM = availableParallelism
? availableParallelism()
: Infinity

return Math.min(AVAILABLE_PARALLELISM, 6)
}
}
Loading
Loading