db-vendo-client/test/throttle.js

42 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

2022-05-07 16:17:37 +02:00
// todo: use import assertions once they're supported by Node.js & ESLint
// https://github.com/tc39/proposal-import-assertions
import {createRequire} from 'module';
const require = createRequire(import.meta.url);
2018-01-23 15:54:39 +01:00
import tap from 'tap';
2018-01-23 15:54:39 +01:00
import {createClient} from '../index.js';
import {withThrottling} from '../throttle.js';
import {profile as vbbProfile} from '../p/vbb/index.js';
const depsRes = require('./fixtures/vbb-departures.json');
2018-01-23 15:54:39 +01:00
const ua = 'public-transport/hafas-client:test';
const spichernstr = '900000042101';
2018-01-23 15:54:39 +01:00
2021-05-20 16:42:43 +01:00
tap.test('withThrottling works', {timeout: 3000}, (t) => {
let calls = 0;
2019-10-31 18:48:11 +01:00
const mockedRequest = async (ctx, userAgent, reqData) => {
calls++;
2019-10-26 00:24:02 +02:00
return {
res: depsRes,
common: ctx.profile.parseCommon({...ctx, res: depsRes}),
};
};
2018-01-23 15:54:39 +01:00
2019-10-31 18:48:11 +01:00
const profile = withThrottling({
...vbbProfile,
request: mockedRequest,
}, 2, 1000);
const client = createClient(profile, ua);
2018-01-23 15:54:39 +01:00
t.plan(3);
2019-10-26 00:24:02 +02:00
for (let i = 0; i < 10; i++) {
const p = client.departures(spichernstr, {duration: 1});
p.catch(() => {});
2019-10-26 00:24:02 +02:00
}
setTimeout(() => t.equal(calls, 2), 500);
setTimeout(() => t.equal(calls, 4), 1500);
setTimeout(() => t.equal(calls, 6), 2500);
});