fix test/throttle 💚

This commit is contained in:
Jannis R 2019-10-26 00:24:02 +02:00
parent 2cfee22287
commit 4652c1694e
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5

View file

@ -1,28 +1,39 @@
'use strict' 'use strict'
const test = require('tape') const tapePromise = require('tape-promise').default
const tape = require('tape')
const withThrottling = require('../throttle') const withThrottling = require('../throttle')
const createClient = require('..') const createClient = require('..')
const vbbProfile = require('../p/vbb') const vbbProfile = require('../p/vbb')
const depsRes = require('./fixtures/vbb-departures.json')
const userAgent = 'public-transport/hafas-client:test' const ua = 'public-transport/hafas-client:test'
const spichernstr = '900000042101' const spichernstr = '900000042101'
// todo: mock request() const test = tapePromise(tape)
test('withThrottling works', (t) => {
let calls = 0
const transformReqBody = (ctx, body) => {
calls++
return vbbProfile.transformReqBody(ctx, body)
}
const mockProfile = Object.assign({}, vbbProfile, {transformReqBody})
const createThrottlingClient = withThrottling(createClient, 2, 1000) test('withThrottling works', async (t) => {
const client = createThrottlingClient(mockProfile, userAgent) const ctx = {profile: vbbProfile, opt: {}}
for (let i = 0; i < 10; i++) client.departures(spichernstr, {duration: 1})
let calls = 0
const mockedRequest = async (ctx, _, reqData) => {
calls++
return {
res: depsRes,
common: ctx.profile.parseCommon({...ctx, res: depsRes})
}
}
const createThrottledClient = withThrottling(createClient, 2, 1000)
const client = createThrottledClient(vbbProfile, ua, mockedRequest)
t.plan(3) t.plan(3)
for (let i = 0; i < 10; i++) {
const p = client.departures(spichernstr, {duration: 1})
p.catch(() => {})
}
setTimeout(() => t.equal(calls, 2), 500) setTimeout(() => t.equal(calls, 2), 500)
setTimeout(() => t.equal(calls, 4), 1500) setTimeout(() => t.equal(calls, 4), 1500)
setTimeout(() => t.equal(calls, 6), 2500) setTimeout(() => t.equal(calls, 6), 2500)