Skip to content

Commit

Permalink
Updated throttle to 2000ms interval and assertions checks results +/-…
Browse files Browse the repository at this point in the history
… 1000ms
  • Loading branch information
jbigman committed Aug 3, 2023
1 parent f16c23a commit d944ed0
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions test/utils.throttle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const sinon = require('sinon');
const assert = require('chai').assert;

describe('Throttle tests', function () {
this.timeout(6000);
let server;

// Create a fake http server to emulate http call and responses.
Expand All @@ -18,13 +19,13 @@ describe('Throttle tests', function () {

const url = 'https://yesno.wtf/api'; // Fake url used in this test, it could be anything.

it('Should make three requests with 1000ms interval. (Throttle function)', function () {
it('Should make three requests with 2000ms interval. (Throttle function)', function () {
// If we don't want to rely on the availability of a particular api we can use mocks.
// The fake server intercept http calls and return specified objects if it mach the same method/url.
server.respondWith('GET', url, JSON.stringify({ test: 'this works' }));
const req = throttled(requestLib, {
limit: 1,
interval: 1000
interval: 2000
});
return Promise.all([req({ url }), req({ url }), req({ url })])
.then((response) => response.map(req => new Date(req.headers.date).getTime()))
Expand All @@ -33,9 +34,9 @@ describe('Throttle tests', function () {
const secondAndThirdReq = dates[2] - dates[1];

assert.isAtLeast(firstAndSecondReq, 1000);
assert.isAtMost(firstAndSecondReq, 2000);
assert.isAtMost(firstAndSecondReq, 3000);
assert.isAtLeast(secondAndThirdReq, 1000);
assert.isAtMost(secondAndThirdReq, 2000);
assert.isAtMost(secondAndThirdReq, 3000);
});
});
});

0 comments on commit d944ed0

Please sign in to comment.