Skip to content

Commit 9b49643

Browse files
committed
refactor(semaphore): move some exceptions to global exceptions
1 parent bbf418e commit 9b49643

8 files changed

+27
-29
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { ConcurrencyException } from "./concurrency.exception";
2+
3+
/**
4+
* When a sync class "lock" exceeds its timeout limit
5+
*/
6+
export class ConcurrencyExceedTimeoutException extends ConcurrencyException {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { ConcurrencyException } from "./concurrency.exception";
2+
3+
/**
4+
* When an invalid value is given for a timeout
5+
*/
6+
export class ConcurrencyInvalidTimeoutException extends ConcurrencyException {}

src/exceptions/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
export * from "./concurrency.exceed-timeout.exception";
12
export * from "./concurrency.exception";
23
export * from "./concurrency.interrupted.exception";
4+
export * from "./concurrency.invalid-timeout.exception";

src/semaphore/exceptions/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
export * from "./semaphore.acquire-timeout.exception";
21
export * from "./semaphore.exception";
32
export * from "./semaphore.invalid-permits.exception";
4-
export * from "./semaphore.invalid-timeout.exception";

src/semaphore/exceptions/semaphore.acquire-timeout.exception.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/semaphore/exceptions/semaphore.invalid-timeout.exception.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/semaphore/semaphore.spec.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import {
2-
SemaphoreAcquireTimeoutException,
3-
SemaphoreInvalidPermitsException,
4-
SemaphoreInvalidTimeoutException
5-
} from "./exceptions";
1+
import { SemaphoreInvalidPermitsException } from "./exceptions";
62
import { Semaphore } from "./semaphore";
73
import { timeFunction } from "../../support/time-function";
84
import { ConcurrencyInterruptedException } from "../exceptions";
5+
import { ConcurrencyExceedTimeoutException } from "../exceptions/concurrency.exceed-timeout.exception";
6+
import { ConcurrencyInvalidTimeoutException } from "../exceptions/concurrency.invalid-timeout.exception";
97

108
describe("Semaphore", () => {
119
describe("Constructor", () => {
@@ -44,7 +42,7 @@ describe("Semaphore", () => {
4442
it("should throw a timeout exception when try-acquiring negative timeout", async () => {
4543
for (const timeout of [-1, -10, -100]) {
4644
await expect(() => semaphore.tryAcquire(timeout)).rejects.toThrow(
47-
SemaphoreInvalidTimeoutException
45+
ConcurrencyInvalidTimeoutException
4846
);
4947
}
5048
});
@@ -222,7 +220,7 @@ describe("Semaphore", () => {
222220
setTimeout(() => expect(semaphore.queueLength).toBe(1), delay / 2);
223221

224222
await expect(() => semaphore.tryAcquire(delay)).rejects.toThrow(
225-
SemaphoreAcquireTimeoutException
223+
ConcurrencyExceedTimeoutException
226224
);
227225

228226
// The queue is reset, since the `tryAcquire` failed
@@ -245,7 +243,7 @@ describe("Semaphore", () => {
245243
}, delay / 2);
246244

247245
await expect(() => semaphore.tryAcquire(delay, 4)).rejects.toThrow(
248-
SemaphoreAcquireTimeoutException
246+
ConcurrencyExceedTimeoutException
249247
);
250248

251249
// The queue is reset, since the `tryAcquire` failed (would have been cleared even on success)

src/semaphore/semaphore.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { SemaphoreInvalidPermitsException } from "./exceptions";
12
import {
2-
SemaphoreAcquireTimeoutException,
3-
SemaphoreInvalidPermitsException,
4-
SemaphoreInvalidTimeoutException
5-
} from "./exceptions";
3+
ConcurrencyExceedTimeoutException,
4+
ConcurrencyInvalidTimeoutException
5+
} from "../exceptions";
66
import { ConcurrencyInterruptedException } from "../exceptions";
77

88
type Resolver = () => void;
@@ -107,14 +107,14 @@ export class Semaphore {
107107
*
108108
* @param timeout maximum time (in ms) to acquire the permits
109109
* @param permits The number of permits to acquire (0 acquire immediately)
110-
* @throws {SemaphoreAcquireTimeoutException} when the `timeout` value is invalid
110+
* @throws {ConcurrencyExceedTimeoutException} when the `timeout` value is invalid
111111
* @throws {SemaphoreInvalidPermitsException} when the `permits` value is invalid
112112
* @returns a promise when the permits have been acquired
113113
*/
114114
public tryAcquire(timeout: number, permits = 1): Promise<void> {
115115
if (timeout < 0) {
116116
return Promise.reject(
117-
new SemaphoreInvalidTimeoutException("Cannot acquire with a negative timeout")
117+
new ConcurrencyInvalidTimeoutException("Cannot acquire with a negative timeout")
118118
);
119119
}
120120
if (permits < 0) {
@@ -150,7 +150,7 @@ export class Semaphore {
150150
}
151151

152152
reject(
153-
new SemaphoreAcquireTimeoutException(
153+
new ConcurrencyExceedTimeoutException(
154154
`Timeout of ${timeout}ms exceed when acquiring.`
155155
)
156156
);

0 commit comments

Comments
 (0)