Skip to content

Commit df9bbbe

Browse files
committed
Semaphore default parameters
PR-URL: #45
1 parent 374a9ac commit df9bbbe

4 files changed

Lines changed: 27 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## [Unreleased][unreleased]
44

5+
- Semaphore default parameters
6+
57
## [3.5.3][] - 2021-05-05
68

79
- Implement 'toLowerCamel' and 'toUpperCamel'

lib/semaphore.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
class Semaphore {
4-
constructor(concurrency, size, timeout) {
4+
constructor(concurrency, size = 0, timeout = 0) {
55
this.counter = concurrency;
66
this.timeout = timeout;
77
this.size = size;

test/semaphore.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,29 @@ metatests.test('Semaphore', async (test) => {
3030
test.end();
3131
});
3232

33+
metatests.test('Semaphore default', async (test) => {
34+
const semaphore = new Semaphore(CONCURRENCY);
35+
await semaphore.enter();
36+
test.strictSame(semaphore.counter, CONCURRENCY - 1);
37+
await semaphore.enter();
38+
test.strictSame(semaphore.counter, CONCURRENCY - 2);
39+
await semaphore.enter();
40+
test.strictSame(semaphore.counter, 0);
41+
try {
42+
await semaphore.enter();
43+
} catch (err) {
44+
test.assert(err);
45+
}
46+
test.strictSame(semaphore.counter, 0);
47+
semaphore.leave();
48+
test.strictSame(semaphore.counter, CONCURRENCY - 2);
49+
semaphore.leave();
50+
test.strictSame(semaphore.counter, CONCURRENCY - 1);
51+
semaphore.leave();
52+
test.strictSame(semaphore.counter, CONCURRENCY);
53+
test.end();
54+
});
55+
3356
metatests.test('Semaphore timeout', async (test) => {
3457
const semaphore = new Semaphore(CONCURRENCY, QUEUE_SIZE, TIMEOUT);
3558
await semaphore.enter();

types/metautil.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export interface QueueElement {
5555
}
5656

5757
export class Semaphore {
58-
constructor(concurrency: number, size: number, timeout: number);
58+
constructor(concurrency: number, size?: number, timeout?: number);
5959
counter: number;
6060
timeout: number;
6161
size: number;

0 commit comments

Comments
 (0)