Skip to content

Commit

Permalink
fix flakey tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunpersad committed May 1, 2022
1 parent 00ba669 commit ccefb38
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "throttled-queue",
"version": "2.1.3",
"version": "2.1.4",
"description": "Throttles arbitrary code to execute a maximum number of times per interval. Best for making throttled API requests.",
"main": "dist/throttledQueue.js",
"types": "dist/throttledQueue.d.ts",
Expand Down
84 changes: 55 additions & 29 deletions test/throttledQueue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,24 @@ describe('throttled-queue', function () {
});

it('should queue the fn and honor the interval', function () {

const requestsPerInterval = 1;
const interval = 500;
const throttle = throttledQueue(requestsPerInterval, interval);
const requestLimit = 100;
let lastIntervalStart = process.hrtime.bigint();
let lastIntervalStart = 0;
let numRequests = 0;
let numRequestsPerInterval = 0;
let failed = 0;

for (let x = 0; x < requestLimit; x++) {
void throttle(() => {
if ((process.hrtime.bigint() - lastIntervalStart) > (interval * 1000000)) {
lastIntervalStart = process.hrtime.bigint();
numRequestsPerInterval = 0;
}
if (++numRequestsPerInterval > requestsPerInterval) {
throw new Error('Did not honor interval.');
const intervalStart = Date.now();
if (x) {
if ((intervalStart - lastIntervalStart) < interval) {
failed++;
}
lastIntervalStart = intervalStart;
} else {
lastIntervalStart = intervalStart;
}
numRequests++;
});
Expand All @@ -46,27 +47,38 @@ describe('throttled-queue', function () {
if (numRequests !== requestLimit) {
throw new Error('Not all callbacks queued.');
}
if (failed) {
throw new Error(`${failed} did not honor the interval.`);
}
});
});

it('should queue the fn and honor the interval with multiple requests per interval', function () {

const requestsPerInterval = 3;
const requestsPerInterval = 5;
const interval = 1000;
const throttle = throttledQueue(requestsPerInterval, interval);
const requestLimit = 100;
let lastIntervalStart = process.hrtime.bigint();
let lastIntervalStart = 0;
let numRequests = 0;
let numRequestsPerInterval = 0;
let failed = 0;
let inInterval = 0;

for (let x = 0; x < requestLimit; x++) {
void throttle(() => {
if ((process.hrtime.bigint() - lastIntervalStart) > (interval * 1000000)) {
lastIntervalStart = process.hrtime.bigint();
numRequestsPerInterval = 0;
}
if (++numRequestsPerInterval > requestsPerInterval) {
throw new Error('Did not honor interval.');
const intervalStart = Date.now();
if (x) {
if ((intervalStart - lastIntervalStart) < interval) {
inInterval++;
} else {
if (inInterval > requestsPerInterval) {
failed++;
throw new Error(`Got ${inInterval} requests per interval, expected ${requestsPerInterval}.`);
}
lastIntervalStart = intervalStart;
inInterval = 0;
}
} else {
lastIntervalStart = intervalStart;
}
numRequests++;
});
Expand All @@ -75,27 +87,38 @@ describe('throttled-queue', function () {
if (numRequests !== requestLimit) {
throw new Error('Not all callbacks queued.');
}
if (failed) {
throw new Error(`${failed} did not honor the interval.`);
}
});
});

it('should queue the fn and honor the interval with multiple evenly spaced requests per interval', function () {

const requestsPerInterval = 3;
const requestsPerInterval = 5;
const interval = 1000;
const throttle = throttledQueue(requestsPerInterval, interval, true);
const requestLimit = 100;
let lastIntervalStart = process.hrtime.bigint();
let lastIntervalStart = 0;
let numRequests = 0;
let numRequestsPerInterval = 0;
let failed = 0;
let inInterval = 0;

for (let x = 0; x < requestLimit; x++) {
void throttle(() => {
if ((process.hrtime.bigint() - lastIntervalStart) > (interval * 1000000)) {
lastIntervalStart = process.hrtime.bigint();
numRequestsPerInterval = 0;
}
if (++numRequestsPerInterval > requestsPerInterval) {
throw new Error('Did not honor interval.');
const intervalStart = Date.now();
if (x) {
if ((intervalStart - lastIntervalStart) < interval) {
inInterval++;
} else {
if (inInterval > requestsPerInterval) {
failed++;
throw new Error(`Got ${inInterval} requests per interval, expected ${requestsPerInterval}.`);
}
lastIntervalStart = intervalStart;
inInterval = 0;
}
} else {
lastIntervalStart = intervalStart;
}
numRequests++;
});
Expand All @@ -104,6 +127,9 @@ describe('throttled-queue', function () {
if (numRequests !== requestLimit) {
throw new Error('Not all callbacks queued.');
}
if (failed) {
throw new Error(`${failed} did not honor the interval.`);
}
});
});

Expand Down

0 comments on commit ccefb38

Please sign in to comment.