Skip to content

Commit 271c3c7

Browse files
committed
Fix tests
1 parent 314e6fe commit 271c3c7

File tree

11 files changed

+174
-136
lines changed

11 files changed

+174
-136
lines changed

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 = [];

test/cluster.js

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ if (process.env.DATASTORE === "redis" || process.env.DATASTORE === "ioredis") {
201201
return runCommand(c.limiter, "ttl", [settings_key]);
202202
})
203203
.then(function (ttl) {
204-
assert(ttl < 0);
204+
c.mustLt(ttl, 0);
205205
});
206206
});
207207

@@ -215,7 +215,8 @@ if (process.env.DATASTORE === "redis" || process.env.DATASTORE === "ioredis") {
215215
return runCommand(c.limiter, "ttl", [settings_key]);
216216
})
217217
.then(function (ttl) {
218-
assert(ttl >= 290 && ttl <= 305);
218+
c.mustGte(ttl, 290);
219+
c.mustLte(ttl, 305);
219220
});
220221
});
221222

@@ -289,7 +290,7 @@ if (process.env.DATASTORE === "redis" || process.env.DATASTORE === "ioredis") {
289290
})
290291
.then(function (values) {
291292
var timestamps = values.slice(-2);
292-
timestamps.forEach((t) => assert(parseInt(t) > Date.now() - 500));
293+
timestamps.forEach((t) => c.mustGt(parseInt(t), Date.now() - 500));
293294
c.mustEqual(values.slice(0, -timestamps.length), [
294295
"2.18.0",
295296
"0",
@@ -537,9 +538,10 @@ if (process.env.DATASTORE === "redis" || process.env.DATASTORE === "ioredis") {
537538
c.mustEqual(sumWeights(client_running), 15);
538539
c.mustEqual(client_num_queued, ["0", "0"]);
539540
c.mustEqual(client_last_registered[1], "0");
540-
assert(client_last_seen[1] > Date.now() - 1000);
541+
c.mustGt(client_last_seen[1], Date.now() - 1000);
541542
var passed = Date.now() - parseFloat(client_last_registered[3]);
542-
assert(passed > 0 && passed < 20);
543+
c.mustGt(passed, 0);
544+
c.mustLt(passed, 20);
543545

544546
return c.wait(170);
545547
})
@@ -564,9 +566,10 @@ if (process.env.DATASTORE === "redis" || process.env.DATASTORE === "ioredis") {
564566
c.mustEqual(sumWeights(client_running), 1);
565567
c.mustEqual(client_num_queued, ["0", "0"]);
566568
c.mustEqual(client_last_registered[1], "0");
567-
assert(client_last_seen[1] > Date.now() - 1000);
569+
c.mustGt(client_last_seen[1], Date.now() - 1000);
568570
var passed = Date.now() - parseFloat(client_last_registered[3]);
569-
assert(passed > 170 && passed < 200);
571+
c.mustGt(passed, 170);
572+
c.mustLt(passed, 200);
570573

571574
c.mustEqual(numExpirations, 4);
572575
})
@@ -701,7 +704,7 @@ if (process.env.DATASTORE === "redis" || process.env.DATASTORE === "ioredis") {
701704
throw e;
702705
}
703706
}
704-
assert(dropped);
707+
assert(dropped, "Expected dropped to be true");
705708

706709
await c.wait(200);
707710

@@ -771,7 +774,7 @@ if (process.env.DATASTORE === "redis" || process.env.DATASTORE === "ioredis") {
771774

772775
return new Promise(function (resolve, reject) {
773776
c.limiter.on("error", function (err) {
774-
assert(err != null);
777+
c.mustExist(err);
775778
resolve();
776779
});
777780

@@ -809,14 +812,20 @@ if (process.env.DATASTORE === "redis" || process.env.DATASTORE === "ioredis") {
809812
c.checkDuration(300);
810813
c.checkResultsOrder([[1], [4], [5], [2], [6], [3]]);
811814

812-
assert(results.calls[0].time >= 100 && results.calls[0].time < 200);
813-
assert(results.calls[1].time >= 100 && results.calls[1].time < 200);
814-
assert(results.calls[2].time >= 100 && results.calls[2].time < 200);
815+
c.mustGte(results.calls[0].time, 100);
816+
c.mustLt(results.calls[0].time, 200);
817+
c.mustGte(results.calls[1].time, 100);
818+
c.mustLt(results.calls[1].time, 200);
819+
c.mustGte(results.calls[2].time, 100);
820+
c.mustLt(results.calls[2].time, 200);
815821

816-
assert(results.calls[3].time >= 200 && results.calls[3].time < 300);
817-
assert(results.calls[4].time >= 200 && results.calls[4].time < 300);
822+
c.mustGte(results.calls[3].time, 200);
823+
c.mustLt(results.calls[3].time, 300);
824+
c.mustGte(results.calls[4].time, 200);
825+
c.mustLt(results.calls[4].time, 300);
818826

819-
assert(results.calls[5].time >= 300 && results.calls[2].time < 400);
827+
c.mustGte(results.calls[5].time, 300);
828+
c.mustLt(results.calls[5].time, 400);
820829
});
821830
});
822831

@@ -841,14 +850,20 @@ if (process.env.DATASTORE === "redis" || process.env.DATASTORE === "ioredis") {
841850
c.checkDuration(300);
842851
c.checkResultsOrder([[1], [4], [5], [2], [6], [3]]);
843852

844-
assert(results.calls[0].time >= 100 && results.calls[0].time < 200);
845-
assert(results.calls[1].time >= 100 && results.calls[1].time < 200);
853+
c.mustGte(results.calls[0].time, 100);
854+
c.mustLt(results.calls[0].time, 200);
855+
c.mustGte(results.calls[1].time, 100);
856+
c.mustLt(results.calls[1].time, 200);
846857

847-
assert(results.calls[2].time >= 200 && results.calls[2].time < 300);
848-
assert(results.calls[3].time >= 200 && results.calls[3].time < 300);
858+
c.mustGte(results.calls[2].time, 200);
859+
c.mustLt(results.calls[2].time, 300);
860+
c.mustGte(results.calls[3].time, 200);
861+
c.mustLt(results.calls[3].time, 300);
849862

850-
assert(results.calls[4].time >= 300 && results.calls[4].time < 400);
851-
assert(results.calls[5].time >= 300 && results.calls[2].time < 400);
863+
c.mustGte(results.calls[4].time, 300);
864+
c.mustLt(results.calls[4].time, 400);
865+
c.mustGte(results.calls[5].time, 300);
866+
c.mustLt(results.calls[5].time, 400);
852867
});
853868
});
854869

