Skip to content

Commit 37a3d1e

Browse files
HugoMendes98Mendes Hugo
authored andcommitted
Merge pull request #10 from heap-code/fix/2-lock-with
fix(ci): set runInBand when testing
2 parents 4045ec2 + a26d8db commit 37a3d1e

3 files changed

Lines changed: 26 additions & 27 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@
7676
"compodoc:coverage": "npm run compodoc:build -- --coverageTest 80",
7777
"lint": "eslint .",
7878
"lint:fix": "npm run lint -- --fix",
79-
"test": "jest --config jest.config.ts",
79+
"test": "jest --config jest.config.ts --runInBand",
8080
"test:coverage": "npm run test -- --coverage",
8181
"test:watch": "npm run test -- --watch"
8282
},
8383
"types": "./dist/types/index.js",
84-
"version": "0.2.0"
84+
"version": "0.2.1"
8585
}

src/mutex/mutex.spec.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import {
88
} from "../exceptions";
99

1010
describe("Mutex", () => {
11-
const delay = 50;
12-
const offset = 5;
11+
const delay = 60;
12+
const offsetLow = 5;
13+
const offset = 15;
1314

1415
describe("Input validation", () => {
1516
const mutex = new Mutex();
@@ -50,7 +51,7 @@ describe("Mutex", () => {
5051
mutex.unlock();
5152
expect(mutex.isLocked).toBeFalse();
5253

53-
expect(elapsed).toBeGreaterThanOrEqual(delay - offset);
54+
expect(elapsed).toBeGreaterThanOrEqual(delay - offsetLow);
5455
expect(elapsed).toBeLessThanOrEqual(delay + offset);
5556
});
5657

@@ -100,15 +101,15 @@ describe("Mutex", () => {
100101
.map(([elapsed]) => elapsed)
101102
.sort((a, b) => a - b);
102103

103-
expect(elapsed1).toBeGreaterThanOrEqual(min - offset);
104+
expect(elapsed1).toBeGreaterThanOrEqual(min - offsetLow);
104105
expect(elapsed1).toBeLessThanOrEqual(min + offset);
105-
expect(elapsed2).toBeGreaterThanOrEqual(med - offset);
106+
expect(elapsed2).toBeGreaterThanOrEqual(med - offsetLow);
106107
expect(elapsed2).toBeLessThanOrEqual(med + offset);
107-
expect(elapsed3).toBeGreaterThanOrEqual(max - offset);
108+
expect(elapsed3).toBeGreaterThanOrEqual(max - offsetLow);
108109
expect(elapsed3).toBeLessThanOrEqual(max + offset);
109110

110111
// The global elapsed time is very close to the slowest timeout
111-
expect(elapsed).toBeGreaterThanOrEqual(max - offset);
112+
expect(elapsed).toBeGreaterThanOrEqual(max - offsetLow);
112113
expect(elapsed).toBeLessThanOrEqual(max + offset);
113114

114115
mutex.unlock();
@@ -131,7 +132,7 @@ describe("Mutex", () => {
131132
await mutex.tryLock(delay * 5);
132133
});
133134

134-
expect(elapsed).toBeGreaterThanOrEqual(delay - offset);
135+
expect(elapsed).toBeGreaterThanOrEqual(delay - offsetLow);
135136
expect(elapsed).toBeLessThanOrEqual(delay + offset);
136137
});
137138

@@ -187,8 +188,8 @@ describe("Mutex", () => {
187188
expect(mutex.queueLength).toBe(0);
188189
});
189190

190-
expect(elapsed).toBeGreaterThanOrEqual(delay * 2 - offset);
191-
expect(elapsed).toBeLessThanOrEqual(delay * 2 + offset);
191+
expect(elapsed).toBeGreaterThanOrEqual(delay * 2 - offsetLow);
192+
expect(elapsed).toBeLessThanOrEqual((delay + offset) * 2);
192193
});
193194

194195
it("should return the value from the critical section", async () => {
@@ -218,7 +219,7 @@ describe("Mutex", () => {
218219
);
219220

220221
// each `tryLockWith` keep the lock for delay / 2 time
221-
expect(elapsed).toBeGreaterThanOrEqual(delay - offset);
222+
expect(elapsed).toBeGreaterThanOrEqual(delay - offsetLow);
222223
expect(elapsed).toBeLessThanOrEqual(delay + offset);
223224
});
224225

src/semaphore/semaphore.spec.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { ConcurrencyExceedTimeoutException } from "../exceptions/concurrency.exc
66
import { ConcurrencyInvalidTimeoutException } from "../exceptions/concurrency.invalid-timeout.exception";
77

88
describe("Semaphore", () => {
9+
const delay = 60;
10+
const offsetLow = 5;
11+
const offset = 15;
12+
913
describe("Constructor", () => {
1014
it("should create an instance", () => {
1115
for (const permits of [1, 10, 100]) {
@@ -71,9 +75,6 @@ describe("Semaphore", () => {
7175
});
7276

7377
describe("`acquire` usage", () => {
74-
const delay = 50;
75-
const offset = 5;
76-
7778
it("should work with a single initial permit", async () => {
7879
// semaphore as a mutex
7980
const semaphore = new Semaphore();
@@ -98,7 +99,7 @@ describe("Semaphore", () => {
9899
await semaphore.acquire();
99100
});
100101

101-
expect(elapsed).toBeGreaterThanOrEqual(delay - offset);
102+
expect(elapsed).toBeGreaterThanOrEqual(delay - offsetLow);
102103
expect(elapsed).toBeLessThanOrEqual(delay + offset);
103104

104105
semaphore.release();
@@ -124,7 +125,7 @@ describe("Semaphore", () => {
124125
await semaphore.acquire();
125126
});
126127

127-
expect(elapsed).toBeGreaterThanOrEqual(delay - offset);
128+
expect(elapsed).toBeGreaterThanOrEqual(delay - offsetLow);
128129
expect(elapsed).toBeLessThanOrEqual(delay + offset);
129130
});
130131

@@ -138,7 +139,7 @@ describe("Semaphore", () => {
138139
await semaphore.acquire(permits + permitOffset);
139140
});
140141

141-
expect(elapsed).toBeGreaterThanOrEqual(delay - offset);
142+
expect(elapsed).toBeGreaterThanOrEqual(delay - offsetLow);
142143
expect(elapsed).toBeLessThanOrEqual(delay + offset);
143144
}
144145
});
@@ -167,25 +168,22 @@ describe("Semaphore", () => {
167168
.map(([elapsed]) => elapsed)
168169
.sort((a, b) => a - b);
169170

170-
expect(elapsed1).toBeGreaterThanOrEqual(min - offset);
171+
expect(elapsed1).toBeGreaterThanOrEqual(min - offsetLow);
171172
expect(elapsed1).toBeLessThanOrEqual(min + offset);
172-
expect(elapsed2).toBeGreaterThanOrEqual(med - offset);
173+
expect(elapsed2).toBeGreaterThanOrEqual(med - offsetLow);
173174
expect(elapsed2).toBeLessThanOrEqual(med + offset);
174-
expect(elapsed3).toBeGreaterThanOrEqual(max - offset);
175+
expect(elapsed3).toBeGreaterThanOrEqual(max - offsetLow);
175176
expect(elapsed3).toBeLessThanOrEqual(max + offset);
176177

177178
// The global elapsed time is very close to the slowest timeout
178-
expect(elapsed).toBeGreaterThanOrEqual(max - offset);
179+
expect(elapsed).toBeGreaterThanOrEqual(max - offsetLow);
179180
expect(elapsed).toBeLessThanOrEqual(max + offset);
180181

181182
expect(elapsed).toBeGreaterThanOrEqual(elapsed3);
182183
});
183184
});
184185

185186
describe("`tryAcquire` usage", () => {
186-
const delay = 50;
187-
const offset = 5;
188-
189187
it("should work with many initial tryAcquires/releases", async () => {
190188
for (const permits of [5, 8, 13]) {
191189
const semaphore = new Semaphore(permits);
@@ -196,7 +194,7 @@ describe("Semaphore", () => {
196194
await semaphore.tryAcquire(delay * 5, permits + permitOffset);
197195
});
198196

199-
expect(elapsed).toBeGreaterThanOrEqual(delay - offset);
197+
expect(elapsed).toBeGreaterThanOrEqual(delay - offsetLow);
200198
expect(elapsed).toBeLessThanOrEqual(delay + offset);
201199
}
202200
});

0 commit comments

Comments
 (0)