db-vendo-client/test/throttle.js

31 lines
836 B
JavaScript
Raw Normal View History

2018-01-23 15:54:39 +01:00
'use strict'
const test = require('tape')
const withThrottling = require('../throttle')
const createClient = require('..')
2018-01-23 15:54:39 +01:00
const vbbProfile = require('../p/vbb')
2018-07-19 21:55:54 +02:00
const userAgent = 'public-transport/hafas-client:test'
2018-01-23 15:54:39 +01:00
const spichernstr = '900000042101'
test('withThrottling works', (t) => {
2018-01-23 15:54:39 +01:00
let calls = 0
const transformReqBody = (body) => {
calls++
return vbbProfile.transformReqBody(body)
}
const mockProfile = Object.assign({}, vbbProfile, {transformReqBody})
const createThrottlingClient = withThrottling(createClient, 2, 1000)
const client = createThrottlingClient(mockProfile, userAgent)
2018-01-23 15:54:39 +01:00
for (let i = 0; i < 10; i++) client.departures(spichernstr, {duration: 1})
t.plan(3)
setTimeout(() => t.equal(calls, 2), 500)
setTimeout(() => t.equal(calls, 4), 1500)
setTimeout(() => t.equal(calls, 6), 2500)
})
// todo