@@ -865,7 +880,7 @@ if (process.env.DATASTORE === "redis" || process.env.DATASTORE === "ioredis") {
865880
.ready()
866881
.then(function () {
867882
var keys = limiterKeys(limiter);
868-
keys.forEach((key) => assert(key.indexOf(randomId) > 0));
883+
keys.forEach((key) => c.mustGt(key.indexOf(randomId), 0));
869884
return deleteKeys(limiter);
870885
})
871886
.then(function (deleted) {
@@ -897,7 +912,7 @@ if (process.env.DATASTORE === "redis" || process.env.DATASTORE === "ioredis") {
897912
return countKeys(limiter);
898913
})
899914
.then(function (count) {
900-
assert(count > 0);
915+
c.mustGt(count, 0);
901916
return limiter.disconnect(false);
902917
});
903918
});
@@ -1086,7 +1101,8 @@ if (process.env.DATASTORE === "redis" || process.env.DATASTORE === "ioredis") {
10861101
return runCommand(limiter, "ttl", [settings_key]);
10871102
})
10881103
.then(function (ttl) {
1089-
assert(ttl >= 290 && ttl <= 305);
1104+
c.mustGte(ttl, 290);
1105+
c.mustLte(ttl, 305);
10901106
})
10911107
.then(function () {
10921108
return group.disconnect(false);
@@ -1137,17 +1153,17 @@ if (process.env.DATASTORE === "redis" || process.env.DATASTORE === "ioredis") {
11371153
})
11381154
.then(function () {
11391155
c.mustEqual(Object.keys(results).length, 6);
1140-
assert(results.a < results.b);
1141-
assert(results.b < results.c);
1142-
assert(results.b - results.a >= 40);
1143-
assert(results.c - results.b >= 40);
1156+
c.mustLt(results.a, results.b);
1157+
c.mustLt(results.b, results.c);
1158+
c.mustGte(results.b - results.a, 40);
1159+
c.mustGte(results.c - results.b, 40);
11441160

1145-
assert(results.d < results.e);
1146-
assert(results.e - results.d >= 40);
1161+
c.mustLt(results.d, results.e);
1162+
c.mustGte(results.e - results.d, 40);
11471163

1148-
assert(Math.abs(results.a - results.d) <= 10);
1149-
assert(Math.abs(results.d - results.f) <= 10);
1150-
assert(Math.abs(results.b - results.e) <= 10);
1164+
c.mustLte(Math.abs(results.a - results.d), 10);
1165+
c.mustLte(Math.abs(results.d - results.f), 10);
1166+
c.mustLte(Math.abs(results.b - results.e), 10);
11511167

11521168
return c.wait(400);
11531169
})
@@ -1512,7 +1528,7 @@ if (process.env.DATASTORE === "redis" || process.env.DATASTORE === "ioredis") {
15121528

15131529
c.checkResultsOrder([["A"], ["B"], ["C"], ["D"], [1], [3], [4], [5], [2]]);
15141530

1515-
assert(Math.abs(t3 - t4) < 15);
1531+
c.mustLt(Math.abs(t3 - t4), 15);
15161532

15171533
await limiter1.disconnect(false);
15181534
await limiter2.disconnect(false);

0 commit comments

Comments
 (0)