Skip to content

Commit 7999caf

Browse files
committed
Make testing more robust
1 parent 0b51aca commit 7999caf

File tree

14 files changed

+745
-145
lines changed

14 files changed

+745
-145
lines changed

.github/workflows/ci.yaml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ concurrency:
1212
cancel-in-progress: true
1313

1414
jobs:
15-
build:
15+
test:
16+
name: Test all
1617
runs-on: ubuntu-latest
1718

1819
steps:
@@ -24,8 +25,8 @@ jobs:
2425
with:
2526
node-version-file: ".nvmrc"
2627

27-
- name: Redis Server in GitHub Actions
28-
uses: supercharge/redis-github-action@1.8.0
28+
- name: Enable corepack
29+
run: corepack enable
2930

3031
- name: Use yarn cache
3132
uses: actions/cache@v4.2.3
@@ -35,11 +36,17 @@ jobs:
3536
restore-keys: |
3637
${{ runner.os }}-node-
3738
38-
- name: Enable corepack
39-
run: corepack enable
40-
4139
- name: Install dependencies
4240
run: yarn
4341

42+
- name: Run linter
43+
run: yarn lint
44+
45+
- name: Check types
46+
run: yarn check-types
47+
48+
- name: Redis Server in GitHub Actions
49+
uses: supercharge/redis-github-action@1.8.0
50+
4451
- name: Run tests
4552
run: yarn test-all

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"test": "mocha test",
99
"test-all": "./scripts/test_all.sh",
1010
"check-types": "tsc --noEmit --strict --lib es2023,dom,dom.iterable test.ts",
11+
"lint": "eslint . --max-warnings 0",
1112
"build": "./scripts/build.sh"
1213
},
1314
"repository": "github:sderrow/bottleneck",

test/DLList.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var DLList = require("../src/DLList");
2-
var assert = require("assert");
32
var c = require("./context")({ datastore: "local" });
43
const { describe, it } = require("mocha");
54

