Skip to content

Commit

Permalink
fix: os api availability issue (#764)
Browse files Browse the repository at this point in the history
* fix: os api availability issue

* test: add compatibility test

* build: upgrade dependencies
  • Loading branch information
scolladon committed Jan 24, 2024
1 parent 86a1800 commit e6d6e34
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 165 deletions.
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

0 comments on commit e6d6e34

Please sign in to comment.