mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 15:19:35 +02:00
fix test/throttle 💚
This commit is contained in:
parent
2cfee22287
commit
4652c1694e
1 changed files with 24 additions and 13 deletions
|
@ -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)
|
||||||
|
|
Loading…
Add table
Reference in a new issue