@@ -54,11 +53,11 @@ describe("DLList", function () {
5453
var queues = new fakeQueues();
5554
var list = new DLList(...queues.fns);
5655
c.mustEqual(list.length, 0);
57-
assert(list.shift() === undefined);
56+
c.mustEqual(list.shift(), undefined);
5857
var arr = list.getArray();
5958
c.mustEqual(arr.length, 0);
6059
c.mustEqual(list.length, 0);
61-
assert(list.shift() === undefined);
60+
c.mustEqual(list.shift(), undefined);
6261
arr = list.getArray();
6362
c.mustEqual(arr.length, 0);
6463
c.mustEqual(list.length, 0);
@@ -121,8 +120,8 @@ describe("DLList", function () {
121120
it("Should return the first value without shifting", function () {
122121
var queues = new fakeQueues();
123122
var list = new DLList(...queues.fns);
124-
assert(list.first() === undefined);
125-
assert(list.first() === undefined);
123+
c.mustEqual(list.first(), undefined);
124+
c.mustEqual(list.first(), undefined);
126125

127126
list.push(1);
128127
c.mustEqual(list.first(), 1);
@@ -137,11 +136,11 @@ describe("DLList", function () {
137136
c.mustEqual(list.first(), 2);
138137

139138
c.mustEqual(list.shift(), 2);
140-
assert(list.first() === undefined);
141-
assert(list.first() === undefined);
139+
c.mustEqual(list.first(), undefined);
140+
c.mustEqual(list.first(), undefined);
142141

143-
assert(list.first() === undefined);
144-
assert(list.shift() === undefined);
145-
assert(list.first() === undefined);
142+
c.mustEqual(list.first(), undefined);
143+
c.mustEqual(list.shift(), undefined);
144+
c.mustEqual(list.first(), undefined);
146145
});
147146
});

test/batcher.js

Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ describe("Batcher", function () {
1818
var t0 = Date.now();
1919
var batches = [];
2020

21-
batcher.on("batch", function (batcher) {
22-
batches.push(batcher);
21+
batcher.on("batch", function (groups) {
22+
batches.push(groups);
2323
});
2424

2525
return Promise.all([
@@ -60,8 +60,8 @@ describe("Batcher", function () {
6060
var t0 = Date.now();
6161
var batches = [];
6262

63-
batcher.on("batch", function (batcher) {
64-
batches.push(batcher);
63+
batcher.on("batch", function (groups) {
64+
batches.push(groups);
6565
});
6666

6767
return Promise.all([
@@ -109,8 +109,8 @@ describe("Batcher", function () {
109109
});
110110
var batches = [];
111111

112-
batcher.on("batch", function (batcher) {
113-
batches.push(batcher);
112+
batcher.on("batch", function (groups) {
113+
batches.push(groups);
114114
});
115115

116116
return Promise.all([
@@ -133,63 +133,55 @@ describe("Batcher", function () {
133133
});
134134
});
135135

136-
it("Should stagger flushes", function () {
136+
it("Should stagger flushes", async function () {
137137
c = makeTest();
138-
var batcher = new Bottleneck.Batcher({
138+
const batcher = new Bottleneck.Batcher({
139139
maxTime: 50,
140140
maxSize: 3,
141141
});
142-
var t0 = Date.now();
143-
var batches = [];
142+
const t0 = Date.now();
143+
const batches = [];
144144

145-
batcher.on("batch", function (batcher) {
146-
batches.push(batcher);
145+
batcher.on("batch", function (groups) {
146+
batches.push(groups);
147147
});
148148

149-
return Promise.all([
150-
batcher.add(1).then((_x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 1)),
151-
batcher.add(2).then((_x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 2)),
152-
])
153-
.then(function (data) {
154-
c.mustEqual(
155-
data.map(([t, x]) => [Math.floor(t / 50), x]),
156-
[
157-
[1, 1],
158-
[1, 2],
159-
],
160-
);
149+
const [first, second] = await Promise.all([
150+
batcher.add(1).then(() => c.limiter.schedule(c.promise, null, Date.now() - t0, 1)),
151+
batcher.add(2).then(() => c.limiter.schedule(c.promise, null, Date.now() - t0, 2)),
152+
]);
161153

162-
var promises = [];
163-
promises.push(
164-
batcher.add(3).then((_x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 3)),
165-
);
154+
c.mustGte(first[0], 50);
155+
c.mustEqual(first[1], 1);
166156

167-
return c.wait(10).then(function () {
168-
promises.push(
169-
batcher.add(4).then((_x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 4)),
170-
);
157+
c.mustGte(second[0], 50);
158+
c.mustEqual(second[1], 2);
171159

172-
return Promise.all(promises);
173-
});
174-
})
175-
.then(function (data) {
176-
c.mustEqual(
177-
data.map(([t, x]) => [Math.floor(t / 50), x]),
178-
[
179-
[2, 3],
180-
[2, 4],
181-
],
182-
);
160+
const promises = [
161+
batcher.add(3).then(() => c.limiter.schedule(c.promise, null, Date.now() - t0, 3)),
162+
];
183163

184-
return c.last();
185-
})
186-
.then(function (_results) {
187-
c.checkDuration(120, 20);
188-
c.mustEqual(batches, [
189-
[1, 2],
190-
[3, 4],
191-
]);
192-
});
164+
await c.wait(10);
165+
166+
promises.push(
167+
batcher.add(4).then(() => c.limiter.schedule(c.promise, null, Date.now() - t0, 4)),
168+
);
169+
170+
const [third, fourth] = await Promise.all(promises);
171+
172+
c.mustGte(third[0], 100);
173+
c.mustEqual(third[1], 3);
174+
175+
c.mustGte(fourth[0], 100);
176+
c.mustEqual(fourth[1], 4);
177+
178+
await c.last();
179+
180+
c.checkDuration(120, 20);
181+
c.mustEqual(batches, [
182+
[1, 2],
183+
[3, 4],
184+
]);
193185
});
194186

195187
it("Should force then stagger flushes", function () {
@@ -201,8 +193,8 @@ describe("Batcher", function () {
201193
var t0 = Date.now();
202194
var batches = [];
203195

204-
batcher.on("batch", function (batcher) {
205-
batches.push(batcher);
196+
batcher.on("batch", function (groups) {
197+
batches.push(groups);
206198
});
207199

208200
var promises = [];

0 commit comments

Comments
 (0)