-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/mongodb-js/compass into COM…
…PASS-8441-global0=-writes-tests
- Loading branch information
Showing
6 changed files
with
77 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { expect } from 'chai'; | ||
import { Semaphore } from './semaphore'; | ||
|
||
describe('semaphore', function () { | ||
const maxConcurrentOps = 5; | ||
let semaphore: Semaphore; | ||
let taskHandler: (id: number) => Promise<number>; | ||
|
||
beforeEach(() => { | ||
semaphore = new Semaphore(maxConcurrentOps); | ||
taskHandler = async (id: number) => { | ||
const release = await semaphore.waitForRelease(); | ||
const delay = Math.floor(Math.random() * 450) + 50; | ||
try { | ||
await new Promise((resolve) => setTimeout(resolve, delay)); | ||
return id; | ||
} finally { | ||
release(); | ||
} | ||
}; | ||
}); | ||
|
||
it('should run operations concurrently', async function () { | ||
const tasks = Array.from({ length: 10 }, (_, i) => taskHandler(i)); | ||
const results = await Promise.all(tasks); | ||
expect(results).to.have.lengthOf(10); | ||
}); | ||
}); |
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,27 @@ | ||
export class Semaphore { | ||
private currentCount = 0; | ||
private queue: (() => void)[] = []; | ||
constructor(private maxConcurrentOps: number) {} | ||
|
||
waitForRelease(): Promise<() => void> { | ||
return new Promise((resolve) => { | ||
const attempt = () => { | ||
this.currentCount++; | ||
resolve(this.release.bind(this)); | ||
}; | ||
if (this.currentCount < this.maxConcurrentOps) { | ||
attempt(); | ||
} else { | ||
this.queue.push(attempt); | ||
} | ||
}); | ||
} | ||
|
||
private release() { | ||
this.currentCount--; | ||
if (this.queue.length > 0) { | ||
const next = this.queue.shift(); | ||
next && next(); | ||
} | ||
} | ||
} |
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